将 Stripe API 语法转换为 Google Apps 脚本

Converting Stripe API syntax to Google Apps Script

正确解释了由于 require Node/JS 库不受 Google Apps 脚本支持,必须进行以下代码更改才能使 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 脚本友好版本。

来自 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:

Log.error
{
  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:

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

and this 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" 发送。

参考文献:

我修改了评论中提到的脚本,现在有一个可以用于此目的的脚本。

// [ BEGIN ] utilities library

/**
 * Returns encoded form suitable for HTTP POST: x-www-urlformencoded
 * @see code: https://gist.github.com/lastguest/1fd181a9c9db0550a847
 * @see context: 
 * @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