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

Add ACSS support for WC Subscriptions #4051

Open
wants to merge 7 commits into
base: fix/3813-saved-payment-methods
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
* Tweak - Fix background in express checkout settings.
* Update - Update Amazon Pay icon to use image from WooCommerce Design Library.
* Add - Add ACSS payment tokenization.
* Add - Add ACSS support for WC Subscriptions.

= 9.2.0 - 2025-02-13 =
* Fix - Fix missing product_id parameter for the express checkout add-to-cart operation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ const PaymentElements = ( {

async function createIntent() {
try {
const response = await api.createIntent(
getBlocksConfiguration()?.orderId,
paymentMethodId
);
const paymentNeeded = getBlocksConfiguration()?.isPaymentNeeded;
const response = paymentNeeded
? await api.createIntent(
getBlocksConfiguration()?.orderId,
paymentMethodId
)
: await api.initSetupIntent( paymentMethodId );

setClientSecret( response.client_secret );
setPaymentIntentId( response.id );
Expand Down
16 changes: 15 additions & 1 deletion includes/abstracts/abstract-wc-stripe-payment-gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -550,8 +550,13 @@ public function process_response( $response, $order ) {
$this->update_fees( $order, is_string( $response->balance_transaction ) ? $response->balance_transaction : $response->balance_transaction->id );
}

// TODO: Refactor and add mandate ID support for other payment methods, if necessary.
// The mandate ID is not available for the intent object, so we need to fetch the charge.
// Mandate ID is necessary for renewal payments for certain payment methods and Indian cards.
if ( isset( $response->payment_method_details->card->mandate ) ) {
$order->update_meta_data( '_stripe_mandate_id', $response->payment_method_details->card->mandate );
} elseif ( isset( $response->payment_method_details->acss_debit->mandate ) ) {
$order->update_meta_data( '_stripe_mandate_id', $response->payment_method_details->acss_debit->mandate );
}

if ( isset( $response->payment_method, $response->payment_method_details ) ) {
Expand Down Expand Up @@ -1618,14 +1623,23 @@ public function save_intent_to_order( $order, $intent ) {
if ( 'payment_intent' === $intent->object ) {
WC_Stripe_Helper::add_payment_intent_to_order( $intent->id, $order );

// Add the mandate id necessary for renewal payments with Indian cards if it's present.
// TODO: Refactor and add mandate ID support for other payment methods, if necessary.
// The mandate ID is not available for the intent object, so we need to fetch the charge.
// Mandate ID is necessary for renewal payments for certain payment methods and Indian cards.
Comment on lines +1626 to +1628
Copy link
Member Author

Choose a reason for hiding this comment

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

The mandate ID here is necessary for renewal orders. Without it, renewal orders will fail.

I think it's best to keep this as a TODO just to indicate that the approach should be refactored if adding new payment methods, and also to make sure we test other payment methods because I figured this is necessary for at least card and ACSS.

@asumaran I couldn't test BACS subscriptions, but this doesn't seem necessary for ACH as far as I have tested. Can you try processing a renewal order for BACS and make sure this is not needed for BACS? There's a similar check in line 1631.

$charge = $this->get_latest_charge_from_intent( $intent );

if ( isset( $charge->payment_method_details->card->mandate ) ) {
$order->update_meta_data( '_stripe_mandate_id', $charge->payment_method_details->card->mandate );
} elseif ( isset( $charge->payment_method_details->acss_debit->mandate ) ) {
$order->update_meta_data( '_stripe_mandate_id', $charge->payment_method_details->acss_debit->mandate );
}
} elseif ( 'setup_intent' === $intent->object ) {
$order->update_meta_data( '_stripe_setup_intent', $intent->id );

// Add mandate for free trial subscriptions.
if ( isset( $intent->mandate ) ) {
$order->update_meta_data( '_stripe_mandate_id', $intent->mandate );
}
Comment on lines +1640 to +1642
Copy link
Member Author

Choose a reason for hiding this comment

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

For payment intents, the mandate ID is only available from the charge object, however for setup intents, the mandate ID can be accessed directly from the intent object.

}

if ( is_callable( [ $order, 'save' ] ) ) {
Expand Down
25 changes: 16 additions & 9 deletions includes/class-wc-stripe-intent-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ public function update_payment_intent_ajax() {
throw new Exception( __( 'Unable to verify your request. Please reload the page and try again.', 'woocommerce-gateway-stripe' ) );
}

wp_send_json_success( $this->update_payment_intent( $payment_intent_id, $order_id, $save_payment_method, $selected_upe_payment_type ), 200 );
wp_send_json_success( $this->update_intent( $payment_intent_id, $order_id, $save_payment_method, $selected_upe_payment_type ), 200 );
} catch ( Exception $e ) {
// Send back error so it can be displayed to the customer.
wp_send_json_error(
Expand All @@ -424,9 +424,10 @@ public function update_payment_intent_ajax() {
}

/**
* Updates payment intent to be able to save payment method.
* Updates payment intent or setup intent to be able to save payment method.
*
* @since 5.6.0
* @version x.x.x
*
* @param {string} $payment_intent_id The id of the payment intent to update.
* @param {int} $order_id The id of the order if intent created from Order.
Expand All @@ -436,7 +437,7 @@ public function update_payment_intent_ajax() {
* @throws Exception If the update intent call returns with an error.
* @return array|null An array with result of the update, or nothing
*/
public function update_payment_intent( $payment_intent_id = '', $order_id = null, $save_payment_method = false, $selected_upe_payment_type = '' ) {
public function update_intent( $payment_intent_id = '', $order_id = null, $save_payment_method = false, $selected_upe_payment_type = '' ) {
Copy link
Member Author

Choose a reason for hiding this comment

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

This method now handles setup intents as well. Looking at the Stripe logs, there was an error where it was incorrectly calling payment_intents/seti_... instead of setup_intents/seti....

Given the similarity of a potential new method for handling only setup intents, I decided to unify both needs into the same function.

I'd appreciate the reviewer double-checking this approach, but I believe it shouldn't affect existing flows, given how minimal the change is.

$order = wc_get_order( $order_id );

if ( ! is_a( $order, 'WC_Order' ) ) {
Expand All @@ -449,15 +450,19 @@ public function update_payment_intent( $payment_intent_id = '', $order_id = null
$customer = new WC_Stripe_Customer( wp_get_current_user()->ID );

if ( $payment_intent_id ) {

$request = [
'amount' => WC_Stripe_Helper::get_stripe_amount( $amount, strtolower( $currency ) ),
'currency' => strtolower( $currency ),
'metadata' => $gateway->get_metadata_from_order( $order ),
/* translators: 1) blog name 2) order number */
'description' => sprintf( __( '%1$s - Order %2$s', 'woocommerce-gateway-stripe' ), wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ), $order->get_order_number() ),
];

$is_setup_intent = substr( $payment_intent_id, 0, 4 ) === 'seti';
if ( ! $is_setup_intent ) {
// These parameters are only supported for payment intents.
$request['amount'] = WC_Stripe_Helper::get_stripe_amount( $amount, strtolower( $currency ) );
$request['currency'] = strtolower( $currency );
}

if ( '' !== $selected_upe_payment_type ) {
// Only update the payment_method_types if we have a reference to the payment type the customer selected.
$request['payment_method_types'] = [ $selected_upe_payment_type ];
Expand Down Expand Up @@ -485,9 +490,11 @@ public function update_payment_intent( $payment_intent_id = '', $order_id = null

$level3_data = $gateway->get_level3_data_from_order( $order );

// Use "setup_intents" endpoint if `$payment_intent_id` starts with `seti_`.
$endpoint = $is_setup_intent ? 'setup_intents' : 'payment_intents';
WC_Stripe_API::request_with_level3_data(
$request,
"payment_intents/{$payment_intent_id}",
"{$endpoint}/{$payment_intent_id}",
$level3_data,
$order
);
Expand Down Expand Up @@ -816,8 +823,8 @@ private function maybe_add_mandate_options( $request, $payment_method_type, $is_
$request['payment_method_options'] = [
WC_Stripe_Payment_Methods::ACSS_DEBIT => [
'mandate_options' => [
'payment_schedule' => 'interval',
'interval_description' => __( 'One-time payment', 'woocommerce-gateway-stripe' ), // TODO: Change to cadence if purchasing a subscription.
'payment_schedule' => 'combined',
'interval_description' => __( 'Payments as per agreement', 'woocommerce-gateway-stripe' ),
Comment on lines +826 to +827
Copy link
Member Author

Choose a reason for hiding this comment

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

I figured it's best to set the payment schedule to combined and the interval description to something that can fit both one-time payments and subscriptions, because the same payment method (and consequently the mandate) can be used for both recurring and non-recurring payments, depending on how the payment method is saved in WooCommerce.

Reference: https://docs.stripe.com/payments/acss-debit#payment-schedule

'transaction_type' => 'personal',
],
],
Expand Down
15 changes: 12 additions & 3 deletions includes/compat/trait-wc-stripe-subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -730,9 +730,9 @@ public function validate_subscription_payment_meta( $payment_method_id, $payment
* mandates for 3DS payments in India. It's ok to apply this across the board; Stripe will
* take care of handling any authorizations.
*
* @param Array $request The HTTP request that will be sent to Stripe to create the payment intent.
* @param array $request The HTTP request that will be sent to Stripe to create the payment intent.
* @param WC_Order $order The renewal order.
* @param Array $prepared_source The source object.
* @param object $prepared_source The source object.
*/
public function add_subscription_information_to_intent( $request, $order, $prepared_source ) {
// Just in case the order doesn't contain a subscription we return the base request.
Expand Down Expand Up @@ -772,6 +772,7 @@ public function add_subscription_information_to_intent( $request, $order, $prepa
}

// Add mandate options to request to create new mandate if mandate id does not already exist in a previous renewal or parent order.
// Note: This is for backwards compatibility if `_stripe_mandate_id` is not set.
$mandate_options = $this->create_mandate_options_for_order( $order, $subscriptions_for_renewal_order );
if ( ! empty( $mandate_options ) ) {
$request['payment_method_options']['card']['mandate_options'] = $mandate_options;
Expand Down Expand Up @@ -989,12 +990,20 @@ public function maybe_render_subscription_payment_method( $payment_method_to_dis
break 3;
case WC_Stripe_Payment_Methods::ACH:
$payment_method_to_display = sprintf(
/* translators: account type (checking, savings), last 4 digits of account. */
/* translators: 1) account type (checking, savings), 2) last 4 digits of account. */
__( 'Via %1$s Account ending in %2$s', 'woocommerce-gateway-stripe' ),
ucfirst( $source->us_bank_account->account_type ),
$source->us_bank_account->last4
);
break 3;
case WC_Stripe_Payment_Methods::ACSS_DEBIT:
$payment_method_to_display = sprintf(
/* translators: 1) bank name, 2) last 4 digits of account. */
__( 'Via %1$s ending in %2$s', 'woocommerce-gateway-stripe' ),
$source->acss_debit->bank_name,
$source->acss_debit->last4
);
break 3;
case WC_Stripe_Payment_Methods::BACS_DEBIT:
/* translators: 1) the Bacs Direct Debit payment method's last 4 numbers */
$payment_method_to_display = sprintf( __( 'Via Bacs Direct Debit ending in (%1$s)', 'woocommerce-gateway-stripe' ), $source->bacs_debit->last4 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ public function process_payment( $order_id, $retry = true, $force_save_source =
if ( $payment_intent_id && ! $this->payment_methods[ $selected_payment_type ]->supports_deferred_intent() ) {
// Adds customer and metadata to PaymentIntent.
// These parameters cannot be added upon updating the intent via the `/confirm` API.
$this->intent_controller->update_payment_intent( $payment_intent_id, $order_id );
$this->intent_controller->update_intent( $payment_intent_id, $order_id );
}

// Flag for using a deferred intent. To be removed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* The Canadian Pre-Authorized Debit (ACSS Debit) Payment Method class extending UPE base class
*/
class WC_Stripe_UPE_Payment_Method_ACSS extends WC_Stripe_UPE_Payment_Method {
use WC_Stripe_Subscriptions_Trait;

const STRIPE_ID = WC_Stripe_Payment_Methods::ACSS_DEBIT;

Expand All @@ -27,6 +28,10 @@ public function __construct() {
);
$this->supports_deferred_intent = false;
$this->supports[] = 'tokenization';
$this->supports[] = 'subscriptions';

// Check if subscriptions are enabled and add support for them.
$this->maybe_init_subscriptions();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*
* Token for ACSS.
*
* @class WC_Payment_Token_ACSS
* @since x.x.x
*/
class WC_Payment_Token_ACSS extends WC_Payment_Token implements WC_Stripe_Payment_Method_Comparison_Interface {
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,6 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
* Tweak - Fix background in express checkout settings.
* Update - Update Amazon Pay icon to use image from WooCommerce Design Library.
* Add - Add ACSS payment tokenization.
* Add - Add ACSS support for WC Subscriptions.

[See changelog for all versions](https://raw.githubusercontent.com/woocommerce/woocommerce-gateway-stripe/trunk/changelog.txt).
Loading