2024-05-28 09:43:53 +03:00
|
|
|
import '../../src/plugins/payment';
|
|
|
|
|
2024-04-30 09:59:54 +03:00
|
|
|
import { INestApplication } from '@nestjs/common';
|
|
|
|
import { PrismaClient } from '@prisma/client';
|
|
|
|
import ava, { TestFn } from 'ava';
|
|
|
|
import Sinon from 'sinon';
|
|
|
|
import Stripe from 'stripe';
|
|
|
|
|
|
|
|
import { AppModule } from '../../src/app.module';
|
|
|
|
import { CurrentUser } from '../../src/core/auth';
|
|
|
|
import { AuthService } from '../../src/core/auth/service';
|
|
|
|
import {
|
|
|
|
EarlyAccessType,
|
|
|
|
FeatureManagementService,
|
|
|
|
} from '../../src/core/features';
|
2024-05-07 12:44:37 +03:00
|
|
|
import { EventEmitter } from '../../src/fundamentals';
|
2024-12-05 11:31:01 +03:00
|
|
|
import { Config, ConfigModule, Runtime } from '../../src/fundamentals/config';
|
2024-12-05 11:31:00 +03:00
|
|
|
import { SubscriptionService } from '../../src/plugins/payment/service';
|
2024-04-30 09:59:54 +03:00
|
|
|
import {
|
|
|
|
CouponType,
|
|
|
|
encodeLookupKey,
|
|
|
|
SubscriptionPlan,
|
|
|
|
SubscriptionRecurring,
|
|
|
|
SubscriptionStatus,
|
2024-10-10 13:12:40 +03:00
|
|
|
SubscriptionVariant,
|
2024-04-30 09:59:54 +03:00
|
|
|
} from '../../src/plugins/payment/types';
|
2024-12-05 11:31:00 +03:00
|
|
|
import { createTestingApp, initTestingDB } from '../utils';
|
2024-04-30 09:59:54 +03:00
|
|
|
|
|
|
|
const PRO_MONTHLY = `${SubscriptionPlan.Pro}_${SubscriptionRecurring.Monthly}`;
|
|
|
|
const PRO_YEARLY = `${SubscriptionPlan.Pro}_${SubscriptionRecurring.Yearly}`;
|
2024-07-08 10:41:26 +03:00
|
|
|
const PRO_LIFETIME = `${SubscriptionPlan.Pro}_${SubscriptionRecurring.Lifetime}`;
|
2024-10-10 13:12:40 +03:00
|
|
|
const PRO_EA_YEARLY = `${SubscriptionPlan.Pro}_${SubscriptionRecurring.Yearly}_${SubscriptionVariant.EA}`;
|
2024-04-30 09:59:54 +03:00
|
|
|
const AI_YEARLY = `${SubscriptionPlan.AI}_${SubscriptionRecurring.Yearly}`;
|
2024-10-10 13:12:40 +03:00
|
|
|
const AI_YEARLY_EA = `${SubscriptionPlan.AI}_${SubscriptionRecurring.Yearly}_${SubscriptionVariant.EA}`;
|
2024-12-05 11:31:01 +03:00
|
|
|
const TEAM_MONTHLY = `${SubscriptionPlan.Team}_${SubscriptionRecurring.Monthly}`;
|
|
|
|
const TEAM_YEARLY = `${SubscriptionPlan.Team}_${SubscriptionRecurring.Yearly}`;
|
2024-10-10 13:12:40 +03:00
|
|
|
// prices for code redeeming
|
|
|
|
const PRO_MONTHLY_CODE = `${SubscriptionPlan.Pro}_${SubscriptionRecurring.Monthly}_${SubscriptionVariant.Onetime}`;
|
|
|
|
const PRO_YEARLY_CODE = `${SubscriptionPlan.Pro}_${SubscriptionRecurring.Yearly}_${SubscriptionVariant.Onetime}`;
|
|
|
|
const AI_YEARLY_CODE = `${SubscriptionPlan.AI}_${SubscriptionRecurring.Yearly}_${SubscriptionVariant.Onetime}`;
|
2024-04-30 09:59:54 +03:00
|
|
|
|
|
|
|
const PRICES = {
|
|
|
|
[PRO_MONTHLY]: {
|
|
|
|
recurring: {
|
|
|
|
interval: 'month',
|
|
|
|
},
|
|
|
|
unit_amount: 799,
|
|
|
|
currency: 'usd',
|
2024-12-05 11:31:00 +03:00
|
|
|
id: PRO_MONTHLY,
|
2024-04-30 09:59:54 +03:00
|
|
|
lookup_key: PRO_MONTHLY,
|
|
|
|
},
|
|
|
|
[PRO_YEARLY]: {
|
|
|
|
recurring: {
|
|
|
|
interval: 'year',
|
|
|
|
},
|
|
|
|
unit_amount: 8100,
|
|
|
|
currency: 'usd',
|
2024-12-05 11:31:00 +03:00
|
|
|
id: PRO_YEARLY,
|
2024-04-30 09:59:54 +03:00
|
|
|
lookup_key: PRO_YEARLY,
|
|
|
|
},
|
2024-07-08 10:41:26 +03:00
|
|
|
[PRO_LIFETIME]: {
|
|
|
|
unit_amount: 49900,
|
|
|
|
currency: 'usd',
|
2024-12-05 11:31:00 +03:00
|
|
|
id: PRO_LIFETIME,
|
2024-07-08 10:41:26 +03:00
|
|
|
lookup_key: PRO_LIFETIME,
|
|
|
|
},
|
2024-04-30 09:59:54 +03:00
|
|
|
[PRO_EA_YEARLY]: {
|
|
|
|
recurring: {
|
|
|
|
interval: 'year',
|
|
|
|
},
|
|
|
|
unit_amount: 5000,
|
|
|
|
currency: 'usd',
|
2024-12-05 11:31:00 +03:00
|
|
|
id: PRO_EA_YEARLY,
|
2024-04-30 09:59:54 +03:00
|
|
|
lookup_key: PRO_EA_YEARLY,
|
|
|
|
},
|
|
|
|
[AI_YEARLY]: {
|
|
|
|
recurring: {
|
|
|
|
interval: 'year',
|
|
|
|
},
|
|
|
|
unit_amount: 10680,
|
|
|
|
currency: 'usd',
|
2024-12-05 11:31:00 +03:00
|
|
|
id: AI_YEARLY,
|
2024-04-30 09:59:54 +03:00
|
|
|
lookup_key: AI_YEARLY,
|
|
|
|
},
|
|
|
|
[AI_YEARLY_EA]: {
|
|
|
|
recurring: {
|
|
|
|
interval: 'year',
|
|
|
|
},
|
|
|
|
unit_amount: 9999,
|
|
|
|
currency: 'usd',
|
2024-12-05 11:31:00 +03:00
|
|
|
id: AI_YEARLY_EA,
|
2024-04-30 09:59:54 +03:00
|
|
|
lookup_key: AI_YEARLY_EA,
|
|
|
|
},
|
2024-10-10 13:12:40 +03:00
|
|
|
[PRO_MONTHLY_CODE]: {
|
|
|
|
unit_amount: 799,
|
|
|
|
currency: 'usd',
|
2024-12-05 11:31:00 +03:00
|
|
|
id: PRO_MONTHLY_CODE,
|
2024-10-10 13:12:40 +03:00
|
|
|
lookup_key: PRO_MONTHLY_CODE,
|
|
|
|
},
|
|
|
|
[PRO_YEARLY_CODE]: {
|
|
|
|
unit_amount: 8100,
|
|
|
|
currency: 'usd',
|
2024-12-05 11:31:00 +03:00
|
|
|
id: PRO_YEARLY_CODE,
|
2024-10-10 13:12:40 +03:00
|
|
|
lookup_key: PRO_YEARLY_CODE,
|
|
|
|
},
|
|
|
|
[AI_YEARLY_CODE]: {
|
|
|
|
unit_amount: 10680,
|
|
|
|
currency: 'usd',
|
2024-12-05 11:31:00 +03:00
|
|
|
id: AI_YEARLY_CODE,
|
2024-10-10 13:12:40 +03:00
|
|
|
lookup_key: AI_YEARLY_CODE,
|
|
|
|
},
|
2024-12-05 11:31:01 +03:00
|
|
|
[TEAM_MONTHLY]: {
|
|
|
|
unit_amount: 1500,
|
|
|
|
currency: 'usd',
|
|
|
|
id: TEAM_MONTHLY,
|
|
|
|
lookup_key: TEAM_MONTHLY,
|
|
|
|
},
|
|
|
|
[TEAM_YEARLY]: {
|
|
|
|
unit_amount: 14400,
|
|
|
|
currency: 'usd',
|
|
|
|
id: TEAM_YEARLY,
|
|
|
|
lookup_key: TEAM_YEARLY,
|
|
|
|
},
|
2024-12-05 11:31:00 +03:00
|
|
|
} as any as Record<string, Stripe.Price>;
|
2024-04-30 09:59:54 +03:00
|
|
|
|
|
|
|
const sub: Stripe.Subscription = {
|
|
|
|
id: 'sub_1',
|
|
|
|
object: 'subscription',
|
|
|
|
cancel_at_period_end: false,
|
|
|
|
canceled_at: null,
|
|
|
|
current_period_end: 1745654236,
|
|
|
|
current_period_start: 1714118236,
|
2024-12-05 11:31:00 +03:00
|
|
|
// @ts-expect-error stub
|
|
|
|
customer: {
|
|
|
|
id: 'cus_1',
|
|
|
|
email: 'u1@affine.pro',
|
|
|
|
},
|
2024-04-30 09:59:54 +03:00
|
|
|
items: {
|
|
|
|
object: 'list',
|
|
|
|
data: [
|
|
|
|
{
|
|
|
|
id: 'si_1',
|
|
|
|
// @ts-expect-error stub
|
|
|
|
price: {
|
|
|
|
id: 'price_1',
|
|
|
|
lookup_key: 'pro_monthly',
|
|
|
|
},
|
|
|
|
subscription: 'sub_1',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
status: 'active',
|
|
|
|
trial_end: null,
|
|
|
|
trial_start: null,
|
|
|
|
schedule: null,
|
2024-12-05 11:31:01 +03:00
|
|
|
metadata: {},
|
2024-04-30 09:59:54 +03:00
|
|
|
};
|
|
|
|
|
2024-12-05 11:31:00 +03:00
|
|
|
const test = ava as TestFn<{
|
|
|
|
u1: CurrentUser;
|
|
|
|
db: PrismaClient;
|
|
|
|
app: INestApplication;
|
|
|
|
service: SubscriptionService;
|
|
|
|
event: Sinon.SinonStubbedInstance<EventEmitter>;
|
|
|
|
feature: Sinon.SinonStubbedInstance<FeatureManagementService>;
|
2024-12-05 11:31:01 +03:00
|
|
|
runtime: Sinon.SinonStubbedInstance<Runtime>;
|
2024-12-05 11:31:00 +03:00
|
|
|
stripe: {
|
|
|
|
customers: Sinon.SinonStubbedInstance<Stripe.CustomersResource>;
|
|
|
|
prices: Sinon.SinonStubbedInstance<Stripe.PricesResource>;
|
|
|
|
subscriptions: Sinon.SinonStubbedInstance<Stripe.SubscriptionsResource>;
|
|
|
|
subscriptionSchedules: Sinon.SinonStubbedInstance<Stripe.SubscriptionSchedulesResource>;
|
|
|
|
checkout: {
|
|
|
|
sessions: Sinon.SinonStubbedInstance<Stripe.Checkout.SessionsResource>;
|
|
|
|
};
|
2024-12-05 11:31:01 +03:00
|
|
|
promotionCodes: Sinon.SinonStubbedInstance<Stripe.PromotionCodesResource>;
|
2024-12-05 11:31:00 +03:00
|
|
|
};
|
|
|
|
}>;
|
|
|
|
|
|
|
|
function getLastCheckoutPrice(checkoutStub: Sinon.SinonStub) {
|
|
|
|
const call = checkoutStub.getCall(checkoutStub.callCount - 1);
|
|
|
|
const arg = call.args[0] as Stripe.Checkout.SessionCreateParams;
|
|
|
|
return {
|
|
|
|
price: arg.line_items?.[0]?.price,
|
|
|
|
coupon: arg.discounts?.[0]?.coupon,
|
|
|
|
};
|
|
|
|
}
|
2024-04-30 09:59:54 +03:00
|
|
|
|
2024-12-05 11:31:00 +03:00
|
|
|
test.before(async t => {
|
|
|
|
const { app } = await createTestingApp({
|
|
|
|
imports: [
|
|
|
|
ConfigModule.forRoot({
|
|
|
|
plugins: {
|
|
|
|
payment: {
|
|
|
|
stripe: {
|
|
|
|
keys: {
|
|
|
|
APIKey: '1',
|
|
|
|
webhookKey: '1',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
AppModule,
|
|
|
|
],
|
|
|
|
tapModule: m => {
|
|
|
|
m.overrideProvider(FeatureManagementService).useValue(
|
|
|
|
Sinon.createStubInstance(FeatureManagementService)
|
|
|
|
);
|
|
|
|
m.overrideProvider(EventEmitter).useValue(
|
|
|
|
Sinon.createStubInstance(EventEmitter)
|
|
|
|
);
|
2024-12-05 11:31:01 +03:00
|
|
|
m.overrideProvider(Runtime).useValue(Sinon.createStubInstance(Runtime));
|
2024-12-05 11:31:00 +03:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
t.context.event = app.get(EventEmitter);
|
|
|
|
t.context.service = app.get(SubscriptionService);
|
|
|
|
t.context.feature = app.get(FeatureManagementService);
|
2024-12-05 11:31:01 +03:00
|
|
|
t.context.runtime = app.get(Runtime);
|
2024-12-05 11:31:00 +03:00
|
|
|
t.context.db = app.get(PrismaClient);
|
|
|
|
t.context.app = app;
|
|
|
|
|
|
|
|
const stripe = app.get(Stripe);
|
|
|
|
const stripeStubs = {
|
|
|
|
customers: Sinon.stub(stripe.customers),
|
|
|
|
prices: Sinon.stub(stripe.prices),
|
|
|
|
subscriptions: Sinon.stub(stripe.subscriptions),
|
|
|
|
subscriptionSchedules: Sinon.stub(stripe.subscriptionSchedules),
|
|
|
|
checkout: {
|
|
|
|
sessions: Sinon.stub(stripe.checkout.sessions),
|
|
|
|
},
|
2024-12-05 11:31:01 +03:00
|
|
|
promotionCodes: Sinon.stub(stripe.promotionCodes),
|
2024-12-05 11:31:00 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
t.context.stripe = stripeStubs;
|
|
|
|
});
|
|
|
|
|
|
|
|
test.beforeEach(async t => {
|
|
|
|
const { db, app, stripe } = t.context;
|
|
|
|
Sinon.reset();
|
|
|
|
await initTestingDB(db);
|
2024-12-05 11:31:01 +03:00
|
|
|
t.context.runtime.fetch
|
|
|
|
.withArgs('plugins.payment/showLifetimePrice')
|
|
|
|
.resolves(true);
|
2024-12-05 11:31:00 +03:00
|
|
|
t.context.u1 = await app.get(AuthService).signUp('u1@affine.pro', '1');
|
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
await db.workspace.create({
|
|
|
|
data: {
|
|
|
|
id: 'ws_1',
|
|
|
|
public: false,
|
|
|
|
},
|
|
|
|
});
|
2024-12-05 11:31:00 +03:00
|
|
|
await db.userStripeCustomer.create({
|
|
|
|
data: {
|
|
|
|
userId: t.context.u1.id,
|
|
|
|
stripeCustomerId: 'cus_1',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// default stubs
|
2024-04-30 09:59:54 +03:00
|
|
|
// @ts-expect-error stub
|
2024-12-05 11:31:00 +03:00
|
|
|
stripe.prices.list.callsFake((params: Stripe.PriceListParams) => {
|
|
|
|
if (params.lookup_keys) {
|
|
|
|
return Promise.resolve({
|
|
|
|
data: params.lookup_keys.map(lk => PRICES[lk]),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve({ data: Object.values(PRICES) });
|
|
|
|
});
|
|
|
|
|
2024-04-30 09:59:54 +03:00
|
|
|
// @ts-expect-error stub
|
2024-12-05 11:31:00 +03:00
|
|
|
stripe.subscriptions.list.resolves({ data: [] });
|
|
|
|
});
|
|
|
|
|
|
|
|
test.after.always(async t => {
|
|
|
|
await t.context.app.close();
|
|
|
|
});
|
|
|
|
|
|
|
|
// ============== prices ==============
|
|
|
|
test('should list normal price for unauthenticated user', async t => {
|
|
|
|
const { service } = t.context;
|
2024-04-30 09:59:54 +03:00
|
|
|
|
|
|
|
const prices = await service.listPrices();
|
|
|
|
|
2024-12-05 11:31:00 +03:00
|
|
|
t.snapshot(prices.map(p => encodeLookupKey(p.lookupKey)));
|
2024-04-30 09:59:54 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should list normal prices for authenticated user', async t => {
|
2024-12-05 11:31:00 +03:00
|
|
|
const { feature, service, u1 } = t.context;
|
2024-04-30 09:59:54 +03:00
|
|
|
|
2024-05-27 14:17:21 +03:00
|
|
|
feature.isEarlyAccessUser.withArgs(u1.id).resolves(false);
|
|
|
|
feature.isEarlyAccessUser.withArgs(u1.id, EarlyAccessType.AI).resolves(false);
|
2024-04-30 09:59:54 +03:00
|
|
|
|
|
|
|
const prices = await service.listPrices(u1);
|
|
|
|
|
2024-12-05 11:31:00 +03:00
|
|
|
t.snapshot(prices.map(p => encodeLookupKey(p.lookupKey)));
|
2024-04-30 09:59:54 +03:00
|
|
|
});
|
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
test('should not show lifetime price if not enabled', async t => {
|
|
|
|
const { service, runtime } = t.context;
|
|
|
|
runtime.fetch.withArgs('plugins.payment/showLifetimePrice').resolves(false);
|
|
|
|
|
|
|
|
const prices = await service.listPrices(t.context.u1);
|
|
|
|
|
|
|
|
t.snapshot(prices.map(p => encodeLookupKey(p.lookupKey)));
|
|
|
|
});
|
|
|
|
|
2024-04-30 09:59:54 +03:00
|
|
|
test('should list early access prices for pro ea user', async t => {
|
2024-12-05 11:31:00 +03:00
|
|
|
const { feature, service, u1 } = t.context;
|
2024-04-30 09:59:54 +03:00
|
|
|
|
2024-05-27 14:17:21 +03:00
|
|
|
feature.isEarlyAccessUser.withArgs(u1.id).resolves(true);
|
|
|
|
feature.isEarlyAccessUser.withArgs(u1.id, EarlyAccessType.AI).resolves(false);
|
2024-04-30 09:59:54 +03:00
|
|
|
|
|
|
|
const prices = await service.listPrices(u1);
|
|
|
|
|
2024-12-05 11:31:00 +03:00
|
|
|
t.snapshot(prices.map(p => encodeLookupKey(p.lookupKey)));
|
2024-04-30 09:59:54 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should list normal prices for pro ea user with old subscriptions', async t => {
|
|
|
|
const { feature, service, u1, stripe } = t.context;
|
|
|
|
|
2024-05-27 14:17:21 +03:00
|
|
|
feature.isEarlyAccessUser.withArgs(u1.id).resolves(true);
|
|
|
|
feature.isEarlyAccessUser.withArgs(u1.id, EarlyAccessType.AI).resolves(false);
|
2024-04-30 09:59:54 +03:00
|
|
|
|
2024-12-05 11:31:00 +03:00
|
|
|
stripe.subscriptions.list.resolves({
|
2024-04-30 09:59:54 +03:00
|
|
|
data: [
|
|
|
|
{
|
|
|
|
id: 'sub_1',
|
|
|
|
status: 'canceled',
|
|
|
|
items: {
|
|
|
|
data: [
|
|
|
|
{
|
|
|
|
// @ts-expect-error stub
|
|
|
|
price: {
|
|
|
|
lookup_key: PRO_YEARLY,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
const prices = await service.listPrices(u1);
|
|
|
|
|
2024-12-05 11:31:00 +03:00
|
|
|
t.snapshot(prices.map(p => encodeLookupKey(p.lookupKey)));
|
2024-04-30 09:59:54 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should list early access prices for ai ea user', async t => {
|
2024-12-05 11:31:00 +03:00
|
|
|
const { feature, service, u1 } = t.context;
|
2024-04-30 09:59:54 +03:00
|
|
|
|
2024-05-27 14:17:21 +03:00
|
|
|
feature.isEarlyAccessUser.withArgs(u1.id).resolves(false);
|
|
|
|
feature.isEarlyAccessUser.withArgs(u1.id, EarlyAccessType.AI).resolves(true);
|
2024-04-30 09:59:54 +03:00
|
|
|
|
|
|
|
const prices = await service.listPrices(u1);
|
|
|
|
|
2024-12-05 11:31:00 +03:00
|
|
|
t.snapshot(prices.map(p => encodeLookupKey(p.lookupKey)));
|
2024-04-30 09:59:54 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should list early access prices for pro and ai ea user', async t => {
|
2024-12-05 11:31:00 +03:00
|
|
|
const { feature, service, u1 } = t.context;
|
2024-04-30 09:59:54 +03:00
|
|
|
|
2024-05-27 14:17:21 +03:00
|
|
|
feature.isEarlyAccessUser.withArgs(u1.id).resolves(true);
|
2024-04-30 09:59:54 +03:00
|
|
|
|
|
|
|
const prices = await service.listPrices(u1);
|
|
|
|
|
2024-12-05 11:31:00 +03:00
|
|
|
t.snapshot(prices.map(p => encodeLookupKey(p.lookupKey)));
|
2024-04-30 09:59:54 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should list normal prices for ai ea user with old subscriptions', async t => {
|
|
|
|
const { feature, service, u1, stripe } = t.context;
|
|
|
|
|
2024-05-27 14:17:21 +03:00
|
|
|
feature.isEarlyAccessUser.withArgs(u1.id).resolves(false);
|
|
|
|
feature.isEarlyAccessUser.withArgs(u1.id, EarlyAccessType.AI).resolves(true);
|
2024-04-30 09:59:54 +03:00
|
|
|
|
2024-12-05 11:31:00 +03:00
|
|
|
stripe.subscriptions.list.resolves({
|
2024-04-30 09:59:54 +03:00
|
|
|
data: [
|
|
|
|
{
|
|
|
|
id: 'sub_1',
|
|
|
|
status: 'canceled',
|
|
|
|
items: {
|
|
|
|
data: [
|
|
|
|
{
|
|
|
|
// @ts-expect-error stub
|
|
|
|
price: {
|
|
|
|
lookup_key: AI_YEARLY,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
|
|
|
const prices = await service.listPrices(u1);
|
|
|
|
|
2024-12-05 11:31:00 +03:00
|
|
|
t.snapshot(prices.map(p => encodeLookupKey(p.lookupKey)));
|
2024-04-30 09:59:54 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// ============= end prices ================
|
|
|
|
|
|
|
|
// ============= checkout ==================
|
|
|
|
test('should throw if user has subscription already', async t => {
|
|
|
|
const { service, u1, db } = t.context;
|
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
await db.subscription.create({
|
2024-04-30 09:59:54 +03:00
|
|
|
data: {
|
2024-12-05 11:31:01 +03:00
|
|
|
targetId: u1.id,
|
2024-04-30 09:59:54 +03:00
|
|
|
stripeSubscriptionId: 'sub_1',
|
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
recurring: SubscriptionRecurring.Monthly,
|
|
|
|
status: SubscriptionStatus.Active,
|
|
|
|
start: new Date(),
|
|
|
|
end: new Date(),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await t.throwsAsync(
|
|
|
|
() =>
|
2024-12-05 11:31:01 +03:00
|
|
|
service.checkout(
|
|
|
|
{
|
2024-12-05 11:31:00 +03:00
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
recurring: SubscriptionRecurring.Monthly,
|
2024-12-05 11:31:01 +03:00
|
|
|
successCallbackLink: '',
|
2024-12-05 11:31:00 +03:00
|
|
|
},
|
2024-12-05 11:31:01 +03:00
|
|
|
{ user: u1 }
|
|
|
|
),
|
2024-06-17 06:30:58 +03:00
|
|
|
{ message: 'You have already subscribed to the pro plan.' }
|
2024-04-30 09:59:54 +03:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should get correct pro plan price for checking out', async t => {
|
2024-12-05 11:31:00 +03:00
|
|
|
const { app, service, u1, stripe, feature } = t.context;
|
2024-04-30 09:59:54 +03:00
|
|
|
// non-ea user
|
|
|
|
{
|
|
|
|
feature.isEarlyAccessUser.resolves(false);
|
2024-12-05 11:31:00 +03:00
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
await service.checkout(
|
|
|
|
{
|
2024-12-05 11:31:00 +03:00
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
recurring: SubscriptionRecurring.Monthly,
|
2024-12-05 11:31:01 +03:00
|
|
|
successCallbackLink: '',
|
2024-12-05 11:31:00 +03:00
|
|
|
},
|
2024-12-05 11:31:01 +03:00
|
|
|
{ user: u1 }
|
|
|
|
);
|
2024-12-05 11:31:00 +03:00
|
|
|
|
|
|
|
t.deepEqual(getLastCheckoutPrice(stripe.checkout.sessions.create), {
|
2024-04-30 09:59:54 +03:00
|
|
|
price: PRO_MONTHLY,
|
|
|
|
coupon: undefined,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// ea user, but monthly
|
|
|
|
{
|
|
|
|
feature.isEarlyAccessUser.resolves(true);
|
2024-12-05 11:31:01 +03:00
|
|
|
await service.checkout(
|
|
|
|
{
|
2024-12-05 11:31:00 +03:00
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
recurring: SubscriptionRecurring.Monthly,
|
2024-12-05 11:31:01 +03:00
|
|
|
successCallbackLink: '',
|
2024-12-05 11:31:00 +03:00
|
|
|
},
|
2024-12-05 11:31:01 +03:00
|
|
|
{ user: u1 }
|
|
|
|
);
|
2024-12-05 11:31:00 +03:00
|
|
|
|
|
|
|
t.deepEqual(getLastCheckoutPrice(stripe.checkout.sessions.create), {
|
2024-04-30 09:59:54 +03:00
|
|
|
price: PRO_MONTHLY,
|
|
|
|
coupon: undefined,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// ea user, yearly
|
|
|
|
{
|
|
|
|
feature.isEarlyAccessUser.resolves(true);
|
2024-12-05 11:31:01 +03:00
|
|
|
await service.checkout(
|
|
|
|
{
|
2024-12-05 11:31:00 +03:00
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
recurring: SubscriptionRecurring.Yearly,
|
2024-12-05 11:31:01 +03:00
|
|
|
successCallbackLink: '',
|
2024-12-05 11:31:00 +03:00
|
|
|
},
|
2024-12-05 11:31:01 +03:00
|
|
|
{ user: u1 }
|
|
|
|
);
|
2024-12-05 11:31:00 +03:00
|
|
|
|
|
|
|
t.deepEqual(getLastCheckoutPrice(stripe.checkout.sessions.create), {
|
2024-04-30 09:59:54 +03:00
|
|
|
price: PRO_EA_YEARLY,
|
|
|
|
coupon: CouponType.ProEarlyAccessOneYearFree,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// ea user, yearly recurring, but has old subscription
|
|
|
|
{
|
|
|
|
feature.isEarlyAccessUser.resolves(true);
|
2024-12-05 11:31:00 +03:00
|
|
|
stripe.subscriptions.list.resolves({
|
2024-04-30 09:59:54 +03:00
|
|
|
data: [
|
|
|
|
{
|
|
|
|
id: 'sub_1',
|
|
|
|
status: 'canceled',
|
|
|
|
items: {
|
|
|
|
data: [
|
|
|
|
{
|
|
|
|
// @ts-expect-error stub
|
|
|
|
price: {
|
|
|
|
lookup_key: PRO_YEARLY,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
await service.checkout(
|
|
|
|
{
|
2024-12-05 11:31:00 +03:00
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
recurring: SubscriptionRecurring.Yearly,
|
2024-12-05 11:31:01 +03:00
|
|
|
successCallbackLink: '',
|
2024-12-05 11:31:00 +03:00
|
|
|
},
|
2024-12-05 11:31:01 +03:00
|
|
|
{ user: u1 }
|
|
|
|
);
|
2024-12-05 11:31:00 +03:00
|
|
|
|
|
|
|
t.deepEqual(getLastCheckoutPrice(stripe.checkout.sessions.create), {
|
2024-04-30 09:59:54 +03:00
|
|
|
price: PRO_YEARLY,
|
|
|
|
coupon: undefined,
|
|
|
|
});
|
2024-12-05 11:31:00 +03:00
|
|
|
|
|
|
|
await t.throwsAsync(
|
|
|
|
() =>
|
2024-12-05 11:31:01 +03:00
|
|
|
service.checkout(
|
|
|
|
{
|
2024-12-05 11:31:00 +03:00
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
recurring: SubscriptionRecurring.Yearly,
|
|
|
|
variant: SubscriptionVariant.EA,
|
2024-12-05 11:31:01 +03:00
|
|
|
successCallbackLink: '',
|
2024-12-05 11:31:00 +03:00
|
|
|
},
|
2024-12-05 11:31:01 +03:00
|
|
|
{ user: u1 }
|
|
|
|
),
|
2024-12-05 11:31:00 +03:00
|
|
|
{
|
|
|
|
message: 'You are trying to access a unknown subscription plan.',
|
|
|
|
}
|
|
|
|
);
|
2024-04-30 09:59:54 +03:00
|
|
|
}
|
2024-07-08 10:41:26 +03:00
|
|
|
|
|
|
|
// any user, lifetime recurring
|
|
|
|
{
|
|
|
|
feature.isEarlyAccessUser.resolves(false);
|
2024-12-05 11:31:00 +03:00
|
|
|
const config = app.get(Config);
|
|
|
|
await config.runtime.set('plugins.payment/showLifetimePrice', true);
|
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
await service.checkout(
|
|
|
|
{
|
2024-12-05 11:31:00 +03:00
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
recurring: SubscriptionRecurring.Lifetime,
|
2024-12-05 11:31:01 +03:00
|
|
|
successCallbackLink: '',
|
2024-12-05 11:31:00 +03:00
|
|
|
},
|
2024-12-05 11:31:01 +03:00
|
|
|
{ user: u1 }
|
|
|
|
);
|
2024-12-05 11:31:00 +03:00
|
|
|
|
|
|
|
t.deepEqual(getLastCheckoutPrice(stripe.checkout.sessions.create), {
|
2024-07-08 10:41:26 +03:00
|
|
|
price: PRO_LIFETIME,
|
|
|
|
coupon: undefined,
|
|
|
|
});
|
|
|
|
}
|
2024-04-30 09:59:54 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should get correct ai plan price for checking out', async t => {
|
|
|
|
const { service, u1, stripe, feature } = t.context;
|
|
|
|
|
|
|
|
// non-ea user
|
|
|
|
{
|
|
|
|
feature.isEarlyAccessUser.resolves(false);
|
2024-12-05 11:31:00 +03:00
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
await service.checkout(
|
|
|
|
{
|
2024-12-05 11:31:00 +03:00
|
|
|
plan: SubscriptionPlan.AI,
|
|
|
|
recurring: SubscriptionRecurring.Yearly,
|
2024-12-05 11:31:01 +03:00
|
|
|
successCallbackLink: '',
|
2024-12-05 11:31:00 +03:00
|
|
|
},
|
2024-12-05 11:31:01 +03:00
|
|
|
{ user: u1 }
|
|
|
|
);
|
2024-12-05 11:31:00 +03:00
|
|
|
|
|
|
|
t.deepEqual(getLastCheckoutPrice(stripe.checkout.sessions.create), {
|
2024-04-30 09:59:54 +03:00
|
|
|
price: AI_YEARLY,
|
|
|
|
coupon: undefined,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// ea user
|
|
|
|
{
|
|
|
|
feature.isEarlyAccessUser.resolves(true);
|
2024-12-05 11:31:00 +03:00
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
await service.checkout(
|
|
|
|
{
|
2024-12-05 11:31:00 +03:00
|
|
|
plan: SubscriptionPlan.AI,
|
|
|
|
recurring: SubscriptionRecurring.Yearly,
|
2024-12-05 11:31:01 +03:00
|
|
|
successCallbackLink: '',
|
2024-12-05 11:31:00 +03:00
|
|
|
},
|
2024-12-05 11:31:01 +03:00
|
|
|
{ user: u1 }
|
|
|
|
);
|
2024-12-05 11:31:00 +03:00
|
|
|
|
|
|
|
t.deepEqual(getLastCheckoutPrice(stripe.checkout.sessions.create), {
|
2024-04-30 09:59:54 +03:00
|
|
|
price: AI_YEARLY_EA,
|
|
|
|
coupon: CouponType.AIEarlyAccessOneYearFree,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// ea user, but has old subscription
|
|
|
|
{
|
2024-12-05 11:31:00 +03:00
|
|
|
feature.isEarlyAccessUser.withArgs(u1.id).resolves(false);
|
|
|
|
feature.isEarlyAccessUser
|
|
|
|
.withArgs(u1.id, EarlyAccessType.AI)
|
|
|
|
.resolves(true);
|
|
|
|
stripe.subscriptions.list.resolves({
|
2024-04-30 09:59:54 +03:00
|
|
|
data: [
|
|
|
|
{
|
|
|
|
id: 'sub_1',
|
|
|
|
status: 'canceled',
|
|
|
|
items: {
|
|
|
|
data: [
|
|
|
|
{
|
|
|
|
// @ts-expect-error stub
|
|
|
|
price: {
|
|
|
|
lookup_key: AI_YEARLY,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
await service.checkout(
|
|
|
|
{
|
2024-12-05 11:31:00 +03:00
|
|
|
plan: SubscriptionPlan.AI,
|
|
|
|
recurring: SubscriptionRecurring.Yearly,
|
2024-12-05 11:31:01 +03:00
|
|
|
successCallbackLink: '',
|
2024-12-05 11:31:00 +03:00
|
|
|
},
|
2024-12-05 11:31:01 +03:00
|
|
|
{ user: u1 }
|
|
|
|
);
|
2024-12-05 11:31:00 +03:00
|
|
|
|
|
|
|
t.deepEqual(getLastCheckoutPrice(stripe.checkout.sessions.create), {
|
2024-04-30 09:59:54 +03:00
|
|
|
price: AI_YEARLY,
|
|
|
|
coupon: undefined,
|
|
|
|
});
|
2024-12-05 11:31:00 +03:00
|
|
|
|
|
|
|
await t.throwsAsync(
|
|
|
|
() =>
|
2024-12-05 11:31:01 +03:00
|
|
|
service.checkout(
|
|
|
|
{
|
2024-12-05 11:31:00 +03:00
|
|
|
plan: SubscriptionPlan.AI,
|
|
|
|
recurring: SubscriptionRecurring.Yearly,
|
|
|
|
variant: SubscriptionVariant.EA,
|
2024-12-05 11:31:01 +03:00
|
|
|
successCallbackLink: '',
|
2024-12-05 11:31:00 +03:00
|
|
|
},
|
2024-12-05 11:31:01 +03:00
|
|
|
{ user: u1 }
|
|
|
|
),
|
2024-12-05 11:31:00 +03:00
|
|
|
{
|
|
|
|
message: 'You are trying to access a unknown subscription plan.',
|
|
|
|
}
|
|
|
|
);
|
2024-04-30 09:59:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// pro ea user
|
|
|
|
{
|
2024-05-27 14:17:21 +03:00
|
|
|
feature.isEarlyAccessUser.withArgs(u1.id).resolves(true);
|
2024-04-30 09:59:54 +03:00
|
|
|
feature.isEarlyAccessUser
|
2024-05-27 14:17:21 +03:00
|
|
|
.withArgs(u1.id, EarlyAccessType.AI)
|
2024-04-30 09:59:54 +03:00
|
|
|
.resolves(false);
|
|
|
|
// @ts-expect-error stub
|
2024-12-05 11:31:00 +03:00
|
|
|
stripe.subscriptions.list.resolves({ data: [] });
|
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
await service.checkout(
|
|
|
|
{
|
2024-12-05 11:31:00 +03:00
|
|
|
plan: SubscriptionPlan.AI,
|
|
|
|
recurring: SubscriptionRecurring.Yearly,
|
2024-12-05 11:31:01 +03:00
|
|
|
successCallbackLink: '',
|
2024-12-05 11:31:00 +03:00
|
|
|
},
|
2024-12-05 11:31:01 +03:00
|
|
|
{ user: u1 }
|
|
|
|
);
|
2024-12-05 11:31:00 +03:00
|
|
|
|
|
|
|
t.deepEqual(getLastCheckoutPrice(stripe.checkout.sessions.create), {
|
2024-04-30 09:59:54 +03:00
|
|
|
price: AI_YEARLY,
|
|
|
|
coupon: CouponType.ProEarlyAccessAIOneYearFree,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// pro ea user, but has old subscription
|
|
|
|
{
|
2024-05-27 14:17:21 +03:00
|
|
|
feature.isEarlyAccessUser.withArgs(u1.id).resolves(true);
|
2024-04-30 09:59:54 +03:00
|
|
|
feature.isEarlyAccessUser
|
2024-05-27 14:17:21 +03:00
|
|
|
.withArgs(u1.id, EarlyAccessType.AI)
|
2024-04-30 09:59:54 +03:00
|
|
|
.resolves(false);
|
2024-12-05 11:31:00 +03:00
|
|
|
stripe.subscriptions.list.resolves({
|
2024-04-30 09:59:54 +03:00
|
|
|
data: [
|
|
|
|
{
|
|
|
|
id: 'sub_1',
|
|
|
|
status: 'canceled',
|
|
|
|
items: {
|
|
|
|
data: [
|
|
|
|
{
|
|
|
|
// @ts-expect-error stub
|
|
|
|
price: {
|
|
|
|
lookup_key: AI_YEARLY,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
await service.checkout(
|
|
|
|
{
|
2024-12-05 11:31:00 +03:00
|
|
|
plan: SubscriptionPlan.AI,
|
|
|
|
recurring: SubscriptionRecurring.Yearly,
|
2024-12-05 11:31:01 +03:00
|
|
|
successCallbackLink: '',
|
2024-12-05 11:31:00 +03:00
|
|
|
},
|
2024-12-05 11:31:01 +03:00
|
|
|
{ user: u1 }
|
|
|
|
);
|
2024-12-05 11:31:00 +03:00
|
|
|
|
|
|
|
t.deepEqual(getLastCheckoutPrice(stripe.checkout.sessions.create), {
|
2024-04-30 09:59:54 +03:00
|
|
|
price: AI_YEARLY,
|
|
|
|
coupon: undefined,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should apply user coupon for checking out', async t => {
|
|
|
|
const { service, u1, stripe } = t.context;
|
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
stripe.promotionCodes.list.resolves({
|
|
|
|
data: [
|
|
|
|
{
|
|
|
|
// @ts-expect-error mock
|
|
|
|
coupon: {
|
|
|
|
id: 'coupon_1',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
});
|
2024-04-30 09:59:54 +03:00
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
await service.checkout(
|
|
|
|
{
|
2024-12-05 11:31:00 +03:00
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
recurring: SubscriptionRecurring.Monthly,
|
2024-12-05 11:31:01 +03:00
|
|
|
successCallbackLink: '',
|
|
|
|
coupon: 'test',
|
2024-12-05 11:31:00 +03:00
|
|
|
},
|
2024-12-05 11:31:01 +03:00
|
|
|
{ user: u1 }
|
|
|
|
);
|
2024-04-30 09:59:54 +03:00
|
|
|
|
2024-12-05 11:31:00 +03:00
|
|
|
t.deepEqual(getLastCheckoutPrice(stripe.checkout.sessions.create), {
|
|
|
|
price: PRO_MONTHLY,
|
|
|
|
coupon: 'coupon_1',
|
|
|
|
});
|
2024-04-30 09:59:54 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// =============== subscriptions ===============
|
|
|
|
test('should be able to create subscription', async t => {
|
2024-12-05 11:31:00 +03:00
|
|
|
const { event, service, db, u1 } = t.context;
|
|
|
|
|
|
|
|
await service.saveStripeSubscription(sub);
|
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
const subInDB = await db.subscription.findFirst({
|
|
|
|
where: { targetId: u1.id },
|
2024-12-05 11:31:00 +03:00
|
|
|
});
|
2024-04-30 09:59:54 +03:00
|
|
|
|
2024-05-07 12:44:37 +03:00
|
|
|
t.true(
|
2024-12-05 11:31:00 +03:00
|
|
|
event.emit.calledOnceWith('user.subscription.activated', {
|
2024-05-07 12:44:37 +03:00
|
|
|
userId: u1.id,
|
|
|
|
plan: SubscriptionPlan.Pro,
|
2024-07-08 10:41:26 +03:00
|
|
|
recurring: SubscriptionRecurring.Monthly,
|
2024-05-07 12:44:37 +03:00
|
|
|
})
|
|
|
|
);
|
2024-04-30 09:59:54 +03:00
|
|
|
t.is(subInDB?.stripeSubscriptionId, sub.id);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should be able to update subscription', async t => {
|
2024-12-05 11:31:00 +03:00
|
|
|
const { event, service, db, u1 } = t.context;
|
|
|
|
await service.saveStripeSubscription(sub);
|
2024-04-30 09:59:54 +03:00
|
|
|
|
2024-12-05 11:31:00 +03:00
|
|
|
await service.saveStripeSubscription({
|
2024-04-30 09:59:54 +03:00
|
|
|
...sub,
|
|
|
|
cancel_at_period_end: true,
|
|
|
|
canceled_at: 1714118236,
|
2024-12-05 11:31:00 +03:00
|
|
|
});
|
|
|
|
|
2024-05-07 12:44:37 +03:00
|
|
|
t.true(
|
2024-12-05 11:31:00 +03:00
|
|
|
event.emit.calledWith('user.subscription.activated', {
|
2024-05-07 12:44:37 +03:00
|
|
|
userId: u1.id,
|
|
|
|
plan: SubscriptionPlan.Pro,
|
2024-07-08 10:41:26 +03:00
|
|
|
recurring: SubscriptionRecurring.Monthly,
|
2024-05-07 12:44:37 +03:00
|
|
|
})
|
|
|
|
);
|
2024-04-30 09:59:54 +03:00
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
const subInDB = await db.subscription.findFirst({
|
|
|
|
where: { targetId: u1.id },
|
2024-04-30 09:59:54 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
t.is(subInDB?.status, SubscriptionStatus.Active);
|
|
|
|
t.is(subInDB?.canceledAt?.getTime(), 1714118236000);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should be able to delete subscription', async t => {
|
2024-12-05 11:31:00 +03:00
|
|
|
const { event, service, db, u1 } = t.context;
|
|
|
|
await service.saveStripeSubscription(sub);
|
2024-04-30 09:59:54 +03:00
|
|
|
|
2024-12-05 11:31:00 +03:00
|
|
|
await service.saveStripeSubscription({
|
|
|
|
...sub,
|
|
|
|
status: 'canceled',
|
2024-04-30 09:59:54 +03:00
|
|
|
});
|
|
|
|
|
2024-05-07 12:44:37 +03:00
|
|
|
t.true(
|
2024-12-05 11:31:00 +03:00
|
|
|
event.emit.calledWith('user.subscription.canceled', {
|
2024-05-07 12:44:37 +03:00
|
|
|
userId: u1.id,
|
|
|
|
plan: SubscriptionPlan.Pro,
|
2024-07-08 10:41:26 +03:00
|
|
|
recurring: SubscriptionRecurring.Monthly,
|
2024-05-07 12:44:37 +03:00
|
|
|
})
|
|
|
|
);
|
2024-04-30 09:59:54 +03:00
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
const subInDB = await db.subscription.findFirst({
|
|
|
|
where: { targetId: u1.id },
|
2024-04-30 09:59:54 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
t.is(subInDB, null);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should be able to cancel subscription', async t => {
|
2024-12-05 11:31:00 +03:00
|
|
|
const { service, db, u1, stripe } = t.context;
|
2024-04-30 09:59:54 +03:00
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
await db.subscription.create({
|
2024-04-30 09:59:54 +03:00
|
|
|
data: {
|
2024-12-05 11:31:01 +03:00
|
|
|
targetId: u1.id,
|
2024-04-30 09:59:54 +03:00
|
|
|
stripeSubscriptionId: 'sub_1',
|
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
recurring: SubscriptionRecurring.Yearly,
|
|
|
|
status: SubscriptionStatus.Active,
|
|
|
|
start: new Date(),
|
|
|
|
end: new Date(),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-12-05 11:31:00 +03:00
|
|
|
stripe.subscriptions.update.resolves({
|
2024-04-30 09:59:54 +03:00
|
|
|
...sub,
|
|
|
|
cancel_at_period_end: true,
|
|
|
|
canceled_at: 1714118236,
|
|
|
|
} as any);
|
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
const subInDB = await service.cancelSubscription({
|
|
|
|
userId: u1.id,
|
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
});
|
2024-12-05 11:31:00 +03:00
|
|
|
|
2024-05-07 12:44:37 +03:00
|
|
|
t.true(
|
2024-12-05 11:31:00 +03:00
|
|
|
stripe.subscriptions.update.calledOnceWith('sub_1', {
|
|
|
|
cancel_at_period_end: true,
|
2024-05-07 12:44:37 +03:00
|
|
|
})
|
|
|
|
);
|
2024-04-30 09:59:54 +03:00
|
|
|
t.is(subInDB.status, SubscriptionStatus.Active);
|
|
|
|
t.truthy(subInDB.canceledAt);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should be able to resume subscription', async t => {
|
2024-12-05 11:31:00 +03:00
|
|
|
const { service, db, u1, stripe } = t.context;
|
2024-04-30 09:59:54 +03:00
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
await db.subscription.create({
|
2024-04-30 09:59:54 +03:00
|
|
|
data: {
|
2024-12-05 11:31:01 +03:00
|
|
|
targetId: u1.id,
|
2024-04-30 09:59:54 +03:00
|
|
|
stripeSubscriptionId: 'sub_1',
|
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
recurring: SubscriptionRecurring.Yearly,
|
|
|
|
status: SubscriptionStatus.Active,
|
|
|
|
start: new Date(),
|
|
|
|
end: new Date(Date.now() + 100000),
|
|
|
|
canceledAt: new Date(),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-12-05 11:31:00 +03:00
|
|
|
stripe.subscriptions.update.resolves(sub as any);
|
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
const subInDB = await service.resumeSubscription({
|
|
|
|
userId: u1.id,
|
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
});
|
2024-04-30 09:59:54 +03:00
|
|
|
|
2024-05-07 12:44:37 +03:00
|
|
|
t.true(
|
2024-12-05 11:31:00 +03:00
|
|
|
stripe.subscriptions.update.calledOnceWith('sub_1', {
|
|
|
|
cancel_at_period_end: false,
|
2024-05-07 12:44:37 +03:00
|
|
|
})
|
|
|
|
);
|
2024-04-30 09:59:54 +03:00
|
|
|
t.is(subInDB.status, SubscriptionStatus.Active);
|
|
|
|
t.falsy(subInDB.canceledAt);
|
|
|
|
});
|
|
|
|
|
|
|
|
const subscriptionSchedule: Stripe.SubscriptionSchedule = {
|
|
|
|
id: 'sub_sched_1',
|
|
|
|
customer: 'cus_1',
|
|
|
|
subscription: 'sub_1',
|
|
|
|
status: 'active',
|
|
|
|
phases: [
|
|
|
|
{
|
|
|
|
items: [
|
|
|
|
// @ts-expect-error mock
|
|
|
|
{
|
|
|
|
price: PRO_MONTHLY,
|
2024-12-05 11:31:01 +03:00
|
|
|
quantity: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
start_date: Math.floor(Date.now() / 1000),
|
|
|
|
end_date: Math.floor((Date.now() + 30 * 24 * 60 * 60 * 1000) / 1000),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
items: [
|
|
|
|
// @ts-expect-error mock
|
|
|
|
{
|
|
|
|
price: PRO_YEARLY,
|
|
|
|
quantity: 1,
|
2024-04-30 09:59:54 +03:00
|
|
|
},
|
|
|
|
],
|
2024-12-05 11:31:01 +03:00
|
|
|
start_date: Math.floor((Date.now() + 30 * 24 * 60 * 60 * 1000) / 1000),
|
2024-04-30 09:59:54 +03:00
|
|
|
},
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
|
|
|
test('should be able to update recurring', async t => {
|
|
|
|
const { service, db, u1, stripe } = t.context;
|
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
await db.subscription.create({
|
2024-04-30 09:59:54 +03:00
|
|
|
data: {
|
2024-12-05 11:31:01 +03:00
|
|
|
targetId: u1.id,
|
2024-04-30 09:59:54 +03:00
|
|
|
stripeSubscriptionId: 'sub_1',
|
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
recurring: SubscriptionRecurring.Monthly,
|
|
|
|
status: SubscriptionStatus.Active,
|
|
|
|
start: new Date(),
|
|
|
|
end: new Date(Date.now() + 100000),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
// 1. turn a subscription into a subscription schedule
|
2024-12-05 11:31:01 +03:00
|
|
|
// 2. update the current phase with an end date
|
2024-12-05 11:31:00 +03:00
|
|
|
stripe.subscriptions.retrieve.resolves(sub as any);
|
|
|
|
stripe.subscriptionSchedules.create.resolves(subscriptionSchedule as any);
|
|
|
|
stripe.subscriptionSchedules.update.resolves(subscriptionSchedule as any);
|
2024-04-30 09:59:54 +03:00
|
|
|
|
|
|
|
await service.updateSubscriptionRecurring(
|
2024-12-05 11:31:01 +03:00
|
|
|
{
|
|
|
|
userId: u1.id,
|
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
},
|
2024-04-30 09:59:54 +03:00
|
|
|
SubscriptionRecurring.Yearly
|
|
|
|
);
|
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
t.true(
|
|
|
|
stripe.subscriptionSchedules.update.calledOnceWith(
|
|
|
|
subscriptionSchedule.id,
|
2024-04-30 09:59:54 +03:00
|
|
|
{
|
2024-12-05 11:31:01 +03:00
|
|
|
phases: [
|
2024-04-30 09:59:54 +03:00
|
|
|
{
|
2024-12-05 11:31:01 +03:00
|
|
|
items: [
|
|
|
|
{
|
|
|
|
price: PRO_MONTHLY,
|
|
|
|
quantity: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
start_date: subscriptionSchedule.phases[0].start_date,
|
|
|
|
end_date: subscriptionSchedule.phases[0].end_date,
|
2024-04-30 09:59:54 +03:00
|
|
|
},
|
|
|
|
{
|
2024-12-05 11:31:01 +03:00
|
|
|
items: [
|
|
|
|
{
|
|
|
|
price: PRO_YEARLY,
|
|
|
|
quantity: 1,
|
|
|
|
},
|
|
|
|
],
|
2024-04-30 09:59:54 +03:00
|
|
|
},
|
|
|
|
],
|
2024-12-05 11:31:01 +03:00
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
2024-04-30 09:59:54 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should release the schedule if the new recurring is the same as the current phase', async t => {
|
|
|
|
const { service, db, u1, stripe } = t.context;
|
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
await db.subscription.create({
|
2024-04-30 09:59:54 +03:00
|
|
|
data: {
|
2024-12-05 11:31:01 +03:00
|
|
|
targetId: u1.id,
|
2024-04-30 09:59:54 +03:00
|
|
|
stripeSubscriptionId: 'sub_1',
|
|
|
|
stripeScheduleId: 'sub_sched_1',
|
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
recurring: SubscriptionRecurring.Yearly,
|
|
|
|
status: SubscriptionStatus.Active,
|
|
|
|
start: new Date(),
|
|
|
|
end: new Date(Date.now() + 100000),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-12-05 11:31:00 +03:00
|
|
|
stripe.subscriptions.retrieve.resolves({
|
2024-04-30 09:59:54 +03:00
|
|
|
...sub,
|
2024-12-05 11:31:00 +03:00
|
|
|
schedule: subscriptionSchedule.id,
|
2024-04-30 09:59:54 +03:00
|
|
|
} as any);
|
2024-12-05 11:31:00 +03:00
|
|
|
stripe.subscriptionSchedules.retrieve.resolves(subscriptionSchedule as any);
|
2024-04-30 09:59:54 +03:00
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
const subInDB = await service.updateSubscriptionRecurring(
|
|
|
|
{
|
|
|
|
userId: u1.id,
|
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
},
|
2024-04-30 09:59:54 +03:00
|
|
|
SubscriptionRecurring.Monthly
|
|
|
|
);
|
|
|
|
|
2024-12-05 11:31:00 +03:00
|
|
|
t.true(
|
|
|
|
stripe.subscriptionSchedules.release.calledOnceWith(subscriptionSchedule.id)
|
|
|
|
);
|
2024-12-05 11:31:01 +03:00
|
|
|
|
|
|
|
t.is(subInDB.recurring, SubscriptionRecurring.Monthly);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should be able to cancel subscription with schedule', async t => {
|
|
|
|
const { service, u1, stripe } = t.context;
|
|
|
|
|
|
|
|
await service.saveStripeSubscription({
|
|
|
|
...sub,
|
|
|
|
schedule: 'sub_sched_1',
|
|
|
|
});
|
|
|
|
|
|
|
|
stripe.subscriptionSchedules.retrieve.resolves(subscriptionSchedule as any);
|
|
|
|
|
|
|
|
const subInDB = await service.cancelSubscription({
|
|
|
|
userId: u1.id,
|
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
});
|
|
|
|
|
|
|
|
t.true(
|
|
|
|
stripe.subscriptionSchedules.update.calledOnceWith(
|
|
|
|
subscriptionSchedule.id,
|
|
|
|
{
|
|
|
|
phases: [
|
|
|
|
{
|
|
|
|
items: [
|
|
|
|
{
|
|
|
|
price: PRO_MONTHLY,
|
|
|
|
quantity: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
coupon: undefined,
|
|
|
|
start_date: subscriptionSchedule.phases[0].start_date,
|
|
|
|
end_date: subscriptionSchedule.phases[0].end_date,
|
|
|
|
metadata: {
|
|
|
|
next_coupon: null,
|
|
|
|
next_price: PRO_YEARLY,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
end_behavior: 'cancel',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
t.is(subInDB.status, SubscriptionStatus.Active);
|
|
|
|
t.truthy(subInDB.canceledAt);
|
|
|
|
t.falsy(subInDB.nextBillAt);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should be able to resume subscription with schedule', async t => {
|
|
|
|
const { service, u1, stripe } = t.context;
|
|
|
|
|
|
|
|
await service.saveStripeSubscription({
|
|
|
|
...sub,
|
|
|
|
canceled_at: 1714118236,
|
|
|
|
schedule: 'sub_sched_1',
|
|
|
|
});
|
|
|
|
|
|
|
|
stripe.subscriptionSchedules.retrieve.resolves({
|
|
|
|
...subscriptionSchedule,
|
|
|
|
phases: [
|
|
|
|
{
|
|
|
|
items: [
|
|
|
|
// @ts-expect-error mock
|
|
|
|
{
|
|
|
|
price: PRO_MONTHLY,
|
|
|
|
quantity: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
start_date: subscriptionSchedule.phases[0].start_date,
|
|
|
|
end_date: subscriptionSchedule.phases[0].end_date,
|
|
|
|
metadata: {
|
|
|
|
next_price: PRO_YEARLY,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
],
|
|
|
|
end_behavior: 'cancel',
|
|
|
|
});
|
|
|
|
|
|
|
|
const subInDB = await service.resumeSubscription({
|
|
|
|
userId: u1.id,
|
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
});
|
|
|
|
|
|
|
|
t.true(
|
|
|
|
stripe.subscriptionSchedules.update.calledOnceWith(
|
|
|
|
subscriptionSchedule.id,
|
|
|
|
{
|
|
|
|
phases: [
|
|
|
|
{
|
|
|
|
items: [{ price: PRO_MONTHLY, quantity: 1 }],
|
|
|
|
start_date: subscriptionSchedule.phases[0].start_date,
|
|
|
|
end_date: subscriptionSchedule.phases[0].end_date,
|
|
|
|
metadata: {
|
|
|
|
next_price: null,
|
|
|
|
next_coupon: null,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
items: [{ price: PRO_YEARLY, quantity: 1 }],
|
|
|
|
coupon: undefined,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
end_behavior: 'release',
|
|
|
|
}
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
t.is(subInDB.status, SubscriptionStatus.Active);
|
|
|
|
t.falsy(subInDB.canceledAt);
|
|
|
|
t.truthy(subInDB.nextBillAt);
|
2024-04-30 09:59:54 +03:00
|
|
|
});
|
2024-07-08 10:41:26 +03:00
|
|
|
|
|
|
|
// ============== Lifetime Subscription ===============
|
2024-10-10 13:12:40 +03:00
|
|
|
const lifetimeInvoice: Stripe.Invoice = {
|
|
|
|
id: 'in_1',
|
2024-07-08 10:41:26 +03:00
|
|
|
object: 'invoice',
|
|
|
|
amount_paid: 49900,
|
|
|
|
total: 49900,
|
|
|
|
customer: 'cus_1',
|
2024-12-05 11:31:00 +03:00
|
|
|
customer_email: 'u1@affine.pro',
|
2024-07-08 10:41:26 +03:00
|
|
|
currency: 'usd',
|
|
|
|
status: 'paid',
|
|
|
|
lines: {
|
|
|
|
data: [
|
2024-12-05 11:31:00 +03:00
|
|
|
// @ts-expect-error stub
|
2024-07-08 10:41:26 +03:00
|
|
|
{
|
|
|
|
price: PRICES[PRO_LIFETIME],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2024-10-10 13:12:40 +03:00
|
|
|
const onetimeMonthlyInvoice: Stripe.Invoice = {
|
|
|
|
id: 'in_2',
|
|
|
|
object: 'invoice',
|
|
|
|
amount_paid: 799,
|
|
|
|
total: 799,
|
|
|
|
customer: 'cus_1',
|
2024-12-05 11:31:00 +03:00
|
|
|
customer_email: 'u1@affine.pro',
|
2024-10-10 13:12:40 +03:00
|
|
|
currency: 'usd',
|
|
|
|
status: 'paid',
|
|
|
|
lines: {
|
|
|
|
data: [
|
2024-12-05 11:31:00 +03:00
|
|
|
// @ts-expect-error stub
|
2024-10-10 13:12:40 +03:00
|
|
|
{
|
|
|
|
price: PRICES[PRO_MONTHLY_CODE],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const onetimeYearlyInvoice: Stripe.Invoice = {
|
|
|
|
id: 'in_3',
|
|
|
|
object: 'invoice',
|
|
|
|
amount_paid: 8100,
|
|
|
|
total: 8100,
|
|
|
|
customer: 'cus_1',
|
2024-12-05 11:31:00 +03:00
|
|
|
customer_email: 'u1@affine.pro',
|
2024-10-10 13:12:40 +03:00
|
|
|
currency: 'usd',
|
|
|
|
status: 'paid',
|
|
|
|
lines: {
|
|
|
|
data: [
|
2024-12-05 11:31:00 +03:00
|
|
|
// @ts-expect-error stub
|
2024-10-10 13:12:40 +03:00
|
|
|
{
|
|
|
|
price: PRICES[PRO_YEARLY_CODE],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2024-07-08 10:41:30 +03:00
|
|
|
test('should not be able to checkout for lifetime recurring if not enabled', async t => {
|
2024-12-05 11:31:01 +03:00
|
|
|
const { service, u1, runtime } = t.context;
|
|
|
|
runtime.fetch.withArgs('plugins.payment/showLifetimePrice').resolves(false);
|
2024-07-08 10:41:30 +03:00
|
|
|
|
|
|
|
await t.throwsAsync(
|
|
|
|
() =>
|
2024-12-05 11:31:01 +03:00
|
|
|
service.checkout(
|
|
|
|
{
|
2024-12-05 11:31:00 +03:00
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
recurring: SubscriptionRecurring.Lifetime,
|
2024-12-05 11:31:01 +03:00
|
|
|
variant: null,
|
|
|
|
successCallbackLink: '',
|
2024-12-05 11:31:00 +03:00
|
|
|
},
|
2024-12-05 11:31:01 +03:00
|
|
|
{
|
|
|
|
user: u1,
|
|
|
|
}
|
|
|
|
),
|
2024-12-05 11:31:00 +03:00
|
|
|
{ message: 'You are trying to access a unknown subscription plan.' }
|
2024-07-08 10:41:30 +03:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should be able to checkout for lifetime recurring', async t => {
|
2024-12-05 11:31:01 +03:00
|
|
|
const { service, u1, stripe } = t.context;
|
2024-07-08 10:41:30 +03:00
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
await service.checkout(
|
|
|
|
{
|
2024-12-05 11:31:00 +03:00
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
recurring: SubscriptionRecurring.Lifetime,
|
2024-12-05 11:31:01 +03:00
|
|
|
variant: null,
|
|
|
|
successCallbackLink: '',
|
2024-12-05 11:31:00 +03:00
|
|
|
},
|
2024-12-05 11:31:01 +03:00
|
|
|
{
|
|
|
|
user: u1,
|
|
|
|
}
|
|
|
|
);
|
2024-07-08 10:41:30 +03:00
|
|
|
|
2024-12-05 11:31:00 +03:00
|
|
|
t.deepEqual(getLastCheckoutPrice(stripe.checkout.sessions.create), {
|
|
|
|
price: PRO_LIFETIME,
|
|
|
|
coupon: undefined,
|
|
|
|
});
|
2024-07-08 10:41:30 +03:00
|
|
|
});
|
|
|
|
|
2024-10-10 13:12:40 +03:00
|
|
|
test('should not be able to checkout for lifetime recurring if already subscribed', async t => {
|
|
|
|
const { service, u1, db } = t.context;
|
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
await db.subscription.create({
|
2024-10-10 13:12:40 +03:00
|
|
|
data: {
|
2024-12-05 11:31:01 +03:00
|
|
|
targetId: u1.id,
|
2024-10-10 13:12:40 +03:00
|
|
|
stripeSubscriptionId: null,
|
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
recurring: SubscriptionRecurring.Lifetime,
|
|
|
|
status: SubscriptionStatus.Active,
|
|
|
|
start: new Date(),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await t.throwsAsync(
|
|
|
|
() =>
|
2024-12-05 11:31:01 +03:00
|
|
|
service.checkout(
|
|
|
|
{
|
2024-12-05 11:31:00 +03:00
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
recurring: SubscriptionRecurring.Lifetime,
|
2024-12-05 11:31:01 +03:00
|
|
|
variant: null,
|
|
|
|
successCallbackLink: '',
|
2024-12-05 11:31:00 +03:00
|
|
|
},
|
2024-12-05 11:31:01 +03:00
|
|
|
{
|
|
|
|
user: u1,
|
|
|
|
}
|
|
|
|
),
|
2024-10-10 13:12:40 +03:00
|
|
|
{ message: 'You have already subscribed to the pro plan.' }
|
|
|
|
);
|
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
await db.subscription.updateMany({
|
|
|
|
where: { targetId: u1.id },
|
2024-10-10 13:12:40 +03:00
|
|
|
data: {
|
|
|
|
stripeSubscriptionId: null,
|
|
|
|
recurring: SubscriptionRecurring.Monthly,
|
|
|
|
variant: SubscriptionVariant.Onetime,
|
|
|
|
end: new Date(Date.now() + 100000),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await t.throwsAsync(
|
|
|
|
() =>
|
2024-12-05 11:31:01 +03:00
|
|
|
service.checkout(
|
|
|
|
{
|
2024-12-05 11:31:00 +03:00
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
recurring: SubscriptionRecurring.Lifetime,
|
2024-12-05 11:31:01 +03:00
|
|
|
variant: null,
|
|
|
|
successCallbackLink: '',
|
2024-12-05 11:31:00 +03:00
|
|
|
},
|
2024-12-05 11:31:01 +03:00
|
|
|
{
|
|
|
|
user: u1,
|
|
|
|
}
|
|
|
|
),
|
2024-10-10 13:12:40 +03:00
|
|
|
{ message: 'You have already subscribed to the pro plan.' }
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2024-07-08 10:41:26 +03:00
|
|
|
test('should be able to subscribe to lifetime recurring', async t => {
|
|
|
|
// lifetime payment isn't a subscription, so we need to trigger the creation by invoice payment event
|
2024-12-05 11:31:00 +03:00
|
|
|
const { service, db, u1, event } = t.context;
|
2024-07-08 10:41:26 +03:00
|
|
|
|
2024-12-05 11:31:00 +03:00
|
|
|
await service.saveStripeInvoice(lifetimeInvoice);
|
2024-07-08 10:41:26 +03:00
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
const subInDB = await db.subscription.findFirst({
|
|
|
|
where: { targetId: u1.id },
|
2024-07-08 10:41:26 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
t.true(
|
2024-12-05 11:31:00 +03:00
|
|
|
event.emit.calledOnceWith('user.subscription.activated', {
|
2024-07-08 10:41:26 +03:00
|
|
|
userId: u1.id,
|
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
recurring: SubscriptionRecurring.Lifetime,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
t.is(subInDB?.plan, SubscriptionPlan.Pro);
|
|
|
|
t.is(subInDB?.recurring, SubscriptionRecurring.Lifetime);
|
|
|
|
t.is(subInDB?.status, SubscriptionStatus.Active);
|
|
|
|
t.is(subInDB?.stripeSubscriptionId, null);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should be able to subscribe to lifetime recurring with old subscription', async t => {
|
|
|
|
const { service, stripe, db, u1, event } = t.context;
|
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
await db.subscription.create({
|
2024-07-08 10:41:26 +03:00
|
|
|
data: {
|
2024-12-05 11:31:01 +03:00
|
|
|
targetId: u1.id,
|
2024-07-08 10:41:26 +03:00
|
|
|
stripeSubscriptionId: 'sub_1',
|
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
recurring: SubscriptionRecurring.Monthly,
|
|
|
|
status: SubscriptionStatus.Active,
|
|
|
|
start: new Date(),
|
|
|
|
end: new Date(),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-12-05 11:31:00 +03:00
|
|
|
stripe.subscriptions.cancel.resolves(sub as any);
|
|
|
|
await service.saveStripeInvoice(lifetimeInvoice);
|
2024-07-08 10:41:26 +03:00
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
const subInDB = await db.subscription.findFirst({
|
|
|
|
where: { targetId: u1.id },
|
2024-07-08 10:41:26 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
t.true(
|
2024-12-05 11:31:00 +03:00
|
|
|
event.emit.calledOnceWith('user.subscription.activated', {
|
2024-07-08 10:41:26 +03:00
|
|
|
userId: u1.id,
|
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
recurring: SubscriptionRecurring.Lifetime,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
t.is(subInDB?.plan, SubscriptionPlan.Pro);
|
|
|
|
t.is(subInDB?.recurring, SubscriptionRecurring.Lifetime);
|
|
|
|
t.is(subInDB?.status, SubscriptionStatus.Active);
|
|
|
|
t.is(subInDB?.stripeSubscriptionId, null);
|
|
|
|
});
|
|
|
|
|
2024-12-05 11:31:00 +03:00
|
|
|
test('should not be able to cancel lifetime subscription', async t => {
|
2024-07-08 10:41:26 +03:00
|
|
|
const { service, db, u1 } = t.context;
|
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
await db.subscription.create({
|
2024-07-08 10:41:26 +03:00
|
|
|
data: {
|
2024-12-05 11:31:01 +03:00
|
|
|
targetId: u1.id,
|
2024-07-08 10:41:26 +03:00
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
recurring: SubscriptionRecurring.Lifetime,
|
|
|
|
status: SubscriptionStatus.Active,
|
|
|
|
start: new Date(),
|
|
|
|
end: new Date(),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await t.throwsAsync(
|
2024-12-05 11:31:01 +03:00
|
|
|
() =>
|
|
|
|
service.cancelSubscription({
|
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
userId: u1.id,
|
|
|
|
}),
|
2024-10-10 13:12:40 +03:00
|
|
|
{ message: 'Onetime payment subscription cannot be canceled.' }
|
2024-07-08 10:41:26 +03:00
|
|
|
);
|
2024-12-05 11:31:00 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should not be able to update lifetime recurring', async t => {
|
|
|
|
const { service, db, u1 } = t.context;
|
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
await db.subscription.create({
|
2024-12-05 11:31:00 +03:00
|
|
|
data: {
|
2024-12-05 11:31:01 +03:00
|
|
|
targetId: u1.id,
|
2024-12-05 11:31:00 +03:00
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
recurring: SubscriptionRecurring.Lifetime,
|
|
|
|
status: SubscriptionStatus.Active,
|
|
|
|
start: new Date(),
|
|
|
|
end: new Date(),
|
|
|
|
},
|
|
|
|
});
|
2024-07-08 10:41:26 +03:00
|
|
|
|
|
|
|
await t.throwsAsync(
|
|
|
|
() =>
|
|
|
|
service.updateSubscriptionRecurring(
|
2024-12-05 11:31:01 +03:00
|
|
|
{
|
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
userId: u1.id,
|
|
|
|
},
|
2024-07-08 10:41:26 +03:00
|
|
|
SubscriptionRecurring.Monthly
|
|
|
|
),
|
2024-10-10 13:12:40 +03:00
|
|
|
{ message: 'You cannot update an onetime payment subscription.' }
|
2024-07-08 10:41:26 +03:00
|
|
|
);
|
2024-10-10 13:12:40 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// ============== Onetime Subscription ===============
|
|
|
|
test('should be able to checkout for onetime payment', async t => {
|
|
|
|
const { service, u1, stripe } = t.context;
|
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
await service.checkout(
|
|
|
|
{
|
2024-12-05 11:31:00 +03:00
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
recurring: SubscriptionRecurring.Monthly,
|
|
|
|
variant: SubscriptionVariant.Onetime,
|
2024-12-05 11:31:01 +03:00
|
|
|
successCallbackLink: '',
|
2024-12-05 11:31:00 +03:00
|
|
|
},
|
2024-12-05 11:31:01 +03:00
|
|
|
{
|
|
|
|
user: u1,
|
|
|
|
}
|
|
|
|
);
|
2024-10-10 13:12:40 +03:00
|
|
|
|
2024-12-05 11:31:00 +03:00
|
|
|
t.true(stripe.checkout.sessions.create.calledOnce);
|
|
|
|
const arg = stripe.checkout.sessions.create.firstCall
|
2024-10-10 13:12:40 +03:00
|
|
|
.args[0] as Stripe.Checkout.SessionCreateParams;
|
|
|
|
t.is(arg.mode, 'payment');
|
2024-12-05 11:31:00 +03:00
|
|
|
t.deepEqual(getLastCheckoutPrice(stripe.checkout.sessions.create), {
|
|
|
|
price: PRO_MONTHLY_CODE,
|
|
|
|
coupon: undefined,
|
|
|
|
});
|
2024-10-10 13:12:40 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should be able to checkout onetime payment if previous subscription is onetime', async t => {
|
|
|
|
const { service, u1, stripe, db } = t.context;
|
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
await db.subscription.create({
|
2024-10-10 13:12:40 +03:00
|
|
|
data: {
|
2024-12-05 11:31:01 +03:00
|
|
|
targetId: u1.id,
|
2024-10-10 13:12:40 +03:00
|
|
|
stripeSubscriptionId: 'sub_1',
|
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
recurring: SubscriptionRecurring.Monthly,
|
|
|
|
variant: SubscriptionVariant.Onetime,
|
|
|
|
status: SubscriptionStatus.Active,
|
|
|
|
start: new Date(),
|
|
|
|
end: new Date(),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
await service.checkout(
|
|
|
|
{
|
2024-12-05 11:31:00 +03:00
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
recurring: SubscriptionRecurring.Monthly,
|
|
|
|
variant: SubscriptionVariant.Onetime,
|
2024-12-05 11:31:01 +03:00
|
|
|
successCallbackLink: '',
|
2024-12-05 11:31:00 +03:00
|
|
|
},
|
2024-12-05 11:31:01 +03:00
|
|
|
{
|
|
|
|
user: u1,
|
|
|
|
}
|
|
|
|
);
|
2024-10-10 13:12:40 +03:00
|
|
|
|
2024-12-05 11:31:00 +03:00
|
|
|
t.true(stripe.checkout.sessions.create.calledOnce);
|
|
|
|
const arg = stripe.checkout.sessions.create.firstCall
|
2024-10-10 13:12:40 +03:00
|
|
|
.args[0] as Stripe.Checkout.SessionCreateParams;
|
|
|
|
t.is(arg.mode, 'payment');
|
2024-12-05 11:31:00 +03:00
|
|
|
t.deepEqual(getLastCheckoutPrice(stripe.checkout.sessions.create), {
|
|
|
|
price: PRO_MONTHLY_CODE,
|
|
|
|
coupon: undefined,
|
|
|
|
});
|
2024-10-10 13:12:40 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
test('should not be able to checkout out onetime payment if previous subscription is not onetime', async t => {
|
|
|
|
const { service, u1, db } = t.context;
|
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
await db.subscription.create({
|
2024-10-10 13:12:40 +03:00
|
|
|
data: {
|
2024-12-05 11:31:01 +03:00
|
|
|
targetId: u1.id,
|
2024-10-10 13:12:40 +03:00
|
|
|
stripeSubscriptionId: 'sub_1',
|
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
recurring: SubscriptionRecurring.Monthly,
|
|
|
|
status: SubscriptionStatus.Active,
|
|
|
|
start: new Date(),
|
|
|
|
end: new Date(),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await t.throwsAsync(
|
|
|
|
() =>
|
2024-12-05 11:31:01 +03:00
|
|
|
service.checkout(
|
|
|
|
{
|
2024-12-05 11:31:00 +03:00
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
recurring: SubscriptionRecurring.Monthly,
|
|
|
|
variant: SubscriptionVariant.Onetime,
|
2024-12-05 11:31:01 +03:00
|
|
|
successCallbackLink: '',
|
2024-12-05 11:31:00 +03:00
|
|
|
},
|
2024-12-05 11:31:01 +03:00
|
|
|
{
|
|
|
|
user: u1,
|
|
|
|
}
|
|
|
|
),
|
2024-10-10 13:12:40 +03:00
|
|
|
{ message: 'You have already subscribed to the pro plan.' }
|
|
|
|
);
|
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
await db.subscription.updateMany({
|
|
|
|
where: { targetId: u1.id },
|
2024-10-10 13:12:40 +03:00
|
|
|
data: {
|
|
|
|
stripeSubscriptionId: null,
|
|
|
|
recurring: SubscriptionRecurring.Lifetime,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await t.throwsAsync(
|
|
|
|
() =>
|
2024-12-05 11:31:01 +03:00
|
|
|
service.checkout(
|
|
|
|
{
|
2024-12-05 11:31:00 +03:00
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
recurring: SubscriptionRecurring.Monthly,
|
|
|
|
variant: SubscriptionVariant.Onetime,
|
2024-12-05 11:31:01 +03:00
|
|
|
successCallbackLink: '',
|
2024-12-05 11:31:00 +03:00
|
|
|
},
|
2024-12-05 11:31:01 +03:00
|
|
|
{
|
|
|
|
user: u1,
|
|
|
|
}
|
|
|
|
),
|
2024-10-10 13:12:40 +03:00
|
|
|
{ message: 'You have already subscribed to the pro plan.' }
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should be able to subscribe onetime payment subscription', async t => {
|
2024-12-05 11:31:00 +03:00
|
|
|
const { service, db, u1, event } = t.context;
|
2024-10-10 13:12:40 +03:00
|
|
|
|
2024-12-05 11:31:00 +03:00
|
|
|
await service.saveStripeInvoice(onetimeMonthlyInvoice);
|
2024-10-10 13:12:40 +03:00
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
const subInDB = await db.subscription.findFirst({
|
|
|
|
where: { targetId: u1.id },
|
2024-10-10 13:12:40 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
t.true(
|
2024-12-05 11:31:00 +03:00
|
|
|
event.emit.calledOnceWith('user.subscription.activated', {
|
2024-10-10 13:12:40 +03:00
|
|
|
userId: u1.id,
|
|
|
|
plan: SubscriptionPlan.Pro,
|
|
|
|
recurring: SubscriptionRecurring.Monthly,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
t.is(subInDB?.plan, SubscriptionPlan.Pro);
|
|
|
|
t.is(subInDB?.recurring, SubscriptionRecurring.Monthly);
|
|
|
|
t.is(subInDB?.status, SubscriptionStatus.Active);
|
|
|
|
t.is(subInDB?.stripeSubscriptionId, null);
|
|
|
|
t.is(
|
|
|
|
subInDB?.end?.toDateString(),
|
|
|
|
new Date(Date.now() + 30 * 24 * 60 * 60 * 1000).toDateString()
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should be able to recalculate onetime payment subscription period', async t => {
|
2024-12-05 11:31:00 +03:00
|
|
|
const { service, db, u1 } = t.context;
|
2024-10-10 13:12:40 +03:00
|
|
|
|
2024-12-05 11:31:00 +03:00
|
|
|
await service.saveStripeInvoice(onetimeMonthlyInvoice);
|
2024-10-10 13:12:40 +03:00
|
|
|
|
2024-12-05 11:31:01 +03:00
|
|
|
let subInDB = await db.subscription.findFirst({
|
|
|
|
where: { targetId: u1.id },
|
2024-10-10 13:12:40 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
t.truthy(subInDB);
|
|
|
|
|
|
|
|
let end = subInDB!.end!;
|
2024-12-05 11:31:00 +03:00
|
|
|
await service.saveStripeInvoice(onetimeMonthlyInvoice);
|
2024-12-05 11:31:01 +03:00
|
|
|
subInDB = await db.subscription.findFirst({
|
|
|
|
where: { targetId: u1.id },
|
2024-10-10 13:12:40 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// add 30 days
|
|
|
|
t.is(subInDB!.end!.getTime(), end.getTime() + 30 * 24 * 60 * 60 * 1000);
|
|
|
|
|
|
|
|
end = subInDB!.end!;
|
2024-12-05 11:31:00 +03:00
|
|
|
await service.saveStripeInvoice(onetimeYearlyInvoice);
|
2024-12-05 11:31:01 +03:00
|
|
|
subInDB = await db.subscription.findFirst({
|
|
|
|
where: { targetId: u1.id },
|
2024-10-10 13:12:40 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// add 365 days
|
|
|
|
t.is(subInDB!.end!.getTime(), end.getTime() + 365 * 24 * 60 * 60 * 1000);
|
|
|
|
|
|
|
|
// make subscription expired
|
2024-12-05 11:31:01 +03:00
|
|
|
await db.subscription.update({
|
2024-10-10 13:12:40 +03:00
|
|
|
where: { id: subInDB!.id },
|
|
|
|
data: {
|
|
|
|
end: new Date(Date.now() - 1000),
|
|
|
|
},
|
|
|
|
});
|
2024-12-05 11:31:00 +03:00
|
|
|
await service.saveStripeInvoice(onetimeYearlyInvoice);
|
2024-12-05 11:31:01 +03:00
|
|
|
subInDB = await db.subscription.findFirst({
|
|
|
|
where: { targetId: u1.id },
|
2024-10-10 13:12:40 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
// add 365 days from now
|
|
|
|
t.is(
|
|
|
|
subInDB?.end?.toDateString(),
|
|
|
|
new Date(Date.now() + 365 * 24 * 60 * 60 * 1000).toDateString()
|
2024-07-08 10:41:26 +03:00
|
|
|
);
|
|
|
|
});
|
2024-12-05 11:31:01 +03:00
|
|
|
|
|
|
|
// TEAM
|
|
|
|
test('should be able to list prices for team', async t => {
|
|
|
|
const { service } = t.context;
|
|
|
|
|
|
|
|
const prices = await service.listPrices(undefined);
|
|
|
|
|
|
|
|
t.snapshot(prices.map(p => encodeLookupKey(p.lookupKey)));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should be able to checkout for team', async t => {
|
|
|
|
const { service, u1, stripe } = t.context;
|
|
|
|
|
|
|
|
await service.checkout(
|
|
|
|
{
|
|
|
|
plan: SubscriptionPlan.Team,
|
|
|
|
recurring: SubscriptionRecurring.Monthly,
|
|
|
|
variant: null,
|
|
|
|
successCallbackLink: '',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
user: u1,
|
|
|
|
workspaceId: 'ws_1',
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
t.deepEqual(getLastCheckoutPrice(stripe.checkout.sessions.create), {
|
|
|
|
price: TEAM_MONTHLY,
|
|
|
|
coupon: undefined,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not be able to checkout for workspace if subscribed', async t => {
|
|
|
|
const { service, u1, db } = t.context;
|
|
|
|
|
|
|
|
await db.subscription.create({
|
|
|
|
data: {
|
|
|
|
targetId: 'ws_1',
|
|
|
|
stripeSubscriptionId: 'sub_1',
|
|
|
|
plan: SubscriptionPlan.Team,
|
|
|
|
recurring: SubscriptionRecurring.Monthly,
|
|
|
|
status: SubscriptionStatus.Active,
|
|
|
|
start: new Date(),
|
|
|
|
end: new Date(),
|
|
|
|
quantity: 1,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
await t.throwsAsync(
|
|
|
|
() =>
|
|
|
|
service.checkout(
|
|
|
|
{
|
|
|
|
plan: SubscriptionPlan.Team,
|
|
|
|
recurring: SubscriptionRecurring.Monthly,
|
|
|
|
variant: null,
|
|
|
|
successCallbackLink: '',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
user: u1,
|
|
|
|
workspaceId: 'ws_1',
|
|
|
|
}
|
|
|
|
),
|
|
|
|
{ message: 'You have already subscribed to the team plan.' }
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
const teamSub: Stripe.Subscription = {
|
|
|
|
...sub,
|
|
|
|
items: {
|
|
|
|
object: 'list',
|
|
|
|
data: [
|
|
|
|
{
|
|
|
|
id: 'si_1',
|
|
|
|
// @ts-expect-error stub
|
|
|
|
price: {
|
|
|
|
id: TEAM_MONTHLY,
|
|
|
|
lookup_key: 'team_monthly',
|
|
|
|
},
|
|
|
|
subscription: 'sub_1',
|
|
|
|
quantity: 1,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
metadata: {
|
|
|
|
workspaceId: 'ws_1',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
test('should be able to create team subscription', async t => {
|
|
|
|
const { event, service, db } = t.context;
|
|
|
|
|
|
|
|
await service.saveStripeSubscription(teamSub);
|
|
|
|
|
|
|
|
const subInDB = await db.subscription.findFirst({
|
|
|
|
where: { targetId: 'ws_1' },
|
|
|
|
});
|
|
|
|
|
|
|
|
t.true(
|
|
|
|
event.emit.calledOnceWith('workspace.subscription.activated', {
|
|
|
|
workspaceId: 'ws_1',
|
|
|
|
plan: SubscriptionPlan.Team,
|
|
|
|
recurring: SubscriptionRecurring.Monthly,
|
|
|
|
quantity: 1,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
t.is(subInDB?.stripeSubscriptionId, sub.id);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should be able to update team subscription', async t => {
|
|
|
|
const { service, db, event } = t.context;
|
|
|
|
|
|
|
|
await service.saveStripeSubscription(teamSub);
|
|
|
|
|
|
|
|
await service.saveStripeSubscription({
|
|
|
|
...teamSub,
|
|
|
|
items: {
|
|
|
|
...teamSub.items,
|
|
|
|
data: [
|
|
|
|
{
|
|
|
|
...teamSub.items.data[0],
|
|
|
|
quantity: 2,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const subInDB = await db.subscription.findFirst({
|
|
|
|
where: { targetId: 'ws_1' },
|
|
|
|
});
|
|
|
|
|
|
|
|
t.is(subInDB?.quantity, 2);
|
|
|
|
|
|
|
|
t.true(
|
|
|
|
event.emit.calledWith('workspace.subscription.activated', {
|
|
|
|
workspaceId: 'ws_1',
|
|
|
|
plan: SubscriptionPlan.Team,
|
|
|
|
recurring: SubscriptionRecurring.Monthly,
|
|
|
|
quantity: 2,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
// NOTE(@forehalo): cancel and resume a team subscription share the same logic with user subscription
|
|
|
|
test.skip('should be able to cancel team subscription', async () => {});
|
|
|
|
test.skip('should be able to resume team subscription', async () => {});
|