Google apps script 将条带API语法转换为Google应用程序脚本
正确解释了由于Google Apps脚本不支持Google apps script 将条带API语法转换为Google应用程序脚本,google-apps-script,stripe-payments,http-status-code-400,urlfetch,stripe-api,Google Apps Script,Stripe Payments,Http Status Code 400,Urlfetch,Stripe Api,正确解释了由于Google Apps脚本不支持requireNode/JS库,因此必须进行以下代码更改,以使Stripe在GAS项目中正常工作: 从…起 到 现在,我想将以下代码转换为谷歌应用程序脚本友好版本 从…起 因此,我尝试以下方法 到 但是,我没有返回预期的响应对象,而是得到以下错误: 异常:请求返回的代码400失败。截断的服务器响应: 日志错误 我做错了什么?我怎样才能概括第一个例子呢?我需要的具体文档中有哪些是我没有看到的 编辑: 根据评论中的建议对有效负载进行字符串化后,现在我得到
require
Node/JS库,因此必须进行以下代码更改,以使Stripe在GAS项目中正常工作:
从…起
到
现在,我想将以下代码转换为谷歌应用程序脚本友好版本
从…起
因此,我尝试以下方法
到
但是,我没有返回预期的响应对象,而是得到以下错误:
异常:请求返回的代码400失败。截断的服务器响应:
日志错误
我做错了什么?我怎样才能概括第一个例子呢?我需要的具体文档中有哪些是我没有看到的
编辑:
根据评论中的建议对有效负载进行字符串化后,现在我得到以下错误:
异常:请求返回的代码400失败。截断的服务器响应:
日志错误
在和中,我认为在本例中,需要将值作为表单数据发送。那么下面的修改呢
修改脚本:
- 我认为这一点可能是
需要作为支付方法类型:[“卡”,“理想”]
和支付方法类型[0]:“卡”
发送支付方法类型[1]:“理想”
// [ BEGIN ] utilities library
/**
* Returns encoded form suitable for HTTP POST: x-www-urlformencoded
* @see code: https://gist.github.com/lastguest/1fd181a9c9db0550a847
* @see context: https://stackoverflow.com/a/63024022
* @param { Object } element a data object needing to be encoded
* @param { String } key not necessary for initial call, but used for recursive call
* @param { Object } result recursively populated return object
* @returns { Object } a stringified object
* @example
* `{
"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}"
}`
*/
const json2urlEncoded = ( element, key, result={}, ) => {
const OBJECT = 'object';
const typeOfElement = typeof( element );
if( typeOfElement === OBJECT ){
for ( const index in element ) {
const elementParam = element[ index ];
const keyParam = key ? `${ key }[${ index }]` : index;
json2urlEncoded( elementParam, keyParam, result, );
}
} else {
result[ key ] = element.toString();
}
return result;
}
// // test
// const json2urlEncoded_test = () => {
// const data = {
// time : +new Date,
// users : [
// { id: 100 , name: 'Alice' , } ,
// { id: 200 , name: 'Bob' , } ,
// { id: 300 , name: 'Charlie' , } ,
// ],
// };
// const test = json2urlEncoded( data, );
// // Logger.log( 'test\n%s', test, );
// return test;
// // Output:
// // users[0][id]=100&users[0][name]=Stefano&users[1][id]=200&users[1][name]=Lucia&users[2][id]=300&users[2][name]=Franco&time=1405014230183
// }
// // quokka
// const test = json2urlEncoded_test();
// const typeOfTest = typeof test;
// typeOfTest
// test
// [ END ] utilities library
将payloadLink链接到您参考的文档的官方页面to@TheMaster:当我对有效负载进行字符串化时,会出现不同的错误-如编辑中所述。这一错误表明,严格化造成了问题。该错误显示在日志和脚本编辑器的弹出窗口中,因此没有指向URL的链接。另外,在第一个示例中,没有必要对有效负载进行字符串化,因为第一个示例通过从服务器返回请求的对象来产生预期的行为。您可以通过简单的复制和粘贴在脚本编辑器中进行测试。不需要特殊设置。API密钥是用于测试目的的公共密钥。我指的是您获得的页面示例。条带文件-api@TheMaster字体哦,对不起,这是第二个。我还对问题进行了编辑,将其包括在内。我会找另一个,再加上。谢谢+1.你知道有哪种GAS库可以将一个常规的JSON对象转换成这种格式吗?@让我来尝试一下,谢谢你的回复。我很高兴你的问题解决了。不幸的是,我不知道这件事。对此我很抱歉。
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())
}
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());
}
{
error: {
message: "Invalid array",
param: "line_items",
type: "invalid_request_error",
},
}
{
"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"
}
}
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())
}
// [ BEGIN ] utilities library
/**
* Returns encoded form suitable for HTTP POST: x-www-urlformencoded
* @see code: https://gist.github.com/lastguest/1fd181a9c9db0550a847
* @see context: https://stackoverflow.com/a/63024022
* @param { Object } element a data object needing to be encoded
* @param { String } key not necessary for initial call, but used for recursive call
* @param { Object } result recursively populated return object
* @returns { Object } a stringified object
* @example
* `{
"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}"
}`
*/
const json2urlEncoded = ( element, key, result={}, ) => {
const OBJECT = 'object';
const typeOfElement = typeof( element );
if( typeOfElement === OBJECT ){
for ( const index in element ) {
const elementParam = element[ index ];
const keyParam = key ? `${ key }[${ index }]` : index;
json2urlEncoded( elementParam, keyParam, result, );
}
} else {
result[ key ] = element.toString();
}
return result;
}
// // test
// const json2urlEncoded_test = () => {
// const data = {
// time : +new Date,
// users : [
// { id: 100 , name: 'Alice' , } ,
// { id: 200 , name: 'Bob' , } ,
// { id: 300 , name: 'Charlie' , } ,
// ],
// };
// const test = json2urlEncoded( data, );
// // Logger.log( 'test\n%s', test, );
// return test;
// // Output:
// // users[0][id]=100&users[0][name]=Stefano&users[1][id]=200&users[1][name]=Lucia&users[2][id]=300&users[2][name]=Franco&time=1405014230183
// }
// // quokka
// const test = json2urlEncoded_test();
// const typeOfTest = typeof test;
// typeOfTest
// test
// [ END ] utilities library