Skip to content

Commit c1aba13

Browse files
committed
Merge remote-tracking branch 'origin/release/6.8.0' into trunk
2 parents 72cbb64 + f21d85e commit c1aba13

18 files changed

+476
-303
lines changed

changelog.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
*** Changelog ***
22

3+
= 6.8.0 - 2022-09-28 =
4+
* Fix - Minor adjustments for Custom Order Tables compatibility.
5+
* Fix - Upgrade from Payment Element beta.
6+
37
= 6.7.0 - 2022-09-06 =
48
* Fix - Check payment method before updating payment method title.
59
* Fix - Use the eslint config at the root of the repo.

client/api/index.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,10 @@ export default class WCStripeAPI {
6868
undefined !== paymentMethodsConfig.card &&
6969
undefined !== paymentMethodsConfig.link;
7070
if ( ! this.stripe ) {
71-
if ( isUPEEnabled ) {
72-
let betas = [ 'payment_element_beta_1' ];
73-
if ( isStripeLinkEnabled ) {
74-
betas = betas.concat( [ 'link_autofill_modal_beta_1' ] );
75-
}
76-
this.stripe = this.createStripe( key, locale, betas );
71+
if ( isUPEEnabled && isStripeLinkEnabled ) {
72+
this.stripe = this.createStripe( key, locale, [
73+
'link_autofill_modal_beta_1',
74+
] );
7775
} else {
7876
this.stripe = this.createStripe( key, locale );
7977
}
@@ -306,12 +304,14 @@ export default class WCStripeAPI {
306304
* Saves the calculated UPE appearance values in a transient.
307305
*
308306
* @param {Object} appearance The UPE appearance object with style values
307+
* @param {boolean} isBlocksCheckout True if save request is for Blocks Checkout. Default false.
309308
*
310309
* @return {Promise} The final promise for the request to the server.
311310
*/
312-
saveUPEAppearance( appearance ) {
311+
saveUPEAppearance( appearance, isBlocksCheckout = false ) {
313312
return this.request( this.getAjaxUrl( 'save_upe_appearance' ), {
314-
appearance,
313+
is_blocks_checkout: isBlocksCheckout,
314+
appearance: JSON.stringify( appearance ),
315315
_ajax_nonce: this.options?.saveUPEAppearanceNonce,
316316
} )
317317
.then( ( response ) => {

client/blocks/upe/confirm-upe-payment.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @param {Object} api The API used for connection both with the server and Stripe.
55
* @param {string} redirectUrl The URL to redirect to after confirming the intent on Stripe.
66
* @param {boolean} paymentNeeded A boolean whether a payment or a setup confirmation is needed.
7-
* @param {Object} paymentElement Reference to the UPE element mounted on the page.
7+
* @param {Object} elements Reference to the Stripe elements.
88
* @param {Object} billingData An object containing the customer's billing data.
99
* @param {Object} emitResponse Various helpers for usage with observer response objects.
1010
* @return {Object} An object, which contains the result from the action.
@@ -13,7 +13,7 @@ export const confirmUpePayment = async (
1313
api,
1414
redirectUrl,
1515
paymentNeeded,
16-
paymentElement,
16+
elements,
1717
billingData,
1818
emitResponse
1919
) => {
@@ -41,7 +41,7 @@ export const confirmUpePayment = async (
4141

4242
if ( paymentNeeded ) {
4343
const { error } = await api.getStripe().confirmPayment( {
44-
element: paymentElement,
44+
elements,
4545
confirmParams,
4646
} );
4747

@@ -50,7 +50,7 @@ export const confirmUpePayment = async (
5050
}
5151
} else {
5252
const { error } = await api.getStripe().confirmSetup( {
53-
element: paymentElement,
53+
elements,
5454
confirmParams,
5555
} );
5656

client/blocks/upe/fields.js

Lines changed: 86 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import { useDispatch, useSelect } from '@wordpress/data';
33
import { __ } from '@wordpress/i18n';
44
import {
55
Elements,
6-
ElementsConsumer,
6+
useStripe,
7+
useElements,
78
PaymentElement,
89
} from '@stripe/react-stripe-js';
9-
import { getAppearance } from '../../styles/upe';
10+
import { getFontRulesFromPage, getAppearance } from '../../styles/upe';
1011
import { confirmUpePayment } from './confirm-upe-payment';
1112
import { getBlocksConfiguration } from 'wcstripe/blocks/utils';
1213
import {
@@ -52,58 +53,26 @@ const UPEField = ( {
5253
api,
5354
activePaymentMethod,
5455
billing: { billingData },
55-
elements,
56-
emitResponse,
5756
eventRegistration: {
5857
onPaymentProcessing,
5958
onCheckoutAfterProcessingWithSuccess,
6059
},
60+
emitResponse,
61+
paymentIntentId,
62+
errorMessage,
6163
shouldSavePayment,
62-
stripe,
6364
} ) => {
64-
const [ clientSecret, setClientSecret ] = useState( null );
65-
const [ paymentIntentId, setPaymentIntentId ] = useState( null );
65+
const stripe = useStripe();
66+
const elements = useElements();
67+
6668
const [ selectedUpePaymentType, setSelectedUpePaymentType ] = useState(
6769
''
6870
);
69-
const [ hasRequestedIntent, setHasRequestedIntent ] = useState( false );
7071
const [ isUpeComplete, setIsUpeComplete ] = useState( false );
71-
const [ errorMessage, setErrorMessage ] = useState( null );
7272

7373
const paymentMethodsConfig = getBlocksConfiguration()?.paymentMethodsConfig;
7474

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-
10575
const customerData = useCustomerData();
106-
10776
useEffect( () => {
10877
if (
10978
paymentMethodsConfig.link !== undefined &&
@@ -126,9 +95,6 @@ const UPEField = ( {
12695
country: 'components-form-token-input-2',
12796
};
12897

129-
const appearance = getAppearance();
130-
elements.update( { appearance } );
131-
13298
enableStripeLinkPaymentMethod( {
13399
api,
134100
elements,
@@ -296,15 +262,11 @@ const UPEField = ( {
296262
selectedUpePaymentType
297263
);
298264

299-
const paymentElement = elements.getElement(
300-
PaymentElement
301-
);
302-
303265
return confirmUpePayment(
304266
api,
305267
paymentDetails.redirect_url,
306268
paymentDetails.payment_needed,
307-
paymentElement,
269+
elements,
308270
billingData,
309271
emitResponse
310272
);
@@ -327,7 +289,6 @@ const UPEField = ( {
327289

328290
const enabledBillingFields = getBlocksConfiguration().enabledBillingFields;
329291
const elementOptions = {
330-
clientSecret,
331292
business: { name: getBlocksConfiguration()?.accountDescriptor },
332293
fields: {
333294
billingDetails: {
@@ -368,6 +329,70 @@ const UPEField = ( {
368329
},
369330
};
370331

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+
371396
if ( ! clientSecret ) {
372397
if ( errorMessage ) {
373398
return (
@@ -382,32 +407,20 @@ const UPEField = ( {
382407
return null;
383408
}
384409

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+
};
397415

398-
export const UPEPaymentForm = ( { api, ...props } ) => {
399416
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+
/>
411424
</Elements>
412425
);
413426
};

0 commit comments

Comments
 (0)