【问题标题】:How to integrate Stripe payments with Google Apps Script如何将 Stripe 支付与 Google Apps 脚本集成
【发布时间】:2020-02-26 07:43:04
【问题描述】:

According to this answer,Gmail 不公开用于发送和接收付款的 API。因此,I am trying to use 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',
  });
})();

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

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

【问题讨论】:

    标签: javascript google-apps-script stripe-payments


    【解决方案1】:

    这个答案怎么样?请认为这只是几个答案之一。

    问题和解决方法:

    很遗憾,Node.js 的模块不能直接用于 Google Apps Script。因此需要为 Google Apps Script 准备脚本。幸运的是,在您的问题中,the official document of the link 有几个示例。使用这个,转换成 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',
      });
    })();
    

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

    注意:

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

    参考资料:

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

    【讨论】:

    • @Mowzer 感谢您回复和测试脚本。很高兴您的问题得到解决。
    • 您对这个非常相似的问题有何看法? stackoverflow.com/q/63013143/1640892
    • @Let Me Tink About It 我检查了它并提出了修改后的脚本。你能确认一下吗?
    • 在这个脚本中拥有你的 API 密钥(例如可用于退还所有付款或向客户收费)对我来说一点都不安全——这应该是安全的存储在后端服务器上。
    猜你喜欢
    • 2018-12-17
    • 2021-06-13
    • 2017-04-30
    • 2013-05-08
    • 1970-01-01
    • 2012-10-12
    • 2017-10-31
    • 1970-01-01
    • 2020-10-26
    相关资源
    最近更新 更多