Skip to content

Add subscriptions support to Bacs #3987

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

Merged
merged 12 commits into from
Mar 6, 2025
Merged
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* Fix - Skip unnecessary save step when already using a saved payment method for legacy checkout.
* Fix - Avoid duplicate payment method element for classic checkout.
* Fix - ACSS: Handle errors and edge cases.
* Add - Add subscriptions support to Bacs.
* Update - Add tracks events for payment method settings updates.

= 9.2.0 - 2025-02-13 =
Expand Down
13 changes: 13 additions & 0 deletions client/blocks/upe/upe-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
PAYMENT_METHOD_AFTERPAY,
PAYMENT_METHOD_AFTERPAY_CLEARPAY,
PAYMENT_METHOD_CLEARPAY,
PAYMENT_METHOD_BACS,
} from 'wcstripe/stripe-utils/constants';
import { getBlocksConfiguration } from 'wcstripe/blocks/utils';
import Icons from 'wcstripe/payment-method-icons';
Expand Down Expand Up @@ -77,6 +78,18 @@ export const upeElement = ( paymentMethod, api, upeConfig ) => {
! isRestrictedInAnyCountry ||
upeConfig.countries.includes( billingCountry );

// Disable Bacs for subscriptions with free trial.
const cartContainsSubscriptions = cartData.cart.cartItems.every(
( item ) => item.type === 'subscription'
);
if (
paymentMethod === PAYMENT_METHOD_BACS &&
cartContainsSubscriptions &&
cartData.cartTotals.total_price === '0'
) {
return false;
}

return isAvailableInTheCountry && !! api.getStripe();
},
// see .wc-block-checkout__payment-method styles in blocks/style.scss
Expand Down
4 changes: 4 additions & 0 deletions includes/compat/trait-wc-stripe-subscriptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,10 @@ public function maybe_render_subscription_payment_method( $payment_method_to_dis
$source->us_bank_account->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 );
break 3;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* @since x.x.x
*/
class WC_Stripe_UPE_Payment_Method_Bacs_Debit extends WC_Stripe_UPE_Payment_Method {
use WC_Stripe_Subscriptions_Trait;

/**
* The Stripe ID for the payment method.
*/
Expand All @@ -29,10 +31,15 @@ public function __construct() {
$this->label = __( 'Bacs Direct Debit', 'woocommerce-gateway-stripe' );
$this->description = __( 'Bacs Direct Debit enables customers in the UK to pay by providing their bank account details.', 'woocommerce-gateway-stripe' );

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

// Remove Bacs from the “Add Payment Method” page for now, as its implementation will be handled later.
if ( ! is_wc_endpoint_url( 'add-payment-method' ) ) {
$this->supports[] = 'tokenization';
if ( is_wc_endpoint_url( 'add-payment-method' ) ) {
unset( $this->supports['tokenization'] );
}

$this->hide_bacs_for_subscriptions_with_free_trials();
}

/**
Expand Down Expand Up @@ -85,4 +92,23 @@ public function create_payment_token_for_user( $user_id, $payment_method ) {
$token->save();
return $token;
}

public function hide_bacs_for_subscriptions_with_free_trials() {
add_filter(
'woocommerce_available_payment_gateways',
function ( $available_gateways ) {
global $post;
$is_checkout_shortcode_page = wc_post_content_has_shortcode( 'woocommerce_checkout' ) || has_block( 'woocommerce/classic-shortcode', $post );
$is_update_order_review_ajax_request = defined( 'DOING_AJAX' ) && DOING_AJAX && isset( $_REQUEST['wc-ajax'] ) && 'update_order_review' === $_REQUEST['wc-ajax'];
if ( $is_checkout_shortcode_page || $is_update_order_review_ajax_request ) {
// Checking if the amount is zero allows us to process orders that include subscriptions with a free trial,
// as long as another product increases the total amount, ensuring compatibility with Bacs.
if ( WC_Subscriptions_Cart::cart_contains_free_trial() && (float) WC()->cart->total === 0.00 ) {
unset( $available_gateways['stripe_bacs_debit'] );
}
}
return $available_gateways;
}
);
}
}
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ If you get stuck, you can ask for help in the [Plugin Forum](https://wordpress.o
* Fix - Skip unnecessary save step when already using a saved payment method for legacy checkout.
* Fix - Avoid duplicate payment method element for classic checkout.
* Fix - ACSS: Handle errors and edge cases.
* Add - Add subscriptions support to Bacs.
* Update - Add tracks events for payment method settings updates.

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