#caleb-hailey_api
1 messages Ā· Page 1 of 1 (latest)
š 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.
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.
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)
hmm, ok
this says default_tax_rates should be an array of objects: https://docs.stripe.com/api/invoices/object#invoice_object-default_tax_rates
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
for invoiceitems I see that tax rates are supposed to be an array of strings
TIL the tax_rates API though https://docs.stripe.com/api/tax_rates
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
ah, I was looking at the wrong section in the docs š¤¦āāļø
Ah yep, gets me every time too!
I was looking at the invoice object, not the "Create an invoice" section
Complete reference documentation for the Stripe API. Includes code snippets and examples for our Python, Java, PHP, Node.js, Go, Ruby, and .NET libraries.
ok, one more qq: can I create tax rates with a custom ID? or are IDs assigned by Stripe?
These IDs are auto created/assigned but you can add a description and metadata to the Tax Rate
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
Sure thing!
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
Yep, one such example is when you create a Price object: https://docs.stripe.com/api/prices/create
You can pass along an ID for product or pass a product_data hash to create a Product in line
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)
Yep, that's the only other example I can think of at the moment