mirror of
https://github.com/sudoxnym/habitica-self-host.git
synced 2026-07-14 18:22:21 +00:00
google subs: make sure the subs can be cancelled if they return a 410 from google (#10201)
This commit is contained in:
parent
cb418882f3
commit
769405ff34
1 changed files with 18 additions and 7 deletions
|
|
@ -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({
|
||||
|
|
|
|||
Loading…
Reference in a new issue