From eb7e052d757836a3485705def4dd5c5284269bd1 Mon Sep 17 00:00:00 2001 From: Sanidhya Kumar Verma Date: Wed, 4 Sep 2024 10:17:45 +0000 Subject: [PATCH] stripe payment endpoints created --- app/(api)/(stripe)/create+api.ts | 60 +++++++++++++++++++++++++ app/(api)/(stripe)/pay+api.ts | 44 +++++++++++++++++++ app/(api)/ride/create+api.ts | 75 ++++++++++++++++++++++++++++++++ 3 files changed, 179 insertions(+) create mode 100644 app/(api)/(stripe)/create+api.ts create mode 100644 app/(api)/(stripe)/pay+api.ts create mode 100644 app/(api)/ride/create+api.ts diff --git a/app/(api)/(stripe)/create+api.ts b/app/(api)/(stripe)/create+api.ts new file mode 100644 index 0000000..c9d67e7 --- /dev/null +++ b/app/(api)/(stripe)/create+api.ts @@ -0,0 +1,60 @@ +import { Stripe } from "stripe"; + +const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!); + +export async function POST(req: Request) { + const body = await req.json(); + const { name, email, amount } = body; + + if (!name || !email || !amount) + return new Response( + JSON.stringify({ error: "Please enter a valid email address" }), + { status: 404 }, + ); + + try { + let customer; + + const existingCustomer = await stripe.customers.list({ email }); + + if (existingCustomer.data.length > 0) customer = existingCustomer.data[0]; + else { + customer = await stripe.customers.create({ + name, + email, + }); + } + + const ephemeralKey = await stripe.ephemeralKeys.create( + { customer: customer.id }, + { apiVersion: "2024-06-20" }, + ); + + const paymentIntent = await stripe.paymentIntents.create({ + amount: parseInt(amount) * 100, + currency: "usd", + customer: customer.id, + automatic_payment_methods: { + enabled: true, + allow_redirects: "never", + }, + }); + + return new Response( + JSON.stringify({ + paymentIntent: paymentIntent, + ephemeralKey: ephemeralKey, + customer: customer.id, + }), + ); + } catch (err) { + console.log("[PAYMENT_CREATE]: ", err); + + return new Response( + JSON.stringify({ + error: "Internal Server Error", + }), + { status: 500 }, + ); + } +} diff --git a/app/(api)/(stripe)/pay+api.ts b/app/(api)/(stripe)/pay+api.ts new file mode 100644 index 0000000..a5520f4 --- /dev/null +++ b/app/(api)/(stripe)/pay+api.ts @@ -0,0 +1,44 @@ +import { Stripe } from "stripe"; + +const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!); + +export async function POST(req: Request) { + const body = await req.json(); + const { payment_method_id, payment_intent_id, customer_id } = body; + + if (!payment_method_id || !payment_intent_id || !customer_id) + return new Response( + JSON.stringify({ error: "Missing required payment information." }), + { status: 404 }, + ); + + try { + const paymentMethod = await stripe.paymentMethods.attach( + payment_method_id, + { + customer: customer_id, + }, + ); + + const result = await stripe.paymentIntents.confirm(payment_intent_id, { + payment_method: paymentMethod.id, + }); + + return new Response( + JSON.stringify({ + success: true, + message: "Payment completed successfully.", + result, + }), + ); + } catch (err) { + console.log("[PAYMENT_PAY]: ", err); + + return new Response( + JSON.stringify({ + error: "Internal Server Error", + }), + { status: 500 }, + ); + } +} diff --git a/app/(api)/ride/create+api.ts b/app/(api)/ride/create+api.ts new file mode 100644 index 0000000..c7b0987 --- /dev/null +++ b/app/(api)/ride/create+api.ts @@ -0,0 +1,75 @@ +import { neon } from "@neondatabase/serverless"; + +export async function POST(request: Request) { + try { + const body = await request.json(); + const { + origin_address, + destination_address, + origin_latitude, + origin_longitude, + destination_latitude, + destination_longitude, + ride_time, + fare_price, + payment_status, + driver_id, + user_id, + } = body; + + if ( + !origin_address || + !destination_address || + !origin_latitude || + !origin_longitude || + !destination_latitude || + !destination_longitude || + !ride_time || + !fare_price || + !payment_status || + !driver_id || + !user_id + ) { + return Response.json( + { error: "Missing required fields" }, + { status: 400 }, + ); + } + + const sql = neon(`${process.env.DATABASE_URL}`); + + const response = await sql` + INSERT INTO rides ( + origin_address, + destination_address, + origin_latitude, + origin_longitude, + destination_latitude, + destination_longitude, + ride_time, + fare_price, + payment_status, + driver_id, + user_id + ) VALUES ( + ${origin_address}, + ${destination_address}, + ${origin_latitude}, + ${origin_longitude}, + ${destination_latitude}, + ${destination_longitude}, + ${ride_time}, + ${fare_price}, + ${payment_status}, + ${driver_id}, + ${user_id} + ) + RETURNING *; + `; + + return Response.json({ data: response[0] }, { status: 201 }); + } catch (error) { + console.error("[CREATE_RIDES]: ", error); + return Response.json({ error: "Internal Server Error" }, { status: 500 }); + } +}