Skip to content

Commit

Permalink
Integreted razorpay in Book Appointments
Browse files Browse the repository at this point in the history
  • Loading branch information
ad956 committed May 30, 2024
1 parent 525d0ab commit 34e3514
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
4 changes: 3 additions & 1 deletion app/(pages)/patient/appointments/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { getPatientData } from "@lib/patient";
export default async function Appointments() {
const patient: Patient = await getPatientData();

const { firstname, lastname, email } = patient;

return (
<section className="flex flex-col gap-5 p-5 overflow-y-scroll">
<Card
Expand Down Expand Up @@ -34,7 +36,7 @@ export default async function Appointments() {
/>
</Card>

<BookAppointment />
<BookAppointment name={`${firstname} ${lastname}`} email={email} />
</section>
);
}
41 changes: 23 additions & 18 deletions app/(pages)/patient/components/BookAppointment/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ type Hospital = {
appointment_charge: string;
};

export default function BookAppointment() {
type BookAppointmentProps = {
name: string;
email: string;
};

export default function BookAppointment({ name, email }: BookAppointmentProps) {
const [states, setStates] = useState<string[]>([]);
const [selectedState, setSelectedState] = useState("");
const [cities, setCities] = useState<string[]>([]);
Expand Down Expand Up @@ -180,18 +185,18 @@ export default function BookAppointment() {
toast.loading("Please wait");

// checks for existing pending appointment request
const result = await pendingAppointmentsRequest(
selectedHospital.hospital_id
);
// const result = await pendingAppointmentsRequest(
// selectedHospital.hospital_id
// );

if (!result.hasPendingAppointment) {
toast.dismiss();
toast.error("You already have a pending appointment request");
return;
}
// if (!result.hasPendingAppointment) {
// toast.dismiss();
// toast.error("You already have a pending appointment request");
// return;
// }

// razorpay payment processing
await processPayment(selectedHospital.appointment_charge);
await processPayment(selectedHospital.appointment_charge, name, email);
toast.dismiss();

// booking appointment after payment
Expand Down Expand Up @@ -425,7 +430,7 @@ export default function BookAppointment() {
);
}

async function processPayment(amount: string) {
async function processPayment(amount: string, name: string, email: string) {
try {
const orderId: string = await createOrderId(amount);
const options = {
Expand All @@ -449,22 +454,22 @@ async function processPayment(amount: string) {
headers: { "Content-Type": "application/json" },
});
const res = await result.json();
if (res.isOk) alert("payment succeed");
if (res.isOk) toast.success("payment succeed");
else {
alert(res.message);
toast.success(res.message);
}
},
prefill: {
name: "Anand Suthar",
email: "[email protected]",
name,
email,
},
theme: {
color: "#3399cc",
},
};
const paymentObject = new window.Razorpay(options);
paymentObject.on("payment.failed", function (response: any) {
alert(response.error.description);
toast.error(response.error.description);
});
paymentObject.open();
} catch (error) {
Expand All @@ -486,13 +491,13 @@ const createOrderId = async (amount: string) => {
});

if (!response.ok) {
throw new Error("Network response was not ok");
throw new Error("Order id creation response was not ok");
}

const data = await response.json();
return data.orderId;
} catch (error) {
console.error("There was a problem with your fetch operation:", error);
console.error("There was a problem with creating order id: ", error);
toast.error("Order id creation failed");
}
};

0 comments on commit 34e3514

Please sign in to comment.