-
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.
Integreted razorpay in Book Appointments
- Loading branch information
Showing
2 changed files
with
26 additions
and
19 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
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 |
---|---|---|
|
@@ -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[]>([]); | ||
|
@@ -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 | ||
|
@@ -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 = { | ||
|
@@ -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) { | ||
|
@@ -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"); | ||
} | ||
}; |