-
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): implement stripe checkout
refs #247
- Loading branch information
1 parent
edf3d3a
commit 8bb52d5
Showing
22 changed files
with
14,926 additions
and
36,817 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
46 changes: 46 additions & 0 deletions
46
apps/api/src/billing/controllers/checkout/checkout.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,46 @@ | ||
import type { Context } from "hono"; | ||
import { singleton } from "tsyringe"; | ||
|
||
import { AuthService } from "@src/auth/services/auth.service"; | ||
import { BillingConfig, InjectBillingConfig } from "@src/billing/providers"; | ||
import { CheckoutService } from "@src/billing/services/checkout/checkout.service"; | ||
import { StripeService } from "@src/billing/services/stripe/stripe.service"; | ||
|
||
@singleton() | ||
export class CheckoutController { | ||
constructor( | ||
private readonly stripe: StripeService, | ||
private readonly authService: AuthService, | ||
private readonly checkoutService: CheckoutService, | ||
@InjectBillingConfig() private readonly billingConfig: BillingConfig | ||
) {} | ||
|
||
async checkout(c: Context) { | ||
const { currentUser } = this.authService; | ||
const protocol = c.req.header("x-forwarded-proto"); | ||
const host = c.req.header("x-forwarded-host"); | ||
const referrer = `${protocol}://${host}`; | ||
|
||
if (!this.billingConfig.ALLOWED_CHECKOUT_REFERRERS.includes(referrer)) { | ||
return c.redirect(`${referrer}?forbidden=true`); | ||
} | ||
|
||
if (!currentUser?.userId) { | ||
return c.redirect(`${referrer}?unauthorized=true`); | ||
} | ||
|
||
const session = await this.checkoutService.checkoutFor(currentUser, referrer); | ||
|
||
return c.redirect(session.url); | ||
} | ||
|
||
async webhook(signature: string, input: string) { | ||
const event = this.stripe.webhooks.constructEvent(input, signature, this.billingConfig.STRIPE_WEBHOOK_SECRET); | ||
|
||
if (event.type) { | ||
const paymentIntentSucceeded = event.data.object; | ||
console.log("DEBUG paymentIntentSucceeded", paymentIntentSucceeded); | ||
// TODO: implement wallet refill here | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { createRoute } from "@hono/zod-openapi"; | ||
import { container } from "tsyringe"; | ||
|
||
import { CheckoutController } from "@src/billing/controllers/checkout/checkout.controller"; | ||
import { OpenApiHonoHandled } from "@src/core/services/open-api-hono-handled/open-api-hono-handled"; | ||
|
||
const route = createRoute({ | ||
method: "get", | ||
path: "/v1/checkout", | ||
summary: "Creates a stripe checkout session and redirects to checkout", | ||
tags: ["Wallets"], | ||
request: {}, | ||
responses: { | ||
301: { | ||
description: "Redirects to the checkout page" | ||
} | ||
} | ||
}); | ||
|
||
export const checkoutRouter = new OpenApiHonoHandled(); | ||
|
||
checkoutRouter.openapi(route, async function routeCheckout(c) { | ||
return await container.resolve(CheckoutController).checkout(c); | ||
}); |
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,3 +1,5 @@ | ||
export * from "@src/billing/routes/create-wallet/create-wallet.router"; | ||
export * from "@src/billing/routes/get-wallet-list/get-wallet-list.router"; | ||
export * from "@src/billing/routes/checkout/checkout.router"; | ||
export * from "@src/billing/routes/sign-and-broadcast-tx/sign-and-broadcast-tx.router"; | ||
export * from "@src/billing/routes/stripe-webhook/stripe-webhook.router"; |
39 changes: 39 additions & 0 deletions
39
apps/api/src/billing/routes/stripe-webhook/stripe-webhook.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,39 @@ | ||
import { createRoute } from "@hono/zod-openapi"; | ||
import { container } from "tsyringe"; | ||
import { z } from "zod"; | ||
|
||
import { CheckoutController } from "@src/billing/controllers/checkout/checkout.controller"; | ||
import { OpenApiHonoHandled } from "@src/core/services/open-api-hono-handled/open-api-hono-handled"; | ||
|
||
const route = createRoute({ | ||
method: "post", | ||
path: "/v1/stripe-webhook", | ||
summary: "", | ||
request: { | ||
body: { | ||
content: { | ||
"application/json": { | ||
schema: z.any() | ||
} | ||
} | ||
} | ||
}, | ||
responses: { | ||
200: { | ||
description: "", | ||
content: { | ||
"application/json": { | ||
schema: z.void() | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
|
||
export const stripeWebhook = new OpenApiHonoHandled(); | ||
|
||
stripeWebhook.openapi(route, async function routeStripeWebhook(c) { | ||
const sig = c.req.header("stripe-signature"); | ||
await container.resolve(CheckoutController).webhook(sig, await c.req.text()); | ||
return c.json({}, 200); | ||
}); |
46 changes: 46 additions & 0 deletions
46
apps/api/src/billing/services/checkout/checkout.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,46 @@ | ||
import { singleton } from "tsyringe"; | ||
|
||
import { BillingConfig, InjectBillingConfig } from "@src/billing/providers"; | ||
import { StripeService } from "@src/billing/services/stripe/stripe.service"; | ||
import { UserOutput, UserRepository } from "@src/user/repositories"; | ||
|
||
@singleton() | ||
export class CheckoutService { | ||
constructor( | ||
private readonly stripe: StripeService, | ||
private readonly userRepository: UserRepository, | ||
@InjectBillingConfig() private readonly billingConfig: BillingConfig | ||
) {} | ||
|
||
async checkoutFor(user: UserOutput, domain: string) { | ||
const { stripeCustomerId } = await this.ensureCustomer(user); | ||
|
||
return await this.stripe.startCheckoutSession({ | ||
customerId: stripeCustomerId, | ||
domain | ||
}); | ||
} | ||
|
||
private async ensureCustomer<T extends UserOutput>(user: T): Promise<Omit<T, "stripeCustomerId"> & Required<Pick<T, "stripeCustomerId">>> { | ||
if (user.stripeCustomerId) { | ||
return user; | ||
} | ||
|
||
const customer = await this.stripe.customers.create({ | ||
email: user.email, | ||
name: user.username, | ||
metadata: { | ||
userId: user.userId | ||
} | ||
}); | ||
|
||
await this.userRepository.updateById(user.id, { | ||
stripeCustomerId: customer.id | ||
}); | ||
|
||
return { | ||
...user, | ||
stripeCustomerId: customer.id | ||
}; | ||
} | ||
} |
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,39 @@ | ||
import Stripe from "stripe"; | ||
import { singleton } from "tsyringe"; | ||
|
||
import { BillingConfig, InjectBillingConfig } from "@src/billing/providers"; | ||
|
||
interface CheckoutOptions { | ||
customerId: string; | ||
domain: string; | ||
} | ||
|
||
@singleton() | ||
export class StripeService extends Stripe { | ||
constructor(@InjectBillingConfig() private readonly billingConfig: BillingConfig) { | ||
super(process.env.STRIPE_SECRET_KEY, { | ||
apiVersion: "2024-06-20" | ||
}); | ||
} | ||
|
||
async startCheckoutSession(options: CheckoutOptions) { | ||
return await this.checkout.sessions.create({ | ||
line_items: [ | ||
{ | ||
price: this.billingConfig.STRIPE_PRICE_ID, | ||
quantity: 1 | ||
} | ||
], | ||
mode: "payment", | ||
customer: options.customerId, | ||
payment_intent_data: { | ||
setup_future_usage: "off_session" | ||
}, | ||
saved_payment_method_options: { | ||
payment_method_save: "enabled" | ||
}, | ||
success_url: `${options.domain}?payment-success=true`, | ||
cancel_url: `${options.domain}?payment-canceled=true` | ||
}); | ||
} | ||
} |
Oops, something went wrong.