Skip to content

Commit

Permalink
Merge pull request #45 from Praashh/cashfree/integration
Browse files Browse the repository at this point in the history
feat: cashfree integration
  • Loading branch information
Praashh authored Oct 16, 2024
2 parents 9a4192f + 6edab96 commit 642a1ef
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 0 deletions.
47 changes: 47 additions & 0 deletions apps/client/app/api/payment/initiate/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { NextRequest, NextResponse } from "next/server";

export async function POST(req: NextRequest) {
try {
const body = await req.json();

const response = await fetch("https://sandbox.cashfree.com/pg/orders", {
method: "POST",
headers: {
accept: "application/json",
"content-type": "application/json",
"x-api-version": "2023-08-01",
"x-client-id": process.env.CASHFREE_CLIENT_ID as string,
"x-client-secret": process.env.CASHFREE_CLIENT_SECRET as string,
},
body: JSON.stringify({
customer_details: {
customer_id: body.customer_id,
customer_phone: body.customer_phone,
},
order_id: body.order_id,
order_amount: body.order_amount,
order_currency: "INR",
order_meta: {
notify_url: "https://webhook.site/41583a44-713e-4ba7-a4f1-4a954d84cf08", // TODO: add a webhook
payment_methods: "cc,dc,upi",
},
}),
});

const data = await response.json();

if (!response.ok) {
return NextResponse.json(
{ message: data.message },
{ status: response.status }
);
}

return NextResponse.json(data, { status: 200 });
} catch (error) {
return NextResponse.json(
{ message: "Something went wrong", error },
{ status: 500 }
);
}
}
27 changes: 27 additions & 0 deletions apps/client/app/api/payment/status/[order_id]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { NextRequest, NextResponse } from "next/server";

export async function GET(
req: NextRequest,
{ params }: { params: { order_id: string } }
) {
const { order_id } = params;
const response = await fetch(
`https://sandbox.cashfree.com/pg/orders/${order_id}`,
{
method: "GET",
headers: {
accept: "application/json",
"content-type": "application/json",
"x-api-version": "2023-08-01",
"x-client-id": process.env.CASHFREE_CLIENT_ID as string,
"x-client-secret": process.env.CASHFREE_CLIENT_SECRET as string,
},
}
);
const data = await response.json();
if (data.order_status === "PAID") {
return NextResponse.redirect(new URL("/", req.url));
} else {
return NextResponse.redirect(new URL("/404", req.url));
}
}
4 changes: 4 additions & 0 deletions apps/client/lib/cashfree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {load} from '@cashfreepayments/cashfree-js';
export const cashfree = await load({
mode: "sandbox" //or production
});
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 642a1ef

Please sign in to comment.