Skip to content

Commit ff518d2

Browse files
Mayishadaledupreezdiegocurbelo
authored
Introduce wc_stripe_force_save_payment_method filter (#4243)
* introduce wc_stripe_force_save_payment_method filter * move the filter under the 'should_save_payment_method_from_request' function * add comments and refactor * move the method to helper class * use helper method * show message about saving pm in shortcode checkout * show the force save terms on block checkout without showing the checkbox * add changelog * Fix bad doc block from merge * bail if the user is not logged in * update comment * apply deprecated filter first * remove unnecessary if block --------- Co-authored-by: daledupreez <[email protected]> Co-authored-by: Dale du Preez <[email protected]> Co-authored-by: Diego Curbelo <[email protected]>
1 parent 2761741 commit ff518d2

File tree

6 files changed

+53
-4
lines changed

6 files changed

+53
-4
lines changed

changelog.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
* Tweak - Remove Payment Method Configurations fallback cache
4444
* Tweak - Update deprecation notice message to specify that legacy checkout experience has been deprecated since version 9.6.0
4545
* Update - Remove legacy checkout checkbox from settings
46+
* Add - Introduced `wc_stripe_force_save_payment_method` filter
4647
* Update - Express Checkout: introduce new WP actions for supporting custom checkout fields for classic, shortcode-based checkout
4748
* Fix - Fix payment processing for $0 subscription with recurring coupon
4849
* Dev - Add e2e tests to cover Affirm purchase flow

client/blocks/utils.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ export const shouldSetupOffSessionPayment = (
2929
) => {
3030
return (
3131
shouldShowSaveOption ||
32-
hasAutoRenewingSubscription( isPaymentMethodReusable )
32+
hasAutoRenewingSubscription( isPaymentMethodReusable ) ||
33+
( isPaymentMethodReusable &&
34+
getBlocksConfiguration()?.forceSavePaymentMethod )
3335
);
3436
};
3537

includes/class-wc-stripe-helper.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1785,6 +1785,44 @@ public static function is_currency_supported_for_indian_recurring_payment_mandat
17851785
return in_array( strtolower( $currency ), $supported_currencies, true );
17861786
}
17871787

1788+
/**
1789+
* Checks if the payment method should be saved.
1790+
*
1791+
* @since 9.6.0
1792+
* @param bool $force_save Whether the payment method should be saved.
1793+
* @param string $order_id Order ID.
1794+
* @return bool
1795+
*/
1796+
public static function should_force_save_payment_method( $force_save = false, $order_id = null ) {
1797+
// Do not save the payment method if the user is not logged in.
1798+
if ( ! is_user_logged_in() ) {
1799+
return false;
1800+
}
1801+
1802+
// Backward compatibility for deprecated 'wc_stripe_force_save_source' filter.
1803+
$force_save_payment_method = apply_filters_deprecated(
1804+
'wc_stripe_force_save_source',
1805+
[ $force_save, $order_id ],
1806+
'9.6.0',
1807+
'wc_stripe_force_save_payment_method',
1808+
'The wc_stripe_force_save_source filter is deprecated since WooCommerce Stripe Gateway 9.6.0. Use wc_stripe_force_save_payment_method instead.'
1809+
);
1810+
1811+
/**
1812+
* Filters the flag that decides if the payment method must be saved in all possible situations.
1813+
*
1814+
* @since 9.6.0
1815+
*
1816+
* @param bool $force_save Whether the payment method must be saved.
1817+
* @param string $order_id Order ID.
1818+
*
1819+
* @return bool Whether the payment method must be saved in all situations.
1820+
*/
1821+
$force_save_payment_method = apply_filters( 'wc_stripe_force_save_payment_method', $force_save_payment_method, $order_id );
1822+
1823+
return $force_save_payment_method;
1824+
}
1825+
17881826
/**
17891827
* Returns the description for a refund reason.
17901828
*

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@ public function javascript_params() {
521521
$stripe_params['cartContainsSubscription'] = $this->is_subscription_item_in_cart();
522522
$stripe_params['subscriptionRequiresManualRenewal'] = WC_Stripe_Subscriptions_Helper::is_manual_renewal_required();
523523
$stripe_params['subscriptionManualRenewalEnabled'] = WC_Stripe_Subscriptions_Helper::is_manual_renewal_enabled();
524+
$stripe_params['forceSavePaymentMethod'] = WC_Stripe_Helper::should_force_save_payment_method();
524525
$stripe_params['accountCountry'] = WC_Stripe::get_instance()->account->get_account_country();
525526
$stripe_params['isPaymentRequestEnabled'] = $express_checkout_helper->is_payment_request_enabled();
526527
$stripe_params['isAmazonPayEnabled'] = $express_checkout_helper->is_amazon_pay_enabled();
@@ -829,7 +830,7 @@ public function payment_fields() {
829830
<?php
830831
$methods_enabled_for_saved_payments = array_filter( $this->get_upe_enabled_payment_method_ids(), [ $this, 'is_enabled_for_saved_payments' ] );
831832
if ( $this->is_saved_cards_enabled() && ! empty( $methods_enabled_for_saved_payments ) ) {
832-
$force_save_payment = ( $display_tokenization && ! apply_filters( 'wc_stripe_display_save_payment_method_checkbox', $display_tokenization ) ) || is_add_payment_method_page();
833+
$force_save_payment = ( $display_tokenization && ! apply_filters( 'wc_stripe_display_save_payment_method_checkbox', $display_tokenization ) ) || is_add_payment_method_page() || WC_Stripe_Helper::should_force_save_payment_method();
833834
$this->save_payment_method_checkbox( $force_save_payment );
834835
}
835836

@@ -2670,6 +2671,11 @@ private function should_save_payment_method_from_request( $order_id, $payment_me
26702671
return false;
26712672
}
26722673

2674+
// Save the payment method when forced by the filter.
2675+
if ( WC_Stripe_Helper::should_force_save_payment_method( false, $order_id ) ) {
2676+
return true;
2677+
}
2678+
26732679
// For card/stripe, the request arg is `wc-stripe-new-payment-method` and for our reusable APMs (i.e. bancontact) it's `wc-stripe_bancontact-new-payment-method`.
26742680
$save_payment_method_request_arg = sprintf( 'wc-stripe%s-new-payment-method', WC_Stripe_Payment_Methods::CARD !== $payment_method_type ? '_' . $payment_method_type : '' );
26752681

@@ -3105,7 +3111,8 @@ private function get_payment_method_types_for_intent_creation(
31053111
private function should_upe_payment_method_show_save_option( $payment_method ) {
31063112
if ( $payment_method->is_reusable() ) {
31073113
// If a subscription in the cart, it will be saved by default so no need to show the option.
3108-
return $this->is_saved_cards_enabled() && ! $this->is_subscription_item_in_cart() && ! $this->is_pre_order_charged_upon_release_in_cart();
3114+
// If force save payment method is true, no need to show the option.
3115+
return $this->is_saved_cards_enabled() && ! $this->is_subscription_item_in_cart() && ! $this->is_pre_order_charged_upon_release_in_cart() && ! WC_Stripe_Helper::should_force_save_payment_method();
31093116
}
31103117

31113118
return false;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ public function payment_fields() {
642642
</fieldset>
643643
<?php
644644
if ( $this->should_show_save_option() ) {
645-
$force_save_payment = ( $display_tokenization && ! apply_filters( 'wc_stripe_display_save_payment_method_checkbox', $display_tokenization ) ) || is_add_payment_method_page();
645+
$force_save_payment = ( $display_tokenization && ! apply_filters( 'wc_stripe_display_save_payment_method_checkbox', $display_tokenization ) ) || is_add_payment_method_page() || WC_Stripe_Helper::should_force_save_payment_method();
646646
if ( is_user_logged_in() ) {
647647
$this->save_payment_method_checkbox( $force_save_payment );
648648
}

readme.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
155155
* Dev - Add Klarna e2e tests
156156
* Tweak - Update deprecation notice message to specify that legacy checkout experience has been deprecated since version 9.6.0
157157
* Update - Remove legacy checkout checkbox from settings
158+
* Add - Introduced `wc_stripe_force_save_payment_method` filter
158159
* Update - Express Checkout: introduce new WP actions for supporting custom checkout fields for classic, shortcode-based checkout
159160
* Fix - Fixes page crash when Klarna payment method is not supported in the merchant's country by returning an empty array instead of throwing an error
160161
* Fix - Fix payment processing for $0 subscription with recurring coupon

0 commit comments

Comments
 (0)