将Stripe API语法转换为Google Apps脚本 [英] Converting Stripe API syntax to Google Apps Script
问题描述
此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())
}
- 我认为要以
"payment_method_types[0]": "card"
和"payment_method_types[1]": "ideal"
的形式发送点可能是payment_method_types: ["card", "ideal"]
. - I think that point might be
payment_method_types: ["card", "ideal"]
is required to be sent as"payment_method_types[0]": "card"
and"payment_method_types[1]": "ideal"
. - Create a Session of official document
- How to integrate Stripe payments with Google Apps Script
这篇关于将Stripe API语法转换为Google Apps脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!