将Stripe API语法转换为Google Apps脚本 [英] Converting Stripe API syntax to Google Apps Script

查看:126
本文介绍了将Stripe API语法转换为Google Apps脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此SO答案正确解释了由于Google Apps脚本不支持require Node/JS库,必须进行以下代码更改才能使Stripe在GAS项目中正常工作:

This SO answer correctly explains that since the require Node/JS library is not supported by Google Apps Script, the following code changes must be made to get Stripe to work properly in a GAS project:

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())
}

现在,我想将以下代码转换为Google Apps脚本友好版本.

Now, I want to convert the following code into the Google Apps Script friendly version.

const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc');

const session = await stripe.checkout.sessions.create({
  payment_method_types: ['card', 'ideal'],
  line_items: [{
    price_data: {
      currency: 'eur',
      product_data: {
        name: 'T-shirt',
      },
      unit_amount: 2000,
    },
    quantity: 1,
  }],
  mode: 'payment',
  success_url: 'https://example.com/success?session_id={CHECKOUT_SESSION_ID}',
  cancel_url: 'https://example.com/cancel',
});

所以,我正在尝试以下方法.

So, I'm trying the following.

function myFunction() {
  var url = "https://api.stripe.com/v1/checkout/sessions";
  var params = {
    method: "post",
    headers: {
      Authorization:
        "Basic " + Utilities.base64Encode("sk_test_4eC39HqLyjWDarjtT1zdp7dc:"),
    },
    payload: {
      payment_method_types: ["card", "ideal"],
      line_items: [
        {
          price_data: {
            currency: "eur",
            product_data: {
              name: "T-shirt",
            },
            unit_amount: 2000,
          },
          quantity: 1,
        },
      ],
      mode: "payment",
      success_url:
        "https://example.com/success?session_id={CHECKOUT_SESSION_ID}",
      cancel_url: "https://example.com/cancel",
    },
  };
  var res = UrlFetchApp.fetch(url, params);
  Logger.log(res.getContentText());
}

但是,我没有得到以下预期的错误,而是得到了以下错误:

However, instead of getting the expected response object returned, I get the following error:

例外: https://api.stripe.com 的请求失败,返回了代码400.服务器响应被截断:

Exception: Request failed for https://api.stripe.com returned code 400. Truncated server response:

对数错误

{
  error: {
    message: "Invalid array",
    param: "line_items",
    type: "invalid_request_error",
  },
}

我做错了什么?我该如何概括第一个示例?我不需要的具体文件是什么?

What am I doing wrong? And how can I generalize the first example? What is the specific documentation I need that I'm not seeing?

根据评论中的建议对有效负载进行了分类之后,现在出现以下错误:

After stringifying the payload per the suggestion from the comments, now I get the following error:

例外: https://api.stripe.com 的请求失败,返回了代码400.服务器响应被截断:

Exception: Request failed for https://api.stripe.com returned code 400. Truncated server response:

对数错误

{
  "error": {
    "code": "parameter_unknown",
    "doc_url": "https://stripe.com/docs/error-codes/parameter-unknown",
    "message": "Received unknown parameter: {\"payment_method_types\":. Did you mean payment_method_types?",
    "param": "{\"payment_method_types\":",
    "type": "invalid_request_error"
  }
}

推荐答案

来自此问题

From this question and this sample script, I thought that in this case, the values are required to be sent as the form data. So how about the following modification?

function myFunction() {
  var url = "https://httpbin.org/anything";
  var params = {
    method: "post",
    headers: {Authorization: "Basic " + Utilities.base64Encode("sk_test_4eC39HqLyjWDarjtT1zdp7dc:")},
    payload: {
      "cancel_url": "https://example.com/cancel",
      "line_items[0][price_data][currency]": "eur",
      "line_items[0][price_data][product_data][name]": "T-shirt",
      "line_items[0][price_data][unit_amount]": "2000",
      "line_items[0][quantity]": "1",
      "mode": "payment",
      "payment_method_types[0]": "card",
      "payment_method_types[1]": "ideal",
      "success_url": "https://example.com/success?session_id={CHECKOUT_SESSION_ID}"
    }
  };
  var res = UrlFetchApp.fetch(url, params);
  Logger.log(res.getContentText());  // or console.log(res.getContentText())
}

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆