From 769405ff3466b1ee91234974e23eaf64b998282c Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Fri, 30 Mar 2018 19:24:51 +0200 Subject: [PATCH] google subs: make sure the subs can be cancelled if they return a 410 from google (#10201) --- website/server/libs/googlePayments.js | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/website/server/libs/googlePayments.js b/website/server/libs/googlePayments.js index 2f201cffd2..b73197f4b5 100644 --- a/website/server/libs/googlePayments.js +++ b/website/server/libs/googlePayments.js @@ -140,16 +140,27 @@ api.cancelSubscribe = async function cancelSubscribe (user, headers) { await iap.setup(); - let googleRes = await iap.validate(iap.GOOGLE, plan.additionalData); + let dateTerminated; - let isValidated = iap.isValidated(googleRes); - if (!isValidated) throw new NotAuthorized(this.constants.RESPONSE_INVALID_RECEIPT); + try { + let googleRes = await iap.validate(iap.GOOGLE, plan.additionalData); - let purchases = iap.getPurchaseData(googleRes); - if (purchases.length === 0) throw new NotAuthorized(this.constants.RESPONSE_INVALID_RECEIPT); - let subscriptionData = purchases[0]; + let isValidated = iap.isValidated(googleRes); + if (!isValidated) throw new NotAuthorized(this.constants.RESPONSE_INVALID_RECEIPT); + + let purchases = iap.getPurchaseData(googleRes); + if (purchases.length === 0) throw new NotAuthorized(this.constants.RESPONSE_INVALID_RECEIPT); + let subscriptionData = purchases[0]; + dateTerminated = new Date(Number(subscriptionData.expirationDate)); + } catch (err) { + // Status:410 means that the subsctiption isn't active anymore and we can safely delete it + if (err && err.message === 'Status:410') { + dateTerminated = new Date(); + } else { + throw err; + } + } - let dateTerminated = new Date(Number(subscriptionData.expirationDate)); if (dateTerminated > new Date()) throw new NotAuthorized(this.constants.RESPONSE_STILL_VALID); await payments.cancelSubscription({