From 45a9d6d17b024da53b5e1126d7afa40a22b39ae4 Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Wed, 27 Feb 2019 18:48:13 +0100 Subject: [PATCH] ios: cancel invalid subscriptions without errors (#11035) --- website/server/libs/payments/apple.js | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/website/server/libs/payments/apple.js b/website/server/libs/payments/apple.js index d680c841e0..34241a1881 100644 --- a/website/server/libs/payments/apple.js +++ b/website/server/libs/payments/apple.js @@ -239,17 +239,26 @@ api.cancelSubscribe = async function cancelSubscribe (user, headers) { await iap.setup(); - let appleRes = await iap.validate(iap.APPLE, plan.additionalData); + let dateTerminated; - let isValidated = iap.isValidated(appleRes); - if (!isValidated) throw new NotAuthorized(this.constants.RESPONSE_INVALID_RECEIPT); + try { + let appleRes = await iap.validate(iap.APPLE, plan.additionalData); - let purchases = iap.getPurchaseData(appleRes); - if (purchases.length === 0) throw new NotAuthorized(this.constants.RESPONSE_INVALID_RECEIPT); - let subscriptionData = purchases[0]; + let isValidated = iap.isValidated(appleRes); + if (!isValidated) throw new NotAuthorized(this.constants.RESPONSE_INVALID_RECEIPT); - let dateTerminated = new Date(Number(subscriptionData.expirationDate)); - if (dateTerminated > new Date()) throw new NotAuthorized(this.constants.RESPONSE_STILL_VALID); + let purchases = iap.getPurchaseData(appleRes); + if (purchases.length === 0) throw new NotAuthorized(this.constants.RESPONSE_INVALID_RECEIPT); + let subscriptionData = purchases[0]; + + dateTerminated = new Date(Number(subscriptionData.expirationDate)); + if (dateTerminated > new Date()) throw new NotAuthorized(this.constants.RESPONSE_STILL_VALID); + } catch (err) { + // If we have an invalid receipt, cancel anyway + if (!err || !err.validatedData || err.validatedData.is_retryable === true || err.validatedData.status !== 21010) { + throw err; + } + } await payments.cancelSubscription({ user,