google-apps-script - 将 Stripe API 语法转换为 Google Apps 脚本

标签 google-apps-script http-status-code-400 urlfetch stripe-payments

This SO answer正确解释自 require Google Apps Script 不支持 Node/JS 库,必须进行以下代码更改才能使 Stripe 在 GAS 项目中正常工作:

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 Script 友好版本。
来自 https://stripe.com/docs/payments/checkout/accept-a-payment#create-checkout-session
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',
});
所以,我正在尝试以下操作。
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());
}
但是,我收到以下错误,而不是返回预期的响应对象:

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",
  },
}
我究竟做错了什么?我如何概括第一个例子?我需要但我没有看到的具体文件是什么?
编辑:
根据评论中的建议对有效负载进行字符串化后,现在出现以下错误:

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"
  }
}

最佳答案

来自 this questionthis sample script ,我认为在这种情况下,需要将值作为表单数据发送。那么下面的修改呢?
修改后的脚本:

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())
}
  • 我认为这一点可能是 payment_method_types: ["card", "ideal"]需要发送为 "payment_method_types[0]": "card""payment_method_types[1]": "ideal" .

  • 引用:
  • Create a Session of official document
  • How to integrate Stripe payments with Google Apps Script
  • 关于google-apps-script - 将 Stripe API 语法转换为 Google Apps 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63013143/

    相关文章:

    google-apps-script - 在多个 Google 表格之间共享应用程序脚本 Web 应用程序

    google-apps-script - 您如何使用 Google Apps 脚本以编程方式更新电子邮件休假回复?

    python - 此 Google Elevation API URL 有什么问题?

    spring-mvc - 如何在 Spring MVC 中自定义 @RequestParam 错误 400 响应

    java - 适用于 Java 的云存储 API 客户端库 : - 400 Bad Request : "error" : "unauthorized_client"

    python - 在 Google App Engine 中使用 urllib2 会抛出 "Deadline exceeded while waiting for HTTP response from URL:..."

    google-apps-script - 在谷歌脚本中测试背景颜色

    javascript - 如何在 App 脚本中比较 2 个数组

    google-app-engine - 使用 java.net.URLConnection 的 Clojure http 请求?

    python - 一个非常简单的多线程并行 URL 获取(无队列)