File "utils.ts"

Full Path: /home/vantageo/public_html/cache/cache/cache/cache/cache/.wp-cli/wp-content/plugins/woocommerce/packages/woocommerce-blocks/assets/js/base/context/providers/cart-checkout/payment-methods/utils.ts
File size: 1.07 KB
MIME-type: text/x-java
Charset: utf-8

/**
 * External dependencies
 */
import { getSetting } from '@woocommerce/settings';

/**
 * Internal dependencies
 */
import type { PaymentMethods, CustomerPaymentMethod } from './types';

/**
 * Gets the payment methods saved for the current user after filtering out disabled ones.
 */
export const getCustomerPaymentMethods = (
	availablePaymentMethods: PaymentMethods = {}
): Record< string, CustomerPaymentMethod > => {
	const customerPaymentMethods = getSetting( 'customerPaymentMethods', {} );
	const paymentMethodKeys = Object.keys( customerPaymentMethods );
	const enabledCustomerPaymentMethods = {} as Record<
		string,
		CustomerPaymentMethod
	>;
	paymentMethodKeys.forEach( ( type ) => {
		const methods = customerPaymentMethods[ type ].filter(
			( {
				method: { gateway },
			}: {
				method: {
					gateway: string;
				};
			} ) =>
				gateway in availablePaymentMethods &&
				availablePaymentMethods[ gateway ].supports?.showSavedCards
		);
		if ( methods.length ) {
			enabledCustomerPaymentMethods[ type ] = methods;
		}
	} );
	return enabledCustomerPaymentMethods;
};