google subs: make sure the subs can be cancelled if they return a 410 from google (#10201)

This commit is contained in:
Matteo Pagliazzi 2018-03-30 19:24:51 +02:00 committed by GitHub
parent cb418882f3
commit 769405ff34
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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({