Skip to content

Commit

Permalink
Endpoint creation
Browse files Browse the repository at this point in the history
Created endpoint that receives user request and body. Creates a checkout 
session with Stripe and returns the clientSecret. 
Created Request and Response models.
  • Loading branch information
jeffplays2005 committed Apr 1, 2024
1 parent 5bc31e1 commit 1e47df8
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
34 changes: 34 additions & 0 deletions server/src/service-layer/controllers/StripeCheckoutSession.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { StripeCheckoutRequestModel } from "service-layer/request-models/StripeCheckoutRequest"
import { StripeCheckoutResponse } from "service-layer/response-models/StripeCheckoutResponse"
import Stripe from "stripe"
import { Controller, Post, Route, SuccessResponse, Request, Body } from "tsoa"

@Route("create-checkout-session")
export class CreateCheckoutSession extends Controller {
@Post()
@SuccessResponse(200, "Successful checkout session creation")
public async createCheckoutSession(
@Request() request: any,
@Body() requestBody: StripeCheckoutRequestModel
): Promise<StripeCheckoutResponse> {
const stripe = new Stripe(process.env.STRIPE_API_KEY)
try {
const session = await stripe.checkout.sessions.create({
// consumer changeable
client_reference_id: requestBody.client_reference_id,
return_url: requestBody.return_url,
line_items: [requestBody.line_item],
metadata: requestBody.metadata,
// configured internally and should not change
ui_mode: "embedded",
mode: "payment",
currency: "NZD"
})
this.setStatus(200)
return { clientSecret: session.client_secret }
} catch (e) {
this.setStatus(400)
return { clientSecret: null }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface StripeCheckoutRequestModel {
client_reference_id: string
return_url: string
line_item: {
price: string
quanitity: number
}
metadata: any
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface StripeCheckoutResponse {
clientSecret: string
}

0 comments on commit 1e47df8

Please sign in to comment.