2020-05-01 11:24:28 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { getSetting } from '@woocommerce/settings';
|
|
|
|
|
2020-04-08 16:36:04 +00:00
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { PAYMENT_METHOD_NAME } from './constants';
|
|
|
|
import { PaymentRequestExpress } from './payment-request-express';
|
|
|
|
import { applePayImage } from './apple-pay-preview';
|
2021-01-29 06:28:44 +00:00
|
|
|
import { getStripeServerData, loadStripe } from '../stripe-utils';
|
2020-04-08 16:36:04 +00:00
|
|
|
|
|
|
|
const ApplePayPreview = () => <img src={ applePayImage } alt="" />;
|
|
|
|
|
|
|
|
const canPayStripePromise = loadStripe();
|
|
|
|
const componentStripePromise = loadStripe();
|
|
|
|
|
2020-07-13 22:52:13 +00:00
|
|
|
let isStripeInitialized = false,
|
|
|
|
canPay = false;
|
|
|
|
|
|
|
|
// Initialise stripe API client and determine if payment method can be used
|
|
|
|
// in current environment (e.g. geo + shopper has payment settings configured).
|
2020-11-20 10:10:04 +00:00
|
|
|
function paymentRequestAvailable( { currencyCode, totalPrice } ) {
|
|
|
|
// Stripe only supports carts of greater value than 30 cents.
|
|
|
|
if ( totalPrice < 30 ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-13 22:52:13 +00:00
|
|
|
// If we've already initialised, return the cached results.
|
|
|
|
if ( isStripeInitialized ) {
|
|
|
|
return canPay;
|
|
|
|
}
|
|
|
|
|
|
|
|
return canPayStripePromise.then( ( stripe ) => {
|
|
|
|
if ( stripe === null ) {
|
|
|
|
isStripeInitialized = true;
|
|
|
|
return canPay;
|
|
|
|
}
|
2020-09-18 10:27:54 +00:00
|
|
|
if ( stripe.error && stripe.error instanceof Error ) {
|
|
|
|
throw stripe.error;
|
|
|
|
}
|
2020-07-13 22:52:13 +00:00
|
|
|
// Do a test payment to confirm if payment method is available.
|
|
|
|
const paymentRequest = stripe.paymentRequest( {
|
|
|
|
total: {
|
2020-11-20 10:10:04 +00:00
|
|
|
label: 'Total',
|
|
|
|
amount: totalPrice,
|
|
|
|
pending: true,
|
2020-07-13 22:52:13 +00:00
|
|
|
},
|
|
|
|
country: getSetting( 'baseLocation', {} )?.country,
|
|
|
|
currency: currencyCode,
|
|
|
|
} );
|
|
|
|
return paymentRequest.canMakePayment().then( ( result ) => {
|
|
|
|
canPay = !! result;
|
|
|
|
isStripeInitialized = true;
|
|
|
|
return canPay;
|
|
|
|
} );
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2020-11-18 22:06:33 +00:00
|
|
|
const paymentRequestPaymentMethod = {
|
2020-04-09 15:22:34 +00:00
|
|
|
name: PAYMENT_METHOD_NAME,
|
2020-04-08 16:36:04 +00:00
|
|
|
content: <PaymentRequestExpress stripe={ componentStripePromise } />,
|
|
|
|
edit: <ApplePayPreview />,
|
2020-04-09 11:44:29 +00:00
|
|
|
canMakePayment: ( cartData ) =>
|
2020-11-20 10:10:04 +00:00
|
|
|
paymentRequestAvailable( {
|
|
|
|
currencyCode: cartData?.cartTotals?.currency_code?.toLowerCase(),
|
|
|
|
totalPrice: parseInt( cartData?.cartTotals?.total_price || 0, 10 ),
|
|
|
|
} ),
|
2020-04-09 15:22:34 +00:00
|
|
|
paymentMethodId: 'stripe',
|
2021-01-29 06:28:44 +00:00
|
|
|
supports: {
|
|
|
|
features: getStripeServerData()?.supports ?? [],
|
|
|
|
},
|
2020-04-08 16:36:04 +00:00
|
|
|
};
|
|
|
|
|
2020-11-18 22:06:33 +00:00
|
|
|
export default paymentRequestPaymentMethod;
|