Skip to content

Commit ab41a2f

Browse files
royschutAntonLantukh
authored andcommitted
chore(payment): add access check retry mechanism
1 parent edb5c32 commit ab41a2f

File tree

7 files changed

+17
-11
lines changed

7 files changed

+17
-11
lines changed

packages/common/src/controllers/AccountController.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ export default class AccountController {
334334

335335
if (response.errors.length > 0) throw new Error(response.errors[0]);
336336

337-
await this.reloadSubscriptions({ delay: 2000 });
337+
await this.reloadSubscriptions({ retry: 10 });
338338

339339
return response?.responseData;
340340
};
@@ -386,7 +386,7 @@ export default class AccountController {
386386
return !!responseData?.accessGranted;
387387
};
388388

389-
reloadSubscriptions = async ({ delay }: { delay: number } = { delay: 0 }): Promise<unknown> => {
389+
reloadSubscriptions = async ({ delay, retry }: { delay?: number; retry?: number } = { delay: 0, retry: 0 }): Promise<unknown> => {
390390
useAccountStore.setState({ loading: true });
391391

392392
const { getAccountInfo } = useAccountStore.getState();
@@ -395,10 +395,10 @@ export default class AccountController {
395395

396396
// The subscription data takes a few seconds to load after it's purchased,
397397
// so here's a delay mechanism to give it time to process
398-
if (delay > 0) {
398+
if (delay && delay > 0) {
399399
return new Promise((resolve: (value?: unknown) => void) => {
400400
setTimeout(() => {
401-
this.reloadSubscriptions().finally(resolve);
401+
this.reloadSubscriptions({ retry }).finally(resolve);
402402
}, delay);
403403
});
404404
}
@@ -418,6 +418,12 @@ export default class AccountController {
418418

419419
let pendingOffer: Offer | null = null;
420420

421+
if (!activeSubscription && !!retry && retry > 0) {
422+
const retryDelay = 1500; // Any initial delay has already occured, so we can set this to a fixed value
423+
424+
return await this.reloadSubscriptions({ delay: retryDelay, retry: retry - 1 });
425+
}
426+
421427
// resolve and fetch the pending offer after upgrade/downgrade
422428
try {
423429
if (activeSubscription?.pendingSwitchId) {

packages/hooks-react/src/useCheckAccess.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const useCheckAccess = () => {
3131
const hasAccess = await accountController.checkEntitlements(offerId);
3232

3333
if (hasAccess) {
34-
await accountController.reloadSubscriptions({ delay: 2000 }); // Delay needed for backend processing (Cleeng API returns empty subscription, even after accessGranted from entitlements call
34+
await accountController.reloadSubscriptions({ retry: 10 });
3535
callback?.(true);
3636
} else if (--iterations === 0) {
3737
window.clearInterval(intervalRef.current);

packages/hooks-react/src/useCheckout.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ const useCheckout = ({ onUpdateOrderSuccess, onSubmitPaymentWithoutDetailsSucces
4747
mutationKey: ['submitPaymentWithoutDetails'],
4848
mutationFn: checkoutController.paymentWithoutDetails,
4949
onSuccess: async () => {
50-
await accountController.reloadSubscriptions({ delay: 1000 });
50+
await accountController.reloadSubscriptions({ retry: 10 });
5151
onSubmitPaymentWithoutDetailsSuccess();
5252
},
5353
});

packages/hooks-react/src/useOffers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const useOffers = () => {
3434
const switchSubscription = useMutation({
3535
mutationKey: ['switchSubscription'],
3636
mutationFn: checkoutController.switchSubscription,
37-
onSuccess: () => accountController.reloadSubscriptions({ delay: 7500 }), // @todo: Is there a better way to wait?
37+
onSuccess: () => accountController.reloadSubscriptions({ delay: 3000, retry: 10 }), // A subscription switch usually takes at least 3 secs
3838
});
3939

4040
useEffect(() => {

packages/ui-react/src/components/FinalizePayment/FinalizePayment.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ const FinalizePayment = () => {
4141

4242
try {
4343
await checkoutController.finalizeAdyenPayment({ redirectResult: decodeURI(redirectResult) }, orderId);
44-
await accountController.reloadSubscriptions({ delay: 2000 });
44+
await accountController.reloadSubscriptions({ retry: 10 });
4545

4646
announce(t('checkout.payment_success'), 'success');
4747
navigate(paymentSuccessUrl);

packages/ui-react/src/containers/AdyenInitialPayment/AdyenInitialPayment.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export default function AdyenInitialPayment({ setUpdatingOrder, type, paymentSuc
7575
handleAction(result.action);
7676
}
7777

78-
await accountController.reloadSubscriptions({ delay: 2000 });
78+
await accountController.reloadSubscriptions({ retry: 10 });
7979
announce(t('account:checkout.payment_success'), 'success');
8080
navigate(paymentSuccessUrl, { replace: true });
8181
} catch (error: unknown) {

packages/ui-react/src/containers/AdyenPaymentDetails/AdyenPaymentDetails.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export default function AdyenPaymentDetails({ setProcessing, type, setPaymentErr
4343
setProcessing(true);
4444

4545
await checkoutController.finalizeAdyenPaymentDetails({ redirectResult: decodeURI(redirectResult) }, paymentMethodId);
46-
await accountController.reloadSubscriptions({ delay: 2000 });
46+
await accountController.reloadSubscriptions({ retry: 10 });
4747

4848
setProcessing(false);
4949

@@ -102,7 +102,7 @@ export default function AdyenPaymentDetails({ setProcessing, type, setPaymentErr
102102
handleAction(result.action);
103103
}
104104

105-
await accountController.reloadSubscriptions({ delay: 2000 });
105+
await accountController.reloadSubscriptions({ retry: 5 });
106106

107107
navigate(paymentSuccessUrl, { replace: true });
108108
} catch (error: unknown) {

0 commit comments

Comments
 (0)