Skip to content

Commit 7857d47

Browse files
committed
Improve token creation
1 parent ee74140 commit 7857d47

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

assets/js/components/monei-cc-component.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,23 @@ export const MoneiCCContent = ( props ) => {
4444
/**
4545
* Create payment token
4646
*/
47+
const tokenPromiseRef = useRef( null );
48+
4749
const createPaymentToken = useCallback( async () => {
48-
if ( tokenRef.current ) {
49-
return tokenRef.current;
50+
if ( tokenPromiseRef.current ) {
51+
return tokenPromiseRef.current;
5052
}
5153

52-
const newToken = await cardInput.createToken();
53-
if ( newToken ) {
54-
tokenRef.current = newToken;
55-
}
56-
return newToken;
54+
tokenPromiseRef.current = cardInput.createToken().then( newToken => {
55+
if ( newToken ) {
56+
tokenRef.current = newToken;
57+
}
58+
return newToken;
59+
} ).finally( () => {
60+
tokenPromiseRef.current = null;
61+
} );
62+
63+
return tokenPromiseRef.current;
5764
}, [ cardInput ] );
5865

5966
/**
@@ -188,14 +195,18 @@ export const MoneiCCContent = ( props ) => {
188195
} );
189196

190197
if ( result.status === 'FAILED' ) {
191-
window.location.href = `${ paymentDetails.failUrl }&status=FAILED`;
198+
const failUrl = new URL( paymentDetails.failUrl );
199+
failUrl.searchParams.set( 'status', 'FAILED' );
200+
window.location.href = failUrl.toString();
192201
} else {
193202
let redirectUrl = paymentDetails.completeUrl;
194203

195204
if ( shouldSavePayment === true ) {
196205
const { orderId, paymentId } = paymentDetails;
197-
redirectUrl = `${ paymentDetails.completeUrl }&id=${ paymentId }&orderId=${ orderId }`;
198-
}
206+
const url = new URL( paymentDetails.completeUrl );
207+
url.searchParams.set( 'id', paymentId );
208+
url.searchParams.set( 'orderId', orderId );
209+
redirectUrl = url.toString(); }
199210

200211
window.location.href = redirectUrl;
201212
}

0 commit comments

Comments
 (0)