mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 07:07:35 +01:00
Fix purchasing non-recurring subscriptions (#11646)
* Fix purchasing non-recurring subscriptions This fixes a bug where non-recurring subscriptions were not correctly purchased on iOS. * fix linter errors * refactor(lint): line length
This commit is contained in:
committed by
Sabe Jones
parent
e4edab2b9d
commit
cc7d065445
@@ -194,23 +194,18 @@ api.noRenewSubscribe = async function noRenewSubscribe (options) {
|
||||
throw new NotAuthorized(api.constants.RESPONSE_NO_ITEM_PURCHASED);
|
||||
}
|
||||
|
||||
let transactionId;
|
||||
let correctReceipt = false;
|
||||
|
||||
/* eslint-disable no-await-in-loop */
|
||||
for (const purchaseData of purchaseDataList) {
|
||||
const dateTerminated = new Date(Number(purchaseData.expirationDate));
|
||||
if (purchaseData.productId === sku && dateTerminated > new Date()) {
|
||||
transactionId = purchaseData.transactionId;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (transactionId) {
|
||||
if (purchaseData.productId === sku) {
|
||||
const { transactionId } = purchaseData;
|
||||
const existingReceipt = await IapPurchaseReceipt.findOne({
|
||||
_id: transactionId,
|
||||
}).exec();
|
||||
if (existingReceipt) throw new NotAuthorized(this.constants.RESPONSE_ALREADY_USED);
|
||||
|
||||
await IapPurchaseReceipt.create({ // eslint-disable-line no-await-in-loop
|
||||
await IapPurchaseReceipt.create({
|
||||
_id: transactionId,
|
||||
consumed: true,
|
||||
// This should always be the buying user even for a gift.
|
||||
@@ -232,10 +227,15 @@ api.noRenewSubscribe = async function noRenewSubscribe (options) {
|
||||
}
|
||||
|
||||
await payments.createSubscription(data);
|
||||
} else {
|
||||
throw new NotAuthorized(api.constants.RESPONSE_INVALID_RECEIPT);
|
||||
correctReceipt = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!correctReceipt) throw new NotAuthorized(api.constants.RESPONSE_INVALID_ITEM);
|
||||
|
||||
return appleRes;
|
||||
};
|
||||
/* eslint-enable no-await-in-loop */
|
||||
|
||||
api.cancelSubscribe = async function cancelSubscribe (user, headers) {
|
||||
const { plan } = user.purchased;
|
||||
|
||||
Reference in New Issue
Block a user