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

Register only reusable payment methods when adding a payment method #4427

Merged
Show file tree
Hide file tree
Changes from 4 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
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.


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

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

@harriswong harriswong Aug 4, 2022

Choose a reason for hiding this comment

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

Ah I see, 'li.woocommerce-PaymentMethod' is used inside http://localhost:8082/my-account/add-payment-method/. I was trying to look for this on the http://localhost:8082/checkout/ but I can't find it.

I wonder if it's worth adding a comment here so we don't accidentally remove it in the future.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes! This was a head-scratcher for me as well. Comment added.

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 @@ -422,7 +435,6 @@ jQuery( function ( $ ) {
useSetUpIntent
);
}
mountUPEElement( useSetUpIntent );
}
}

Expand Down Expand Up @@ -743,15 +755,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']"
Copy link
Contributor

Choose a reason for hiding this comment

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

Are we removing this because in http://localhost:8082/my-account/add-payment-method/, it will no longer be only "Credit Card/debit card"?

image

Do you know why we needed to return; early before?

Copy link
Member Author

Choose a reason for hiding this comment

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

Are we removing this because in http://localhost:8082/my-account/add-payment-method/, it will no longer be only "Credit Card/debit card"?

Yep. woocommerce_payments is the attribute we'll see for Credit Card/debit card while other gateways will append the gateway name, like woocommerce_payments_sepa_debit.

Do you know why we needed to return; early before?

I don't exactly 🤔.

).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;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it makes sense that http://localhost:8082/my-account/add-payment-method/ only display payment methods that are saved, but not every single payment method we support. 👍

}
}

Expand Down