Better handling for cancellation when user had multiple subs

This commit is contained in:
Phillip Thelen
2022-11-04 13:30:50 +01:00
committed by Phillip Thelen
parent 967717a010
commit ddf1b4060d
2 changed files with 44 additions and 37 deletions

View File

@@ -97,8 +97,8 @@ api.subscribe = async function subscribe (user, receipt, headers, nextPaymentPro
if ((!newestDate || datePurchased > newestDate) && dateTerminated > new Date()) {
originalTransactionId = purchaseData.originalTransactionId;
newTransactionId = purchaseData.transactionId;
newestDate = datePurchased
sku = purchaseData.productId
newestDate = datePurchased;
sku = purchaseData.productId;
}
}
@@ -255,9 +255,16 @@ api.cancelSubscribe = async function cancelSubscribe (user, headers) {
const purchases = iap.getPurchaseData(appleRes);
if (purchases.length === 0) throw new NotAuthorized(this.constants.RESPONSE_INVALID_RECEIPT);
const subscriptionData = purchases[0];
let newestDate;
for (const purchaseData of purchases) {
const datePurchased = new Date(Number(purchaseData.purchaseDate));
if (!newestDate || datePurchased > newestDate) {
dateTerminated = new Date(Number(purchaseData.expirationDate));
newestDate = datePurchased;
}
}
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