Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display Link as payment method on order received page with setup intents #8764

Merged
merged 9 commits into from
May 6, 2024
5 changes: 5 additions & 0 deletions changelog/fix-link-setup-intent-pm-title
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: fix
Comment: Minor change to Link payment method display.


33 changes: 23 additions & 10 deletions includes/class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -1775,7 +1775,7 @@ public function process_payment_for_order( $cart, $payment_information, $schedul
}
} else {
$payment_method_details = false;
$payment_method_type = $intent->get_payment_method_type();
$payment_method_type = $this->get_payment_method_type_for_setup_intent( $intent, $token );
}

if ( empty( $_POST['payment_request_type'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
Expand Down Expand Up @@ -3430,11 +3430,6 @@ public function update_order_status() {
);
}

$payment_method_id = isset( $_POST['payment_method_id'] ) ? wc_clean( wp_unslash( $_POST['payment_method_id'] ) ) : '';
if ( 'null' === $payment_method_id ) {
$payment_method_id = '';
}
Comment on lines -3433 to -3436
Copy link
Member Author

@mdmoore mdmoore May 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update_order_status() is used to update 3DS orders after authentication. Shortcode checkout includes payment_method_id in the update_order_status() AJAX request for setup intents but blocks checkout doesn't. This payment method is only used in adding the token to the user. We need the token to determine the payment method type in order to correctly set the payment method title. This is all to say that we can safely remove this as it's now being set below, pulled from the intent.

Incidentally, this might solve #8611 but I haven't had a chance to verify that yet.


// Check that the intent saved in the order matches the intent used as part of the
// authentication process. The ID of the intent used is sent with
// the AJAX request. We are about to use the status of the intent saved in
Expand All @@ -3447,7 +3442,8 @@ public function update_order_status() {
);
}

$amount = $order->get_total();
$amount = $order->get_total();
$payment_method_details = false;

if ( $amount > 0 ) {
// An exception is thrown if an intent can't be found for the given intent ID.
Expand All @@ -3466,9 +3462,10 @@ public function update_order_status() {
// For $0 orders, fetch the Setup Intent instead.
$setup_intent_request = Get_Setup_Intention::create( $intent_id );
/** @var WC_Payments_API_Setup_Intention $setup_intent */ // phpcs:ignore Generic.Commenting.DocComment.MissingShort
$intent = $setup_intent_request->send();
$status = $intent->get_status();
$charge_id = '';
$intent = $setup_intent_request->send();
$status = $intent->get_status();
$charge_id = '';
$payment_method_id = $intent->get_payment_method_id();
}

if ( Intent_Status::SUCCEEDED === $status ) {
Expand All @@ -3484,6 +3481,11 @@ public function update_order_status() {
try {
$token = $this->token_service->add_payment_method_to_user( $payment_method_id, wp_get_current_user() );
$this->add_token_to_order( $order, $token );

if ( ! empty( $token ) ) {
$payment_method_type = $this->get_payment_method_type_for_setup_intent( $intent, $token );
$this->set_payment_method_title_for_order( $order, $payment_method_type, $payment_method_details );
}
} catch ( Exception $e ) {
// If saving the token fails, log the error message but catch the error to avoid crashing the checkout flow.
Logger::log( 'Error when saving payment method: ' . $e->getMessage() );
Expand Down Expand Up @@ -4277,6 +4279,17 @@ private function get_payment_method_type_from_payment_details( $payment_method_d
return $payment_method_details['type'] ?? null;
}

/**
* Get the payment method used with a setup intent.
*
* @param WC_Payments_API_Setup_Intention $intent The PaymentIntent object.
* @param WC_Payment_Token $token The payment token.
* @return string|null The payment method type.
*/
private function get_payment_method_type_for_setup_intent( $intent, $token ) {
return 'wcpay_link' !== $token->get_type() ? $intent->get_payment_method_type() : Link_Payment_Method::PAYMENT_METHOD_STRIPE_ID;
}

/**
* This function wraps WC_Payments::get_payment_method_map, useful for unit testing.
*
Expand Down
10 changes: 10 additions & 0 deletions includes/class-wc-payment-token-wcpay-link.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ public function get_redacted_email( $context = 'view' ) {
return $this->redact_email_address( $this->get_email( $context ) );
}

/**
* Returns the type of this payment token (CC, eCheck, or something else).
*
* @param string $deprecated Deprecated since WooCommerce 3.0.
* @return string Payment Token Type (CC, eCheck)
*/
public function get_type( $deprecated = '' ) {
return self::TYPE;
}

/**
* Transforms email address into redacted/shortened format like ***[email protected].
* Using shortened length of four characters will mimic CC last-4 digits of card number.
Expand Down
Loading