2024-09-06 03:21:27 +02:00
|
|
|
import { useEffect, useCallback, useMemo } from '@wordpress/element';
|
2024-09-05 21:17:36 +02:00
|
|
|
|
|
|
|
const cardIcons = {
|
|
|
|
VISA: 'visa-light.svg',
|
|
|
|
MASTER_CARD: 'mastercard-light.svg',
|
|
|
|
AMEX: 'amex-light.svg',
|
|
|
|
DISCOVER: 'discover-light.svg',
|
|
|
|
DINERS: 'dinersclub-light.svg',
|
|
|
|
JCB: 'jcb-light.svg',
|
|
|
|
UNIONPAY: 'unionpay-light.svg',
|
|
|
|
};
|
|
|
|
|
|
|
|
export const Payment = ( {
|
|
|
|
fastlaneSdk,
|
|
|
|
card,
|
|
|
|
shippingAddress,
|
2024-09-06 03:21:27 +02:00
|
|
|
isGuest,
|
2024-09-05 21:17:36 +02:00
|
|
|
onPaymentLoad,
|
|
|
|
} ) => {
|
|
|
|
const { brand, lastDigits, expiry } = card?.paymentSource?.card ?? {};
|
|
|
|
const { fullName } = shippingAddress?.name ?? {};
|
|
|
|
|
2024-09-06 03:21:27 +02:00
|
|
|
// Memoized Fastlane card rendering
|
|
|
|
const loadPaymentComponent = useCallback( async () => {
|
|
|
|
if ( isGuest ) {
|
|
|
|
const paymentComponent = await fastlaneSdk.FastlaneCardComponent(
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
paymentComponent.render( `#fastlane-card` );
|
|
|
|
onPaymentLoad( paymentComponent );
|
2024-09-05 21:17:36 +02:00
|
|
|
}
|
2024-09-06 03:21:27 +02:00
|
|
|
}, [ isGuest, fastlaneSdk, onPaymentLoad ] );
|
2024-09-05 21:17:36 +02:00
|
|
|
|
2024-09-06 03:21:27 +02:00
|
|
|
useEffect( () => {
|
|
|
|
loadPaymentComponent();
|
|
|
|
}, [ loadPaymentComponent ] );
|
|
|
|
|
|
|
|
// Memoized card logo rendering
|
|
|
|
const cardLogo = useMemo( () => {
|
|
|
|
return cardIcons[ brand ] ? (
|
|
|
|
<img
|
|
|
|
className="wc-block-axo-block-card__meta-icon"
|
|
|
|
title={ brand }
|
|
|
|
src={ `${ window.wc_ppcp_axo.icons_directory }${ cardIcons[ brand ] }` }
|
|
|
|
alt={ brand }
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<span>{ brand }</span>
|
|
|
|
);
|
|
|
|
}, [ brand ] );
|
2024-09-05 21:17:36 +02:00
|
|
|
|
|
|
|
const formattedExpiry = expiry
|
|
|
|
? `${ expiry.split( '-' )[ 1 ] }/${ expiry.split( '-' )[ 0 ] }`
|
|
|
|
: '';
|
|
|
|
|
2024-09-06 03:21:27 +02:00
|
|
|
return isGuest ? (
|
2024-09-05 21:17:36 +02:00
|
|
|
<div id="fastlane-card" key="fastlane-card" />
|
|
|
|
) : (
|
|
|
|
<div key="custom-card" className="wc-block-checkout-axo-block-card">
|
|
|
|
<div className="wc-block-checkout-axo-block-card__meta-container">
|
|
|
|
<div className="wc-block-axo-block-card__meta">
|
2024-09-06 03:21:27 +02:00
|
|
|
<span className="wc-block-axo-block-card__meta__digits">
|
|
|
|
{ `**** **** **** ${ lastDigits }` }
|
|
|
|
</span>
|
2024-09-05 21:17:36 +02:00
|
|
|
{ cardLogo }
|
|
|
|
</div>
|
|
|
|
<div className="wc-block-axo-block-card__meta">
|
|
|
|
<span>{ fullName }</span>
|
|
|
|
<span>{ formattedExpiry }</span>{ ' ' }
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|