Skip to content

Commit

Permalink
Display Link as payment method on order received page with setup inte…
Browse files Browse the repository at this point in the history
…nts (#8764)

Co-authored-by: Timur Karimov <[email protected]>
Co-authored-by: Timur Karimov <[email protected]>
Co-authored-by: Brett Shumaker <[email protected]>
  • Loading branch information
4 people authored May 6, 2024
1 parent 1d5b91a commit 7f75c4d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 10 deletions.
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 = '';
}

// 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

0 comments on commit 7f75c4d

Please sign in to comment.