#caleb-hailey_api

1 messages Ā· Page 1 of 1 (latest)

white pagodaBOT
#

šŸ‘‹ Welcome to your new thread!

ā²ļø We'll be here soon! We typically respond in a few minutes, but in some cases we might need a bit more time (e.g., server's busy, you've got a complex question, etc.).

ā±ļø We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can start a new thread if you have another question.

šŸ”— This thread will always be available, even after it's closed. You can find it again using Discord's search, or you can save this link: https://discord.com/channels/841573134531821608/1247301822461251675

šŸ“ Have more to share? Add details, code, screenshots, videos, etc. below.

outer salmon
#

OK, for a longer explanation... I'm flattening an object that looks like this:

{
  number: REDACTED,
  collection_method: 'send_invoice',
  auto_advance: false,
  currency: 'usd',
  customer: REDACTED,
  default_tax_rates: [
    {
      country: 'US',
      state: 'WA',
      description: 'Washington State Tax',
      display_name: 'Washington State Tax',
      jurisdiction: 'Washington State Tax',
      percentage: 0.065,
      inclusive: false,
      tax_type: 'sales_tax',
      exempt: 'none'
    },
    {
      country: 'US',
      state: 'WA',
      description: 'Seattle City Tax',
      display_name: 'Seattle City Tax',
      jurisdiction: 'Seattle City Tax',
      percentage: 0.0385,
      inclusive: false,
      tax_type: 'sales_tax',
      exempt: 'none'
    }
  ],
  days_until_due: 30,
  effective_at: 1717448352
}
#

Before converting to URLSearchParams, I'm flattening the object to look like this:

var invoice = {
  'number': REDACTED,
  'collection_method': 'send_invoice',
  'auto_advance': false,
  'currency': 'usd',
  'customer': REDACTED,
  'default_tax_rates[0][country]': 'US',
  'default_tax_rates[0][state]': 'WA',
  'default_tax_rates[0][description]': 'Washington State Tax',
  'default_tax_rates[0][display_name]': 'Washington State Tax',
  'default_tax_rates[0][jurisdiction]': 'Washington State Tax',
  'default_tax_rates[0][percentage]': 0.065,
  'default_tax_rates[0][inclusive]': false,
  'default_tax_rates[0][tax_type]': 'sales_tax',
  'default_tax_rates[0][exempt]': 'none',
  'default_tax_rates[1][country]': 'US',
  'default_tax_rates[1][state]': 'WA',
  'default_tax_rates[1][description]': 'Seattle City Tax',
  'default_tax_rates[1][display_name]': 'Seattle City Tax',
  'default_tax_rates[1][jurisdiction]': 'Seattle City Tax',
  'default_tax_rates[1][percentage]': 0.0385,
  'default_tax_rates[1][inclusive]': false,
  'default_tax_rates[1][tax_type]': 'sales_tax',
  'default_tax_rates[1][exempt]': 'none',
  'days_until_due': 30,
  'effective_at': 1717448352
}
#

Then I'm converting the object to URLSearchParams like this:

var params = new URLSearchParams(invoice);

Then constructing & submitting the reqeust like this:

var request = new Request(url, {
    method: "POST",
    headers: {
        "Content-Type": "application/x-www-form-urlencoded",
        "Authorization": `Bearer ${token}`,
    },
    body: params,
});
var result = await fetch(request);

However, Stripe is parsing the request body default_tax_rates property as an object, not an array:

{
  "effective_at": "1717443372",
  "number": "REDACTED",
  "default_tax_rates": {
    "0": {
      "description": "Washington State Tax",
      "tax_type": "sales_tax",
      "exempt": "none",
      "country": "US",
      "display_name": "Washington State Tax",
      "state": "WA",
      "jurisdiction": "Washington State Tax",
      "percentage": "0.065",
      "inclusive": "false"
    },
    "1": {
      "description": "Seattle City Tax",
      "tax_type": "sales_tax",
      "exempt": "none",
      "country": "US",
      "display_name": "Seattle City Tax",
      "state": "WA",
      "percentage": "0.0385",
      "jurisdiction": "Seattle City Tax",
      "inclusive": "false"
    }
  },
  "auto_advance": "false",
  "currency": "usd",
  "collection_method": "send_invoice",
  "days_until_due": "30",
  "customer": "REDACTED"
}

The resulting error is "Invalid string" for the default_tax_rates[0] parameter.

lethal lantern
#

default_tax_rates should be an array of tax rate IDs in string form, e.g. ['txr_123abc', 'txr_123def']

#

You'll need to split this up into more than one request: create the Tax Rate(s) first, then create the Invoice and pass the relevant Tax Rate(s)

outer salmon
#

hmm, ok

#

for invoiceitems I see that tax rates are supposed to be an array of strings

#

ah, I was looking at the wrong section in the docs šŸ¤¦ā€ā™‚ļø

lethal lantern
#

Ah yep, gets me every time too!

outer salmon
#

I was looking at the invoice object, not the "Create an invoice" section

#

ok, one more qq: can I create tax rates with a custom ID? or are IDs assigned by Stripe?

lethal lantern
#

These IDs are auto created/assigned but you can add a description and metadata to the Tax Rate

outer salmon
#

kk

#

for my use case that probably means a few extra requests to check and see if a tax rate matching a certain description or metadata property exists before creating it

#

thanks for your help!

#

I was really confused by this one

lethal lantern
#

Sure thing!

outer salmon
#

maybe one more qq: it's been interesting working with an application/x-www-form-urlencoded API vs application/json (when I'm more used to the latter). In general, is there any place where a Stripe API endpoint accepts nested objects? And if so, what is the correct way to format those requests?

#

just wanna make sure the logic I outlined above isn't fundamentally wrong

lethal lantern
outer salmon
#

ah then in that case it's a nest object (which is essentially what I was constructing), not a nested array (which I was unable to construct)

lethal lantern
#

Yep, that's the only other example I can think of at the moment