mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 14:47:53 +01:00
fix(api): Grant remaining perks when canceling an Amazon subscription
* Add tests for payments lib * Add placeholder it blocks to be filled in closes #7660 closes #4840
This commit is contained in:
@@ -4,84 +4,233 @@ import { model as User } from '../../../../../website/server/models/user';
|
|||||||
import moment from 'moment';
|
import moment from 'moment';
|
||||||
|
|
||||||
describe('payments/index', () => {
|
describe('payments/index', () => {
|
||||||
let fakeSend;
|
|
||||||
let data;
|
|
||||||
let user;
|
|
||||||
|
|
||||||
describe('#createSubscription', () => {
|
describe('#createSubscription', () => {
|
||||||
const MYSTERY_AWARD_COUNT = 2;
|
let user;
|
||||||
const MYSTERY_AWARD_UNIX_TIME = 1464725113000;
|
|
||||||
let fakeClock;
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
user = new User();
|
user = new User();
|
||||||
fakeClock = sinon.useFakeTimers(MYSTERY_AWARD_UNIX_TIME);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
context('Purchasing a subscription as a gift', () => {
|
||||||
fakeClock.restore();
|
it('adds extra months to an existing subscription');
|
||||||
|
|
||||||
|
it('sets a dateTerminated date for a user without an existing subscription');
|
||||||
|
|
||||||
|
it('sets plan.dateUpdated if it did not previously exist');
|
||||||
|
|
||||||
|
it('does not change plan.customerId if it already exists');
|
||||||
|
|
||||||
|
it('sets plan.customerId to "Gift" if it does not already exist');
|
||||||
|
|
||||||
|
it('increases the buyer\'s transaction count');
|
||||||
|
|
||||||
|
it('sends a private message about the gift');
|
||||||
|
|
||||||
|
it('sends an email about the gift');
|
||||||
|
|
||||||
|
it('sends a push notification about the gift');
|
||||||
|
|
||||||
|
it('tracks subscription purchase as gift (if prod)');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('succeeds', async () => {
|
context('Purchasing a subscription for self', () => {
|
||||||
data = { user, sub: { key: 'basic_3mo' } };
|
it('creates a subscription', async () => {
|
||||||
expect(user.purchased.plan.planId).to.not.exist;
|
let data = {
|
||||||
await api.createSubscription(data);
|
user,
|
||||||
expect(user.purchased.plan.planId).to.exist;
|
sub: {
|
||||||
|
key: 'basic_3mo',
|
||||||
|
},
|
||||||
|
customerId: 'customer-id',
|
||||||
|
paymentMethod: 'Payment Method',
|
||||||
|
};
|
||||||
|
|
||||||
|
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');
|
||||||
|
expect(user.purchased.plan.dateUpdated).to.exist;
|
||||||
|
expect(user.purchased.plan.gemsBought).to.eql(0);
|
||||||
|
expect(user.purchased.plan.paymentMethod).to.eql('Payment Method');
|
||||||
|
expect(user.purchased.plan.extraMonths).to.eql(0);
|
||||||
|
expect(user.purchased.plan.dateTerminated).to.eql(null);
|
||||||
|
expect(user.purchased.plan.lastBillingDate).to.not.exist;
|
||||||
|
expect(user.purchased.plan.dateCreated).to.exist;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('sets extraMonths if plan has dateTerminated date');
|
||||||
|
|
||||||
|
it('sets lastBillingDate if payment method is "Amazon Payments"');
|
||||||
|
|
||||||
|
it('increases the user\'s transcation count');
|
||||||
|
|
||||||
|
it('sends a transaction email (if prod)');
|
||||||
|
|
||||||
|
it('tracks subscription purchase (if prod)');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('awards mystery items', async () => {
|
context('Block subscription perks', () => {
|
||||||
data = { user, sub: { key: 'basic_3mo' } };
|
it('adds block months to plan.consecutive.offset');
|
||||||
await api.createSubscription(data);
|
|
||||||
expect(user.purchased.plan.mysteryItems.length).to.eql(MYSTERY_AWARD_COUNT);
|
it('does not add to plans.consecutive.offset if 1 month subscription');
|
||||||
|
|
||||||
|
it('adds 5 to plan.consecutive.gemCapExtra for every 3 months');
|
||||||
|
|
||||||
|
it('does not raise plan.consecutive.gemCapExtra higher than 25');
|
||||||
|
|
||||||
|
it('adds a plan.consecutive.trinkets for every 3 months');
|
||||||
|
});
|
||||||
|
|
||||||
|
context('Mystery Items', () => {
|
||||||
|
it('awards mystery items when within the timeframe for a mystery item', async () => {
|
||||||
|
let mayMysteryItemTimeframe = 1464725113000; // May 31st 2016
|
||||||
|
let fakeClock = sinon.useFakeTimers(mayMysteryItemTimeframe);
|
||||||
|
let data = { user, sub: { key: 'basic_3mo' } };
|
||||||
|
|
||||||
|
await api.createSubscription(data);
|
||||||
|
|
||||||
|
expect(user.purchased.plan.mysteryItems).to.have.a.lengthOf(2);
|
||||||
|
expect(user.purchased.plan.mysteryItems).to.include('armor_mystery_201605');
|
||||||
|
expect(user.purchased.plan.mysteryItems).to.include('head_mystery_201605');
|
||||||
|
|
||||||
|
fakeClock.restore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not awards mystery items when not within the timeframe for a mystery item', async () => {
|
||||||
|
const noMysteryItemTimeframe = 1462183920000; // May 2nd 2016
|
||||||
|
let fakeClock = sinon.useFakeTimers(noMysteryItemTimeframe);
|
||||||
|
let data = { user, sub: { key: 'basic_3mo' } };
|
||||||
|
|
||||||
|
await api.createSubscription(data);
|
||||||
|
|
||||||
|
expect(user.purchased.plan.mysteryItems).to.have.a.lengthOf(0);
|
||||||
|
|
||||||
|
fakeClock.restore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not award mystery item when user already owns the item', async () => {
|
||||||
|
let mayMysteryItemTimeframe = 1464725113000; // May 31st 2016
|
||||||
|
let fakeClock = sinon.useFakeTimers(mayMysteryItemTimeframe);
|
||||||
|
let mayMysteryItem = 'armor_mystery_201605';
|
||||||
|
user.items.gear.owned[mayMysteryItem] = true;
|
||||||
|
|
||||||
|
let data = { user, sub: { key: 'basic_3mo' } };
|
||||||
|
|
||||||
|
await api.createSubscription(data);
|
||||||
|
|
||||||
|
expect(user.purchased.plan.mysteryItems).to.have.a.lengthOf(1);
|
||||||
|
expect(user.purchased.plan.mysteryItems).to.include('head_mystery_201605');
|
||||||
|
|
||||||
|
fakeClock.restore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('does not award mystery item when user already has the item in the mystery box', async () => {
|
||||||
|
let mayMysteryItemTimeframe = 1464725113000; // May 31st 2016
|
||||||
|
let fakeClock = sinon.useFakeTimers(mayMysteryItemTimeframe);
|
||||||
|
let mayMysteryItem = 'armor_mystery_201605';
|
||||||
|
user.purchased.plan.mysteryItems = [mayMysteryItem];
|
||||||
|
|
||||||
|
sandbox.spy(user.purchased.plan.mysteryItems, 'push');
|
||||||
|
|
||||||
|
let data = { user, sub: { key: 'basic_3mo' } };
|
||||||
|
|
||||||
|
await api.createSubscription(data);
|
||||||
|
|
||||||
|
expect(user.purchased.plan.mysteryItems.push).to.be.calledOnce;
|
||||||
|
expect(user.purchased.plan.mysteryItems.push).to.be.calledWith('head_mystery_201605');
|
||||||
|
|
||||||
|
fakeClock.restore();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('#cancelSubscription', () => {
|
describe('#cancelSubscription', () => {
|
||||||
|
let data, user;
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
fakeSend = sinon.spy(sender, 'sendTxn');
|
sandbox.spy(sender, 'sendTxn');
|
||||||
data = { user: new User() };
|
user = new User();
|
||||||
|
data = { user };
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
fakeSend.restore();
|
sandbox.restore();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('plan.extraMonths is defined', () => {
|
it('adds a month termination date by default', () => {
|
||||||
api.cancelSubscription(data);
|
api.cancelSubscription(data);
|
||||||
let terminated = data.user.purchased.plan.dateTerminated;
|
|
||||||
data.user.purchased.plan.extraMonths = 2;
|
let now = new Date();
|
||||||
api.cancelSubscription(data);
|
let daysTillTermination = moment(user.purchased.plan.dateTerminated).diff(now, 'days');
|
||||||
let difference = Math.abs(moment(terminated).diff(data.user.purchased.plan.dateTerminated, 'days'));
|
|
||||||
expect(difference - 60).to.be.lessThan(3); // the difference is approximately two months, +/- 2 days
|
expect(daysTillTermination).to.be.within(29, 30); // 1 month +/- 1 days
|
||||||
});
|
});
|
||||||
|
|
||||||
it('plan.extraMonth is a fraction', () => {
|
it('adds extraMonths to dateTerminated value', () => {
|
||||||
|
user.purchased.plan.extraMonths = 2;
|
||||||
|
|
||||||
api.cancelSubscription(data);
|
api.cancelSubscription(data);
|
||||||
let terminated = data.user.purchased.plan.dateTerminated;
|
|
||||||
data.user.purchased.plan.extraMonths = 0.3;
|
let now = new Date();
|
||||||
api.cancelSubscription(data);
|
let daysTillTermination = moment(user.purchased.plan.dateTerminated).diff(now, 'days');
|
||||||
let difference = Math.abs(moment(terminated).diff(data.user.purchased.plan.dateTerminated, 'days'));
|
|
||||||
expect(difference - 10).to.be.lessThan(3); // the difference should be 10 days.
|
expect(daysTillTermination).to.be.within(89, 90); // 3 months +/- 1 days
|
||||||
});
|
});
|
||||||
|
|
||||||
it('nextBill is defined', () => {
|
it('handles extra month fractions', () => {
|
||||||
|
user.purchased.plan.extraMonths = 0.3;
|
||||||
|
|
||||||
api.cancelSubscription(data);
|
api.cancelSubscription(data);
|
||||||
let terminated = data.user.purchased.plan.dateTerminated;
|
|
||||||
data.nextBill = moment().add({ days: 25 });
|
let now = new Date();
|
||||||
api.cancelSubscription(data);
|
let daysTillTermination = moment(user.purchased.plan.dateTerminated).diff(now, 'days');
|
||||||
let difference = Math.abs(moment(terminated).diff(data.user.purchased.plan.dateTerminated, 'days'));
|
|
||||||
expect(difference - 5).to.be.lessThan(2); // the difference should be 5 days, +/- 1 day
|
expect(daysTillTermination).to.be.within(38, 39); // should be about 1 month + 1/3 month
|
||||||
});
|
});
|
||||||
|
|
||||||
it('saves the canceled subscription for the user', () => {
|
it('terminates at next billing date if it exists', () => {
|
||||||
expect(data.user.purchased.plan.dateTerminated).to.not.exist;
|
data.nextBill = moment().add({ days: 15 });
|
||||||
|
|
||||||
api.cancelSubscription(data);
|
api.cancelSubscription(data);
|
||||||
expect(data.user.purchased.plan.dateTerminated).to.exist;
|
|
||||||
|
let now = new Date();
|
||||||
|
let daysTillTermination = moment(user.purchased.plan.dateTerminated).diff(now, 'days');
|
||||||
|
|
||||||
|
expect(daysTillTermination).to.be.within(13, 15);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('sends a text', async () => {
|
it('resets plan.extraMonths', () => {
|
||||||
|
user.purchased.plan.extraMonths = 5;
|
||||||
|
|
||||||
|
api.cancelSubscription(data);
|
||||||
|
|
||||||
|
expect(user.purchased.plan.extraMonths).to.eql(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('sends an email', async () => {
|
||||||
await api.cancelSubscription(data);
|
await api.cancelSubscription(data);
|
||||||
sinon.assert.called(fakeSend);
|
|
||||||
|
expect(sender.sendTxn).to.be.calledOnce;
|
||||||
|
expect(sender.sendTxn).to.be.calledWith(user, 'cancel-subscription');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('#buyGems', () => {
|
||||||
|
context('Self Purchase', () => {
|
||||||
|
it('amount property defaults to 5');
|
||||||
|
|
||||||
|
it('sends a donation email (if prod)');
|
||||||
|
});
|
||||||
|
|
||||||
|
context('Gift', () => {
|
||||||
|
it('calculates balance from gem amount if gift');
|
||||||
|
|
||||||
|
it('sends a gifted-gems email (if prod)');
|
||||||
|
|
||||||
|
it('sends a message from purchaser to recipient');
|
||||||
|
|
||||||
|
it('sends a push notification if user did not gift to self');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -239,9 +239,12 @@ api.subscribeCancel = {
|
|||||||
AmazonBillingAgreementId: billingAgreementId,
|
AmazonBillingAgreementId: billingAgreementId,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
let subscriptionBlock = shared.content.subscriptionBlocks[user.purchased.plan.planId];
|
||||||
|
let subscriptionLength = subscriptionBlock.months * 30;
|
||||||
|
|
||||||
await payments.cancelSubscription({
|
await payments.cancelSubscription({
|
||||||
user,
|
user,
|
||||||
nextBill: moment(user.purchased.plan.lastBillingDate).add({ days: 30 }),
|
nextBill: moment(user.purchased.plan.lastBillingDate).add({ days: subscriptionLength }),
|
||||||
paymentMethod: 'Amazon Payments',
|
paymentMethod: 'Amazon Payments',
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -127,14 +127,16 @@ api.cancelSubscription = async function cancelSubscription (data) {
|
|||||||
let plan = data.user.purchased.plan;
|
let plan = data.user.purchased.plan;
|
||||||
let now = moment();
|
let now = moment();
|
||||||
let remaining = data.nextBill ? moment(data.nextBill).diff(new Date(), 'days') : 30;
|
let remaining = data.nextBill ? moment(data.nextBill).diff(new Date(), 'days') : 30;
|
||||||
|
let extraDays = Math.ceil(30 * plan.extraMonths);
|
||||||
let nowStr = `${now.format('MM')}/${moment(plan.dateUpdated).format('DD')}/${now.format('YYYY')}`;
|
let nowStr = `${now.format('MM')}/${moment(plan.dateUpdated).format('DD')}/${now.format('YYYY')}`;
|
||||||
let nowStrFormat = 'MM/DD/YYYY';
|
let nowStrFormat = 'MM/DD/YYYY';
|
||||||
|
|
||||||
plan.dateTerminated =
|
plan.dateTerminated =
|
||||||
moment(nowStr, nowStrFormat)
|
moment(nowStr, nowStrFormat)
|
||||||
.add({days: remaining}) // end their subscription 1mo from their last payment
|
.add({days: remaining})
|
||||||
.add({days: Math.ceil(30 * plan.extraMonths)}) // plus any extra time (carry-over, gifted subscription, etc) they have.
|
.add({days: extraDays})
|
||||||
.toDate();
|
.toDate();
|
||||||
|
|
||||||
plan.extraMonths = 0; // clear extra time. If they subscribe again, it'll be recalculated from p.dateTerminated
|
plan.extraMonths = 0; // clear extra time. If they subscribe again, it'll be recalculated from p.dateTerminated
|
||||||
|
|
||||||
await data.user.save();
|
await data.user.save();
|
||||||
|
|||||||
Reference in New Issue
Block a user