mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-14 21:27:23 +01:00
Revert "Allow sub upgrades/downgrades on iOS (#14303)"
This reverts commit 9e98e56e9b.
This commit is contained in:
@@ -218,7 +218,6 @@ describe('Apple Payments', () => {
|
|||||||
headers = {};
|
headers = {};
|
||||||
receipt = `{"token": "${token}"}`;
|
receipt = `{"token": "${token}"}`;
|
||||||
nextPaymentProcessing = moment.utc().add({ days: 2 });
|
nextPaymentProcessing = moment.utc().add({ days: 2 });
|
||||||
user = new User();
|
|
||||||
|
|
||||||
iapSetupStub = sinon.stub(iap, 'setup')
|
iapSetupStub = sinon.stub(iap, 'setup')
|
||||||
.resolves();
|
.resolves();
|
||||||
@@ -299,7 +298,6 @@ describe('Apple Payments', () => {
|
|||||||
expirationDate: moment.utc().add({ day: 1 }).toDate(),
|
expirationDate: moment.utc().add({ day: 1 }).toDate(),
|
||||||
productId: option.sku,
|
productId: option.sku,
|
||||||
transactionId: token,
|
transactionId: token,
|
||||||
originalTransactionId: token,
|
|
||||||
}]);
|
}]);
|
||||||
sub = common.content.subscriptionBlocks[option.subKey];
|
sub = common.content.subscriptionBlocks[option.subKey];
|
||||||
|
|
||||||
@@ -323,111 +321,12 @@ describe('Apple Payments', () => {
|
|||||||
nextPaymentProcessing,
|
nextPaymentProcessing,
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
if (option !== subOptions[3]) {
|
|
||||||
const newOption = subOptions[3];
|
|
||||||
it(`upgrades a subscription from ${option.sku} to ${newOption.sku}`, async () => {
|
|
||||||
const oldSub = common.content.subscriptionBlocks[option.subKey];
|
|
||||||
user.profile.name = 'sender';
|
|
||||||
user.purchased.plan.paymentMethod = applePayments.constants.PAYMENT_METHOD_APPLE;
|
|
||||||
user.purchased.plan.customerId = token;
|
|
||||||
user.purchased.plan.planId = option.subKey;
|
|
||||||
user.purchased.plan.additionalData = receipt;
|
|
||||||
iap.getPurchaseData.restore();
|
|
||||||
iapGetPurchaseDataStub = sinon.stub(iap, 'getPurchaseData')
|
|
||||||
.returns([{
|
|
||||||
expirationDate: moment.utc().add({ day: 2 }).toDate(),
|
|
||||||
productId: newOption.sku,
|
|
||||||
transactionId: `${token}new`,
|
|
||||||
originalTransactionId: token,
|
|
||||||
}]);
|
|
||||||
sub = common.content.subscriptionBlocks[newOption.subKey];
|
|
||||||
|
|
||||||
await applePayments.subscribe(newOption.sku,
|
|
||||||
user,
|
|
||||||
receipt,
|
|
||||||
headers,
|
|
||||||
nextPaymentProcessing);
|
|
||||||
|
|
||||||
expect(iapSetupStub).to.be.calledOnce;
|
|
||||||
expect(iapValidateStub).to.be.calledOnce;
|
|
||||||
expect(iapValidateStub).to.be.calledWith(iap.APPLE, receipt);
|
|
||||||
expect(iapIsValidatedStub).to.be.calledOnce;
|
|
||||||
expect(iapIsValidatedStub).to.be.calledWith({});
|
|
||||||
expect(iapGetPurchaseDataStub).to.be.calledOnce;
|
|
||||||
|
|
||||||
expect(paymentsCreateSubscritionStub).to.be.calledOnce;
|
|
||||||
expect(paymentsCreateSubscritionStub).to.be.calledWith({
|
|
||||||
user,
|
|
||||||
customerId: token,
|
|
||||||
paymentMethod: applePayments.constants.PAYMENT_METHOD_APPLE,
|
|
||||||
sub,
|
|
||||||
headers,
|
|
||||||
additionalData: receipt,
|
|
||||||
nextPaymentProcessing,
|
|
||||||
updatedFrom: oldSub,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (option !== subOptions[0]) {
|
|
||||||
const newOption = subOptions[0];
|
|
||||||
it(`downgrades a subscription from ${option.sku} to ${newOption.sku}`, async () => {
|
|
||||||
const oldSub = common.content.subscriptionBlocks[option.subKey];
|
|
||||||
user.profile.name = 'sender';
|
|
||||||
user.purchased.plan.paymentMethod = applePayments.constants.PAYMENT_METHOD_APPLE;
|
|
||||||
user.purchased.plan.customerId = token;
|
|
||||||
user.purchased.plan.planId = option.subKey;
|
|
||||||
user.purchased.plan.additionalData = receipt;
|
|
||||||
iap.getPurchaseData.restore();
|
|
||||||
iapGetPurchaseDataStub = sinon.stub(iap, 'getPurchaseData')
|
|
||||||
.returns([{
|
|
||||||
expirationDate: moment.utc().add({ day: 2 }).toDate(),
|
|
||||||
productId: newOption.sku,
|
|
||||||
transactionId: `${token}new`,
|
|
||||||
originalTransactionId: token,
|
|
||||||
}]);
|
|
||||||
sub = common.content.subscriptionBlocks[newOption.subKey];
|
|
||||||
|
|
||||||
await applePayments.subscribe(newOption.sku,
|
|
||||||
user,
|
|
||||||
receipt,
|
|
||||||
headers,
|
|
||||||
nextPaymentProcessing);
|
|
||||||
|
|
||||||
expect(iapSetupStub).to.be.calledOnce;
|
|
||||||
expect(iapValidateStub).to.be.calledOnce;
|
|
||||||
expect(iapValidateStub).to.be.calledWith(iap.APPLE, receipt);
|
|
||||||
expect(iapIsValidatedStub).to.be.calledOnce;
|
|
||||||
expect(iapIsValidatedStub).to.be.calledWith({});
|
|
||||||
expect(iapGetPurchaseDataStub).to.be.calledOnce;
|
|
||||||
|
|
||||||
expect(paymentsCreateSubscritionStub).to.be.calledOnce;
|
|
||||||
expect(paymentsCreateSubscritionStub).to.be.calledWith({
|
|
||||||
user,
|
|
||||||
customerId: token,
|
|
||||||
paymentMethod: applePayments.constants.PAYMENT_METHOD_APPLE,
|
|
||||||
sub,
|
|
||||||
headers,
|
|
||||||
additionalData: receipt,
|
|
||||||
nextPaymentProcessing,
|
|
||||||
updatedFrom: oldSub,
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('errors when a user is using the same subscription', async () => {
|
it('errors when a user is already subscribed', async () => {
|
||||||
|
payments.createSubscription.restore();
|
||||||
user = new User();
|
user = new User();
|
||||||
await user.save();
|
await user.save();
|
||||||
payments.createSubscription.restore();
|
|
||||||
|
|
||||||
iap.getPurchaseData.restore();
|
|
||||||
iapGetPurchaseDataStub = sinon.stub(iap, 'getPurchaseData')
|
|
||||||
.returns([{
|
|
||||||
expirationDate: moment.utc().add({ day: 1 }).toDate(),
|
|
||||||
productId: sku,
|
|
||||||
transactionId: token,
|
|
||||||
originalTransactionId: token,
|
|
||||||
}]);
|
|
||||||
|
|
||||||
await applePayments.subscribe(sku, user, receipt, headers, nextPaymentProcessing);
|
await applePayments.subscribe(sku, user, receipt, headers, nextPaymentProcessing);
|
||||||
|
|
||||||
|
|||||||
@@ -465,89 +465,6 @@ describe('payments/index', () => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
context('Upgrades subscription', () => {
|
|
||||||
it('from basic_earned to basic_6mo', async () => {
|
|
||||||
data.sub.key = 'basic_earned';
|
|
||||||
expect(user.purchased.plan.planId).to.not.exist;
|
|
||||||
|
|
||||||
await api.createSubscription(data);
|
|
||||||
|
|
||||||
expect(user.purchased.plan.planId).to.eql('basic_earned');
|
|
||||||
expect(user.purchased.plan.customerId).to.eql('customer-id');
|
|
||||||
const created = user.purchased.plan.dateCreated;
|
|
||||||
const updated = user.purchased.plan.dateUpdated;
|
|
||||||
|
|
||||||
data.sub.key = 'basic_6mo';
|
|
||||||
data.updatedFrom = { key: 'basic_earned' };
|
|
||||||
await api.createSubscription(data);
|
|
||||||
expect(user.purchased.plan.planId).to.eql('basic_6mo');
|
|
||||||
expect(user.purchased.plan.dateCreated).to.eql(created);
|
|
||||||
expect(user.purchased.plan.dateUpdated).to.not.eql(updated);
|
|
||||||
expect(user.purchased.plan.customerId).to.eql('customer-id');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('from basic_3mo to basic_12mo', async () => {
|
|
||||||
expect(user.purchased.plan.planId).to.not.exist;
|
|
||||||
|
|
||||||
await api.createSubscription(data);
|
|
||||||
|
|
||||||
expect(user.purchased.plan.planId).to.eql('basic_3mo');
|
|
||||||
expect(user.purchased.plan.customerId).to.eql('customer-id');
|
|
||||||
const created = user.purchased.plan.dateCreated;
|
|
||||||
const updated = user.purchased.plan.dateUpdated;
|
|
||||||
|
|
||||||
data.sub.key = 'basic_12mo';
|
|
||||||
data.updatedFrom = { key: 'basic_3mo' };
|
|
||||||
await api.createSubscription(data);
|
|
||||||
expect(user.purchased.plan.planId).to.eql('basic_12mo');
|
|
||||||
expect(user.purchased.plan.dateCreated).to.eql(created);
|
|
||||||
expect(user.purchased.plan.dateUpdated).to.not.eql(updated);
|
|
||||||
expect(user.purchased.plan.customerId).to.eql('customer-id');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
context('Downgrades subscription', () => {
|
|
||||||
it('from basic_6mo to basic_earned', async () => {
|
|
||||||
data.sub.key = 'basic_6mo';
|
|
||||||
expect(user.purchased.plan.planId).to.not.exist;
|
|
||||||
|
|
||||||
await api.createSubscription(data);
|
|
||||||
|
|
||||||
expect(user.purchased.plan.planId).to.eql('basic_6mo');
|
|
||||||
expect(user.purchased.plan.customerId).to.eql('customer-id');
|
|
||||||
const created = user.purchased.plan.dateCreated;
|
|
||||||
const updated = user.purchased.plan.dateUpdated;
|
|
||||||
|
|
||||||
data.sub.key = 'basic_earned';
|
|
||||||
data.updatedFrom = { key: 'basic_6mo' };
|
|
||||||
await api.createSubscription(data);
|
|
||||||
expect(user.purchased.plan.planId).to.eql('basic_earned');
|
|
||||||
expect(user.purchased.plan.dateCreated).to.eql(created);
|
|
||||||
expect(user.purchased.plan.dateUpdated).to.not.eql(updated);
|
|
||||||
expect(user.purchased.plan.customerId).to.eql('customer-id');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('from basic_12mo to basic_3mo', async () => {
|
|
||||||
expect(user.purchased.plan.planId).to.not.exist;
|
|
||||||
|
|
||||||
data.sub.key = 'basic_12mo';
|
|
||||||
await api.createSubscription(data);
|
|
||||||
|
|
||||||
expect(user.purchased.plan.planId).to.eql('basic_12mo');
|
|
||||||
expect(user.purchased.plan.customerId).to.eql('customer-id');
|
|
||||||
const created = user.purchased.plan.dateCreated;
|
|
||||||
const updated = user.purchased.plan.dateUpdated;
|
|
||||||
|
|
||||||
data.sub.key = 'basic_3mo';
|
|
||||||
data.updatedFrom = { key: 'basic_12mo' };
|
|
||||||
await api.createSubscription(data);
|
|
||||||
expect(user.purchased.plan.planId).to.eql('basic_3mo');
|
|
||||||
expect(user.purchased.plan.dateCreated).to.eql(created);
|
|
||||||
expect(user.purchased.plan.dateUpdated).to.not.eql(updated);
|
|
||||||
expect(user.purchased.plan.customerId).to.eql('customer-id');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
context('Block subscription perks', () => {
|
context('Block subscription perks', () => {
|
||||||
@@ -571,6 +488,7 @@ describe('payments/index', () => {
|
|||||||
|
|
||||||
it('adds 10 to plan.consecutive.gemCapExtra for 6 month block', async () => {
|
it('adds 10 to plan.consecutive.gemCapExtra for 6 month block', async () => {
|
||||||
data.sub.key = 'basic_6mo';
|
data.sub.key = 'basic_6mo';
|
||||||
|
|
||||||
await api.createSubscription(data);
|
await api.createSubscription(data);
|
||||||
|
|
||||||
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(10);
|
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(10);
|
||||||
@@ -578,6 +496,7 @@ describe('payments/index', () => {
|
|||||||
|
|
||||||
it('adds 20 to plan.consecutive.gemCapExtra for 12 month block', async () => {
|
it('adds 20 to plan.consecutive.gemCapExtra for 12 month block', async () => {
|
||||||
data.sub.key = 'basic_12mo';
|
data.sub.key = 'basic_12mo';
|
||||||
|
|
||||||
await api.createSubscription(data);
|
await api.createSubscription(data);
|
||||||
|
|
||||||
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(20);
|
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(20);
|
||||||
@@ -613,134 +532,6 @@ describe('payments/index', () => {
|
|||||||
|
|
||||||
expect(user.purchased.plan.consecutive.trinkets).to.eql(4);
|
expect(user.purchased.plan.consecutive.trinkets).to.eql(4);
|
||||||
});
|
});
|
||||||
|
|
||||||
context('Upgrades subscription', () => {
|
|
||||||
it('Adds 10 to plan.consecutive.gemCapExtra from basic_earned to basic_6mo', async () => {
|
|
||||||
data.sub.key = 'basic_earned';
|
|
||||||
expect(user.purchased.plan.planId).to.not.exist;
|
|
||||||
|
|
||||||
await api.createSubscription(data);
|
|
||||||
|
|
||||||
expect(user.purchased.plan.planId).to.eql('basic_earned');
|
|
||||||
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(0);
|
|
||||||
|
|
||||||
data.sub.key = 'basic_6mo';
|
|
||||||
data.updatedFrom = { key: 'basic_earned' };
|
|
||||||
await api.createSubscription(data);
|
|
||||||
expect(user.purchased.plan.planId).to.eql('basic_6mo');
|
|
||||||
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(10);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('Adds 15 to plan.consecutive.gemCapExtra when upgrading from basic_3mo to basic_12mo', async () => {
|
|
||||||
expect(user.purchased.plan.planId).to.not.exist;
|
|
||||||
|
|
||||||
await api.createSubscription(data);
|
|
||||||
|
|
||||||
expect(user.purchased.plan.planId).to.eql('basic_3mo');
|
|
||||||
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(5);
|
|
||||||
|
|
||||||
data.sub.key = 'basic_12mo';
|
|
||||||
data.updatedFrom = { key: 'basic_3mo' };
|
|
||||||
await api.createSubscription(data);
|
|
||||||
expect(user.purchased.plan.planId).to.eql('basic_12mo');
|
|
||||||
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(20);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('Adds 2 to plan.consecutive.trinkets from basic_earned to basic_6mo', async () => {
|
|
||||||
data.sub.key = 'basic_earned';
|
|
||||||
expect(user.purchased.plan.planId).to.not.exist;
|
|
||||||
|
|
||||||
await api.createSubscription(data);
|
|
||||||
|
|
||||||
expect(user.purchased.plan.planId).to.eql('basic_earned');
|
|
||||||
expect(user.purchased.plan.consecutive.trinkets).to.eql(0);
|
|
||||||
|
|
||||||
data.sub.key = 'basic_6mo';
|
|
||||||
data.updatedFrom = { key: 'basic_earned' };
|
|
||||||
await api.createSubscription(data);
|
|
||||||
expect(user.purchased.plan.planId).to.eql('basic_6mo');
|
|
||||||
expect(user.purchased.plan.consecutive.trinkets).to.eql(2);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('Adds 3 to plan.consecutive.trinkets when upgrading from basic_3mo to basic_12mo', async () => {
|
|
||||||
expect(user.purchased.plan.planId).to.not.exist;
|
|
||||||
|
|
||||||
await api.createSubscription(data);
|
|
||||||
|
|
||||||
expect(user.purchased.plan.planId).to.eql('basic_3mo');
|
|
||||||
expect(user.purchased.plan.consecutive.trinkets).to.eql(1);
|
|
||||||
|
|
||||||
data.sub.key = 'basic_12mo';
|
|
||||||
data.updatedFrom = { key: 'basic_3mo' };
|
|
||||||
await api.createSubscription(data);
|
|
||||||
expect(user.purchased.plan.planId).to.eql('basic_12mo');
|
|
||||||
expect(user.purchased.plan.consecutive.trinkets).to.eql(4);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
context('Downgrades subscription', () => {
|
|
||||||
it('does not remove from plan.consecutive.gemCapExtra from basic_6mo to basic_earned', async () => {
|
|
||||||
data.sub.key = 'basic_6mo';
|
|
||||||
expect(user.purchased.plan.planId).to.not.exist;
|
|
||||||
|
|
||||||
await api.createSubscription(data);
|
|
||||||
|
|
||||||
expect(user.purchased.plan.planId).to.eql('basic_6mo');
|
|
||||||
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(10);
|
|
||||||
|
|
||||||
data.sub.key = 'basic_earned';
|
|
||||||
data.updatedFrom = { key: 'basic_6mo' };
|
|
||||||
await api.createSubscription(data);
|
|
||||||
expect(user.purchased.plan.planId).to.eql('basic_earned');
|
|
||||||
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(10);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('does not remove from plan.consecutive.gemCapExtra from basic_12mo to basic_3mo', async () => {
|
|
||||||
expect(user.purchased.plan.planId).to.not.exist;
|
|
||||||
|
|
||||||
data.sub.key = 'basic_12mo';
|
|
||||||
await api.createSubscription(data);
|
|
||||||
|
|
||||||
expect(user.purchased.plan.planId).to.eql('basic_12mo');
|
|
||||||
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(20);
|
|
||||||
|
|
||||||
data.sub.key = 'basic_3mo';
|
|
||||||
data.updatedFrom = { key: 'basic_12mo' };
|
|
||||||
await api.createSubscription(data);
|
|
||||||
expect(user.purchased.plan.consecutive.gemCapExtra).to.eql(20);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('does not remove from plan.consecutive.trinkets from basic_6mo to basic_earned', async () => {
|
|
||||||
data.sub.key = 'basic_6mo';
|
|
||||||
expect(user.purchased.plan.planId).to.not.exist;
|
|
||||||
|
|
||||||
await api.createSubscription(data);
|
|
||||||
|
|
||||||
expect(user.purchased.plan.planId).to.eql('basic_6mo');
|
|
||||||
expect(user.purchased.plan.consecutive.trinkets).to.eql(2);
|
|
||||||
|
|
||||||
data.sub.key = 'basic_earned';
|
|
||||||
data.updatedFrom = { key: 'basic_6mo' };
|
|
||||||
await api.createSubscription(data);
|
|
||||||
expect(user.purchased.plan.planId).to.eql('basic_earned');
|
|
||||||
expect(user.purchased.plan.consecutive.trinkets).to.eql(2);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('does not remove from plan.consecutive.trinkets from basic_12mo to basic_3mo', async () => {
|
|
||||||
expect(user.purchased.plan.planId).to.not.exist;
|
|
||||||
|
|
||||||
data.sub.key = 'basic_12mo';
|
|
||||||
await api.createSubscription(data);
|
|
||||||
|
|
||||||
expect(user.purchased.plan.planId).to.eql('basic_12mo');
|
|
||||||
expect(user.purchased.plan.consecutive.trinkets).to.eql(4);
|
|
||||||
|
|
||||||
data.sub.key = 'basic_3mo';
|
|
||||||
data.updatedFrom = { key: 'basic_12mo' };
|
|
||||||
await api.createSubscription(data);
|
|
||||||
expect(user.purchased.plan.consecutive.trinkets).to.eql(4);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
context('Mystery Items', () => {
|
context('Mystery Items', () => {
|
||||||
|
|||||||
@@ -106,6 +106,10 @@ api.verifyGemPurchase = async function verifyGemPurchase (options) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
api.subscribe = async function subscribe (sku, user, receipt, headers, nextPaymentProcessing) {
|
api.subscribe = async function subscribe (sku, user, receipt, headers, nextPaymentProcessing) {
|
||||||
|
if (user && user.isSubscribed()) {
|
||||||
|
throw new NotAuthorized(this.constants.RESPONSE_ALREADY_USED);
|
||||||
|
}
|
||||||
|
|
||||||
if (!sku) throw new BadRequest(shared.i18n.t('missingSubscriptionCode'));
|
if (!sku) throw new BadRequest(shared.i18n.t('missingSubscriptionCode'));
|
||||||
|
|
||||||
let subCode;
|
let subCode;
|
||||||
@@ -136,49 +140,33 @@ api.subscribe = async function subscribe (sku, user, receipt, headers, nextPayme
|
|||||||
throw new NotAuthorized(api.constants.RESPONSE_NO_ITEM_PURCHASED);
|
throw new NotAuthorized(api.constants.RESPONSE_NO_ITEM_PURCHASED);
|
||||||
}
|
}
|
||||||
|
|
||||||
let originalTransactionId;
|
let transactionId;
|
||||||
let newTransactionId;
|
|
||||||
|
|
||||||
for (const purchaseData of purchaseDataList) {
|
for (const purchaseData of purchaseDataList) {
|
||||||
const dateTerminated = new Date(Number(purchaseData.expirationDate));
|
const dateTerminated = new Date(Number(purchaseData.expirationDate));
|
||||||
if (purchaseData.productId === sku && dateTerminated > new Date()) {
|
if (purchaseData.productId === sku && dateTerminated > new Date()) {
|
||||||
originalTransactionId = purchaseData.originalTransactionId;
|
transactionId = purchaseData.transactionId;
|
||||||
newTransactionId = purchaseData.transactionId;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (originalTransactionId) {
|
if (transactionId) {
|
||||||
let existingSub;
|
|
||||||
if (user && user.isSubscribed()) {
|
|
||||||
if (user.purchased.plan.customerId !== originalTransactionId) {
|
|
||||||
throw new NotAuthorized(this.constants.RESPONSE_ALREADY_USED);
|
|
||||||
}
|
|
||||||
existingSub = shared.content.subscriptionBlocks[user.purchased.plan.planId];
|
|
||||||
}
|
|
||||||
const existingUser = await User.findOne({
|
const existingUser = await User.findOne({
|
||||||
'purchased.plan.customerId': originalTransactionId,
|
'purchased.plan.customerId': transactionId,
|
||||||
}).exec();
|
}).exec();
|
||||||
if (existingUser
|
if (existingUser) throw new NotAuthorized(this.constants.RESPONSE_ALREADY_USED);
|
||||||
&& (originalTransactionId === newTransactionId || existingUser._id !== user._id)) {
|
|
||||||
throw new NotAuthorized(this.constants.RESPONSE_ALREADY_USED);
|
|
||||||
}
|
|
||||||
|
|
||||||
nextPaymentProcessing = nextPaymentProcessing || moment.utc().add({ days: 2 }); // eslint-disable-line max-len, no-param-reassign
|
nextPaymentProcessing = nextPaymentProcessing || moment.utc().add({ days: 2 }); // eslint-disable-line max-len, no-param-reassign
|
||||||
|
|
||||||
const data = {
|
await payments.createSubscription({
|
||||||
user,
|
user,
|
||||||
customerId: originalTransactionId,
|
customerId: transactionId,
|
||||||
paymentMethod: this.constants.PAYMENT_METHOD_APPLE,
|
paymentMethod: this.constants.PAYMENT_METHOD_APPLE,
|
||||||
sub,
|
sub,
|
||||||
headers,
|
headers,
|
||||||
nextPaymentProcessing,
|
nextPaymentProcessing,
|
||||||
additionalData: receipt,
|
additionalData: receipt,
|
||||||
};
|
});
|
||||||
if (existingSub) {
|
|
||||||
data.updatedFrom = existingSub;
|
|
||||||
}
|
|
||||||
await payments.createSubscription(data);
|
|
||||||
} else {
|
} else {
|
||||||
throw new NotAuthorized(api.constants.RESPONSE_INVALID_RECEIPT);
|
throw new NotAuthorized(api.constants.RESPONSE_INVALID_RECEIPT);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -74,15 +74,7 @@ async function prepareSubscriptionValues (data) {
|
|||||||
? data.gift.subscription.key
|
? data.gift.subscription.key
|
||||||
: data.sub.key];
|
: data.sub.key];
|
||||||
const autoRenews = data.autoRenews !== undefined ? data.autoRenews : true;
|
const autoRenews = data.autoRenews !== undefined ? data.autoRenews : true;
|
||||||
const updatedFrom = data.updatedFrom
|
const months = Number(block.months);
|
||||||
? shared.content.subscriptionBlocks[data.updatedFrom.key]
|
|
||||||
: undefined;
|
|
||||||
let months;
|
|
||||||
if (updatedFrom && Number(updatedFrom.months) !== 1) {
|
|
||||||
months = Math.max(0, Number(block.months) - Number(updatedFrom.months));
|
|
||||||
} else {
|
|
||||||
months = Number(block.months);
|
|
||||||
}
|
|
||||||
const today = new Date();
|
const today = new Date();
|
||||||
let group;
|
let group;
|
||||||
let groupId;
|
let groupId;
|
||||||
|
|||||||
Reference in New Issue
Block a user