|
|
@ -53,32 +53,13 @@ export class SubscriptionService { |
|
|
priceId: string; |
|
|
priceId: string; |
|
|
user: UserWithSettings; |
|
|
user: UserWithSettings; |
|
|
}): Promise<CreateStripeCheckoutSessionResponse> { |
|
|
}): Promise<CreateStripeCheckoutSessionResponse> { |
|
|
if (!priceId) { |
|
|
const subscriptionOffer = user.subscription?.offer; |
|
|
throw new BadRequestException('Invalid priceId'); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const subscriptionOffers: { |
|
|
|
|
|
[offer in SubscriptionOfferKey]: SubscriptionOffer; |
|
|
|
|
|
} = |
|
|
|
|
|
(await this.propertyService.getByKey<any>(PROPERTY_STRIPE_CONFIG)) ?? {}; |
|
|
|
|
|
|
|
|
|
|
|
const subscriptionOffersForPrice = Object.values(subscriptionOffers).filter( |
|
|
if (!priceId || subscriptionOffer?.priceId !== priceId) { |
|
|
(subscriptionOffer) => { |
|
|
|
|
|
return subscriptionOffer.priceId === priceId; |
|
|
|
|
|
} |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
if (subscriptionOffersForPrice.length === 0) { |
|
|
|
|
|
throw new BadRequestException('Invalid priceId'); |
|
|
throw new BadRequestException('Invalid priceId'); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const subscriptionOffer = subscriptionOffersForPrice.find( |
|
|
if (subscriptionOffer.couponId !== couponId) { |
|
|
(subscriptionOffer) => { |
|
|
|
|
|
return subscriptionOffer.couponId === couponId; |
|
|
|
|
|
} |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
if (!subscriptionOffer) { |
|
|
|
|
|
throw new BadRequestException('Invalid couponId'); |
|
|
throw new BadRequestException('Invalid couponId'); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@ -91,7 +72,7 @@ export class SubscriptionService { |
|
|
client_reference_id: user.id, |
|
|
client_reference_id: user.id, |
|
|
line_items: [ |
|
|
line_items: [ |
|
|
{ |
|
|
{ |
|
|
price: priceId, |
|
|
price: subscriptionOffer.priceId, |
|
|
quantity: 1 |
|
|
quantity: 1 |
|
|
} |
|
|
} |
|
|
], |
|
|
], |
|
|
@ -103,10 +84,10 @@ export class SubscriptionService { |
|
|
)}/api/v1/subscription/stripe/callback?checkoutSessionId={CHECKOUT_SESSION_ID}` |
|
|
)}/api/v1/subscription/stripe/callback?checkoutSessionId={CHECKOUT_SESSION_ID}` |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
if (couponId) { |
|
|
if (subscriptionOffer.couponId) { |
|
|
stripeCheckoutSessionCreateParams.discounts = [ |
|
|
stripeCheckoutSessionCreateParams.discounts = [ |
|
|
{ |
|
|
{ |
|
|
coupon: couponId |
|
|
coupon: subscriptionOffer.couponId |
|
|
} |
|
|
} |
|
|
]; |
|
|
]; |
|
|
} |
|
|
} |
|
|
|