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

ECE - Set the order payment method title to the wallet used to process payments #3482

Merged
merged 16 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,6 @@ public function ajax_pay_for_order() {
// Process the payment.
$result = WC_Stripe::get_instance()->get_main_stripe_gateway()->process_payment( $order_id );

$this->express_checkout_helper->add_order_payment_method_title( $order );

// process_payment() should only return `success` or throw an exception.
if ( ! is_array( $result ) || ! isset( $result['result'] ) || 'success' !== $result['result'] || ! isset( $result['redirect'] ) ) {
throw new Exception( __( 'Unable to determine payment success.', 'woocommerce-gateway-stripe' ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1320,36 +1320,6 @@ public function maybe_restore_recurring_chosen_shipping_methods( $previous_chose
WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods );
}

/**
* Adds the express checkout payment method title to the order.
*
* @param WC_Order $order The order.
*/
public function add_order_payment_method_title( $order ) {
if ( empty( $_POST['express_payment_type'] ) || ! isset( $_POST['payment_method'] ) || 'stripe' !== $_POST['payment_method'] ) { // phpcs:ignore WordPress.Security.NonceVerification
return;
}

$express_payment_type = wc_clean( wp_unslash( $_POST['express_payment_type'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
$express_payment_titles = [
'apple_pay' => 'Apple Pay',
'google_pay' => 'Google Pay',
];
$payment_method_title = $express_payment_titles[ $express_payment_type ] ?? false;

if ( ! $payment_method_title ) {
return;
}

$suffix = apply_filters( 'wc_stripe_payment_request_payment_method_title_suffix', 'Stripe' );
if ( ! empty( $suffix ) ) {
$suffix = " ($suffix)";
}

$order->set_payment_method_title( $payment_method_title . $suffix );
$order->save();
}

/**
* Calculates taxes as displayed on cart, based on a product and a particular price.
*
Expand Down
11 changes: 6 additions & 5 deletions includes/payment-methods/class-wc-stripe-upe-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ private function process_payment_with_deferred_intent( int $order_id ) {
}

// Set the selected UPE payment method type title in the WC order.
$this->set_payment_method_title_for_order( $order, $selected_payment_type );
$this->set_payment_method_title_for_order( $order, $selected_payment_type, $payment_method );

// Save the preferred card brand on the order.
$this->maybe_set_preferred_card_brand_for_order( $order, $payment_method );
Expand Down Expand Up @@ -1607,18 +1607,19 @@ public function is_saved_cards_enabled() {
* Set formatted readable payment method title for order,
* using payment method details from accompanying charge.
*
* @param WC_Order $order WC Order being processed.
* @param string $payment_method_type Stripe payment method key.
* @param WC_Order $order WC Order being processed.
* @param string $payment_method_type Stripe payment method key.
* @param stdClass|bool $stripe_payment_method Stripe payment method object.
*
* @since 5.5.0
* @version 5.5.0
*/
public function set_payment_method_title_for_order( $order, $payment_method_type ) {
public function set_payment_method_title_for_order( $order, $payment_method_type, $stripe_payment_method = false ) {
if ( ! isset( $this->payment_methods[ $payment_method_type ] ) ) {
return;
}
$payment_method = $this->payment_methods[ $payment_method_type ];
$payment_method_title = $payment_method->get_title();
$payment_method_title = $payment_method->get_title( $stripe_payment_method );
$payment_method_id = $payment_method instanceof WC_Stripe_UPE_Payment_Method_CC ? $this->id : $payment_method->id;

$order->set_payment_method( $payment_method_id );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function __construct() {
/**
* Returns payment method title
*
* @param array|bool $payment_details Optional payment details from charge object.
* @param stdClass|array|bool $payment_details Optional payment details from charge object.
*
* @return string
*/
Expand Down
50 changes: 33 additions & 17 deletions includes/payment-methods/class-wc-stripe-upe-payment-method-cc.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,16 @@ public function __construct() {
/**
* Returns payment method title
*
* @param array|bool $payment_details Optional payment details from charge object.
* @param stdClass|array|bool $payment_details Optional payment details from charge object.
*
* @return string
*/
public function get_title( $payment_details = false ) {
if ( ! $payment_details ) {
return parent::get_title();
if ( $payment_details && isset( $payment_details->card->wallet->type ) ) {
return $this->get_card_wallet_type_title( $payment_details->card->wallet->type );
}

$details = $payment_details[ $this->stripe_id ];
$funding_types = [
'credit' => __( 'credit', 'woocommerce-gateway-stripe' ),
'debit' => __( 'debit', 'woocommerce-gateway-stripe' ),
'prepaid' => __( 'prepaid', 'woocommerce-gateway-stripe' ),
'unknown' => __( 'unknown', 'woocommerce-gateway-stripe' ),
];

return sprintf(
// Translators: %1$s card brand, %2$s card funding (prepaid, credit, etc.).
__( '%1$s %2$s card', 'woocommerce-gateway-stripe' ),
ucfirst( $details->network ),
$funding_types[ $details->funding ]
);
return parent::get_title();
}

/**
Expand Down Expand Up @@ -127,4 +114,33 @@ public function get_testing_instructions() {
'</a>'
);
}

/**
* Returns the title for the card wallet type.
* This is used to display the title for Apple Pay and Google Pay.
*
* @param $express_payment_type The type of express payment method.
*
* @return string The title for the card wallet type.
*/
private function get_card_wallet_type_title( $express_payment_type ) {
$express_payment_titles = [
'apple_pay' => 'Apple Pay',
'google_pay' => 'Google Pay',
];

$payment_method_title = $express_payment_titles[ $express_payment_type ] ?? false;

if ( ! $payment_method_title ) {
return parent::get_title();
}

$suffix = apply_filters( 'wc_stripe_payment_request_payment_method_title_suffix', 'Stripe' );

if ( ! empty( $suffix ) ) {
$suffix = " ($suffix)";
}

return $payment_method_title . $suffix;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public function is_available() {
/**
* Returns payment method title
*
* @param array|bool $payment_details Optional payment details from charge object.
* @param stdClass|array|bool $payment_details Optional payment details from charge object.
*
* @return string
*/
Expand Down
43 changes: 43 additions & 0 deletions tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -2408,4 +2408,47 @@ public function test_set_payment_method_title_for_order_custom_title() {

$this->assertEquals( 'Custom SEPA Title', $order->get_payment_method_title() );
}

/**
* Test test_set_payment_method_title_for_order with ECE wallet PM.
*/
public function test_set_payment_method_title_for_order_ECE_title() {
$order = WC_Helper_Order::create_order();
update_option( WC_Stripe_Feature_Flags::ECE_FEATURE_FLAG_NAME, 'yes' );

// GOOGLE PAY
$mock_ece_payment_method = (object) [
'card' => (object) [
'brand' => 'visa',
'wallet' => (object) [
'type' => 'google_pay',
],
],
];

$this->mock_gateway->set_payment_method_title_for_order( $order, WC_Stripe_UPE_Payment_Method_CC::STRIPE_ID, $mock_ece_payment_method );
$this->assertEquals( 'Google Pay (Stripe)', $order->get_payment_method_title() );

// APPLE PAY
$mock_ece_payment_method->card->wallet->type = 'apple_pay';
$this->mock_gateway->set_payment_method_title_for_order( $order, WC_Stripe_UPE_Payment_Method_CC::STRIPE_ID, $mock_ece_payment_method );
$this->assertEquals( 'Apple Pay (Stripe)', $order->get_payment_method_title() );

// INVALID
$mock_ece_payment_method->card->wallet->type = 'invalid';
$this->mock_gateway->set_payment_method_title_for_order( $order, WC_Stripe_UPE_Payment_Method_CC::STRIPE_ID, $mock_ece_payment_method );

// Invalid wallet type should default to Credit / Debit Card.
$this->assertEquals( 'Credit / Debit Card', $order->get_payment_method_title() );

// NO WALLET
unset( $mock_ece_payment_method->card->wallet->type );
$this->mock_gateway->set_payment_method_title_for_order( $order, WC_Stripe_UPE_Payment_Method_CC::STRIPE_ID, $mock_ece_payment_method );

// No wallet type should default to Credit / Debit Card.
$this->assertEquals( 'Credit / Debit Card', $order->get_payment_method_title() );

// Unset the feature flag.
delete_option( WC_Stripe_Feature_Flags::ECE_FEATURE_FLAG_NAME );
}
}
20 changes: 0 additions & 20 deletions tests/phpunit/test-class-wc-stripe-upe-payment-method.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,24 +198,6 @@ private function get_id( $payment_method ) {
* Tests basic properties for payment methods.
*/
public function test_payment_methods_show_correct_default_outputs() {
$mock_visa_details = [
'type' => WC_Stripe_Payment_Methods::CARD,
WC_Stripe_Payment_Methods::CARD => $this->array_to_object(
[
'network' => 'visa',
'funding' => 'debit',
]
),
];
$mock_mastercard_details = [
'type' => WC_Stripe_Payment_Methods::CARD,
WC_Stripe_Payment_Methods::CARD => $this->array_to_object(
[
'network' => 'mastercard',
'funding' => 'credit',
]
),
];
$mock_alipay_details = [
'type' => WC_Stripe_Payment_Methods::ALIPAY,
];
Expand Down Expand Up @@ -266,8 +248,6 @@ public function test_payment_methods_show_correct_default_outputs() {
$this->assertEquals( WC_Stripe_Payment_Methods::CARD, $card_method->get_id() );
$this->assertEquals( 'Credit / Debit Card', $card_method->get_label() );
$this->assertEquals( 'Credit / Debit Card', $card_method->get_title() );
$this->assertEquals( 'Visa debit card', $card_method->get_title( $mock_visa_details ) );
$this->assertEquals( 'Mastercard credit card', $card_method->get_title( $mock_mastercard_details ) );
$this->assertTrue( $card_method->is_reusable() );
$this->assertEquals( WC_Stripe_Payment_Methods::CARD, $card_method->get_retrievable_type() );
$this->assertEquals(
Expand Down
Loading