This commit is contained in:
Sewmina 2025-06-25 01:32:06 +05:30
parent a7b6600e07
commit 94883bd04d
2 changed files with 28 additions and 4 deletions

View File

@ -33,7 +33,8 @@ export async function POST(req: Request) {
boxId, boxId,
boxName, boxName,
amount, amount,
currency currency,
req
); );
console.log('Checkout session created successfully:', { sessionId: session.id }); console.log('Checkout session created successfully:', { sessionId: session.id });

View File

@ -1,4 +1,5 @@
import { loadStripe } from '@stripe/stripe-js'; import { loadStripe } from '@stripe/stripe-js';
import { headers } from 'next/headers';
// Initialize Stripe on the client // Initialize Stripe on the client
export const getStripe = () => { export const getStripe = () => {
@ -56,7 +57,8 @@ export async function createCheckoutSession(
boxId: string, boxId: string,
boxName: string, boxName: string,
amount: number, amount: number,
currency: string = 'usd' currency: string = 'usd',
request?: Request
) { ) {
try { try {
// This function should only be called from server-side code // This function should only be called from server-side code
@ -67,6 +69,27 @@ export async function createCheckoutSession(
typescript: true, typescript: true,
}); });
// Determine the base URL dynamically
let baseUrl = process.env.NEXT_PUBLIC_BASE_URL;
if (!baseUrl && request) {
// Fallback: extract from request headers
const headersList = await headers();
const host = headersList.get('host');
const protocol = headersList.get('x-forwarded-proto') || 'http';
if (host) {
baseUrl = `${protocol}://${host}`;
}
}
// Final fallback for development
if (!baseUrl) {
baseUrl = 'http://localhost:3000';
}
console.log('Using base URL for Stripe checkout:', baseUrl);
const session = await stripe.checkout.sessions.create({ const session = await stripe.checkout.sessions.create({
payment_method_types: ['card'], payment_method_types: ['card'],
line_items: [ line_items: [
@ -83,8 +106,8 @@ export async function createCheckoutSession(
}, },
], ],
mode: 'payment', mode: 'payment',
success_url: `${process.env.NEXT_PUBLIC_BASE_URL}/boxes/success?box_id=${boxId}&session_id={CHECKOUT_SESSION_ID}`, success_url: `${baseUrl}/boxes/success?box_id=${boxId}&session_id={CHECKOUT_SESSION_ID}`,
cancel_url: `${process.env.NEXT_PUBLIC_BASE_URL}/boxes?canceled=true`, cancel_url: `${baseUrl}/boxes?canceled=true`,
metadata: { metadata: {
boxId, boxId,
}, },