2020-07-15 14:14:41 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { registerPaymentMethod } from '@woocommerce/blocks-registry';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2023-08-07 10:30:53 +00:00
|
|
|
import { getPaymentMethodData } from '@woocommerce/settings';
|
2020-07-15 14:14:41 +00:00
|
|
|
import { decodeEntities } from '@wordpress/html-entities';
|
2023-12-12 15:23:22 +00:00
|
|
|
import { sanitizeHTML } from '@woocommerce/utils';
|
|
|
|
import { RawHTML } from '@wordpress/element';
|
2020-07-15 14:14:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { PAYMENT_METHOD_NAME } from './constants';
|
|
|
|
|
2023-08-07 10:30:53 +00:00
|
|
|
const settings = getPaymentMethodData( 'cod', {} );
|
2023-12-09 13:44:54 +00:00
|
|
|
const defaultLabel = __( 'Cash on delivery', 'woocommerce' );
|
2023-08-07 10:30:53 +00:00
|
|
|
const label = decodeEntities( settings?.title || '' ) || defaultLabel;
|
2020-07-15 14:14:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Content component
|
|
|
|
*/
|
|
|
|
const Content = () => {
|
2023-12-12 15:23:22 +00:00
|
|
|
return <RawHTML>{ sanitizeHTML( settings.description || '' ) }</RawHTML>;
|
2020-07-15 14:14:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Label component
|
|
|
|
*
|
|
|
|
* @param {*} props Props from payment API.
|
|
|
|
*/
|
|
|
|
const Label = ( props ) => {
|
|
|
|
const { PaymentMethodLabel } = props.components;
|
|
|
|
return <PaymentMethodLabel text={ label } />;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determine whether COD is available for this cart/order.
|
|
|
|
*
|
2022-04-08 13:47:19 +00:00
|
|
|
* @param {Object} props Incoming props for the component.
|
|
|
|
* @param {boolean} props.cartNeedsShipping True if the cart contains any physical/shippable products.
|
2020-09-20 23:54:08 +00:00
|
|
|
* @param {boolean} props.selectedShippingMethods
|
|
|
|
*
|
|
|
|
* @return {boolean} True if COD payment method should be displayed as a payment option.
|
2020-07-15 14:14:41 +00:00
|
|
|
*/
|
|
|
|
const canMakePayment = ( { cartNeedsShipping, selectedShippingMethods } ) => {
|
|
|
|
if ( ! settings.enableForVirtual && ! cartNeedsShipping ) {
|
|
|
|
// Store doesn't allow COD for virtual orders AND
|
|
|
|
// order doesn't contain any shippable products.
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-09-14 14:36:14 +00:00
|
|
|
if ( ! settings.enableForShippingMethods.length ) {
|
2020-07-15 14:14:41 +00:00
|
|
|
// Store does not limit COD to specific shipping methods.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-07-21 19:57:21 +00:00
|
|
|
// Look for a supported shipping method in the user's selected
|
|
|
|
// shipping methods. If one is found, then COD is allowed.
|
|
|
|
const selectedMethods = Object.values( selectedShippingMethods );
|
|
|
|
// supported shipping methods might be global (eg. "Any flat rate"), hence
|
|
|
|
// this is doing a `String.prototype.includes` match vs a `Array.prototype.includes` match.
|
|
|
|
return settings.enableForShippingMethods.some( ( shippingMethodId ) => {
|
|
|
|
return selectedMethods.some( ( selectedMethod ) => {
|
|
|
|
return selectedMethod.includes( shippingMethodId );
|
|
|
|
} );
|
|
|
|
} );
|
2020-07-15 14:14:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cash on Delivery (COD) payment method config object.
|
|
|
|
*/
|
|
|
|
const cashOnDeliveryPaymentMethod = {
|
|
|
|
name: PAYMENT_METHOD_NAME,
|
|
|
|
label: <Label />,
|
|
|
|
content: <Content />,
|
|
|
|
edit: <Content />,
|
|
|
|
canMakePayment,
|
|
|
|
ariaLabel: label,
|
2021-01-29 06:28:44 +00:00
|
|
|
supports: {
|
|
|
|
features: settings?.supports ?? [],
|
|
|
|
},
|
2020-07-15 14:14:41 +00:00
|
|
|
};
|
|
|
|
|
2020-11-18 22:06:33 +00:00
|
|
|
registerPaymentMethod( cashOnDeliveryPaymentMethod );
|