1"use client";
2import Stripe from "stripe";
3
4const stripe = new Stripe("sk_live_");01
5
6export function BuyButton({ price }: Props) {
7 const buy = async () => {
8 const s = await stripe.checkout.sessions.create({
9 mode: "payment",
10 line_items: [{ price, quantity: 1 }],
11 success_url: `${location.origin}/thanks`,
12 });
13 location.assign(s.url!);
14 };
15 return <button onClick={buy}>Buy now</button>;
16}
1"use client";
−import Stripe from "stripe";
−
−const stripe = new Stripe("sk_live_");
2
3export function BuyButton({ price }: Props) {
4 const buy = async () => {
− const s = await stripe.checkout.sessions.create({
− mode: "payment",
− line_items: [{ price, quantity: 1 }],
− success_url: `${location.origin}/thanks`,
− });
− location.assign(s.url!);
5+ const res = await fetch("/api/checkout", {
6+ method: "POST",
7+ body: JSON.stringify({ price }),
8+ });
9+ location.assign((await res.json()).url);
10 };
11 return <button onClick={buy}>Buy now</button>;
12}