Skip to content

Commit

Permalink
test(server): payment tests
Browse files Browse the repository at this point in the history
  • Loading branch information
forehalo committed Apr 29, 2024
1 parent 964e475 commit ce739c5
Show file tree
Hide file tree
Showing 2 changed files with 781 additions and 31 deletions.
48 changes: 17 additions & 31 deletions packages/backend/server/src/plugins/payment/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ export class SubscriptionService {

if (currentSubscription) {
throw new BadRequestException(
`You've already subscripted to the ${plan} plan`
`You've already subscribed to the ${plan} plan`
);
}

Expand All @@ -181,16 +181,16 @@ export class SubscriptionService {

let discounts: Stripe.Checkout.SessionCreateParams['discounts'] = [];

if (promotionCode) {
if (coupon) {
discounts = [{ coupon }];
} else if (promotionCode) {
const code = await this.getAvailablePromotionCode(
promotionCode,
customer.stripeCustomerId
);
if (code) {
discounts = [{ promotion_code: code }];
}
} else if (coupon) {
discounts = [{ coupon }];
}

return await this.stripe.checkout.sessions.create(
Expand Down Expand Up @@ -241,7 +241,7 @@ export class SubscriptionService {

const subscriptionInDB = user?.subscriptions.find(s => s.plan === plan);
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) {
Expand All @@ -260,8 +260,7 @@ export class SubscriptionService {
user,
await this.stripe.subscriptions.retrieve(
subscriptionInDB.stripeSubscriptionId
),
false
)
);
} else {
// let customer contact support if they want to cancel immediately
Expand Down Expand Up @@ -295,7 +294,7 @@ export class SubscriptionService {

const subscriptionInDB = user?.subscriptions.find(s => s.plan === plan);
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) {
Expand All @@ -317,8 +316,7 @@ export class SubscriptionService {
user,
await this.stripe.subscriptions.retrieve(
subscriptionInDB.stripeSubscriptionId
),
false
)
);
} else {
const subscription = await this.stripe.subscriptions.update(
Expand Down Expand Up @@ -351,12 +349,12 @@ export class SubscriptionService {
}
const subscriptionInDB = user?.subscriptions.find(s => s.plan === plan);
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) {
throw new BadRequestException(
'Your subscription has already been canceled '
'Your subscription has already been canceled'
);
}

Expand Down Expand Up @@ -415,7 +413,6 @@ export class SubscriptionService {
@OnEvent('customer.subscription.created')
@OnEvent('customer.subscription.updated')
async onSubscriptionChanges(subscription: Stripe.Subscription) {
// webhook call may not in sequential order, get the latest status
subscription = await this.stripe.subscriptions.retrieve(subscription.id);
if (subscription.status === 'active') {
const user = await this.retrieveUserFromCustomer(
Expand All @@ -432,7 +429,6 @@ export class SubscriptionService {

@OnEvent('customer.subscription.deleted')
async onSubscriptionDeleted(subscription: Stripe.Subscription) {
subscription = await this.stripe.subscriptions.retrieve(subscription.id);
const user = await this.retrieveUserFromCustomer(
typeof subscription.customer === 'string'
? subscription.customer
Expand Down Expand Up @@ -553,16 +549,8 @@ export class SubscriptionService {

private async saveSubscription(
user: User,
subscription: Stripe.Subscription,
fromWebhook = true
subscription: Stripe.Subscription
): 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;
if (!price.lookup_key) {
throw new Error('Unexpected subscription with no key');
Expand Down Expand Up @@ -768,13 +756,12 @@ export class SubscriptionService {
});

if (plan === SubscriptionPlan.Pro) {
const canHaveEADiscount = isEaUser && !subscribed;
const canHaveEADiscount =
isEaUser && !subscribed && recurring === SubscriptionRecurring.Yearly;
const price = await this.getPrice(
plan,
recurring,
canHaveEADiscount && recurring === SubscriptionRecurring.Yearly
? SubscriptionPriceVariant.EA
: undefined
canHaveEADiscount ? SubscriptionPriceVariant.EA : undefined
);
return {
price,
Expand All @@ -788,13 +775,12 @@ export class SubscriptionService {
EarlyAccessType.AI
);

const canHaveEADiscount = isAIEaUser && !subscribed;
const canHaveEADiscount =
isAIEaUser && !subscribed && recurring === SubscriptionRecurring.Yearly;
const price = await this.getPrice(
plan,
recurring,
canHaveEADiscount && recurring === SubscriptionRecurring.Yearly
? SubscriptionPriceVariant.EA
: undefined
canHaveEADiscount ? SubscriptionPriceVariant.EA : undefined
);

return {
Expand Down
Loading

0 comments on commit ce739c5

Please sign in to comment.