2024-12-12 16:55:19 +01:00
|
|
|
import { __, sprintf } from '@wordpress/i18n';
|
2024-12-12 17:54:38 +01:00
|
|
|
|
2024-12-12 18:29:36 +01:00
|
|
|
import { CommonHooks } from '../../data';
|
2024-12-12 16:55:19 +01:00
|
|
|
import { countryPriceInfo } from '../../utils/countryPriceInfo';
|
2024-12-12 18:29:36 +01:00
|
|
|
import { formatPrice } from '../../utils/formatPrice';
|
2024-12-12 16:55:19 +01:00
|
|
|
import TitleBadge, { TITLE_BADGE_INFO } from './TitleBadge';
|
2024-12-12 18:29:36 +01:00
|
|
|
|
2024-12-20 11:46:00 +01:00
|
|
|
const getFixedAmount = ( currency, priceList, itemFixedAmount ) => {
|
|
|
|
if ( priceList[ currency ] ) {
|
|
|
|
const sum = priceList[ currency ] + itemFixedAmount;
|
|
|
|
return formatPrice( sum, currency );
|
2024-12-12 18:29:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const [ defaultCurrency, defaultPrice ] = Object.entries( priceList )[ 0 ];
|
2024-12-20 11:46:00 +01:00
|
|
|
const sum = defaultPrice + itemFixedAmount;
|
|
|
|
return formatPrice( sum, defaultCurrency );
|
2024-12-12 18:29:36 +01:00
|
|
|
};
|
2024-12-12 16:55:19 +01:00
|
|
|
|
2024-12-12 17:54:38 +01:00
|
|
|
const PricingTitleBadge = ( { item } ) => {
|
2024-12-20 11:46:00 +01:00
|
|
|
const { storeCountry, storeCurrency } = CommonHooks.useWooSettings();
|
2024-12-12 17:54:38 +01:00
|
|
|
const infos = countryPriceInfo[ storeCountry ];
|
2024-12-20 11:46:00 +01:00
|
|
|
const itemKey = item.split(' ')[0]; // Extract the first word, fastlane has more than one
|
2024-12-12 16:55:19 +01:00
|
|
|
|
2024-12-20 11:46:00 +01:00
|
|
|
if ( ! infos || ! infos[ itemKey ] ) {
|
2024-12-12 16:55:19 +01:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-12-20 11:46:00 +01:00
|
|
|
const percentage = typeof infos[itemKey] === 'number' ? infos[itemKey].toFixed(2) : infos[itemKey]['percentage'].toFixed(2);
|
|
|
|
const itemFixedAmount = infos[itemKey]['fixedFee'] ? infos[itemKey]['fixedFee'] : 0;
|
|
|
|
const fixedAmount = getFixedAmount( storeCurrency, infos.fixedFee, itemFixedAmount );
|
2024-12-12 16:55:19 +01:00
|
|
|
|
|
|
|
const label = sprintf(
|
2024-12-12 18:29:36 +01:00
|
|
|
__( 'from %1$s%% + %2$s', 'woocommerce-paypal-payments' ),
|
2024-12-12 16:55:19 +01:00
|
|
|
percentage,
|
2024-12-12 18:29:36 +01:00
|
|
|
fixedAmount
|
2024-12-12 16:55:19 +01:00
|
|
|
);
|
|
|
|
|
2024-12-12 18:28:23 +01:00
|
|
|
return (
|
|
|
|
<TitleBadge
|
|
|
|
type={ TITLE_BADGE_INFO }
|
|
|
|
text={ `${ label }<sup>1</sup>` }
|
|
|
|
/>
|
|
|
|
);
|
2024-12-12 16:55:19 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export default PricingTitleBadge;
|