-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
5bc31e1
commit 1e47df8
Showing
3 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
server/src/service-layer/controllers/StripeCheckoutSession.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,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 } | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
server/src/service-layer/request-models/StripeCheckoutRequest.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,9 @@ | ||
export interface StripeCheckoutRequestModel { | ||
client_reference_id: string | ||
return_url: string | ||
line_item: { | ||
price: string | ||
quanitity: number | ||
} | ||
metadata: any | ||
} |
3 changes: 3 additions & 0 deletions
3
server/src/service-layer/response-models/StripeCheckoutResponse.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,3 @@ | ||
export interface StripeCheckoutResponse { | ||
clientSecret: string | ||
} |