Skip to content

Commit

Permalink
Register only reusable payment methods when adding a payment method (#…
Browse files Browse the repository at this point in the history
…4427)

* Register only reusable payment methods when adding a payment method

* Changelog entry

* Remove unnecessary UPE mount

* Get selected gateway on Add Payment Method page

* Add comment regarding payment method selection.
  • Loading branch information
mdmoore authored Aug 8, 2022
1 parent db49291 commit 7ce0482
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
5 changes: 5 additions & 0 deletions changelog/fix-4396-filter-reusable-add-payment-methods
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Significance: patch
Type: fix
Comment: This is a minor fix to a larger PR that is forthcoming.


30 changes: 18 additions & 12 deletions client/checkout/classic/upe.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,26 @@ jQuery( function ( $ ) {
*/
const getSelectedGatewayPaymentMethod = () => {
const gatewayCardId = getUPEConfig( 'gatewayId' );
const selectedGatewayId = $(
'li.wc_payment_method input.input-radio:checked'
).attr( 'id' );
let selectedGatewayId = null;

// Handle payment method selection on the Checkout page or Add Payment Method page where class names differ.

if ( $( 'li.wc_payment_method' ).length ) {
selectedGatewayId = $(
'li.wc_payment_method input.input-radio:checked'
).attr( 'id' );
} else if ( $( 'li.woocommerce-PaymentMethod' ).length ) {
selectedGatewayId = $(
'li.woocommerce-PaymentMethod input.input-radio:checked'
).attr( 'id' );
}

if ( 'payment_method_woocommerce_payments' === selectedGatewayId ) {
selectedGatewayId = 'payment_method_woocommerce_payments_card';
}

let selectedPaymentMethod = null;

for ( const paymentMethodType in paymentMethodsConfig ) {
if (
`payment_method_${ gatewayCardId }_${ paymentMethodType }` ===
Expand Down Expand Up @@ -747,15 +762,6 @@ jQuery( function ( $ ) {

// Handle the add payment method form for WooCommerce Payments.
$( 'form#add_payment_method' ).on( 'submit', function () {
if (
'woocommerce_payments' !==
$(
"#add_payment_method input:checked[name='payment_method']"
).val()
) {
return;
}

if ( ! $( '#wcpay-setup-intent' ).val() ) {
const paymentMethodType = getSelectedGatewayPaymentMethod();
const paymentIntentId =
Expand Down
1 change: 0 additions & 1 deletion includes/class-wc-payment-gateway-wcpay.php
Original file line number Diff line number Diff line change
Expand Up @@ -2580,7 +2580,6 @@ public function schedule_order_tracking( $order_id, $order = null ) {
*
* @throws Exception - When an error occurs in intent creation.
*/

public function create_intent( WC_Order $order, array $selected_payment_method_type, string $capture_method = 'automatic', array $metadata = [], string $customer_id = null ) {
$currency = strtolower( $order->get_currency() );
$converted_amount = WC_Payments_Utils::prepare_amount( $order->get_total(), $currency );
Expand Down
18 changes: 15 additions & 3 deletions includes/class-wc-payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -491,15 +491,27 @@ public static function add_plugin_links( $links ) {
* @return array The list of payment gateways that will be available, including WooCommerce Payments' Gateway class.
*/
public static function register_gateway( $gateways ) {
$gateways[] = self::$legacy_card_gateway;
$gateways[] = self::$legacy_card_gateway;
$reusable_methods[] = self::$legacy_card_gateway;

if ( WC_Payments_Features::is_upe_enabled() ) {
foreach ( self::$card_gateway->get_payment_method_ids_enabled_at_checkout() as $payment_method_id ) {
if ( 'card' === $payment_method_id ) {
continue;
}
$upe_gateway = self::get_payment_gateway_by_id( $payment_method_id );
$gateways[] = $upe_gateway;
$upe_gateway = self::get_payment_gateway_by_id( $payment_method_id );
$upe_payment_method = self::get_payment_method_by_id( $payment_method_id );

if ( $upe_payment_method->is_reusable() ) {
$reusable_methods[] = $upe_gateway;
}

$gateways[] = $upe_gateway;

}

if ( is_add_payment_method_page() ) {
return $reusable_methods;
}
}

Expand Down

0 comments on commit 7ce0482

Please sign in to comment.