-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(billing): enable checkout options with promo codes
- Loading branch information
1 parent
1242954
commit 0cb439d
Showing
27 changed files
with
2,110 additions
and
2,527 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
apps/api/src/billing/controllers/stripe/stripe.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { singleton } from "tsyringe"; | ||
|
||
import { Protected } from "@src/auth/services/auth.service"; | ||
import { StripePricesOutputResponse } from "@src/billing"; | ||
import { StripeService } from "@src/billing/services/stripe/stripe.service"; | ||
|
||
@singleton() | ||
export class StripeController { | ||
constructor(private readonly stripe: StripeService) {} | ||
|
||
@Protected([{ action: "read", subject: "StripePrice" }]) | ||
async findPrices(): Promise<StripePricesOutputResponse> { | ||
return { data: await this.stripe.findPrices() }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
apps/api/src/billing/routes/stripe-prices/stripe-prices.router.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { createRoute } from "@hono/zod-openapi"; | ||
import { container } from "tsyringe"; | ||
import { z } from "zod"; | ||
|
||
import { StripeController } from "@src/billing/controllers/stripe/stripe.controller"; | ||
import { OpenApiHonoHandler } from "@src/core/services/open-api-hono-handler/open-api-hono-handler"; | ||
|
||
const StripePricesOutputSchema = z.object({ | ||
currency: z.string().openapi({}), | ||
unitAmount: z.number().openapi({}), | ||
isCustom: z.boolean().openapi({}) | ||
}); | ||
|
||
export const StripePricesResponseOutputSchema = z.object({ | ||
data: z.array(StripePricesOutputSchema) | ||
}); | ||
|
||
export type StripePricesOutputResponse = z.infer<typeof StripePricesResponseOutputSchema>; | ||
|
||
const route = createRoute({ | ||
method: "get", | ||
path: "/v1/stripe-prices", | ||
summary: "", | ||
request: {}, | ||
responses: { | ||
200: { | ||
description: "", | ||
content: { | ||
"application/json": { | ||
schema: StripePricesResponseOutputSchema | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
|
||
export const stripePricesRouter = new OpenApiHonoHandler(); | ||
|
||
stripePricesRouter.openapi(route, async function routeStripePrices(c) { | ||
await container.resolve(StripeController).findPrices(); | ||
return c.json(await container.resolve(StripeController).findPrices(), 200); | ||
}); |
11 changes: 11 additions & 0 deletions
11
apps/api/src/billing/services/billing-config/billing-config.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { singleton } from "tsyringe"; | ||
|
||
import { envSchema } from "@src/billing/config/env.config"; | ||
import { ConfigService } from "@src/core/services/config/config.service"; | ||
|
||
@singleton() | ||
export class BillingConfigService extends ConfigService<typeof envSchema, unknown> { | ||
constructor() { | ||
super({ envSchema }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,74 @@ | ||
import assert from "http-assert"; | ||
import orderBy from "lodash/orderBy"; | ||
import Stripe from "stripe"; | ||
import { singleton } from "tsyringe"; | ||
|
||
import { BillingConfig, InjectBillingConfig } from "@src/billing/providers"; | ||
import { BillingConfigService } from "@src/billing/services/billing-config/billing-config.service"; | ||
|
||
interface CheckoutOptions { | ||
customerId: string; | ||
redirectUrl: string; | ||
amount?: string; | ||
} | ||
|
||
interface StripePrices { | ||
unitAmount: number; | ||
isCustom: boolean; | ||
currency: string; | ||
} | ||
|
||
@singleton() | ||
export class StripeService extends Stripe { | ||
constructor(@InjectBillingConfig() private readonly billingConfig: BillingConfig) { | ||
constructor(private readonly billingConfig: BillingConfigService) { | ||
super(process.env.STRIPE_SECRET_KEY, { | ||
apiVersion: "2024-06-20" | ||
}); | ||
} | ||
|
||
async startCheckoutSession(options: CheckoutOptions) { | ||
const price = await this.getPrice(options.amount); | ||
|
||
return await this.checkout.sessions.create({ | ||
line_items: [ | ||
{ | ||
price: this.billingConfig.STRIPE_PRICE_ID, | ||
price: price.id, | ||
quantity: 1 | ||
} | ||
], | ||
mode: "payment", | ||
allow_promotion_codes: this.billingConfig.STRIPE_ENABLE_COUPONS === "true", | ||
allow_promotion_codes: !!options.amount, | ||
customer: options.customerId, | ||
success_url: `${options.redirectUrl}?session_id={CHECKOUT_SESSION_ID}&payment-success=true`, | ||
cancel_url: `${options.redirectUrl}?session_id={CHECKOUT_SESSION_ID}&payment-canceled=true` | ||
}); | ||
} | ||
|
||
private async getPrice(amount?: string) { | ||
const { data: prices } = await this.prices.list({ product: this.billingConfig.get("STRIPE_PRODUCT_ID") }); | ||
|
||
const price = prices.find(price => { | ||
const isCustom = !amount && !!price.custom_unit_amount; | ||
|
||
if (isCustom) { | ||
return true; | ||
} | ||
|
||
return price.unit_amount === Number(amount) * 100; | ||
}); | ||
|
||
assert(price, 400, "Price invalid"); | ||
|
||
return price; | ||
} | ||
|
||
async findPrices(): Promise<StripePrices[]> { | ||
const { data: prices } = await this.prices.list(); | ||
const responsePrices = prices.map(price => ({ | ||
unitAmount: price.custom_unit_amount ? undefined : price.unit_amount / 100, | ||
isCustom: !!price.custom_unit_amount, | ||
currency: price.currency | ||
})); | ||
|
||
return orderBy(responsePrices, ["isCustom", "unitAmount"], ["asc", "asc"]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.