mirror of
https://github.com/lukevella/rallly.git
synced 2025-06-07 21:21:49 +02:00
🐛 Create stripe customer on checkout (#1620)
This commit is contained in:
parent
ebe265ddc3
commit
19cfd33d66
1 changed files with 26 additions and 10 deletions
|
@ -39,6 +39,7 @@ export async function POST(request: NextRequest) {
|
||||||
},
|
},
|
||||||
select: {
|
select: {
|
||||||
email: true,
|
email: true,
|
||||||
|
name: true,
|
||||||
customerId: true,
|
customerId: true,
|
||||||
subscription: {
|
subscription: {
|
||||||
select: {
|
select: {
|
||||||
|
@ -52,6 +53,26 @@ export async function POST(request: NextRequest) {
|
||||||
return new NextResponse(null, { status: 404 });
|
return new NextResponse(null, { status: 404 });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let customerId = user.customerId;
|
||||||
|
|
||||||
|
if (!customerId) {
|
||||||
|
const customer = await stripe.customers.create({
|
||||||
|
email: user.email,
|
||||||
|
name: user.name,
|
||||||
|
});
|
||||||
|
|
||||||
|
await prisma.user.update({
|
||||||
|
where: {
|
||||||
|
id: userSession.user.id,
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
customerId: customer.id,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
customerId = customer.id;
|
||||||
|
}
|
||||||
|
|
||||||
if (user.subscription?.active === true) {
|
if (user.subscription?.active === true) {
|
||||||
// User already has an active subscription. Take them to customer portal
|
// User already has an active subscription. Take them to customer portal
|
||||||
return NextResponse.redirect(
|
return NextResponse.redirect(
|
||||||
|
@ -67,16 +88,11 @@ export async function POST(request: NextRequest) {
|
||||||
return_path ?? "/api/stripe/portal?session_id={CHECKOUT_SESSION_ID}",
|
return_path ?? "/api/stripe/portal?session_id={CHECKOUT_SESSION_ID}",
|
||||||
),
|
),
|
||||||
cancel_url: absoluteUrl(return_path),
|
cancel_url: absoluteUrl(return_path),
|
||||||
...(user.customerId
|
customer: customerId,
|
||||||
? {
|
customer_update: {
|
||||||
customer: user.customerId,
|
name: "auto",
|
||||||
customer_update: {
|
address: "auto",
|
||||||
name: "auto",
|
},
|
||||||
},
|
|
||||||
}
|
|
||||||
: {
|
|
||||||
customer_email: user.email,
|
|
||||||
}),
|
|
||||||
mode: "subscription",
|
mode: "subscription",
|
||||||
allow_promotion_codes: true,
|
allow_promotion_codes: true,
|
||||||
billing_address_collection: "auto",
|
billing_address_collection: "auto",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue