-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(payment): stripe checkout (#25)
* feat(payment): stripe checkout * build(changelog): update to 1.2.0
- Loading branch information
Showing
7 changed files
with
153 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { NextResponse } from "next/server"; | ||
import { Stripe } from "stripe"; | ||
|
||
export async function GET() { | ||
return NextResponse.json({ message: "success" }, { status: 200 }); | ||
} | ||
export async function POST(request: Request) { | ||
const stripe = new Stripe(process.env.STRIPE_API_SECRET as string, { | ||
apiVersion: "2022-11-15", | ||
}); | ||
|
||
const data = await request.json(); | ||
const price_id = data.price_id; | ||
|
||
if (!price_id) { | ||
return NextResponse.json( | ||
"create-checkout: please provide a valid price id", | ||
{ status: 422 }, | ||
); | ||
} | ||
|
||
let event; | ||
try { | ||
event = await stripe.checkout.sessions.create({ | ||
success_url: process.env.STRIPE_SUCCESS_URL as string, | ||
cancel_url: process.env.STRIPE_CANCEL_URL as string, | ||
mode: "subscription", | ||
line_items: [ | ||
{ | ||
price: price_id, | ||
quantity: 1, | ||
}, | ||
], | ||
}); | ||
return NextResponse.json( | ||
{ message: "success", checkout_url: event.url }, | ||
{ status: 200 }, | ||
); | ||
} catch (error: any) { | ||
return NextResponse.json( | ||
{ | ||
message: `create-checkout: ${ | ||
error?.message || "unexpected error" | ||
}`, | ||
}, | ||
{ | ||
status: error?.status || 512, | ||
statusText: error || "unexpected error", | ||
}, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
"use client"; | ||
|
||
import React, { useState } from "react"; | ||
import { Button, ButtonProps } from "../ui/button"; | ||
import LoadingDots from "../ui/loading-dots"; | ||
|
||
export default function CheckoutButton({ children, ...rest }: ButtonProps) { | ||
const [isLoading, setLoadingState] = useState<boolean>(false); | ||
|
||
const handleCheckout = async () => { | ||
setLoadingState(true); | ||
const response = await fetch("/api/checkout", { | ||
method: "POST", | ||
body: JSON.stringify({ | ||
price_id: | ||
process.env.NEXT_PUBLIC_STRIPE_PRICE_ID_SUBSCRIPTION_HOBBY, | ||
}), | ||
}); | ||
|
||
if (!response.ok) { | ||
throw new Error( | ||
`client(${response.status}): ${ | ||
response.statusText || "unexpected error" | ||
}`, | ||
); | ||
} | ||
|
||
const data = await response.json(); | ||
const checkout_url = data?.checkout_url; | ||
if (!checkout_url) | ||
throw new Error("client: no checkout_url found, please try again"); | ||
|
||
setLoadingState(false); | ||
window.location = checkout_url; | ||
}; | ||
|
||
return ( | ||
<Button {...rest} disabled={isLoading} onClick={handleCheckout}> | ||
{isLoading ? <LoadingDots /> : children} | ||
</Button> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters