Experimental Code / Get Payment Link via Stripe

Hi all!

I’m trying to create a button with a stripe payment link generated based on the selection of the user. I managed to do with with a google apps script, but it is not fast enough (script + refresh in the app), so I’m trying with experimental code.
I tested the function in javascript and it works. However, when I add it to the window.function, it doesn’t work anymore. I welcome any help.
The code seems to stops where I declare const stripe = require(‘stripe’)…

You can find below the code where the bug occurs (using experimental code), and also the code that works (testing in the same repl).

Thank you so much in advance for any insights!

Code where error occurs:

// The function here takes the parameters that you
// have declared in the `glide.json` file, in the
// same order.

// npm install stripe --save

window.function = async function(priceID, qty) {
  // For each parameter, its `.value` contains
  // either its value in the type you've declared,
  // or it's `undefined`.  This is a good place to
  // extract the `.value`s and assign default
  // values.
  priceID = priceID.value ?? "price_1LrkJsArd3XJGhJHbjjhGCKd";
  qty = qty.value ?? 1;

 const stripe = require('stripe')('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');

  /* const paymentLink = await stripe.paymentLinks.create({
      line_items: [{ price: priceID, quantity: qty }],*/
  // return paymentLink.url;

  // Rest of code here
    return 'test';

  // Your function should return the exact type
  // you've declared for the `result` in
  // `glide.json`, or `undefined` if there's an
  // error or no result can be produced, because a
  // required input is `undefined`, for example.

Code that works

// npm install stripe --save

async function start() {
  let result;
  let error;
  try {
    //result = await window.function(...params);
    const stripe = require('stripe')('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');
    const paymentLink = await stripe.paymentLinks.create({
      line_items: [{ price: 'price_1LrkJsArd3XJGhJHbjjhGCKd', quantity: 2 }],
    result = paymentLink.url;
    return result;
  } catch (e) {
    result = undefined;
    try {
      error = e.toString();
    } catch (e) {
      error = "Exception can't be stringified.";


The below is probably worth reading through. In short, require works differently in node.js than it does in common JavaScript. It appears you need to use a slightly different method for loading node modules.

1 Like

Thank you very much Darren, I will look into it!