Не подтверждена Коммит f566457d создал по автору forehalo's avatar forehalo
Просмотр файлов

test(server): payment tests (#6737)

владелец 1f974373
...@@ -197,7 +197,7 @@ export class ScheduleManager { ...@@ -197,7 +197,7 @@ export class ScheduleManager {
throw new Error('Unexpected subscription schedule status'); throw new Error('Unexpected subscription schedule status');
} }
// if current phase's plan matches target, and no coupon change, just release the schedule // if current phase's plan matches target, just release the schedule
if (this.currentPhase.items[0].price === price) { if (this.currentPhase.items[0].price === price) {
await this.stripe.subscriptionSchedules.release(this._schedule.id, { await this.stripe.subscriptionSchedules.release(this._schedule.id, {
idempotencyKey, idempotencyKey,
...@@ -221,13 +221,8 @@ export class ScheduleManager { ...@@ -221,13 +221,8 @@ export class ScheduleManager {
items: [ items: [
{ {
price: price, price: price,
quantity: 1,
}, },
], ],
coupon:
typeof this.currentPhase.coupon === 'string'
? this.currentPhase.coupon
: this.currentPhase.coupon?.id ?? undefined,
}, },
], ],
}, },
......
...@@ -164,7 +164,7 @@ export class SubscriptionService { ...@@ -164,7 +164,7 @@ export class SubscriptionService {
if (currentSubscription) { if (currentSubscription) {
throw new BadRequestException( throw new BadRequestException(
`You've already subscripted to the ${plan} plan` `You've already subscribed to the ${plan} plan`
); );
} }
...@@ -181,7 +181,9 @@ export class SubscriptionService { ...@@ -181,7 +181,9 @@ export class SubscriptionService {
let discounts: Stripe.Checkout.SessionCreateParams['discounts'] = []; let discounts: Stripe.Checkout.SessionCreateParams['discounts'] = [];
if (promotionCode) { if (coupon) {
discounts = [{ coupon }];
} else if (promotionCode) {
const code = await this.getAvailablePromotionCode( const code = await this.getAvailablePromotionCode(
promotionCode, promotionCode,
customer.stripeCustomerId customer.stripeCustomerId
...@@ -189,8 +191,6 @@ export class SubscriptionService { ...@@ -189,8 +191,6 @@ export class SubscriptionService {
if (code) { if (code) {
discounts = [{ promotion_code: code }]; discounts = [{ promotion_code: code }];
} }
} else if (coupon) {
discounts = [{ coupon }];
} }
return await this.stripe.checkout.sessions.create( return await this.stripe.checkout.sessions.create(
...@@ -241,7 +241,7 @@ export class SubscriptionService { ...@@ -241,7 +241,7 @@ export class SubscriptionService {
const subscriptionInDB = user?.subscriptions.find(s => s.plan === plan); const subscriptionInDB = user?.subscriptions.find(s => s.plan === plan);
if (!subscriptionInDB) { if (!subscriptionInDB) {
throw new BadRequestException(`You didn't subscript to the ${plan} plan`); throw new BadRequestException(`You didn't subscribe to the ${plan} plan`);
} }
if (subscriptionInDB.canceledAt) { if (subscriptionInDB.canceledAt) {
...@@ -260,8 +260,7 @@ export class SubscriptionService { ...@@ -260,8 +260,7 @@ export class SubscriptionService {
user, user,
await this.stripe.subscriptions.retrieve( await this.stripe.subscriptions.retrieve(
subscriptionInDB.stripeSubscriptionId subscriptionInDB.stripeSubscriptionId
), )
false
); );
} else { } else {
// let customer contact support if they want to cancel immediately // let customer contact support if they want to cancel immediately
...@@ -295,7 +294,7 @@ export class SubscriptionService { ...@@ -295,7 +294,7 @@ export class SubscriptionService {
const subscriptionInDB = user?.subscriptions.find(s => s.plan === plan); const subscriptionInDB = user?.subscriptions.find(s => s.plan === plan);
if (!subscriptionInDB) { if (!subscriptionInDB) {
throw new BadRequestException(`You didn't subscript to the ${plan} plan`); throw new BadRequestException(`You didn't subscribe to the ${plan} plan`);
} }
if (!subscriptionInDB.canceledAt) { if (!subscriptionInDB.canceledAt) {
...@@ -317,8 +316,7 @@ export class SubscriptionService { ...@@ -317,8 +316,7 @@ export class SubscriptionService {
user, user,
await this.stripe.subscriptions.retrieve( await this.stripe.subscriptions.retrieve(
subscriptionInDB.stripeSubscriptionId subscriptionInDB.stripeSubscriptionId
), )
false
); );
} else { } else {
const subscription = await this.stripe.subscriptions.update( const subscription = await this.stripe.subscriptions.update(
...@@ -351,12 +349,12 @@ export class SubscriptionService { ...@@ -351,12 +349,12 @@ export class SubscriptionService {
} }
const subscriptionInDB = user?.subscriptions.find(s => s.plan === plan); const subscriptionInDB = user?.subscriptions.find(s => s.plan === plan);
if (!subscriptionInDB) { if (!subscriptionInDB) {
throw new BadRequestException(`You didn't subscript to the ${plan} plan`); throw new BadRequestException(`You didn't subscribe to the ${plan} plan`);
} }
if (subscriptionInDB.canceledAt) { if (subscriptionInDB.canceledAt) {
throw new BadRequestException( throw new BadRequestException(
'Your subscription has already been canceled ' 'Your subscription has already been canceled'
); );
} }
...@@ -415,7 +413,6 @@ export class SubscriptionService { ...@@ -415,7 +413,6 @@ export class SubscriptionService {
@OnEvent('customer.subscription.created') @OnEvent('customer.subscription.created')
@OnEvent('customer.subscription.updated') @OnEvent('customer.subscription.updated')
async onSubscriptionChanges(subscription: Stripe.Subscription) { async onSubscriptionChanges(subscription: Stripe.Subscription) {
// webhook call may not in sequential order, get the latest status
subscription = await this.stripe.subscriptions.retrieve(subscription.id); subscription = await this.stripe.subscriptions.retrieve(subscription.id);
if (subscription.status === 'active') { if (subscription.status === 'active') {
const user = await this.retrieveUserFromCustomer( const user = await this.retrieveUserFromCustomer(
...@@ -432,7 +429,6 @@ export class SubscriptionService { ...@@ -432,7 +429,6 @@ export class SubscriptionService {
@OnEvent('customer.subscription.deleted') @OnEvent('customer.subscription.deleted')
async onSubscriptionDeleted(subscription: Stripe.Subscription) { async onSubscriptionDeleted(subscription: Stripe.Subscription) {
subscription = await this.stripe.subscriptions.retrieve(subscription.id);
const user = await this.retrieveUserFromCustomer( const user = await this.retrieveUserFromCustomer(
typeof subscription.customer === 'string' typeof subscription.customer === 'string'
? subscription.customer ? subscription.customer
...@@ -553,16 +549,8 @@ export class SubscriptionService { ...@@ -553,16 +549,8 @@ export class SubscriptionService {
private async saveSubscription( private async saveSubscription(
user: User, user: User,
subscription: Stripe.Subscription, subscription: Stripe.Subscription
fromWebhook = true
): Promise<UserSubscription> { ): Promise<UserSubscription> {
// webhook events may not in sequential order
// always fetch the latest subscription and save
// see https://stripe.com/docs/webhooks#behaviors
if (fromWebhook) {
subscription = await this.stripe.subscriptions.retrieve(subscription.id);
}
const price = subscription.items.data[0].price; const price = subscription.items.data[0].price;
if (!price.lookup_key) { if (!price.lookup_key) {
throw new Error('Unexpected subscription with no key'); throw new Error('Unexpected subscription with no key');
...@@ -768,13 +756,12 @@ export class SubscriptionService { ...@@ -768,13 +756,12 @@ export class SubscriptionService {
}); });
if (plan === SubscriptionPlan.Pro) { if (plan === SubscriptionPlan.Pro) {
const canHaveEADiscount = isEaUser && !subscribed; const canHaveEADiscount =
isEaUser && !subscribed && recurring === SubscriptionRecurring.Yearly;
const price = await this.getPrice( const price = await this.getPrice(
plan, plan,
recurring, recurring,
canHaveEADiscount && recurring === SubscriptionRecurring.Yearly canHaveEADiscount ? SubscriptionPriceVariant.EA : undefined
? SubscriptionPriceVariant.EA
: undefined
); );
return { return {
price, price,
...@@ -788,13 +775,12 @@ export class SubscriptionService { ...@@ -788,13 +775,12 @@ export class SubscriptionService {
EarlyAccessType.AI EarlyAccessType.AI
); );
const canHaveEADiscount = isAIEaUser && !subscribed; const canHaveEADiscount =
isAIEaUser && !subscribed && recurring === SubscriptionRecurring.Yearly;
const price = await this.getPrice( const price = await this.getPrice(
plan, plan,
recurring, recurring,
canHaveEADiscount && recurring === SubscriptionRecurring.Yearly canHaveEADiscount ? SubscriptionPriceVariant.EA : undefined
? SubscriptionPriceVariant.EA
: undefined
); );
return { return {
......
Это отличие свёрнуто
Поддерживает Markdown
0% или .
You are about to add 0 people to the discussion. Proceed with caution.
Сначала завершите редактирование этого сообщения!
Пожалуйста, зарегистрируйтесь или чтобы прокомментировать