How to Request Payments with Razorpay and Google Sheets

Advertisements

Hey, friends today I will teach you How to Request Payments with Razorpay and Google Sheets. so let get started with today Code snippets. Getting different problems is altogether gives a very different experience. today the Code snippets I am going to share with you is How to Request Payments with Razorpay and Google Sheets.

How to Request Payments with Razorpay and Google Sheets

Razorpay is a well-known payment gateway in India that allows you to accept online payments from customers all over the world. Credit cards, debit cards, Google Pay, Walmart’s PhonePe, and other UPI apps are all accepted by your customers.

You might also like our trending code snippets

Advertisements

Razorpay, like Stripe, provides a simple no-code tool for creating payment links that you can send to customers via SMS, WhatsApp, or email. When a customer clicks on the link, they are taken to a secure checkout page hosted by Razorpay, where they can pay using their preferred payment method.

Here’s a sample payment link generated with Razorpay – https://rzp.io/i/6uBBFWBfv

Creating payment links with Razorpay is as simple as one step. Sign into your Razorpay account, navigate to the Payment Links section, and then click the Create Payment Link button.

Advertisements

The built-in wizard is great for creating a few links, but if you need to create payment links in bulk for multiple products and amounts, Google Sheets can help.

Here’s an example:

Razorpay Google Sheets

To begin, open your Razorpay dashboard and navigate to Settings > API Keys > Generate Key to generate your account’s Key Id and Key Secret.

Advertisements

Create a copy of the Razorpay sheet in Google Drive. Replace the Key Id and Key Secret with the ones generated in the previous step by going to Tools > Script Editor. Then, from the File menu, select Run to authorise the script with your Google Account.

Switch to Google Sheets and you can now generate dynamic payment links using the custom Google Sheets function RAZORPAY().

If you would like to generate payment links for multiple rows in the Google Sheet, just write the formula in the first row and drag the crosshairs to the other rows as show in the demo below. Array Formulas are not supported yet.

Advertisements
Razorpay Google Sheets

Mail Merge with Gmail can be used to request payments from customers via email. If the column title in Google Sheets is Payment Link, simply put Payment Link in the email template, and these will be replaced with the actual Razorpay payment links customised for each customer.

You can also use Document Studio to create PDF invoices with payment links embedded directly in the invoice. To learn more, please watch this video tutorial.

How Razorpay Works with Google Sheets

If you are curious to know-how integration of Google Sheets and Razorpay works, the answer is Google Apps Script. The underlying code invokes the Razorpay API with your credentials and writes the generated payment links in the Google Sheet.

Advertisements

The custom Google Sheets function uses the built-in caching service of Apps Script to reduce latency and improve performance.

const RAZORPAY_KEY_ID = "<<Your Razorpay Key Id>>";
const RAZORPAY_KEY_SECRET = "<<Your Razorpay Key Secret>>";

/**
 * Generate payment links for Razorpay in Google Sheets
 *
 * @param {number} amount The amount to be paid using Razorpay
 * @param {string} currency The 3-letter currency code (optional)
 * @param {string} description A short description of the payment request (optional)
 * @return Razorpay Payment Link
 * @customfunction
 */

const RAZORPAY = (amount, currency, description) => {
  const payload = JSON.stringify({
    amount: amount * 100,
    currency,
    description,
  });

  // Use caching to improve performance
  const cachedLink = CacheService.getScriptCache().get(payload);

  if (cachedLink) return cachedLink;

  // Generate the Authorization header token
  const base64token = Utilities.base64Encode(
    `${RAZORPAY_KEY_ID}:${RAZORPAY_KEY_SECRET}`
  );

  // Invoke the Razorpay Payment Links API
  const response = UrlFetchApp.fetch(
    "https://api.razorpay.com/v1/payment_links/",
    {
      method: "POST",
      headers: {
        Authorization: `Basic ${base64token}`,
        "Content-Type": "application/json",
      },
      muteHttpExceptions: true,
      payload: payload,
    }
  );

  // The short_url contains the unique payment link
  const { short_url = "" } = JSON.parse(response);

  // Store the generated payment link in the cache for 6 hours
  CacheService.getScriptCache().put(payload, short_url, 21600);

  return short_url;
};

Leave a Comment