mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 07:07:35 +01:00
ios: cancel invalid subscriptions without errors (#11035)
This commit is contained in:
@@ -239,17 +239,26 @@ api.cancelSubscribe = async function cancelSubscribe (user, headers) {
|
|||||||
|
|
||||||
await iap.setup();
|
await iap.setup();
|
||||||
|
|
||||||
let appleRes = await iap.validate(iap.APPLE, plan.additionalData);
|
let dateTerminated;
|
||||||
|
|
||||||
let isValidated = iap.isValidated(appleRes);
|
try {
|
||||||
if (!isValidated) throw new NotAuthorized(this.constants.RESPONSE_INVALID_RECEIPT);
|
let appleRes = await iap.validate(iap.APPLE, plan.additionalData);
|
||||||
|
|
||||||
let purchases = iap.getPurchaseData(appleRes);
|
let isValidated = iap.isValidated(appleRes);
|
||||||
if (purchases.length === 0) throw new NotAuthorized(this.constants.RESPONSE_INVALID_RECEIPT);
|
if (!isValidated) throw new NotAuthorized(this.constants.RESPONSE_INVALID_RECEIPT);
|
||||||
let subscriptionData = purchases[0];
|
|
||||||
|
|
||||||
let dateTerminated = new Date(Number(subscriptionData.expirationDate));
|
let purchases = iap.getPurchaseData(appleRes);
|
||||||
if (dateTerminated > new Date()) throw new NotAuthorized(this.constants.RESPONSE_STILL_VALID);
|
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({
|
await payments.cancelSubscription({
|
||||||
user,
|
user,
|
||||||
|
|||||||
Reference in New Issue
Block a user