2022-07-08 05:53:24 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import {
|
2023-06-28 08:43:38 +00:00
|
|
|
CanMakePaymentArgument,
|
2022-07-08 05:53:24 +00:00
|
|
|
ExpressPaymentMethodConfigInstance,
|
|
|
|
PaymentMethodConfigInstance,
|
2022-12-23 11:59:02 +00:00
|
|
|
} from '@woocommerce/types';
|
2022-07-08 05:53:24 +00:00
|
|
|
import { CURRENT_USER_IS_ADMIN, getSetting } from '@woocommerce/settings';
|
|
|
|
import { dispatch, select } from '@wordpress/data';
|
2022-10-20 15:02:43 +00:00
|
|
|
import {
|
|
|
|
deriveSelectedShippingRates,
|
|
|
|
emptyHiddenAddressFields,
|
|
|
|
} from '@woocommerce/base-utils';
|
2022-07-08 05:53:24 +00:00
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
|
|
|
import {
|
|
|
|
getExpressPaymentMethods,
|
|
|
|
getPaymentMethods,
|
|
|
|
} from '@woocommerce/blocks-registry';
|
2022-10-20 15:02:43 +00:00
|
|
|
import { previewCart } from '@woocommerce/resource-previews';
|
2022-07-08 05:53:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2022-11-16 09:38:48 +00:00
|
|
|
import { STORE_KEY as CART_STORE_KEY } from '../../cart/constants';
|
|
|
|
import { STORE_KEY as PAYMENT_STORE_KEY } from '../constants';
|
|
|
|
import { noticeContexts } from '../../../base/context/event-emit';
|
2022-10-20 15:02:43 +00:00
|
|
|
import {
|
|
|
|
EMPTY_CART_ERRORS,
|
|
|
|
EMPTY_CART_ITEM_ERRORS,
|
|
|
|
EMPTY_EXTENSIONS,
|
2022-11-24 14:20:44 +00:00
|
|
|
} from '../../../data/constants';
|
|
|
|
import { defaultCartState } from '../../../data/cart/default-state';
|
2022-07-08 05:53:24 +00:00
|
|
|
|
2023-06-28 08:43:38 +00:00
|
|
|
/**
|
|
|
|
* Get the argument that will be passed to a payment method's `canMakePayment` method.
|
|
|
|
*/
|
|
|
|
export const getCanMakePaymentArg = (): CanMakePaymentArgument => {
|
2022-07-08 05:53:24 +00:00
|
|
|
const isEditor = !! select( 'core/editor' );
|
2023-06-28 08:43:38 +00:00
|
|
|
let canPayArgument: CanMakePaymentArgument;
|
2022-10-20 15:02:43 +00:00
|
|
|
|
|
|
|
if ( ! isEditor ) {
|
|
|
|
const store = select( CART_STORE_KEY );
|
|
|
|
const cart = store.getCartData();
|
|
|
|
const cartErrors = store.getCartErrors();
|
|
|
|
const cartTotals = store.getCartTotals();
|
|
|
|
const cartIsLoading = ! store.hasFinishedResolution( 'getCartData' );
|
|
|
|
const isLoadingRates = store.isCustomerDataUpdating();
|
|
|
|
const selectedShippingMethods = deriveSelectedShippingRates(
|
|
|
|
cart.shippingRates
|
|
|
|
);
|
|
|
|
|
2023-06-28 08:43:38 +00:00
|
|
|
const cartForCanPayArgument = {
|
2022-10-20 15:02:43 +00:00
|
|
|
cartCoupons: cart.coupons,
|
|
|
|
cartItems: cart.items,
|
|
|
|
crossSellsProducts: cart.crossSells,
|
|
|
|
cartFees: cart.fees,
|
|
|
|
cartItemsCount: cart.itemsCount,
|
|
|
|
cartItemsWeight: cart.itemsWeight,
|
|
|
|
cartNeedsPayment: cart.needsPayment,
|
|
|
|
cartNeedsShipping: cart.needsShipping,
|
|
|
|
cartItemErrors: cart.errors,
|
|
|
|
cartTotals,
|
|
|
|
cartIsLoading,
|
|
|
|
cartErrors,
|
|
|
|
billingData: emptyHiddenAddressFields( cart.billingAddress ),
|
|
|
|
billingAddress: emptyHiddenAddressFields( cart.billingAddress ),
|
|
|
|
shippingAddress: emptyHiddenAddressFields( cart.shippingAddress ),
|
|
|
|
extensions: cart.extensions,
|
|
|
|
shippingRates: cart.shippingRates,
|
|
|
|
isLoadingRates,
|
|
|
|
cartHasCalculatedShipping: cart.hasCalculatedShipping,
|
|
|
|
paymentRequirements: cart.paymentRequirements,
|
|
|
|
receiveCart: dispatch( CART_STORE_KEY ).receiveCart,
|
|
|
|
};
|
|
|
|
canPayArgument = {
|
|
|
|
cart: cartForCanPayArgument,
|
|
|
|
cartTotals: cart.totals,
|
|
|
|
cartNeedsShipping: cart.needsShipping,
|
|
|
|
billingData: cart.billingAddress,
|
|
|
|
billingAddress: cart.billingAddress,
|
|
|
|
shippingAddress: cart.shippingAddress,
|
|
|
|
selectedShippingMethods,
|
2023-03-13 10:29:17 +00:00
|
|
|
paymentMethods: cart.paymentMethods,
|
2022-10-20 15:02:43 +00:00
|
|
|
paymentRequirements: cart.paymentRequirements,
|
|
|
|
};
|
|
|
|
} else {
|
2023-06-28 08:43:38 +00:00
|
|
|
const cartForCanPayArgument = {
|
2022-10-20 15:02:43 +00:00
|
|
|
cartCoupons: previewCart.coupons,
|
|
|
|
cartItems: previewCart.items,
|
|
|
|
crossSellsProducts: previewCart.cross_sells,
|
|
|
|
cartFees: previewCart.fees,
|
|
|
|
cartItemsCount: previewCart.items_count,
|
|
|
|
cartItemsWeight: previewCart.items_weight,
|
|
|
|
cartNeedsPayment: previewCart.needs_payment,
|
|
|
|
cartNeedsShipping: previewCart.needs_shipping,
|
|
|
|
cartItemErrors: EMPTY_CART_ITEM_ERRORS,
|
|
|
|
cartTotals: previewCart.totals,
|
|
|
|
cartIsLoading: false,
|
|
|
|
cartErrors: EMPTY_CART_ERRORS,
|
2022-11-24 14:20:44 +00:00
|
|
|
billingData: defaultCartState.cartData.billingAddress,
|
|
|
|
billingAddress: defaultCartState.cartData.billingAddress,
|
|
|
|
shippingAddress: defaultCartState.cartData.shippingAddress,
|
2022-10-20 15:02:43 +00:00
|
|
|
extensions: EMPTY_EXTENSIONS,
|
|
|
|
shippingRates: previewCart.shipping_rates,
|
|
|
|
isLoadingRates: false,
|
|
|
|
cartHasCalculatedShipping: previewCart.has_calculated_shipping,
|
2022-11-16 09:38:48 +00:00
|
|
|
paymentRequirements: previewCart.payment_requirements,
|
|
|
|
receiveCart: () => undefined,
|
2022-10-20 15:02:43 +00:00
|
|
|
};
|
|
|
|
canPayArgument = {
|
|
|
|
cart: cartForCanPayArgument,
|
2023-06-28 08:43:38 +00:00
|
|
|
cartTotals: cartForCanPayArgument.cartTotals,
|
|
|
|
cartNeedsShipping: cartForCanPayArgument.cartNeedsShipping,
|
2022-10-20 15:02:43 +00:00
|
|
|
billingData: cartForCanPayArgument.billingAddress,
|
|
|
|
billingAddress: cartForCanPayArgument.billingAddress,
|
|
|
|
shippingAddress: cartForCanPayArgument.shippingAddress,
|
|
|
|
selectedShippingMethods: deriveSelectedShippingRates(
|
|
|
|
cartForCanPayArgument.shippingRates
|
|
|
|
),
|
2023-03-13 10:29:17 +00:00
|
|
|
paymentMethods: previewCart.payment_methods,
|
2022-10-20 15:02:43 +00:00
|
|
|
paymentRequirements: cartForCanPayArgument.paymentRequirements,
|
|
|
|
};
|
|
|
|
}
|
2022-07-08 05:53:24 +00:00
|
|
|
|
2023-06-28 08:43:38 +00:00
|
|
|
return canPayArgument;
|
|
|
|
};
|
|
|
|
|
|
|
|
const registrationErrorNotice = (
|
|
|
|
paymentMethod:
|
|
|
|
| ExpressPaymentMethodConfigInstance
|
|
|
|
| PaymentMethodConfigInstance,
|
|
|
|
errorMessage: string,
|
|
|
|
express = false
|
|
|
|
) => {
|
|
|
|
const { createErrorNotice } = dispatch( 'core/notices' );
|
|
|
|
const noticeContext = express
|
|
|
|
? noticeContexts.EXPRESS_PAYMENTS
|
|
|
|
: noticeContexts.PAYMENTS;
|
|
|
|
const errorText = sprintf(
|
|
|
|
/* translators: %s the id of the payment method being registered (bank transfer, cheque...) */
|
|
|
|
__(
|
|
|
|
`There was an error registering the payment method with id '%s': `,
|
2023-12-12 22:12:36 +00:00
|
|
|
'woocommerce'
|
2023-06-28 08:43:38 +00:00
|
|
|
),
|
|
|
|
paymentMethod.paymentMethodId
|
|
|
|
);
|
|
|
|
createErrorNotice( `${ errorText } ${ errorMessage }`, {
|
|
|
|
context: noticeContext,
|
|
|
|
id: `wc-${ paymentMethod.paymentMethodId }-registration-error`,
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
export const checkPaymentMethodsCanPay = async ( express = false ) => {
|
|
|
|
let availablePaymentMethods = {};
|
|
|
|
|
|
|
|
const paymentMethods = express
|
|
|
|
? getExpressPaymentMethods()
|
|
|
|
: getPaymentMethods();
|
|
|
|
|
|
|
|
const addAvailablePaymentMethod = (
|
|
|
|
paymentMethod:
|
|
|
|
| PaymentMethodConfigInstance
|
|
|
|
| ExpressPaymentMethodConfigInstance
|
|
|
|
) => {
|
2024-09-18 19:49:27 +00:00
|
|
|
if ( express ) {
|
|
|
|
const { name, title, description, gatewayId, supports } =
|
|
|
|
paymentMethod as ExpressPaymentMethodConfigInstance;
|
|
|
|
|
|
|
|
availablePaymentMethods = {
|
|
|
|
...availablePaymentMethods,
|
|
|
|
[ paymentMethod.name ]: {
|
|
|
|
name,
|
|
|
|
title,
|
|
|
|
description,
|
|
|
|
gatewayId,
|
|
|
|
supportsStyle: supports?.style,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
const { name } = paymentMethod as PaymentMethodConfigInstance;
|
|
|
|
|
|
|
|
availablePaymentMethods = {
|
|
|
|
...availablePaymentMethods,
|
|
|
|
[ paymentMethod.name ]: {
|
|
|
|
name,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
2023-06-28 08:43:38 +00:00
|
|
|
};
|
|
|
|
|
2023-03-13 10:29:17 +00:00
|
|
|
// Order payment methods.
|
|
|
|
const paymentMethodsOrder = express
|
|
|
|
? Object.keys( paymentMethods )
|
|
|
|
: Array.from(
|
|
|
|
new Set( [
|
2023-08-07 10:30:53 +00:00
|
|
|
...( getSetting( 'paymentMethodSortOrder', [] ) as [] ),
|
2023-03-13 10:29:17 +00:00
|
|
|
...Object.keys( paymentMethods ),
|
|
|
|
] )
|
|
|
|
);
|
2023-06-28 08:43:38 +00:00
|
|
|
const canPayArgument = getCanMakePaymentArg();
|
2023-03-13 10:29:17 +00:00
|
|
|
const cartPaymentMethods = canPayArgument.paymentMethods as string[];
|
2023-06-28 08:43:38 +00:00
|
|
|
const isEditor = !! select( 'core/editor' );
|
2022-07-08 05:53:24 +00:00
|
|
|
|
|
|
|
for ( let i = 0; i < paymentMethodsOrder.length; i++ ) {
|
|
|
|
const paymentMethodName = paymentMethodsOrder[ i ];
|
|
|
|
const paymentMethod = paymentMethods[ paymentMethodName ];
|
2023-03-13 10:29:17 +00:00
|
|
|
|
2022-07-08 05:53:24 +00:00
|
|
|
if ( ! paymentMethod ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// See if payment method should be available. This always evaluates to true in the editor context.
|
|
|
|
try {
|
2023-03-13 10:29:17 +00:00
|
|
|
const validForCart =
|
|
|
|
isEditor || express
|
|
|
|
? true
|
|
|
|
: cartPaymentMethods.includes( paymentMethodName );
|
2022-07-08 05:53:24 +00:00
|
|
|
const canPay = isEditor
|
|
|
|
? true
|
2023-03-13 10:29:17 +00:00
|
|
|
: validForCart &&
|
|
|
|
( await Promise.resolve(
|
2022-07-08 05:53:24 +00:00
|
|
|
paymentMethod.canMakePayment( canPayArgument )
|
2023-03-13 10:29:17 +00:00
|
|
|
) );
|
2022-07-08 05:53:24 +00:00
|
|
|
|
|
|
|
if ( canPay ) {
|
|
|
|
if ( typeof canPay === 'object' && canPay.error ) {
|
|
|
|
throw new Error( canPay.error.message );
|
|
|
|
}
|
|
|
|
addAvailablePaymentMethod( paymentMethod );
|
|
|
|
}
|
|
|
|
} catch ( e ) {
|
|
|
|
if ( CURRENT_USER_IS_ADMIN || isEditor ) {
|
2023-03-13 10:29:17 +00:00
|
|
|
registrationErrorNotice( paymentMethod, e as string, express );
|
2022-07-08 05:53:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-13 10:29:17 +00:00
|
|
|
|
|
|
|
const availablePaymentMethodNames = Object.keys( availablePaymentMethods );
|
2022-07-08 05:53:24 +00:00
|
|
|
const currentlyAvailablePaymentMethods = express
|
2022-10-06 12:46:46 +00:00
|
|
|
? select( PAYMENT_STORE_KEY ).getAvailableExpressPaymentMethods()
|
|
|
|
: select( PAYMENT_STORE_KEY ).getAvailablePaymentMethods();
|
2022-07-08 05:53:24 +00:00
|
|
|
|
|
|
|
if (
|
|
|
|
Object.keys( currentlyAvailablePaymentMethods ).length ===
|
|
|
|
availablePaymentMethodNames.length &&
|
|
|
|
Object.keys( currentlyAvailablePaymentMethods ).every( ( current ) =>
|
|
|
|
availablePaymentMethodNames.includes( current )
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
// All the names are the same, no need to dispatch more actions.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-10-05 10:04:16 +00:00
|
|
|
const {
|
|
|
|
__internalSetAvailablePaymentMethods,
|
|
|
|
__internalSetAvailableExpressPaymentMethods,
|
2022-10-06 12:46:46 +00:00
|
|
|
} = dispatch( PAYMENT_STORE_KEY );
|
2023-03-13 10:29:17 +00:00
|
|
|
|
|
|
|
const setCallback = express
|
|
|
|
? __internalSetAvailableExpressPaymentMethods
|
|
|
|
: __internalSetAvailablePaymentMethods;
|
|
|
|
|
|
|
|
setCallback( availablePaymentMethods );
|
2022-07-08 05:53:24 +00:00
|
|
|
return true;
|
|
|
|
};
|