-
Notifications
You must be signed in to change notification settings - Fork 204
/
index.js
64 lines (57 loc) · 1.97 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { getSetting } from '@woocommerce/settings';
import { PAYMENT_METHOD_NAME } from './constants';
import { PaymentRequestExpress } from './payment-request-express';
import { applePayImage } from './apple-pay-preview';
import { loadStripe } from 'wcstripe/blocks/load-stripe';
import {
getBlocksConfiguration,
createPaymentRequestUsingCart,
} from 'wcstripe/blocks/utils';
const ApplePayPreview = () => <img src={ applePayImage } alt="" />;
const componentStripePromise = loadStripe();
const paymentRequestPaymentMethod = {
name: PAYMENT_METHOD_NAME,
content: <PaymentRequestExpress stripe={ componentStripePromise } />,
edit: <ApplePayPreview />,
canMakePayment: ( cartData ) => {
// If in the editor context, always return true to display the `edit` prop preview.
// https://github.com/woocommerce/woocommerce-gutenberg-products-block/issues/4101.
if ( getBlocksConfiguration()?.isAdmin ?? false ) {
return true;
}
if ( ! getBlocksConfiguration()?.shouldShowPaymentRequestButton ) {
return false;
}
return loadStripe().then( ( stripe ) => {
// Create a payment request and check if we can make a payment to determine whether to
// show the Payment Request Button or not. This is necessary because a browser might be
// able to load the Stripe JS object, but not support Payment Requests.
const fakeCart = {
order_data: {
total: {
label: 'Total',
amount: parseInt(
cartData?.cartTotals?.total_price ?? 0,
10
),
pending: true,
},
currency: cartData?.cartTotals?.currency_code?.toLowerCase(),
country_code: getSetting( 'baseLocation', {} )?.country,
displayItems: [],
},
shipping_required: false,
};
const paymentRequest = createPaymentRequestUsingCart(
stripe,
fakeCart
);
return paymentRequest.canMakePayment();
} );
},
paymentMethodId: 'stripe',
supports: {
features: getBlocksConfiguration()?.supports ?? [],
},
};
export default paymentRequestPaymentMethod;