4

According to this answer, Gmail does not expose an API for sending and receiving payments. Therefore, I am trying to use Stripe to accomplish that.

Code.js
// Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc');

(async () => {
  const product = await stripe.products.create({
    name: 'My SaaS Platform',
    type: 'service',
  });
})();

However, GAS does not directly support async and require at this time. Is there any possible workaround so I can use Stripe to send and receive payments in my GAS app?

If that's not possible, what direction should I go from here?

Let Me Tink About It
  • 15,156
  • 21
  • 98
  • 207

1 Answers1

8

How about this answer? Please think of this as just one of several answers.

Issue and workaround:

Unfortunately, the module of Node.js cannot be directly used for Google Apps Script. So it is required to prepare the script for Google Apps Script. Fortunately, at the official document of the link in your question, there are several samples. Using this, how about converting to the script of Google Apps Script?

Sample script:

When your script in your question is converted to Google Apps Script, it becomes as follows.

From:

// Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc');

(async () => {
  const product = await stripe.products.create({
    name: 'My SaaS Platform',
    type: 'service',
  });
})();

To

function myFunction() {
  var url = "https://api.stripe.com/v1/products";
  var params = {
    method: "post",
    headers: {Authorization: "Basic " + Utilities.base64Encode("sk_test_4eC39HqLyjWDarjtT1zdp7dc:")},
    payload: {name: "My SaaS Platform", type: "service"}
  };
  var res = UrlFetchApp.fetch(url, params);
  Logger.log(res.getContentText())
}
  • In this case, both requests of Node.js and Google Apps Script are the same.

Note:

  • At the sample script of Node.js, sk_test_4eC39HqLyjWDarjtT1zdp7dc is used for the key. But in this case, because the basic authorization is used, please use sk_test_4eC39HqLyjWDarjtT1zdp7dc: by adding :.

References:

If I misunderstood your question and this was not the direction you want, I apologize.

Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • 1
    @Mowzer Thank you for replying and testing the script. I'm glad your issue was resolved. – Tanaike Oct 31 '19 at 11:38
  • What are your thoughts on this very similar question? https://stackoverflow.com/q/63013143/1640892 – Let Me Tink About It Jul 21 '20 at 11:07
  • 1
    @Let Me Tink About It I checked it and proposed a modified script. Could you please confirm it? – Tanaike Jul 21 '20 at 22:27
  • It doesn't feel very safe to me at all to have your secret API key(that can be used to for example refund all your payments or charge your customers) in this script — that should be securely stored on a backend server. – karllekko Sep 25 '20 at 14:29