Friday, 27 August 2021

How to Request Payments with Razorpay and Google Sheets

Razorpay is a popular payment gateway in India that allows you to accept online payments from customers anywhere in the world. Your customers can pay with credit cards, debit cards, Google Pay, Walmart’s PhonePe and other UPI apps.

Google Sheets + Razorpay

Razorpay, similar to Stripe, offers a simple no-code tool for generating payment links that you can share with customers over SMS, WhatsApp, or email. When a customer clicks on the link, they are redirected to a secure checkout page hosted on Razorpay where they can can make the payment using their preferred payment method.

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

It takes one easy step to generate payment links with Razorpay. Sign-in to your Razorpay account, go to the Payment Links section and click on the Create Payment Link button.

The built-in wizard is perfect for generating a few links but if you are however looking to generate payment links in bulk for multiple products and varying amounts, Google Sheets can help.

Here’s a sample demo:

Razorpay Google Sheets

To get started, open your Razorpay dashboard, go to Settings > API Keys > Generate Key to generate the Key Id and Key Secret for your account.

Next, make a copy of the Razorpay sheet in your Google Drive. Go to Tools > Script Editor and replace the Key Id and Key Secret with the ones generated in the previous step. Then, click on the Run menu to authorize the script with your Google Account.

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

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.

Razorpay Google Sheets

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

You may also use Document Studio to create PDF invoices and embed the payment links directly in the invoice. Please watch this video tutorial to learn more.

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.

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;
};


from Digital Inspiration https://ift.tt/3yolScH

Thursday, 26 August 2021

How to Share Files in Google Drive with Multiple Users

The Google Drive API makes it easy to share files and folders with other users programmatically with the help of Apps Script.

For instance, here’s a snippet of code that will let you share the file with another Google Account user and provide them edit access to the file. Replace the role from writer to reader to give them read-only access.

const shareFilesInGoogleDrive = (fileOrFolderId, emailAddress) => {
  Drive.Permissions.insert(
    {
      role: "writer", // or "reader" or "commenter"
      value: emailAddress,
      type: "user",
    },
    fileOrFolderId,
    {
      supportsAllDrives: true,
      sendNotificationEmails: true,
    }
  );
};

It is recommended that you set the sendNotifications flag to true as it will send an email notification when the file is shared with a user who may not have a Google account.

Share Files with Multiple Users

A limitation of the Drive API is that you can only share files with one user at a time. Google Apps Script is synchronous - it doesn’t support the async/await pattern of JavaScript Promises and you therefore cannot run the code in parallel.

There’s however a simple workaround to help you share a file or folder in Google Drive with multiple users in one go in parallel using the UrlFetchApp service.

const shareGoogleDriveFileWithMultipleUsers = () => {
  const fileId = "<Drive File Id>";
  const editors = ["angus@gmail.com", "kiran@school.edu", "jacob@corp.com"];

  const API = "https://www.googleapis.com/drive/v3/files";
  const queryString = "supportsAllDrives=true&sendNotifications=true";
  const accessToken = ScriptApp.getOAuthToken();

  const requests = editors.map((emailAddress) => ({
    url: `${API}/${fileId}/permissions?${queryString}`,
    method: "POST",
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${accessToken}`,
    },
    muteHttpExceptions: true,
    payload: JSON.stringify({
      role: "writer",
      type: "user",
      emailAddress: emailAddress,
    }),
  }));

  UrlFetchApp.fetchAll(requests);
};

In the snippet above, we are directly invoking the Google Drive API (v3) instead of the DriveApp service of App Script. The fetchAll allows you make multiple HTTP requests in a single request and returns an array of responses.

Please ensure that the following scopes are added in your appsscript.json file:

  {
    ...
    "oauthScopes": [
      "https://www.googleapis.com/auth/script.external_request",
      "https://www.googleapis.com/auth/drive",
    ],
   ...
  }


from Digital Inspiration https://ift.tt/3kqjmh4

Tuesday, 24 August 2021

Useful npm Tips and Tricks that Developers Should Know

Node Package Manager, or npm, is a tool to install and manage JavaScript packages in your project. And if you have Node installed on your computer, you have already have npm as well.

NPM Tips and Tricks

npm Commands You Should Know

This is not a tutorial for learning npm, the official docs are good place to get started, but a collection of tips and tricks that will help you do more with the npm utility.

Let’s jump right into the list of useful commands:

Instantly run packages without installing

The NPM registry is a treasure trove for finding packages that do useful stuff and aren’t just for programmers.

For instance, the speed-test package shows the speed of your internet connection. The emoj packages helps you search for emojis from the terminal. And wifi-passwords is a simple way to know the password of your current WiFi network.

You can run these utility packages directly from the command line without installing them using the npx command.

npx speed-test
npx emoj unicorn
npx public-ip-cli
npx wifi-password-cli

Install npm packages faster

You’ve probably used npm install to install packages, and dependencies, in the local node_modules folder of a project. Replace this command with npm-ci and you’ll be able to install packages significantly faster.

npm ci

If a node_modules folder is already present, it will be automatically removed before npm ci begins to install packages.

Recover space

If you have been working with npm packages for some time, the various node_modules folders on the disks could be consuming several gigabytes of space. The very useful npkill finds all node_modules folders on your system and lets you delete them interactively.

npx npkill

Quickly download a Git repository

Most developers use the git clone command to download a Git repository. However, this also downloads the entire git history making the process slower. The degit package can download the latest commit to the master branch locally and you need not specify the full Github URL.

npx degit username/repo
npx degit labnol/apps-script-starter

List installed packages

Generate a list of all npm packages that are installed on the system with global scope. Remove the -g flag to list only packages installed in the current project directory.

npm ls --depth=0
npm ls -g

Find unused dependencies

The depcheck command will list all the npm packages that are not used in the project based on the dependencies in package.json.

npx depcheck

Use the command npm uninstall <package-name> to uninstall any unused package.

Find outdated dependencies

Get a list of all outdated packages in your current project. This command checks every single module listed in the package.json file and compares it with the latest version available in the NPM registry.

Add the -g flag to get all outdated packages that are installed globally on the system.

npm outdated
npm outdated -g

Update the package versions

The ncu command will update the package.json file with the latest version of the packages listed in the dependencies and devDependencies sections.

Or use the npm-check -u command to update packages to their latest version in interactive mode.

npm-check
npm-check -u
ncu -u

Remove extra packages

Use the prune command to remove all packages that are installed locally but not listed in the package.json file. If the —dry-run flag is used then no changes will actually be made.

npm prune

Alternatively, you can remove the node_modules folder and run npm ci again.

Find vulnerable packages

Run the audit command to check for vulnerabilities in the packages listed in the dependencies and devDependencies sections. Add the fix flag to automatically apply the fixes, if any.

npm audit
npm audit fix

Useful NPM Package Websites

  • bundlephobia.com - Upload your package.json file and get an idea of how much it would cost (size-wise) to install the dependencies.
  • diff.intrinsic.com - Compare any two versions of a npm package and know which files have changed in the update.
  • npmtrends.com - Compare the relative popularity of packages across the npm registry based on the number of downloads.


from Digital Inspiration https://ift.tt/3Bc47iD

Monday, 23 August 2021

Essential Date Functions for Google Sheets

Google Sheets Date Functions

Dates are internally stored as sequential serial numbers in Google Sheets. This serial number represents the number of days elapsed since December 31, 1899.

You can use the DATEVALUE function to convert any date input to a number that represents the date. For instance, both the functions DATEVALUE("Jan 1") and DATEVALUE("01-Jan-2021") return the same number (44197) though the inputs have vastly different formats.

The function TODAY() returns the current date while the function NOW() returns the current date and time. Both these functions do not require any arguments and they update when any cell in the Google Sheet is changed.

The function NOW() + 2 returns the current date and time plus two days while NOW() - 9/24 returns the date and time 9 hours ago since 1 = 24 hours.

The functions YEAR(), MONTH() and DAY() can be used extract the year, month and day of the date that is passed as an argument.

The DAYS() function calculates the number of days between two dates. Internally, it calculates the DATEVALUE of the first date and the DATEVALUE of the second date and subtracts the two numbers.

If you want to calculate the number of months between two dates, you can use the DATEDIF() function with the third argument set to M. For instance, the function =DATEDIF("Jan 1, 1951", TODAY(), "M") returns the number of months between January 1951 and today.

The YEARFRAC() function calculates the number of years that has passed between two dates.

Tip: You may use these date functions in Google Sheets with Array Formulas to schedule emails with Gmail Mail Merge.

Use the EDATE() function to calculate a date that is a specified number of months before or after a specified date. For instance, EDATE(TODAY(), -1) returns the date that is one month before the current date.

The EOMONTH() function helps you calculate the last day of the given month. For instance, EOMONTH(TODAY(), -1) returns the last day of the previous month. Add 1 to the result, =EOMONTH(TODAY(),-1)+1, and you’ll get the first day of the current month.

The WEEKDAY() function returns the day of the week corresponding to a date with Sunday representing 1, the first day of the week. Set the second argument to 2 and days of the week will be numbered starting with Monday.

The WORKDAY() function calculates the date that is a specified number of days before or after a specified date, excluding weekends. For instance, WORKDAY(TODAY(), -7) returns the date that is 7 working days before the current date.

Likewise, the NETWORKDAYS() function calculates the number of working days between two dates provided as arguments. Combine this with EOMONTH to calculate the number of working days that are left till the end of the the current month =NETWORKDAYS(TODAY(), EOMONTH(TODAY(),0))

Google Sheets Date Formulas for Common Scenarios

Task Working Formula
Add number of days to a date =A1 + 5
Get a day that is 6 months prior to a date =EDATE(A1, -5)
Add number of years to a date =DATE(YEAR(A1) + 5, MONTH(A1), DAY(A1))
Difference in days between two dates =DAYS(A1, A2)
Total working days between two dates =NETWORKDAYS(A1, A2)
Get a date that is 10 working days from now =WORKDAY(TODAY(), 10)
Get the total number of months between two dates =DATEIF(A1, A2, "M")
Get the difference in years between two dates =DATEIF(A1, A2, "Y")
Get the number of days in the current month =EOMONTH(TODAY(), 0) - (EOMONTH(TODAY(), -1) + 1)
Print the day of the week =TEXT(TODAY(), "ddddd")
Calculate the age in years =ROUNDDOWN(YEARFRAC(A1, TODAY(), 1))
Days until your next birthday =DAYS(DATE(YEAR(A1)+DATEDIF(A1,TODAY(),"Y")+1, MONTH(A1),DAY(A1), TODAY())
Months and days between two dates =DATEDIF(A1,A2,"YM")&" months, "&DATEDIF(A1,A2,"MD")&" days"

You can copy this Google Sheet to get all the working formulas mentioned in this tutorial.



from Digital Inspiration https://ift.tt/3zeEexO

Saturday, 14 August 2021

How to Create a Telegram Bot for Sending Notifications using Google Apps Script

Would you like to receive notifications in your Telegram messenger when a new form response is submitted in Google Forms. Or maybe send a…

from Digital Inspiration https://ift.tt/3xNeoj8