Skip to content

Commit 3a1da1f

Browse files
james-allanMayisha
andauthored
ECE - Set the order payment method title to the wallet used to process payments (#3482)
* Use customisable title rather than label to set order's payment method title * Add changelog entires * Add unit test to verify custom payment method titles * Remove tests which verify subscriptions * Remove function which specifically sets the payment method title on pay-for-order page * Set the express payment method title from payment method information * Add unit tests * Remove whitespace in unit tests * Remove legacy codewhich set the payment method title based on the card type (visa vs mastercard) * Put more expensive logic towards the end of the condition * Remove ece feature flag now that PRBs also use the UPE processing payment flow --------- Co-authored-by: Mayisha <[email protected]>
1 parent 0903542 commit 3a1da1f

8 files changed

+84
-76
lines changed

includes/payment-methods/class-wc-stripe-express-checkout-ajax-handler.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,8 +376,6 @@ public function ajax_pay_for_order() {
376376
// Process the payment.
377377
$result = WC_Stripe::get_instance()->get_main_stripe_gateway()->process_payment( $order_id );
378378

379-
$this->express_checkout_helper->add_order_payment_method_title( $order );
380-
381379
// process_payment() should only return `success` or throw an exception.
382380
if ( ! is_array( $result ) || ! isset( $result['result'] ) || 'success' !== $result['result'] || ! isset( $result['redirect'] ) ) {
383381
throw new Exception( __( 'Unable to determine payment success.', 'woocommerce-gateway-stripe' ) );

includes/payment-methods/class-wc-stripe-express-checkout-helper.php

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,36 +1320,6 @@ public function maybe_restore_recurring_chosen_shipping_methods( $previous_chose
13201320
WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods );
13211321
}
13221322

1323-
/**
1324-
* Adds the express checkout payment method title to the order.
1325-
*
1326-
* @param WC_Order $order The order.
1327-
*/
1328-
public function add_order_payment_method_title( $order ) {
1329-
if ( empty( $_POST['express_payment_type'] ) || ! isset( $_POST['payment_method'] ) || 'stripe' !== $_POST['payment_method'] ) { // phpcs:ignore WordPress.Security.NonceVerification
1330-
return;
1331-
}
1332-
1333-
$express_payment_type = wc_clean( wp_unslash( $_POST['express_payment_type'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
1334-
$express_payment_titles = [
1335-
'apple_pay' => 'Apple Pay',
1336-
'google_pay' => 'Google Pay',
1337-
];
1338-
$payment_method_title = $express_payment_titles[ $express_payment_type ] ?? false;
1339-
1340-
if ( ! $payment_method_title ) {
1341-
return;
1342-
}
1343-
1344-
$suffix = apply_filters( 'wc_stripe_payment_request_payment_method_title_suffix', 'Stripe' );
1345-
if ( ! empty( $suffix ) ) {
1346-
$suffix = " ($suffix)";
1347-
}
1348-
1349-
$order->set_payment_method_title( $payment_method_title . $suffix );
1350-
$order->save();
1351-
}
1352-
13531323
/**
13541324
* Calculates taxes as displayed on cart, based on a product and a particular price.
13551325
*

includes/payment-methods/class-wc-stripe-upe-payment-gateway.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ private function process_payment_with_deferred_intent( int $order_id ) {
856856
}
857857

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

861861
// Save the preferred card brand on the order.
862862
$this->maybe_set_preferred_card_brand_for_order( $order, $payment_method );
@@ -1607,18 +1607,19 @@ public function is_saved_cards_enabled() {
16071607
* Set formatted readable payment method title for order,
16081608
* using payment method details from accompanying charge.
16091609
*
1610-
* @param WC_Order $order WC Order being processed.
1611-
* @param string $payment_method_type Stripe payment method key.
1610+
* @param WC_Order $order WC Order being processed.
1611+
* @param string $payment_method_type Stripe payment method key.
1612+
* @param stdClass|bool $stripe_payment_method Stripe payment method object.
16121613
*
16131614
* @since 5.5.0
16141615
* @version 5.5.0
16151616
*/
1616-
public function set_payment_method_title_for_order( $order, $payment_method_type ) {
1617+
public function set_payment_method_title_for_order( $order, $payment_method_type, $stripe_payment_method = false ) {
16171618
if ( ! isset( $this->payment_methods[ $payment_method_type ] ) ) {
16181619
return;
16191620
}
16201621
$payment_method = $this->payment_methods[ $payment_method_type ];
1621-
$payment_method_title = $payment_method->get_title();
1622+
$payment_method_title = $payment_method->get_title( $stripe_payment_method );
16221623
$payment_method_id = $payment_method instanceof WC_Stripe_UPE_Payment_Method_CC ? $this->id : $payment_method->id;
16231624

16241625
$order->set_payment_method( $payment_method_id );

includes/payment-methods/class-wc-stripe-upe-payment-method-afterpay-clearpay.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function __construct() {
7070
/**
7171
* Returns payment method title
7272
*
73-
* @param array|bool $payment_details Optional payment details from charge object.
73+
* @param stdClass|array|bool $payment_details Optional payment details from charge object.
7474
*
7575
* @return string
7676
*/

includes/payment-methods/class-wc-stripe-upe-payment-method-cc.php

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,29 +36,16 @@ public function __construct() {
3636
/**
3737
* Returns payment method title
3838
*
39-
* @param array|bool $payment_details Optional payment details from charge object.
39+
* @param stdClass|array|bool $payment_details Optional payment details from charge object.
4040
*
4141
* @return string
4242
*/
4343
public function get_title( $payment_details = false ) {
44-
if ( ! $payment_details ) {
45-
return parent::get_title();
44+
if ( $payment_details && isset( $payment_details->card->wallet->type ) ) {
45+
return $this->get_card_wallet_type_title( $payment_details->card->wallet->type );
4646
}
4747

48-
$details = $payment_details[ $this->stripe_id ];
49-
$funding_types = [
50-
'credit' => __( 'credit', 'woocommerce-gateway-stripe' ),
51-
'debit' => __( 'debit', 'woocommerce-gateway-stripe' ),
52-
'prepaid' => __( 'prepaid', 'woocommerce-gateway-stripe' ),
53-
'unknown' => __( 'unknown', 'woocommerce-gateway-stripe' ),
54-
];
55-
56-
return sprintf(
57-
// Translators: %1$s card brand, %2$s card funding (prepaid, credit, etc.).
58-
__( '%1$s %2$s card', 'woocommerce-gateway-stripe' ),
59-
ucfirst( $details->network ),
60-
$funding_types[ $details->funding ]
61-
);
48+
return parent::get_title();
6249
}
6350

6451
/**
@@ -127,4 +114,33 @@ public function get_testing_instructions() {
127114
'</a>'
128115
);
129116
}
117+
118+
/**
119+
* Returns the title for the card wallet type.
120+
* This is used to display the title for Apple Pay and Google Pay.
121+
*
122+
* @param $express_payment_type The type of express payment method.
123+
*
124+
* @return string The title for the card wallet type.
125+
*/
126+
private function get_card_wallet_type_title( $express_payment_type ) {
127+
$express_payment_titles = [
128+
'apple_pay' => 'Apple Pay',
129+
'google_pay' => 'Google Pay',
130+
];
131+
132+
$payment_method_title = $express_payment_titles[ $express_payment_type ] ?? false;
133+
134+
if ( ! $payment_method_title ) {
135+
return parent::get_title();
136+
}
137+
138+
$suffix = apply_filters( 'wc_stripe_payment_request_payment_method_title_suffix', 'Stripe' );
139+
140+
if ( ! empty( $suffix ) ) {
141+
$suffix = " ($suffix)";
142+
}
143+
144+
return $payment_method_title . $suffix;
145+
}
130146
}

includes/payment-methods/class-wc-stripe-upe-payment-method.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public function is_available() {
178178
/**
179179
* Returns payment method title
180180
*
181-
* @param array|bool $payment_details Optional payment details from charge object.
181+
* @param stdClass|array|bool $payment_details Optional payment details from charge object.
182182
*
183183
* @return string
184184
*/

tests/phpunit/test-class-wc-stripe-upe-payment-gateway.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2408,4 +2408,47 @@ public function test_set_payment_method_title_for_order_custom_title() {
24082408

24092409
$this->assertEquals( 'Custom SEPA Title', $order->get_payment_method_title() );
24102410
}
2411+
2412+
/**
2413+
* Test test_set_payment_method_title_for_order with ECE wallet PM.
2414+
*/
2415+
public function test_set_payment_method_title_for_order_ECE_title() {
2416+
$order = WC_Helper_Order::create_order();
2417+
update_option( WC_Stripe_Feature_Flags::ECE_FEATURE_FLAG_NAME, 'yes' );
2418+
2419+
// GOOGLE PAY
2420+
$mock_ece_payment_method = (object) [
2421+
'card' => (object) [
2422+
'brand' => 'visa',
2423+
'wallet' => (object) [
2424+
'type' => 'google_pay',
2425+
],
2426+
],
2427+
];
2428+
2429+
$this->mock_gateway->set_payment_method_title_for_order( $order, WC_Stripe_UPE_Payment_Method_CC::STRIPE_ID, $mock_ece_payment_method );
2430+
$this->assertEquals( 'Google Pay (Stripe)', $order->get_payment_method_title() );
2431+
2432+
// APPLE PAY
2433+
$mock_ece_payment_method->card->wallet->type = 'apple_pay';
2434+
$this->mock_gateway->set_payment_method_title_for_order( $order, WC_Stripe_UPE_Payment_Method_CC::STRIPE_ID, $mock_ece_payment_method );
2435+
$this->assertEquals( 'Apple Pay (Stripe)', $order->get_payment_method_title() );
2436+
2437+
// INVALID
2438+
$mock_ece_payment_method->card->wallet->type = 'invalid';
2439+
$this->mock_gateway->set_payment_method_title_for_order( $order, WC_Stripe_UPE_Payment_Method_CC::STRIPE_ID, $mock_ece_payment_method );
2440+
2441+
// Invalid wallet type should default to Credit / Debit Card.
2442+
$this->assertEquals( 'Credit / Debit Card', $order->get_payment_method_title() );
2443+
2444+
// NO WALLET
2445+
unset( $mock_ece_payment_method->card->wallet->type );
2446+
$this->mock_gateway->set_payment_method_title_for_order( $order, WC_Stripe_UPE_Payment_Method_CC::STRIPE_ID, $mock_ece_payment_method );
2447+
2448+
// No wallet type should default to Credit / Debit Card.
2449+
$this->assertEquals( 'Credit / Debit Card', $order->get_payment_method_title() );
2450+
2451+
// Unset the feature flag.
2452+
delete_option( WC_Stripe_Feature_Flags::ECE_FEATURE_FLAG_NAME );
2453+
}
24112454
}

tests/phpunit/test-class-wc-stripe-upe-payment-method.php

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -198,24 +198,6 @@ private function get_id( $payment_method ) {
198198
* Tests basic properties for payment methods.
199199
*/
200200
public function test_payment_methods_show_correct_default_outputs() {
201-
$mock_visa_details = [
202-
'type' => WC_Stripe_Payment_Methods::CARD,
203-
WC_Stripe_Payment_Methods::CARD => $this->array_to_object(
204-
[
205-
'network' => 'visa',
206-
'funding' => 'debit',
207-
]
208-
),
209-
];
210-
$mock_mastercard_details = [
211-
'type' => WC_Stripe_Payment_Methods::CARD,
212-
WC_Stripe_Payment_Methods::CARD => $this->array_to_object(
213-
[
214-
'network' => 'mastercard',
215-
'funding' => 'credit',
216-
]
217-
),
218-
];
219201
$mock_alipay_details = [
220202
'type' => WC_Stripe_Payment_Methods::ALIPAY,
221203
];
@@ -266,8 +248,6 @@ public function test_payment_methods_show_correct_default_outputs() {
266248
$this->assertEquals( WC_Stripe_Payment_Methods::CARD, $card_method->get_id() );
267249
$this->assertEquals( 'Credit / Debit Card', $card_method->get_label() );
268250
$this->assertEquals( 'Credit / Debit Card', $card_method->get_title() );
269-
$this->assertEquals( 'Visa debit card', $card_method->get_title( $mock_visa_details ) );
270-
$this->assertEquals( 'Mastercard credit card', $card_method->get_title( $mock_mastercard_details ) );
271251
$this->assertTrue( $card_method->is_reusable() );
272252
$this->assertEquals( WC_Stripe_Payment_Methods::CARD, $card_method->get_retrievable_type() );
273253
$this->assertEquals(

0 commit comments

Comments
 (0)