Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
c2565ec
Show payment methods without currency support at the end of the payme…
daledupreez Jul 24, 2025
dcaba9a
Add JSDoc
daledupreez Jul 24, 2025
7e0455d
Merge branch 'develop' into try/relevance-sort-in-payment-method-page
daledupreez Jul 24, 2025
7377fbf
Memoize and refactor sorting
daledupreez Jul 24, 2025
b3956c5
Fix JS unit tests
daledupreez Jul 24, 2025
4b1506e
Merge branch 'develop' into try/relevance-sort-in-payment-method-page
daledupreez Jul 31, 2025
bf373e3
Add changelog
daledupreez Jul 31, 2025
f1b8d38
Use getSetting() instead of global reference; rename variable
daledupreez Jul 31, 2025
c966d88
Update unit tests to mock getSetting() and centralise currency mocking
daledupreez Jul 31, 2025
9f3e05e
Return early when UPE is disabled; update notes
daledupreez Jul 31, 2025
a0f6d46
Merge branch 'develop' into try/relevance-sort-in-payment-method-page
daledupreez Aug 14, 2025
f3a981a
Refactor logic into central helper function
daledupreez Aug 15, 2025
28c4eb3
Merge branch 'develop' into try/relevance-sort-in-payment-method-page
daledupreez Aug 15, 2025
a2230e5
Update unit tests
daledupreez Aug 15, 2025
3089fce
Add tests for getPaymentMethodUnavailableReason
daledupreez Aug 15, 2025
a13b5c6
Sort plugin conflicts before currency issues
daledupreez Aug 18, 2025
8416544
Merge branch 'develop' into try/relevance-sort-in-payment-method-page
daledupreez Aug 18, 2025
0e5a1fe
Merge branch 'develop' into try/relevance-sort-in-payment-method-page
daledupreez Aug 19, 2025
f74b2bf
Fix changelog entry location
daledupreez Aug 19, 2025
3c6c016
Merge branch 'develop' into try/relevance-sort-in-payment-method-page
daledupreez Aug 19, 2025
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
63 changes: 59 additions & 4 deletions client/settings/general-settings-section/payment-methods-list.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/* global wc_stripe_settings_params */
import { sprintf } from '@wordpress/i18n';
import React from 'react';
import React, { useContext, useMemo } from 'react';
import styled from '@emotion/styled';
import classnames from 'classnames';
import { Icon as IconComponent, dragHandle } from '@wordpress/icons';
import { Reorder } from 'framer-motion';
import interpolateComponents from 'interpolate-components';
import PaymentMethodsMap from '../../payment-methods-map';
import UpeToggleContext from '../upe-toggle/context';
import PaymentMethodDescription from './payment-method-description';
import PaymentMethod from './payment-method';
import {
Expand All @@ -16,6 +17,7 @@ import {
} from 'wcstripe/data';
import { useAccount } from 'wcstripe/data/account';
import PaymentMethodFeesPill from 'wcstripe/components/payment-method-fees-pill';
import { getPaymentMethodCurrencies } from 'wcstripe/utils/use-payment-method-currencies';
import {
PAYMENT_METHOD_AFFIRM,
PAYMENT_METHOD_AFTERPAY_CLEARPAY,
Expand Down Expand Up @@ -126,6 +128,55 @@ const StyledFees = styled( PaymentMethodFeesPill )`
flex: 1 0 auto;
`;

/**
* Hook to sort the payment methods based on whether the payment method is supported by the store currency.
* Unsupported payment methods are placed at the end of the list so irrelevant payment methods don't clutter the screen.
*
* @param {string[]} orderedPaymentMethodIds Ordered payment method IDs.
* @return {string[]} Sorted payment method IDs.
*/
const usePaymentMethodsSortedByStoreCurrencySupport = (
orderedPaymentMethodIds
) => {
const { isUpeEnabled } = useContext( UpeToggleContext );

const storeCurrency = window?.wcSettings?.currency?.code;

// In the logic below, note that getPaymentMethodCurrencies() can return []
// when the payment method supports all currencies.
// Note that when we don't have a store currency, we put all methods in the supported list.

const supportedPaymentMethodIds = useMemo( () => {
return orderedPaymentMethodIds.filter( ( paymentMethodId ) => {
const paymentMethodCurrencies = getPaymentMethodCurrencies(
paymentMethodId,
isUpeEnabled
);
return (
! storeCurrency ||
paymentMethodCurrencies.length === 0 ||
paymentMethodCurrencies.includes( storeCurrency )
);
} );
}, [ orderedPaymentMethodIds, storeCurrency, isUpeEnabled ] );

const unsupportedPaymentMethodIds = useMemo( () => {
return orderedPaymentMethodIds.filter( ( paymentMethodId ) => {
const paymentMethodCurrencies = getPaymentMethodCurrencies(
paymentMethodId,
isUpeEnabled
);
return (
storeCurrency &&
paymentMethodCurrencies.length > 0 &&
! paymentMethodCurrencies.includes( storeCurrency )
);
} );
}, [ orderedPaymentMethodIds, storeCurrency, isUpeEnabled ] );

return [ ...supportedPaymentMethodIds, ...unsupportedPaymentMethodIds ];
};

/**
* Formats the payment method description with the account default currency.
*
Expand Down Expand Up @@ -190,13 +241,17 @@ const GeneralSettingsSection = ( { isChangingDisplayOrder } ) => {
setOrderedPaymentMethodIds( newOrderedPaymentMethodIds );
};

const sortedPaymentMethodIds = usePaymentMethodsSortedByStoreCurrencySupport(
availablePaymentMethods
);

return isChangingDisplayOrder ? (
<DraggableList
axis="y"
values={ availablePaymentMethods }
values={ sortedPaymentMethodIds }
onReorder={ onReorder }
>
{ availablePaymentMethods.map( ( method ) => {
{ sortedPaymentMethodIds.map( ( method ) => {
// Skip giropay as it was deprecated by Jun, 30th 2024.
if ( method === PAYMENT_METHOD_GIROPAY ) {
return null;
Expand Down Expand Up @@ -258,7 +313,7 @@ const GeneralSettingsSection = ( { isChangingDisplayOrder } ) => {
</DraggableList>
) : (
<List>
{ availablePaymentMethods.map( ( method ) => {
{ sortedPaymentMethodIds.map( ( method ) => {
// Skip giropay as it was deprecated by Jun, 30th 2024.
if ( method === PAYMENT_METHOD_GIROPAY ) {
return null;
Expand Down
25 changes: 22 additions & 3 deletions client/utils/use-payment-method-currencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,15 @@ const getAmazonPayCurrencies = () => {
}
};

export const usePaymentMethodCurrencies = ( paymentMethodId ) => {
const { isUpeEnabled } = useContext( UpeToggleContext );

/**
* Returns the currencies supported by a payment method.
* Note that [] is returned for payment methods that support all currencies.
*
* @param {string} paymentMethodId
* @param {boolean} isUpeEnabled
* @return {string[]} Array of currencies supported by that payment method.
*/
export const getPaymentMethodCurrencies = ( paymentMethodId, isUpeEnabled ) => {
switch ( paymentMethodId ) {
case PAYMENT_METHOD_ALIPAY:
return getAliPayCurrencies( isUpeEnabled );
Expand All @@ -247,4 +253,17 @@ export const usePaymentMethodCurrencies = ( paymentMethodId ) => {
}
};

/**
* Hook to return the currencies supported by a payment method.
* Note that [] is returned for payment methods that support all currencies.
*
* @param {string} paymentMethodId
* @return {string[]} Array of currencies supported by that payment method.
*/
export const usePaymentMethodCurrencies = ( paymentMethodId ) => {
const { isUpeEnabled } = useContext( UpeToggleContext );

return getPaymentMethodCurrencies( paymentMethodId, isUpeEnabled );
};

export default usePaymentMethodCurrencies;
Loading