Skip to content

Feat / subscription retry logic #508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions packages/common/src/controllers/AccountController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ export default class AccountController {

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

await this.reloadSubscriptions({ delay: 2000 });
await this.reloadSubscriptions({ retry: 10 });

return response?.responseData;
};
Expand Down Expand Up @@ -386,7 +386,7 @@ export default class AccountController {
return !!responseData?.accessGranted;
};

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

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

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

let pendingOffer: Offer | null = null;

if (!activeSubscription && !!retry && retry > 0) {
const retryDelay = 1500; // Any initial delay has already occured, so we can set this to a fixed value

return await this.reloadSubscriptions({ delay: retryDelay, retry: retry - 1 });
}

// resolve and fetch the pending offer after upgrade/downgrade
try {
if (activeSubscription?.pendingSwitchId) {
Expand Down
2 changes: 1 addition & 1 deletion packages/hooks-react/src/useCheckAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const useCheckAccess = () => {
const hasAccess = await accountController.checkEntitlements(offerId);

if (hasAccess) {
await accountController.reloadSubscriptions({ delay: 2000 }); // Delay needed for backend processing (Cleeng API returns empty subscription, even after accessGranted from entitlements call
await accountController.reloadSubscriptions({ retry: 10, delay: 2000 });
callback?.(true);
} else if (--iterations === 0) {
window.clearInterval(intervalRef.current);
Expand Down
2 changes: 1 addition & 1 deletion packages/hooks-react/src/useCheckout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const useCheckout = ({ onUpdateOrderSuccess, onSubmitPaymentWithoutDetailsSucces
mutationKey: ['submitPaymentWithoutDetails'],
mutationFn: checkoutController.paymentWithoutDetails,
onSuccess: async () => {
await accountController.reloadSubscriptions({ delay: 1000 });
await accountController.reloadSubscriptions({ retry: 10 });
onSubmitPaymentWithoutDetailsSuccess();
},
});
Expand Down
2 changes: 1 addition & 1 deletion packages/hooks-react/src/useOffers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const useOffers = () => {
const switchSubscription = useMutation({
mutationKey: ['switchSubscription'],
mutationFn: checkoutController.switchSubscription,
onSuccess: () => accountController.reloadSubscriptions({ delay: 7500 }), // @todo: Is there a better way to wait?
onSuccess: () => accountController.reloadSubscriptions({ delay: 3000, retry: 10 }), // A subscription switch usually takes at least 3 secs
Copy link
Collaborator

@AntonLantukh AntonLantukh May 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@royschut Will it work properly if we already have an "activeSubscription"? I mean the case here when we switch a subscription. So it looks like this condition !activeSubscription && !!retry && retry > 0 won't work.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, probably not. We could fix it using the approach proposed by @dbudzins in #483, but that's too large of a refactor for this moment. I have noted this for another moment.

But until that, for now I see two options:

  • We set the initial delay for this case back to 7500, and merge this PR as it is still a step forward
  • We set this PR back into Draft for now

I'm fine with both options. :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am good with the first option. Just wanted to make sure this is something you are aware of.

});

useEffect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const FinalizePayment = () => {

try {
await checkoutController.finalizeAdyenPayment({ redirectResult: decodeURI(redirectResult) }, orderId);
await accountController.reloadSubscriptions({ delay: 2000 });
await accountController.reloadSubscriptions({ retry: 10 });

announce(t('checkout.payment_success'), 'success');
navigate(paymentSuccessUrl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default function AdyenInitialPayment({ setUpdatingOrder, type, paymentSuc
handleAction(result.action);
}

await accountController.reloadSubscriptions({ delay: 2000 });
await accountController.reloadSubscriptions({ retry: 10 });
announce(t('account:checkout.payment_success'), 'success');
navigate(paymentSuccessUrl, { replace: true });
} catch (error: unknown) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export default function AdyenPaymentDetails({ setProcessing, type, setPaymentErr
setProcessing(true);

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

setProcessing(false);

Expand Down Expand Up @@ -102,7 +102,7 @@ export default function AdyenPaymentDetails({ setProcessing, type, setPaymentErr
handleAction(result.action);
}

await accountController.reloadSubscriptions({ delay: 2000 });
await accountController.reloadSubscriptions({ retry: 5 });

navigate(paymentSuccessUrl, { replace: true });
} catch (error: unknown) {
Expand Down