Skip to content
This repository was archived by the owner on Feb 23, 2024. It is now read-only.

Commit 0f0db2e

Browse files
authored
Fix retrying after failed payment processed server side. (#2655)
* add state for selected saved token in payment data * add handling for flipping payment status back to pristine when checkout has error If payment status is successful client side, but the checkout state goes into an error after server side processing, then we reset payment status to pristine to allow for reprocessing of payment method client side. This is skipped for saved payment method tokens because they effectively are _only_ processed server side.
1 parent 9b76ea7 commit 0f0db2e

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

assets/js/base/components/payment-methods/saved-payment-method-options.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const getCcOrEcheckPaymentMethodOption = (
4545
setPaymentStatus().success( {
4646
payment_method: method.gateway,
4747
[ savedTokenKey ]: token,
48+
isSavedToken: true,
4849
} );
4950
},
5051
};
@@ -77,6 +78,7 @@ const getDefaultPaymentMethodOptions = (
7778
setPaymentStatus().success( {
7879
payment_method: method.gateway,
7980
[ savedTokenKey ]: token,
81+
isSavedToken: true,
8082
} );
8183
},
8284
};

assets/js/base/context/cart-checkout/payment-methods/constants.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ export const DEFAULT_PAYMENT_DATA = {
3535
// wants to pass along for payment
3636
// processing server side.
3737
},
38+
hasSavedToken: false,
3839
errorMessage: '',
3940
paymentMethods: {},
4041
expressPaymentMethods: {},

assets/js/base/context/cart-checkout/payment-methods/payment-method-data-context.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,23 @@ export const PaymentMethodDataProvider = ( { children } ) => {
276276
}
277277
}, [ checkoutIsIdle, currentStatus.isSuccessful ] );
278278

279+
// if checkout has an error and payment is not being made with a saved token
280+
// and payment status is success, then let's sync payment status back to
281+
// pristine.
282+
useEffect( () => {
283+
if (
284+
checkoutHasError &&
285+
currentStatus.isSuccessful &&
286+
! paymentData.hasSavedToken
287+
) {
288+
dispatch( statusOnly( PRISTINE ) );
289+
}
290+
}, [
291+
checkoutHasError,
292+
currentStatus.isSuccessful,
293+
paymentData.hasSavedToken,
294+
] );
295+
279296
// set initial active payment method if it's undefined.
280297
useEffect( () => {
281298
const paymentMethodKeys = Object.keys( paymentData.paymentMethods );

assets/js/base/context/cart-checkout/payment-methods/reducer.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ const {
1515
SET_SHOULD_SAVE_PAYMENT_METHOD,
1616
} = ACTION_TYPES;
1717

18+
const hasSavedPaymentToken = ( paymentMethodData ) => {
19+
return !! (
20+
typeof paymentMethodData === 'object' && paymentMethodData.isSavedToken
21+
);
22+
};
23+
1824
/**
1925
* Reducer for payment data state
2026
*
@@ -64,6 +70,9 @@ const reducer = (
6470
currentStatus: SUCCESS,
6571
paymentMethodData:
6672
paymentMethodData || state.paymentMethodData,
73+
hasSavedToken: hasSavedPaymentToken(
74+
paymentMethodData
75+
),
6776
}
6877
: state;
6978
case PROCESSING:

0 commit comments

Comments
 (0)