@@ -3,10 +3,11 @@ import { useDispatch, useSelect } from '@wordpress/data';
3
3
import { __ } from '@wordpress/i18n' ;
4
4
import {
5
5
Elements ,
6
- ElementsConsumer ,
6
+ useStripe ,
7
+ useElements ,
7
8
PaymentElement ,
8
9
} from '@stripe/react-stripe-js' ;
9
- import { getAppearance } from '../../styles/upe' ;
10
+ import { getFontRulesFromPage , getAppearance } from '../../styles/upe' ;
10
11
import { confirmUpePayment } from './confirm-upe-payment' ;
11
12
import { getBlocksConfiguration } from 'wcstripe/blocks/utils' ;
12
13
import {
@@ -52,58 +53,26 @@ const UPEField = ( {
52
53
api,
53
54
activePaymentMethod,
54
55
billing : { billingData } ,
55
- elements,
56
- emitResponse,
57
56
eventRegistration : {
58
57
onPaymentProcessing,
59
58
onCheckoutAfterProcessingWithSuccess,
60
59
} ,
60
+ emitResponse,
61
+ paymentIntentId,
62
+ errorMessage,
61
63
shouldSavePayment,
62
- stripe,
63
64
} ) => {
64
- const [ clientSecret , setClientSecret ] = useState ( null ) ;
65
- const [ paymentIntentId , setPaymentIntentId ] = useState ( null ) ;
65
+ const stripe = useStripe ( ) ;
66
+ const elements = useElements ( ) ;
67
+
66
68
const [ selectedUpePaymentType , setSelectedUpePaymentType ] = useState (
67
69
''
68
70
) ;
69
- const [ hasRequestedIntent , setHasRequestedIntent ] = useState ( false ) ;
70
71
const [ isUpeComplete , setIsUpeComplete ] = useState ( false ) ;
71
- const [ errorMessage , setErrorMessage ] = useState ( null ) ;
72
72
73
73
const paymentMethodsConfig = getBlocksConfiguration ( ) ?. paymentMethodsConfig ;
74
74
75
- useEffect ( ( ) => {
76
- if ( paymentIntentId || hasRequestedIntent ) {
77
- return ;
78
- }
79
-
80
- async function createIntent ( ) {
81
- try {
82
- const paymentNeeded = getBlocksConfiguration ( ) ?. isPaymentNeeded ;
83
- const response = paymentNeeded
84
- ? await api . createIntent (
85
- getBlocksConfiguration ( ) ?. orderId
86
- )
87
- : await api . initSetupIntent ( ) ;
88
- setPaymentIntentId ( response . id ) ;
89
- setClientSecret ( response . client_secret ) ;
90
- } catch ( error ) {
91
- setErrorMessage (
92
- error ?. message ??
93
- __ (
94
- 'There was an error loading the payment gateway' ,
95
- 'woocommerce-gateway-stripe'
96
- )
97
- ) ;
98
- }
99
- }
100
-
101
- setHasRequestedIntent ( true ) ;
102
- createIntent ( ) ;
103
- } , [ paymentIntentId , hasRequestedIntent , api , errorMessage ] ) ;
104
-
105
75
const customerData = useCustomerData ( ) ;
106
-
107
76
useEffect ( ( ) => {
108
77
if (
109
78
paymentMethodsConfig . link !== undefined &&
@@ -126,9 +95,6 @@ const UPEField = ( {
126
95
country : 'components-form-token-input-2' ,
127
96
} ;
128
97
129
- const appearance = getAppearance ( ) ;
130
- elements . update ( { appearance } ) ;
131
-
132
98
enableStripeLinkPaymentMethod ( {
133
99
api,
134
100
elements,
@@ -296,15 +262,11 @@ const UPEField = ( {
296
262
selectedUpePaymentType
297
263
) ;
298
264
299
- const paymentElement = elements . getElement (
300
- PaymentElement
301
- ) ;
302
-
303
265
return confirmUpePayment (
304
266
api ,
305
267
paymentDetails . redirect_url ,
306
268
paymentDetails . payment_needed ,
307
- paymentElement ,
269
+ elements ,
308
270
billingData ,
309
271
emitResponse
310
272
) ;
@@ -327,7 +289,6 @@ const UPEField = ( {
327
289
328
290
const enabledBillingFields = getBlocksConfiguration ( ) . enabledBillingFields ;
329
291
const elementOptions = {
330
- clientSecret,
331
292
business : { name : getBlocksConfiguration ( ) ?. accountDescriptor } ,
332
293
fields : {
333
294
billingDetails : {
@@ -368,6 +329,70 @@ const UPEField = ( {
368
329
} ,
369
330
} ;
370
331
332
+ return (
333
+ < div className = "wc-block-gateway-container" >
334
+ < PaymentElement
335
+ options = { elementOptions }
336
+ onChange = { ( event ) => {
337
+ setIsUpeComplete ( event . complete ) ;
338
+ setSelectedUpePaymentType ( event . value . type ) ;
339
+ } }
340
+ />
341
+ </ div >
342
+ ) ;
343
+ } ;
344
+
345
+ export const UPEPaymentForm = ( { api, ...props } ) => {
346
+ const [ clientSecret , setClientSecret ] = useState ( null ) ;
347
+ const [ paymentIntentId , setPaymentIntentId ] = useState ( null ) ;
348
+ const [ hasRequestedIntent , setHasRequestedIntent ] = useState ( false ) ;
349
+ const [ errorMessage , setErrorMessage ] = useState ( null ) ;
350
+ const [ appearance , setAppearance ] = useState (
351
+ getBlocksConfiguration ( ) ?. wcBlocksUPEAppearance
352
+ ) ;
353
+
354
+ useEffect ( ( ) => {
355
+ async function generateUPEAppearance ( ) {
356
+ // Generate UPE input styles.
357
+ const upeAppearance = getAppearance ( true ) ;
358
+ await api . saveUPEAppearance ( upeAppearance , true ) ;
359
+
360
+ // Update appearance state
361
+ setAppearance ( upeAppearance ) ;
362
+ }
363
+ if ( ! appearance ) {
364
+ generateUPEAppearance ( ) ;
365
+ }
366
+
367
+ if ( paymentIntentId || hasRequestedIntent ) {
368
+ return ;
369
+ }
370
+
371
+ async function createIntent ( ) {
372
+ try {
373
+ const paymentNeeded = getBlocksConfiguration ( ) ?. isPaymentNeeded ;
374
+ const response = paymentNeeded
375
+ ? await api . createIntent (
376
+ getBlocksConfiguration ( ) ?. orderId
377
+ )
378
+ : await api . initSetupIntent ( ) ;
379
+ setPaymentIntentId ( response . id ) ;
380
+ setClientSecret ( response . client_secret ) ;
381
+ } catch ( error ) {
382
+ setErrorMessage (
383
+ error ?. message ??
384
+ __ (
385
+ 'There was an error loading the payment gateway' ,
386
+ 'woocommerce-gateway-stripe'
387
+ )
388
+ ) ;
389
+ }
390
+ }
391
+
392
+ setHasRequestedIntent ( true ) ;
393
+ createIntent ( ) ;
394
+ } , [ paymentIntentId , hasRequestedIntent , api , errorMessage , appearance ] ) ;
395
+
371
396
if ( ! clientSecret ) {
372
397
if ( errorMessage ) {
373
398
return (
@@ -382,32 +407,20 @@ const UPEField = ( {
382
407
return null ;
383
408
}
384
409
385
- return (
386
- < div className = "wc-block-gateway-container" >
387
- < PaymentElement
388
- options = { elementOptions }
389
- onChange = { ( event ) => {
390
- setIsUpeComplete ( event . complete ) ;
391
- setSelectedUpePaymentType ( event . value . type ) ;
392
- } }
393
- />
394
- </ div >
395
- ) ;
396
- } ;
410
+ const options = {
411
+ clientSecret,
412
+ appearance,
413
+ fonts : getFontRulesFromPage ( ) ,
414
+ } ;
397
415
398
- export const UPEPaymentForm = ( { api, ...props } ) => {
399
416
return (
400
- < Elements stripe = { api . getStripe ( ) } >
401
- < ElementsConsumer >
402
- { ( { stripe, elements } ) => (
403
- < UPEField
404
- api = { api }
405
- elements = { elements }
406
- stripe = { stripe }
407
- { ...props }
408
- />
409
- ) }
410
- </ ElementsConsumer >
417
+ < Elements stripe = { api . getStripe ( ) } options = { options } >
418
+ < UPEField
419
+ api = { api }
420
+ paymentIntentId = { paymentIntentId }
421
+ errorMessage = { errorMessage }
422
+ { ...props }
423
+ />
411
424
</ Elements >
412
425
) ;
413
426
} ;
0 commit comments