'use client'; import { axiosWithAuth } from '@/api/interceptors'; import { absoluteUrl } from '@/lib/absolute-url'; const Payment = () => { const handleClick = async (e: React.FormEvent<HTMLFormElement>) => { e.preventDefault(); const res = await axiosWithAuth.post(`/stripe/subscription`, { amount: 1, // dollars redirectUrl: absoluteUrl('/test') }); console.log('res ', res.data?.url); window.location.href = res.data?.url; }; return ( <form onClick={handleClick}> <h1>hello</h1> <button type='submit'>PAY</button> </form> ); }; export default Payment; import { Controller, Post, Body, ValidationPipe, UsePipes } from '@nestjs/common'; import { StripeService } from './stripe.service'; import { SubscriptionDto } from './dto/subscription.dto'; @Controller('stripe') export class StripeController { constructor(private stripeService: StripeService) {} @Post('subscription') @UsePipes(new ValidationPipe()) subscription(@Body() dto: SubscriptionDto) { return this.stripeService.subscription(dto); } } import { Injectable } from '@nestjs/common'; import Stripe from 'stripe'; import { SubscriptionDto } from './dto/subscription.dto'; @Injectable() export class StripeService { private stripe: Stripe; constructor() { this.stripe = new Stripe(process.env.STRIPE_SECRET_KEY, { apiVersion: '2024-10-28.acacia', }) } subscription(dto: SubscriptionDto) { const totalPrice = dto.amount const redirectUrl = dto.redirectUrl const userId = dto.userId return this.stripe.checkout.sessions.create({ mode: 'subscription', payment_method_types: ["card"], line_items: [ { price_data: { currency: 'usd', unit_amount: Math.round(totalPrice * 100), // cents product_data: { name: 'Cart Payment', }, recurring: { interval: 'month', interval_count: 1 } }, quantity: 1, }, ], success_url: redirectUrl, cancel_url: redirectUrl, }); } }