From 576cab37f246908cb2594a2aff8f6f6175982cbe Mon Sep 17 00:00:00 2001 From: Vins Date: Wed, 24 Apr 2024 17:34:23 +0200 Subject: [PATCH] payment frequency --- package.json | 2 +- src/app/api/admin/StripeController.ts | 9 +++++++- .../common/StripeService/StripeService.ts | 22 +++++++++++++++++-- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 5afa8195..fa053511 100644 --- a/package.json +++ b/package.json @@ -59,7 +59,7 @@ "file-type-checker": "^1.0.8", "fp-ts": "^2.16.1", "jsonwebtoken": "^9.0.0", - "le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.132", + "le-coffre-resources": "git@github.com:smart-chain-fr/leCoffre-resources.git#v2.134", "module-alias": "^2.2.2", "monocle-ts": "^2.3.13", "multer": "^1.4.5-lts.1", diff --git a/src/app/api/admin/StripeController.ts b/src/app/api/admin/StripeController.ts index 9f3338bb..b5e5d5a9 100644 --- a/src/app/api/admin/StripeController.ts +++ b/src/app/api/admin/StripeController.ts @@ -7,6 +7,7 @@ import StripeService from "@Services/common/StripeService/StripeService"; import { validateOrReject } from "class-validator"; import { Request, Response } from "express"; import { Subscription } from "le-coffre-resources/dist/Admin"; +import { EPaymentFrequency } from "le-coffre-resources/dist/Admin/Subscription"; import { Service } from "typedi"; @Controller() @@ -27,12 +28,18 @@ export default class StripeController extends ApiController { //add office id to request body req.body.office = { uid: officeId }; + const frequency : EPaymentFrequency = req.body.frequency; + if(!frequency || !Object.values(EPaymentFrequency).includes(frequency)) { + this.httpBadRequest(response, "Invalid frequency"); + return; + } + //init Subscription resource with request body values const subscriptionEntity = Subscription.hydrate(req.body, { strategy: "excludeAll" }); await validateOrReject(subscriptionEntity, { groups: ["createSubscription"], forbidUnknownValues: false }); - const stripeSession = await this.stripeService.createCheckoutSession(subscriptionEntity); + const stripeSession = await this.stripeService.createCheckoutSession(subscriptionEntity, frequency); this.httpCreated(response, stripeSession); diff --git a/src/services/common/StripeService/StripeService.ts b/src/services/common/StripeService/StripeService.ts index 2bf9be89..eabfe670 100644 --- a/src/services/common/StripeService/StripeService.ts +++ b/src/services/common/StripeService/StripeService.ts @@ -1,5 +1,6 @@ import { BackendVariables } from "@Common/config/variables/Variables"; import { Subscription } from "le-coffre-resources/dist/Admin"; +import { EPaymentFrequency } from "le-coffre-resources/dist/Admin/Subscription"; import Stripe from "stripe"; import { Service } from "typedi"; @@ -14,8 +15,25 @@ export default class StripeService { return this.client; } - public async createCheckoutSession(subscription: Subscription) { - const priceId = subscription.type === "STANDARD" ? this.variables.STRIPE_STANDARD_SUBSCRIPTION_PRICE_ID : this.variables.STRIPE_UNLIMITED_SUBSCRIPTION_PRICE_ID; + public async createCheckoutSession(subscription: Subscription, frequency: EPaymentFrequency) { + let priceId = this.variables.STRIPE_UNLIMITED_SUBSCRIPTION_PRICE_ID; + if(subscription.type === "STANDARD") { + if(frequency === EPaymentFrequency.Yearly) { + priceId = this.variables.STRIPE_STANDARD_ANNUAL_SUBSCRIPTION_PRICE_ID; + } + else{ + priceId = this.variables.STRIPE_STANDARD_SUBSCRIPTION_PRICE_ID; + } + } + else if(subscription.type === "UNLIMITED") { + if(frequency === EPaymentFrequency.Yearly) { + priceId = this.variables.STRIPE_UNLIMITED_ANNUAL_SUBSCRIPTION_PRICE_ID; + } + else{ + priceId = this.variables.STRIPE_UNLIMITED_SUBSCRIPTION_PRICE_ID; + } + } + return this.client.checkout.sessions.create({ mode: "subscription", payment_method_types: ["card"],