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:
Phillip Thelen
2019-12-19 18:13:39 +01:00
committed by Sabe Jones
parent e4edab2b9d
commit cc7d065445

View File

@@ -194,23 +194,18 @@ api.noRenewSubscribe = async function noRenewSubscribe (options) {
throw new NotAuthorized(api.constants.RESPONSE_NO_ITEM_PURCHASED); 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) { for (const purchaseData of purchaseDataList) {
const dateTerminated = new Date(Number(purchaseData.expirationDate)); if (purchaseData.productId === sku) {
if (purchaseData.productId === sku && dateTerminated > new Date()) { const { transactionId } = purchaseData;
transactionId = purchaseData.transactionId;
break;
}
}
if (transactionId) {
const existingReceipt = await IapPurchaseReceipt.findOne({ const existingReceipt = await IapPurchaseReceipt.findOne({
_id: transactionId, _id: transactionId,
}).exec(); }).exec();
if (existingReceipt) throw new NotAuthorized(this.constants.RESPONSE_ALREADY_USED); 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, _id: transactionId,
consumed: true, consumed: true,
// This should always be the buying user even for a gift. // 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); await payments.createSubscription(data);
} else { correctReceipt = true;
throw new NotAuthorized(api.constants.RESPONSE_INVALID_RECEIPT); 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) { api.cancelSubscribe = async function cancelSubscribe (user, headers) {
const { plan } = user.purchased; const { plan } = user.purchased;