-
Notifications
You must be signed in to change notification settings - Fork 69
/
payment-request-button-preview.js
235 lines (209 loc) · 6.39 KB
/
payment-request-button-preview.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/** @format */
/**
* External dependencies
*/
import { React, useState, useEffect, useContext } from 'react';
import { __, sprintf } from '@wordpress/i18n';
import {
PaymentRequestButtonElement,
useStripe,
} from '@stripe/react-stripe-js';
/**
* Internal dependencies
*/
import { shouldUseGooglePayBrand } from 'utils/express-checkout';
import InlineNotice from 'components/inline-notice';
import { WoopayExpressCheckoutButton } from 'wcpay/checkout/woopay/express-button/woopay-express-checkout-button';
import {
usePaymentRequestButtonSize,
usePaymentRequestButtonTheme,
usePaymentRequestButtonType,
usePaymentRequestButtonBorderRadius,
usePaymentRequestEnabledSettings,
useWooPayEnabledSettings,
} from '../../data';
import WCPaySettingsContext from '../wcpay-settings-context';
import { ExpressCheckoutPreviewComponent } from 'wcpay/express-checkout/blocks/components/express-checkout-preview';
const isPaymentRequestSettingsPage = () =>
document.getElementById( 'wcpay-express-checkout-settings-container' )
?.dataset.methodId === 'payment_request';
/**
* stripePromise is used to pass into <Elements>'s stripe props.
* The stripe prop in <Elements> can't be change once passed in.
* Keeping this outside of <PaymentRequestButtonPreview> so that
* re-rendering does not change it.
*/
const BrowserHelpText = () => {
if ( ! isPaymentRequestSettingsPage() ) return null;
let browser = 'Google Chrome';
let paymentMethodName = 'Google Pay';
if ( shouldUseGooglePayBrand() ) {
browser = 'Safari';
paymentMethodName = 'Apple Pay';
}
return (
<p className="payment-method-settings__preview-help-text">
{ sprintf(
__(
/* translators: %1: Payment method name %2: Browser name. */
'To preview the %1$s button, view this page in the %2$s browser.',
'woocommerce-payments'
),
paymentMethodName,
browser
) }
</p>
);
};
const buttonSizeToPxMap = {
small: 40,
medium: 48,
large: 56,
};
const WooPayButtonPreview = ( { size, buttonType, theme, radius } ) => (
<WoopayExpressCheckoutButton
isPreview={ true }
buttonSettings={ {
type: buttonType,
text: 'Buy',
theme: theme,
height: `${
buttonSizeToPxMap[ size ] || buttonSizeToPxMap.medium
}px`,
size,
radius,
} }
/>
);
const ButtonPreviewWrapper = ( { theme, children } ) => (
<>
<div className="payment-method-settings__preview" data-theme={ theme }>
{ children }
</div>
<BrowserHelpText />
</>
);
const PreviewRequirementsNotice = () => (
<InlineNotice icon status="info" isDismissible={ false }>
{ __(
'To preview the express checkout buttons, ' +
'ensure your store is served over HTTPS on a domain available to the public internet, ' +
'your device is configured to use Apple Pay or Google Pay, ' +
'and view this page using the Safari or Chrome browsers.',
'woocommerce-payments'
) }
</InlineNotice>
);
const PaymentRequestButtonPreview = () => {
const stripe = useStripe();
const [ paymentRequest, setPaymentRequest ] = useState();
const [ isLoading, setIsLoading ] = useState( true );
const [ buttonType ] = usePaymentRequestButtonType();
const [ size ] = usePaymentRequestButtonSize();
const [ theme ] = usePaymentRequestButtonTheme();
const [ radius ] = usePaymentRequestButtonBorderRadius();
const [ isWooPayEnabled ] = useWooPayEnabledSettings();
const [ isPaymentRequestEnabled ] = usePaymentRequestEnabledSettings();
const {
featureFlags: { isStripeEceEnabled },
} = useContext( WCPaySettingsContext );
useEffect( () => {
if ( ! stripe ) {
return;
}
// We don't need a payment request when using the ECE buttons.
if ( isStripeEceEnabled ) {
setIsLoading( false );
return;
}
// Create a preview for payment button. The label and its total are placeholders.
const stripePaymentRequest = stripe.paymentRequest( {
country: 'US',
currency: 'usd',
total: {
label: __( 'Total', 'woocommerce-payments' ),
amount: 99,
},
requestPayerName: true,
requestPayerEmail: true,
} );
// Check the availability of the Payment Request API.
stripePaymentRequest.canMakePayment().then( ( result ) => {
if ( result ) {
setPaymentRequest( stripePaymentRequest );
}
setIsLoading( false );
} );
}, [ stripe, setPaymentRequest, setIsLoading, isStripeEceEnabled ] );
/**
* If stripe is loading, then display nothing.
* If stripe finished loading but payment request button failed to load (null), display info section.
* If stripe finished loading and payment request button loads, display the button.
*/
// No need to check `isStripeEceEnabled` since that's not what controls whether the express checkout
// buttons are displayed or not, that's always controlled by `isPaymentRequestEnabled`.
if ( ! isWooPayEnabled && ! isPaymentRequestEnabled ) {
return (
<InlineNotice icon status="info" isDismissible={ false }>
{ __(
'To preview the express checkout buttons, ' +
'activate at least one express checkout.',
'woocommerce-payments'
) }
</InlineNotice>
);
}
const woopayPreview = isWooPayEnabled ? (
<WooPayButtonPreview
size={ size }
buttonType={ buttonType }
theme={ theme }
radius={ radius }
/>
) : null;
const expressCheckoutButtonPreview =
isPaymentRequestEnabled && isStripeEceEnabled ? (
<ExpressCheckoutPreviewComponent
stripe={ stripe }
buttonType={ buttonType }
theme={ theme }
height={ buttonSizeToPxMap[ size ] || buttonSizeToPxMap.medium }
radius={ radius }
/>
) : null;
const prbButtonPreview =
isPaymentRequestEnabled && paymentRequest && ! isLoading ? (
<PaymentRequestButtonElement
key={ `${ buttonType }-${ theme }-${ size }` }
onClick={ ( e ) => {
e.preventDefault();
} }
options={ {
paymentRequest: paymentRequest,
style: {
paymentRequestButton: {
type: buttonType,
theme: theme,
height: `${
buttonSizeToPxMap[ size ] ||
buttonSizeToPxMap.medium
}px`,
},
},
} }
/>
) : null;
if ( woopayPreview || expressCheckoutButtonPreview || prbButtonPreview ) {
return (
<ButtonPreviewWrapper theme={ theme }>
{ woopayPreview }
{ /* We never want to show both ECE and PRB previews at the same time. */ }
{ ( window.location.protocol === 'https:' &&
expressCheckoutButtonPreview ) ||
prbButtonPreview }
</ButtonPreviewWrapper>
);
}
return <PreviewRequirementsNotice />;
};
export default PaymentRequestButtonPreview;