How to Request Payments with Stripe Checkout and Google Sheets

Hey, friends today I will teach you How to Request Payments with Stripe Checkout 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 Stripe Checkout and Google Sheets.

Stripe Payment joins to make it simple for you to acknowledge charge card Payment from clients anyplace on the planet without having a site. You can utilize the Stripe dashboard to create instalment connections and afterwards send the connections over email, WhatsApp, SMS, or offer them on your online media pages.

A limit of Stripe Payment joins is that you can create them physically. Stripe has a component rich API yet it doesn’t permit you to produce instalment connects consequently.

You might also like our trending code snippets

If you want to generate custom payment links for Stripe in bulk and want to send them to your customers, then you can consider using the Stripe Checkout method is the best for that purpose. These are payment forms hosted on the Stripe website which will allow you to collect only payments from your customers.

It is important to note that Stripe Checkout sessions will automatically expire after 24 hours. As an alternative, you can use the Stripe API to generate invoices and email the invoice link to your customers.

Stripe API Key

if you want to start first of all you need to, open your Stripe dashboard, go to Developers > API Keys > Created restricted API key.

Give your key a descriptive name, choose the Write permission under Checkout Sessions and click Create key.

The next step is to make a copy of the Stripe Google Sheet in your Google Drive. for that you have to Go to Tools > Script Editor and replace them Stripe API Key with the key generated in the previous step. and now click on the Run menu once to authorize the script with your Google Account.

Now you have to Switch to the Google Sheet and so that you can now use the custom Google Sheets function STRIPE() to generate Stripe Checkout sessions for accepting online payments.

If youwant to like to generate payment links for multiple rows in the Google Sheet then you just have to write the formula in the first row and simply drag the crosshairs to the other rows as I have shown in the demo below. Array Formulas are not supported yet.

Stripe Payment Links

How Stripe Checkout Works with Google Sheets

If you want to know how the integration of Google Sheets and Stripe works, here I came with the answer which is Google Apps Script. The underlying code invokes the Stripe API with your secret API key and writes the generated checkout session 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. The code can be extended to accept recurring payments for subscriptions.

/**
 *
 *  Author  Amit Agarwal
 *  Email   amit@labnol.org
 *  Web     https://digitalinspiration.com/
 *
 **/

const STRIPE_API_KEY = "<< Stripe API Key >>";
const STRIPE_SUCCESS_URL = "https://digitalinspiration.com";
const STRIPE_CANCEL_URL = "https://digitalinspiration.com";

/**
 * Generate Stripe payment links in Google Sheets
 *
 * @param {number} amount The amount to be paid using Stripe
 * @param {string} currency The 3-letter currency code (optional)
 * @param {string} description A short description of the item name (optional)
 * @return Stripe checkout session link
 * @customfunction
 */

const STRIPE = (amount, currency, description) => {
  const input = {
    "line_items[0][price_data][currency]": currency || "USD",
    "line_items[0][price_data][product_data][name]": description || "Name",
    "line_items[0][price_data][unit_amount]": Math.ceil(amount * 100),
    "line_items[0][quantity]": 1,
  };

  const cacheKey = JSON.stringify(input);

  const cachedLink = CacheService.getScriptCache().get(cacheKey);

  if (cachedLink) return cachedLink;

  const params = {
    cancel_url: STRIPE_CANCEL_URL,
    success_url: STRIPE_SUCCESS_URL,
    mode: "payment",
    billing_address_collection: "required",
    "payment_method_types[]": "card",
    ...input,
  };

  const payload = Object.entries(params)
    .map(([key, value]) =>
      [encodeURIComponent(key), encodeURIComponent(value)].join("=")
    )
    .join("&");

  const response = UrlFetchApp.fetch(
    "https://api.stripe.com/v1/checkout/sessions",
    {
      method: "POST",
      headers: {
        Authorization: `Bearer ${STRIPE_API_KEY}`,
        "Content-Type": "application/x-www-form-urlencoded",
      },
      payload,
      muteHttpExceptions: true,
    }
  );

  const { url, error } = JSON.parse(response);

  if (url) {
    CacheService.getScriptCache().put(cacheKey, url, 21600);
  }

  return error ? error.message : url;
};

You can use Mail Merge with Gmail to request online payments from your customers over email. You may also use Document Studio to create PDF invoices and embed the payment links directly in the customer’s invoice.

Leave a Comment