gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 15:22:26 24 4
gpt4 key购买 nike

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/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com