Argument Too Large - JSON.stringify

Viewed 42

I've created a script in Google's Apps Script that creates Stripe Payment Links for my Client Database, The problem is that i wanna pass the mail argument + subscription link in the HTTP Request.

The input is:

line_items[0][price_data][currency]":"RON","line_items[0][price_data][recurring][interval]":"month","line_items[0][price_data][recurring][interval_count]":4,"line_items[0][quantity]":1,"line_items[0][price_data][product_data][name]":"Name","line_items[0][price_data][unit_amount]":31000,"[customer_email]":"test@test.com"

I have to pass this in

Cache.put(input, url, 21600);

BUT Apps Script doesnt allow arguments > 250 Characters.

Is it possible to: make PUT Requests with multiple objects: put(input1,input2, url, 21600);

OR

Is it possible to shorten the input somehow? Because there are many strings that repeat themselves, like the following line_items[0][price_data] - Given that i have no access to the backend (Backend is Stripe)

OR

Is it possible to increase Google Apps Script max Argument size bigger than 250?

Thank you in advance

Source Code

const STRIPESUB = (amount, currency, description, customer_email) => {

  const input = {
    "line_items[0][price_data][currency]": currency || "RON",
    "line_items[0][price_data][recurring][interval]" : "month",
    "line_items[0][price_data][recurring][interval_count]" : 4,
    "line_items[0][quantity]" : 1,
    "line_items[0][price_data][product_data][name]": description || "Name",
    "line_items[0][price_data][unit_amount]": Math.ceil(amount * 100),
    "[customer_email]" : customer_email || "",
  };
  
  const cacheKey = JSON.stringify(input);
  
  const cachedLink = CacheService.getScriptCache().get(cacheKey);
  if (cachedLink) return cachedLink;


  const params = {
    cancel_url: STRIPE_CANCEL_URL,
    success_url: STRIPE_SUCCESS_URL,
    mode: "subscription",
    billing_address_collection: "required",
    "payment_method_types[]": "card",
    ...input,
  };

  const payload = Object.entries(params)
    .map(([key, value]) =>
      [encodeURIComponent(key), encodeURIComponent(value)].join("=")
    )
    .join("&");

  const response = UrlFetchApp.fetch(
    "https://api.stripe.com/v1/checkout/sessions",
    {
      method: "POST",
      headers: {
        Authorization: `Bearer ${STRIPE_API_KEY}`,
        "Content-Type": "application/x-www-form-urlencoded",
      },
      payload,
      muteHttpExceptions: true,
    }
  );

  const { url, error } = JSON.parse(response);

  if (url) {
    CacheService.getScriptCache().put(cacheKey, url, 21600);
  }

  return error ? error.message : url;
};
0 Answers
Related