|
| 1 | +--- |
| 2 | +title: "Razorpay" |
| 3 | +description: "Accept payments." |
| 4 | +logo: "razorpay.png" |
| 5 | +--- |
| 6 | + |
| 7 | +## Code |
| 8 | + |
| 9 | +```js:index.js |
| 10 | +import Head from "next/head"; |
| 11 | +import styles from "../styles/Home.module.css"; |
| 12 | + |
| 13 | +export default function Home() { |
| 14 | + async function displayRazorpay() { |
| 15 | + const res = await loadRazorpay(); |
| 16 | + |
| 17 | + if (!res) { |
| 18 | + alert("Razorpay SDK Failed to load"); |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + // Make API call to the serverless API |
| 23 | + const data = await fetch("/api/razorpay", { method: "POST" }).then((t) => |
| 24 | + t.json() |
| 25 | + ); |
| 26 | + console.log(data); |
| 27 | + var options = { |
| 28 | + key: process.env.RAZORPAY_KEY, // Enter the Key ID generated from the Dashboard |
| 29 | + name: "Manu Arora Pvt Ltd", |
| 30 | + currency: data.currency, |
| 31 | + amount: data.amount, |
| 32 | + order_id: data.id, |
| 33 | + description: "Thankyou for your test donation", |
| 34 | + image: "https://manuarora.in/logo.png", |
| 35 | + handler: function (response) { |
| 36 | + alert(response.razorpay_payment_id); |
| 37 | + alert(response.razorpay_order_id); |
| 38 | + alert(response.razorpay_signature); |
| 39 | + }, |
| 40 | + prefill: { |
| 41 | + name: "Manu Arora", |
| 42 | + email: "manuarorawork@gmail.com", |
| 43 | + contact: "9999999999", |
| 44 | + }, |
| 45 | + }; |
| 46 | + |
| 47 | + const paymentObject = new window.Razorpay(options); |
| 48 | + paymentObject.open(); |
| 49 | + } |
| 50 | + const loadRazorpay = () => { |
| 51 | + return new Promise((resolve) => { |
| 52 | + const script = document.createElement("script"); |
| 53 | + script.src = "https://checkout.razorpay.com/v1/checkout.js"; |
| 54 | + // document.body.appendChild(script); |
| 55 | + |
| 56 | + script.onload = () => { |
| 57 | + resolve(true); |
| 58 | + }; |
| 59 | + script.onerror = () => { |
| 60 | + resolve(false); |
| 61 | + }; |
| 62 | + |
| 63 | + document.body.appendChild(script); |
| 64 | + }); |
| 65 | + }; |
| 66 | + |
| 67 | + return ( |
| 68 | + <div className={styles.container}> |
| 69 | + <Head> |
| 70 | + <title>Create Next App</title> |
| 71 | + <link rel="icon" href="/favicon.ico" /> |
| 72 | + </Head> |
| 73 | + |
| 74 | + <main className={styles.main}> |
| 75 | + <h1 className={styles.title}> |
| 76 | + Welcome to{" "} |
| 77 | + <a href="https://github.com/manuarora700"> |
| 78 | + Razorpay Payments with Next.js |
| 79 | + </a> |
| 80 | + </h1> |
| 81 | + |
| 82 | + <div className={styles.makePayment}> |
| 83 | + <a onClick={displayRazorpay}> |
| 84 | + <h3>Make Payment →</h3> |
| 85 | + </a> |
| 86 | + </div> |
| 87 | + </main> |
| 88 | + </div> |
| 89 | + ); |
| 90 | +} |
| 91 | + |
| 92 | +``` |
| 93 | + |
| 94 | +```js:/api/razorpay.js |
| 95 | +const Razorpay = require("razorpay"); |
| 96 | +const shortid = require("shortid"); |
| 97 | + |
| 98 | +export default async function handler(req, res) { |
| 99 | + if (req.method === "POST") { |
| 100 | + // Initialize razorpay object |
| 101 | + const razorpay = new Razorpay({ |
| 102 | + key_id: process.env.RAZORPAY_KEY, |
| 103 | + key_secret: process.env.RAZORPAY_SECRET, |
| 104 | + }); |
| 105 | + |
| 106 | + // Create an order -> generate the OrderID -> Send it to the Front-end |
| 107 | + // Also, check the amount and currency on the backend (Security measure) |
| 108 | + |
| 109 | + const payment_capture = 1; |
| 110 | + const amount = 499; |
| 111 | + const currency = "INR"; |
| 112 | + const options = { |
| 113 | + amount: (amount * 100).toString(), |
| 114 | + currency, |
| 115 | + receipt: shortid.generate(), |
| 116 | + payment_capture, |
| 117 | + }; |
| 118 | + |
| 119 | + try { |
| 120 | + const response = await razorpay.orders.create(options); |
| 121 | + |
| 122 | + res.status(200).json({ |
| 123 | + id: response.id, |
| 124 | + currency: response.currency, |
| 125 | + amount: response.amount, |
| 126 | + }); |
| 127 | + } catch (err) { |
| 128 | + console.log(err); |
| 129 | + res.status(400).json(err); |
| 130 | + } |
| 131 | + } else { |
| 132 | + // Handle any other HTTP method |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +``` |
| 137 | + |
| 138 | +## Usage |
| 139 | + |
| 140 | +<Step number={1} title="Create an Account on Razorpay" /> |
| 141 | + |
| 142 | +- Head over to [Razorpay](https://razorpay.com) and create an account |
| 143 | + <Image src="/static/images/razorpay/1.png" width={1200} height={700} /> |
| 144 | +- Generate the [API keys](https://https://dashboard.razorpay.com/app/keys) which can be found in the `Test Mode` dashboard |
| 145 | + <Image src="/static/images/razorpay/2.png" width={1200} height={700} /> |
| 146 | +- Store the API keys in environment variables, example environment file is provided in the source code. |
| 147 | +- We use serverless functions (api routes in Next.js) which acts as our backend to create `order_id`. |
| 148 | + |
| 149 | + [Source Code](https://github.com/manuarora700/razorpay-payments/tree/master/) |
0 commit comments