Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.4k views
in Technique[技术] by (71.8m points)

http status code 400 - Converting Stripe API syntax to Google Apps Script

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:

from
const stripe = require('stripe')('sk_test_4eC39HqLyjWDarjtT1zdp7dc');
(async () => {
  const product = await stripe.products.create({
    name: 'My SaaS Platform',
    type: 'service',
  });
})();
to
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())
}

Now, I want to convert the following code into the Google Apps Script friendly version.

from 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',
});

So, I'm trying the following.

to
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:

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

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?

Edit:

After stringifying the payload per the suggestion from the comments, now I get the following 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"
  }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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?

Modified 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())
}
  • 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".

References:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...