mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +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);
|
||||
}
|
||||
|
||||
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;
|
||||
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({
|
||||
_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;
|
||||
}
|
||||
}
|
||||
if (!correctReceipt) throw new NotAuthorized(api.constants.RESPONSE_INVALID_ITEM);
|
||||
|
||||
if (transactionId) {
|
||||
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);
|
||||
}
|
||||
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