How to Request Payments with Razorpay and Google Sheets

Introduction

Razorpay is a popular payment gateway that allows businesses to accept payments online. By integrating Razorpay with Google Sheets, you can automate payment requests, manage customer data, and send invoices directly from your sheet. In this tutorial, we will walk you through how to use Razorpay and Google Sheets to request payments efficiently.

Step 1: Set Up Your Razorpay Account

Before you can request payments through Razorpay, you need to have an active Razorpay account. If you don’t already have one, follow these steps to create an account:

  1. Go to the Razorpay website.
  2. Click on the Sign Up button and complete the registration process.
  3. After registering, log in to your Razorpay dashboard.
  4. Navigate to Settings > API Keys, and copy your Key ID and Key Secret. These will be used to authenticate API calls.

Step 2: Set Up Google Sheets

Next, set up your Google Sheets to store customer information and payment details. Create columns for customer names, email addresses, amounts to be paid, and payment statuses. Here's an example layout:

| Customer Name | Email               | Amount | Payment Status |
|---------------|---------------------|--------|----------------|
| John Doe      | [email protected]     | 100.00 | Pending        |
| Jane Smith    | [email protected]     | 250.00 | Pending        |
        

This simple structure will help you manage the payment requests effectively. The "Payment Status" column can be updated to reflect whether the payment has been successfully completed or is still pending.

Step 3: Set Up Google Apps Script to Integrate Razorpay

To automate payment requests from Google Sheets, you'll need to use Google Apps Script to interact with the Razorpay API. Here's how you can set it up:

  1. Open your Google Sheet.
  2. Go to Extensions > Apps Script to open the script editor.
  3. In the editor, paste the following script:
  4. function createPaymentLink(customerName, email, amount) {
        var razorpayKeyId = 'your_razorpay_key_id';  // Replace with your Razorpay Key ID
        var razorpayKeySecret = 'your_razorpay_key_secret';  // Replace with your Razorpay Key Secret
    
        var url = 'https://api.razorpay.com/v1/checkout/links';
        var payload = {
            "amount": amount * 100,  // Convert to paise (Razorpay works with paise)
            "currency": "INR",
            "description": "Payment for " + customerName,
            "email": email,
            "callback_url": "https://yourwebsite.com/callback"  // Optional: A URL to receive payment updates
        };
    
        var options = {
            "method": "post",
            "headers": {
                "Authorization": "Basic " + Utilities.base64Encode(razorpayKeyId + ':' + razorpayKeySecret)
            },
            "payload": JSON.stringify(payload)
        };
    
        var response = UrlFetchApp.fetch(url, options);
        var jsonResponse = JSON.parse(response.getContentText());
    
        // Extract the payment link from the response
        return jsonResponse.short_url;  // This is the URL where the customer can make the payment
    }
    
    function sendPaymentRequest() {
        var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');  // Adjust sheet name as needed
        var data = sheet.getRange(2, 1, sheet.getLastRow() - 1, 4).getValues();  // Get customer data from the sheet
    
        data.forEach(function(row, index) {
            var customerName = row[0];
            var email = row[1];
            var amount = row[2];
            
            // Generate payment link for the customer
            var paymentLink = createPaymentLink(customerName, email, amount);
    
            // Update the sheet with the payment link
            sheet.getRange(index + 2, 4).setValue(paymentLink);  // Update Payment Status with the link
            
            // Send an email with the payment link to the customer
            MailApp.sendEmail(email, "Payment Request", 
                "Hello " + customerName + ",\n\nPlease make your payment using the following link: " + paymentLink);
        });
    }
                
  5. Replace `'your_razorpay_key_id'` and `'your_razorpay_key_secret'` with the actual credentials from your Razorpay account.
  6. Click Save in the script editor.

Step 4: Run the Script to Generate Payment Links

Once the script is saved, you can run it to generate payment links and send them to your customers:

  1. In the Apps Script editor, click on the play button (▶️) to run the sendPaymentRequest function.
  2. The script will automatically generate a payment link for each customer in your sheet and send an email with the link.
  3. The generated payment link will also be updated in the "Payment Status" column in your Google Sheet.

Step 5: Handling Payment Updates (Optional)

If you wish to track the payment status of each customer automatically, you can set up a webhook on your server to receive updates from Razorpay. When a payment is completed, Razorpay will send a notification to your callback URL, and you can update the "Payment Status" column in Google Sheets accordingly.

For detailed information on webhooks, refer to the Razorpay Webhooks Documentation.

Step 6: Automate the Process with Triggers

If you want the payment request process to run automatically at certain intervals (e.g., daily, weekly), you can set up a trigger in Google Apps Script:

  1. In the Apps Script editor, go to Triggers > Add Trigger.
  2. Choose the sendPaymentRequest function, and set the trigger type to run based on time (e.g., every day, week, etc.).
  3. Once the trigger is set, the script will run automatically at the specified time and send payment links to customers on your list.

Conclusion

By integrating Razorpay with Google Sheets, you can streamline the payment request process and automate payment collection. The combination of Razorpay’s secure payment gateway and Google Sheets’ easy-to-use interface allows you to manage customer payments efficiently and professionally. Whether you're invoicing clients, collecting subscription fees, or requesting one-time payments, this setup simplifies the process and saves you time.