mirror of
https://github.com/lukevella/rallly.git
synced 2025-06-01 10:11:50 +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: {
|
||||
email: true,
|
||||
name: true,
|
||||
customerId: true,
|
||||
subscription: {
|
||||
select: {
|
||||
|
@ -52,6 +53,26 @@ export async function POST(request: NextRequest) {
|
|||
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) {
|
||||
// User already has an active subscription. Take them to customer portal
|
||||
return NextResponse.redirect(
|
||||
|
@ -67,16 +88,11 @@ export async function POST(request: NextRequest) {
|
|||
return_path ?? "/api/stripe/portal?session_id={CHECKOUT_SESSION_ID}",
|
||||
),
|
||||
cancel_url: absoluteUrl(return_path),
|
||||
...(user.customerId
|
||||
? {
|
||||
customer: user.customerId,
|
||||
customer_update: {
|
||||
name: "auto",
|
||||
},
|
||||
}
|
||||
: {
|
||||
customer_email: user.email,
|
||||
}),
|
||||
customer: customerId,
|
||||
customer_update: {
|
||||
name: "auto",
|
||||
address: "auto",
|
||||
},
|
||||
mode: "subscription",
|
||||
allow_promotion_codes: true,
|
||||
billing_address_collection: "auto",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue