-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
409 additions
and
88 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Stripe } from "stripe"; | ||
|
||
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!); | ||
|
||
export async function POST(request: Request) { | ||
const body = await request.json(); | ||
const { name, email, amount } = body; | ||
|
||
if (!name || !email || !amount) { | ||
return new Response(JSON.stringify({ error: "Missing required fields" }), { | ||
status: 400, | ||
}); | ||
} | ||
|
||
let customer; | ||
const doesCustomerExist = await stripe.customers.list({ | ||
email, | ||
}); | ||
|
||
if (doesCustomerExist.data.length > 0) { | ||
customer = doesCustomerExist.data[0]; | ||
} else { | ||
const newCustomer = await stripe.customers.create({ | ||
name, | ||
email, | ||
}); | ||
|
||
customer = newCustomer; | ||
} | ||
|
||
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, | ||
}), | ||
); | ||
} |
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,37 @@ | ||
import { Stripe } from "stripe"; | ||
|
||
const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!); | ||
|
||
export async function POST(request: Request) { | ||
try { | ||
const body = await request.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 fields for payment" }), { | ||
status: 400, | ||
}); | ||
} | ||
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 Confirm succesfull", | ||
result: result | ||
}) | ||
) | ||
} catch (error) { | ||
console.log(error) | ||
return new Response( | ||
JSON.stringify({ | ||
error: error, | ||
status: 500 | ||
}) | ||
) | ||
} | ||
} |
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,13 @@ | ||
import { data } from "@/constants" | ||
import { neon } from "@neondatabase/serverless" | ||
|
||
export async function GET() { | ||
try { | ||
const sql = neon(`${process.env.DATABASE_URL}`) | ||
const response = await sql`SELECT*FROM drivers` | ||
return Response.json({ data :response}) | ||
} catch (error) { | ||
console.log(error) | ||
return Response.json({ error: 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,46 @@ | ||
import {neon} from "@neondatabase/serverless"; | ||
|
||
export async function GET(request: Request, {id}: { id: string }) { | ||
if (!id) | ||
return Response.json({error: "Missing required fields"}, {status: 400}); | ||
|
||
try { | ||
const sql = neon(`${process.env.DATABASE_URL}`); | ||
const response = await sql` | ||
SELECT | ||
rides.ride_id, | ||
rides.origin_address, | ||
rides.destination_address, | ||
rides.origin_latitude, | ||
rides.origin_longitude, | ||
rides.destination_latitude, | ||
rides.destination_longitude, | ||
rides.ride_time, | ||
rides.fare_price, | ||
rides.payment_status, | ||
rides.created_at, | ||
'driver', json_build_object( | ||
'driver_id', drivers.id, | ||
'first_name', drivers.first_name, | ||
'last_name', drivers.last_name, | ||
'profile_image_url', drivers.profile_image_url, | ||
'car_image_url', drivers.car_image_url, | ||
'car_seats', drivers.car_seats, | ||
'rating', drivers.rating | ||
) AS driver | ||
FROM | ||
rides | ||
INNER JOIN | ||
drivers ON rides.driver_id = drivers.id | ||
WHERE | ||
rides.user_id = ${id} | ||
ORDER BY | ||
rides.created_at DESC; | ||
`; | ||
|
||
return Response.json({data: response}); | ||
} catch (error) { | ||
console.error("Error fetching recent rides:", error); | ||
return Response.json({error: "Internal Server Error"}, {status: 500}); | ||
} | ||
} |
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,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("Error inserting data into recent_rides:", error); | ||
return Response.json({error: "Internal Server Error"}, {status: 500}); | ||
} | ||
} |
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
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
Oops, something went wrong.