如何将 Stripe 支付与 Google Apps 脚本集成

Mow*_*zer 2 javascript google-apps-script stripe-payments

根据这个答案,Gmail 没有公开用于发送和接收付款的 API。因此,我正在尝试使用 Stripe来实现这一点。

代码.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',
  });
})();
Run Code Online (Sandbox Code Playgroud)

但是,GAS 目前并不直接支持asyncrequire。是否有任何可能的解决方法,以便我可以使用 Stripe 在我的 GAS 应用程序中发送和接收付款?

如果那不可能,我应该从这里往哪个方向走?

Tan*_*ike 6

这个答案怎么样?请将此视为几个答案之一。

问题和解决方法:

不幸的是,Node.js 的模块不能直接用于 Google Apps Script。因此需要为 Google Apps Script 准备脚本。幸运的是,在您问题中链接的官方文档中,有几个示例。使用这个,如何转换为Google Apps Script的脚本?

示例脚本:

当您的问题中的脚本转换为 Google Apps 脚本时,它变成如下。

从:

// 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',
  });
})();
Run Code Online (Sandbox Code Playgroud)

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())
}
Run Code Online (Sandbox Code Playgroud)
  • 在这种情况下,Node.js 和 Google Apps Script 的请求是相同的。

笔记:

  • 在 Node.js 的示例脚本中,sk_test_4eC39HqLyjWDarjtT1zdp7dc用于密钥。但在这种情况下,因为使用的是基本授权,所以请sk_test_4eC39HqLyjWDarjtT1zdp7dc:添加:.

参考:

如果我误解了您的问题并且这不是您想要的方向,我深表歉意。