45 lines
1.6 KiB
TypeScript
45 lines
1.6 KiB
TypeScript
import { Body, Controller, Get, Post, Redirect } from "@nestjs/common";
|
|
import Stripe from 'stripe'
|
|
import { PurchaseService } from "./purchase.service";
|
|
import { UserService } from "./user.service";
|
|
import { PurchaseItem } from "./dto/purchase-item";
|
|
|
|
@Controller('/buy')
|
|
export class BuyController {
|
|
private static TEST_KEY = "sk_test_51SVqfTBQui1OXGpt6kgnIuDpbwAqQBZqNoVXkn77UeoxfOMgjC3xFuXCet1h51REAq9XeP72qBDWdXlbaTvxb4yN00YWF6TWFm"
|
|
private readonly stripe = new Stripe(BuyController.TEST_KEY)
|
|
|
|
constructor(private readonly purchaseService: PurchaseService, private readonly userService: UserService) {}
|
|
|
|
@Post("/start")
|
|
@Redirect()
|
|
public async getStripeSessionUrl(@Body("userId") userId: number, @Body("quantity") quantity: number) {
|
|
const user = this.userService.getUserById(userId)
|
|
const purchase = this.purchaseService.recordPurchaseStart(user, PurchaseItem.PAELLA, quantity)
|
|
const session = await this.stripe.checkout.sessions.create({
|
|
line_items: [
|
|
{
|
|
price: purchase.purchasedProduct,
|
|
quantity: purchase.purchasedUnits
|
|
}
|
|
],
|
|
mode: "payment",
|
|
success_url: "https://localhost:5173/buy/complete",
|
|
cancel_url: "https://localhost:5173/buy/cancel",
|
|
payment_method_types: ['card'],
|
|
})
|
|
console.log(session)
|
|
|
|
return {url: session.url}
|
|
}
|
|
|
|
@Get("/complete")
|
|
public complete() {
|
|
|
|
}
|
|
|
|
@Get("/cancel")
|
|
public cancel() {
|
|
|
|
}
|
|
} |