stripe payment endpoints created
This commit is contained in:
committed by
GitHub
parent
c5c50e75db
commit
eb7e052d75
@@ -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 },
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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 },
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user