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,48 +194,48 @@ 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;
|
const existingReceipt = await IapPurchaseReceipt.findOne({
|
||||||
|
_id: transactionId,
|
||||||
|
}).exec();
|
||||||
|
if (existingReceipt) throw new NotAuthorized(this.constants.RESPONSE_ALREADY_USED);
|
||||||
|
|
||||||
|
await IapPurchaseReceipt.create({
|
||||||
|
_id: transactionId,
|
||||||
|
consumed: true,
|
||||||
|
// This should always be the buying user even for a gift.
|
||||||
|
userId: user._id,
|
||||||
|
});
|
||||||
|
const data = {
|
||||||
|
user,
|
||||||
|
paymentMethod: this.constants.PAYMENT_METHOD_APPLE,
|
||||||
|
headers,
|
||||||
|
sub,
|
||||||
|
autoRenews: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (gift) {
|
||||||
|
gift.member = await User.findById(gift.uuid).exec();
|
||||||
|
gift.subscription = sub;
|
||||||
|
data.gift = gift;
|
||||||
|
data.paymentMethod = this.constants.PAYMENT_METHOD_GIFT;
|
||||||
|
}
|
||||||
|
|
||||||
|
await payments.createSubscription(data);
|
||||||
|
correctReceipt = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (!correctReceipt) throw new NotAuthorized(api.constants.RESPONSE_INVALID_ITEM);
|
||||||
|
|
||||||
if (transactionId) {
|
return appleRes;
|
||||||
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
|
|
||||||
_id: transactionId,
|
|
||||||
consumed: true,
|
|
||||||
// This should always be the buying user even for a gift.
|
|
||||||
userId: user._id,
|
|
||||||
});
|
|
||||||
const data = {
|
|
||||||
user,
|
|
||||||
paymentMethod: this.constants.PAYMENT_METHOD_APPLE,
|
|
||||||
headers,
|
|
||||||
sub,
|
|
||||||
autoRenews: false,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (gift) {
|
|
||||||
gift.member = await User.findById(gift.uuid).exec();
|
|
||||||
gift.subscription = sub;
|
|
||||||
data.gift = gift;
|
|
||||||
data.paymentMethod = this.constants.PAYMENT_METHOD_GIFT;
|
|
||||||
}
|
|
||||||
|
|
||||||
await payments.createSubscription(data);
|
|
||||||
} else {
|
|
||||||
throw new NotAuthorized(api.constants.RESPONSE_INVALID_RECEIPT);
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
/* 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;
|
||||||
|
|||||||
Reference in New Issue
Block a user