Skip to content

Commit 6056111

Browse files
Move capability ID to a helper function (#4073)
* Move capability check ID to a helper function * Fix capability tests * Fix ACH capability checks in JS * Move comment to more relevant spot in code --------- Co-authored-by: Diego Curbelo <[email protected]>
1 parent 9b931ab commit 6056111

File tree

7 files changed

+71
-41
lines changed

7 files changed

+71
-41
lines changed

client/components/payment-method-capability-status-pill/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ const IconComponent = ( { children, ...props } ) => (
4747

4848
const PaymentMethodCapabilityStatusPill = ( { id, label } ) => {
4949
const capabilities = useGetCapabilities();
50-
const capabilityStatus = capabilities[ `${ id }_payments` ];
50+
const capabilityStatus =
51+
id === 'us_bank_account'
52+
? capabilities[ `${ id }_ach_payments` ]
53+
: capabilities[ `${ id }_payments` ];
5154

5255
return (
5356
<>

client/data/account/actions.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,18 @@ export function* refreshAccount() {
4040

4141
// Check new payment methods available for account.
4242
const newPaymentMethods = activeCapabilitiesAfterRefresh.filter(
43-
( paymentMethod ) =>
44-
! activeCapabilitiesBeforeRefresh.includes( paymentMethod ) &&
45-
PaymentMethodsMap[
46-
paymentMethod.replace( '_payments', '' )
47-
] !== undefined
43+
( capability ) => {
44+
const paymentMethodFromCapability =
45+
capability === 'us_bank_account_ach_payments'
46+
? 'us_bank_account'
47+
: capability.replace( '_payments', '' );
48+
49+
return (
50+
! activeCapabilitiesBeforeRefresh.includes( capability ) &&
51+
PaymentMethodsMap[ paymentMethodFromCapability ] !==
52+
undefined
53+
);
54+
}
4855
);
4956

5057
// If there are new payment methods available, show a toast informing the user.
@@ -57,9 +64,14 @@ export function* refreshAccount() {
5764
'woocommerce-gateway-stripe'
5865
),
5966
newPaymentMethods
60-
.map( ( method ) => {
67+
.map( ( capability ) => {
68+
const paymentMethodFromCapability =
69+
capability === 'us_bank_account_ach_payments'
70+
? 'us_bank_account'
71+
: capability.replace( '_payments', '' );
72+
6173
return PaymentMethodsMap[
62-
method.replace( '_payments', '' )
74+
paymentMethodFromCapability
6375
].label;
6476
} )
6577
.join( ', ' )

client/settings/general-settings-section/payment-methods-unavailable-list.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,13 @@ const PaymentMethodsUnavailableList = () => {
1010
const capabilities = useGetCapabilities();
1111
const upePaymentMethodIds = useGetAvailablePaymentMethodIds();
1212
const unavailablePaymentMethodIds = upePaymentMethodIds
13-
.filter(
14-
( methodId ) =>
15-
! capabilities.hasOwnProperty( `${ methodId }_payments` )
16-
)
13+
.filter( ( methodId ) => {
14+
const capabilityId =
15+
methodId === 'us_bank_account'
16+
? `${ methodId }_ach_payments`
17+
: `${ methodId }_payments`;
18+
return ! capabilities.hasOwnProperty( capabilityId );
19+
} )
1720
.filter( ( id ) => id !== PAYMENT_METHOD_LINK );
1821
const unavailablePaymentMethods = unavailablePaymentMethodIds
1922
.filter( ( methodId, idx ) => idx < countIconsToDisplay )

includes/class-wc-stripe-helper.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -737,10 +737,7 @@ public static function filter_payment_methods_with_capabilities( $payment_method
737737
$payment_method_ids_with_capability = [];
738738

739739
foreach ( $payment_method_ids as $payment_method_id ) {
740-
$key = $payment_method_id . '_payments';
741-
if ( WC_Stripe_UPE_Payment_Method_ACH::STRIPE_ID === $payment_method_id ) {
742-
$key = $payment_method_id . '_ach_payments';
743-
}
740+
$key = self::get_payment_method_capability_id( $payment_method_id );
744741
// Check if the payment method has capabilities set in the account data.
745742
// Generally the key is the payment method id appended with '_payments' (i.e. 'card_payments', 'sepa_debit_payments', 'klarna_payments').
746743
// In some cases, the Stripe account might have the legacy key set. For example, for Klarna, the legacy key is 'klarna'.
@@ -1656,4 +1653,19 @@ public static function maybe_log_ip_issues( $ip_address ) {
16561653
WC_Stripe_Logger::log( 'Invalid IP address detected. Data: ' . wp_json_encode( $log_data ) );
16571654
}
16581655
}
1656+
1657+
/**
1658+
* Return capability ID based on payment method ID.
1659+
*
1660+
* @param string $payment_method_id The payment method ID.
1661+
* @return string The capability ID.
1662+
*/
1663+
public static function get_payment_method_capability_id( $payment_method_id ) {
1664+
// "_payments" is a suffix that comes from Stripe API, except when it is "transfers" or ACH.
1665+
if ( WC_Stripe_UPE_Payment_Method_ACH::STRIPE_ID === $payment_method_id ) {
1666+
return $payment_method_id . '_ach_payments';
1667+
}
1668+
1669+
return $payment_method_id . '_payments';
1670+
}
16591671
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1841,7 +1841,7 @@ public function generate_upe_checkout_experience_accepted_payments_html( $key, $
18411841
foreach ( $this->payment_methods as $method_id => $method ) {
18421842
$method_enabled = in_array( $method_id, $this->get_upe_enabled_payment_method_ids(), true ) && ( $is_automatic_capture_enabled || ! $method->requires_automatic_capture() ) ? 'enabled' : 'disabled';
18431843
$method_enabled_label = 'enabled' === $method_enabled ? __( 'enabled', 'woocommerce-gateway-stripe' ) : __( 'disabled', 'woocommerce-gateway-stripe' );
1844-
$capability_id = "{$method_id}_payments"; // "_payments" is a suffix that comes from Stripe API, except when it is "transfers", which does not apply here
1844+
$capability_id = WC_Stripe_Helper::get_payment_method_capability_id( $method_id );
18451845
$method_status = isset( $stripe_capabilities[ $capability_id ] ) ? $stripe_capabilities[ $capability_id ] : 'inactive';
18461846
$subtext_messages = $method->get_subtext_messages( $method_status );
18471847
$aria_label = sprintf(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ public function is_capability_active() {
342342
if ( empty( $capabilities ) ) {
343343
return false;
344344
}
345-
$key = $this->get_id() . '_payments';
345+
$key = WC_Stripe_Helper::get_payment_method_capability_id( $this->get_id() );
346346
return isset( $capabilities[ $key ] ) && 'active' === $capabilities[ $key ];
347347
}
348348

tests/phpunit/payment-methods/test-class-wc-stripe-upe-payment-method.php

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -125,28 +125,28 @@ class WC_Stripe_UPE_Payment_Method_Test extends WP_UnitTestCase {
125125
* Mock capabilities object from Stripe response--all active.
126126
*/
127127
const MOCK_ACTIVE_CAPABILITIES_RESPONSE = [
128-
'alipay_payments' => 'active',
129-
'bancontact_payments' => 'active',
130-
'card_payments' => 'active',
131-
'eps_payments' => 'active',
132-
'giropay_payments' => 'active',
133-
'klarna_payments' => 'active',
134-
'affirm_payments' => 'active',
135-
'clearpay_afterpay_payments' => 'active',
136-
'ideal_payments' => 'active',
137-
'p24_payments' => 'active',
138-
'sepa_debit_payments' => 'active',
139-
'sofort_payments' => 'active',
140-
'transfers' => 'active',
141-
'multibanco_payments' => 'active',
142-
'boleto_payments' => 'active',
143-
'oxxo_payments' => 'active',
144-
'link_payments' => 'active',
145-
'cashapp_payments' => 'active',
146-
'wechat_pay_payments' => 'active',
147-
'acss_debit_payments' => 'active',
148-
'us_bank_account_payments' => 'active',
149-
'bacs_debit_payments' => 'active',
128+
'alipay_payments' => 'active',
129+
'bancontact_payments' => 'active',
130+
'card_payments' => 'active',
131+
'eps_payments' => 'active',
132+
'giropay_payments' => 'active',
133+
'klarna_payments' => 'active',
134+
'affirm_payments' => 'active',
135+
'clearpay_afterpay_payments' => 'active',
136+
'ideal_payments' => 'active',
137+
'p24_payments' => 'active',
138+
'sepa_debit_payments' => 'active',
139+
'sofort_payments' => 'active',
140+
'transfers' => 'active',
141+
'multibanco_payments' => 'active',
142+
'boleto_payments' => 'active',
143+
'oxxo_payments' => 'active',
144+
'link_payments' => 'active',
145+
'cashapp_payments' => 'active',
146+
'wechat_pay_payments' => 'active',
147+
'acss_debit_payments' => 'active',
148+
'us_bank_account_ach_payments' => 'active',
149+
'bacs_debit_payments' => 'active',
150150
];
151151

152152
/**
@@ -482,7 +482,7 @@ public function test_payment_methods_are_only_enabled_when_capability_is_active(
482482

483483
$this->assertFalse( $payment_method->is_enabled_at_checkout( null, $currency ) );
484484

485-
$capability_key = $payment_method->get_id() . '_payments';
485+
$capability_key = WC_Stripe_Helper::get_payment_method_capability_id( $payment_method->get_id() );
486486
$mock_capabilities_response[ $capability_key ] = 'active';
487487

488488
$this->set_mock_payment_method_return_value( 'get_capabilities_response', $mock_capabilities_response, true );

0 commit comments

Comments
 (0)