#nugax80_invoiceitems-and-tax
1 messages ยท Page 1 of 1 (latest)
๐ Welcome to your new thread!
โฒ๏ธ We'll be here soon! Typically we respond in a few minutes, but sometimes we might take a bit longer if the server is busy or if you have a particularly tricky question.
โฑ๏ธ We close idle threads, which makes them read-only. Once a thread is closed it won't be reopened, but you can always 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/1431023982555168881
๐ Have more to share? Add more details, code, screenshots, videos, etc. below.
Hi Pal!
Howdy!
what guide are you following? Could I have a look at the Invoice that you're working with?
sure
Sign in to the Stripe Dashboard to manage business payments and operations in your account. Manage payments and refunds, respond to disputes and more.
how can i give it to you
in_1SLPWvH0hGHXsp6redUEd8T5
it correctly lists the invoice amount in the invoice and in my php program with tax
but the "tax_amounts": [],
"tax_rates": [], from the docs are both empty
in my array
here is same invoice in my php program
Is that when you're retrieving the Invoice? Or in the response to a request?
so you can see the totals are right
when you retrieve the invoice
its in draft status
you can see the tax is correct (one of those items does not have tax on it
but I cant get the tax info for each line
Are you familiar with the concept of expanding responses in Stripe's API? I imagine you just need to expand the fields you want here
to show in the Taxes: col
I'd recommend reading this: https://docs.stripe.com/expand
if (!empty($lineitem->tax_rates)) {
print("here");
foreach ($lineitem->tax_rates as $tax_rate) {
// Get the percentage from the embedded TaxRate object
$percentage = $tax_rate->percentage ?? 0;
if ($percentage > 0) {
// Calculation: Price * (Percentage / 100)
// Use round() to mimic Stripe's rounding to the nearest cent.
$tax_amount = round($price_cents * ($percentage / 100));
$line_item_tax_in_cents += $tax_amount;
}
}
}
percentage is empty
no i have no idea what expand does.
i thought that array should be populated if data was there
here is where I pull
//Get Invoice From Stripe
try {
//Create Stripe Client
$stripe = new \Stripe\StripeClient($secret_key);
//Create Draft Invoice
$invoice = $stripe->invoices->retrieve(
$stripe_invoice_id_given,
// ['expand' => ['lines.data.price']]
);
//Get Payment PDF
$payment_pdf = $invoice->invoice_pdf;
} catch (\Stripe\Exception\ApiErrorException $e) {
echo 'Stripe Error: ' . $e->getMessage();
die();
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
die();
}
I guess I did have expand there!
maybe i looked into that at some point!
what does the first entry in lines.data look like in the response that you get from this code?
I think you want to be expanding lines.data.taxes
how do i tell?
well you want to do something like retrieve the Invoice and then log out the response from Stripe. I'm not a php developer but something like
$invoice = $stripe->invoices->retrieve('in_1SLPWvH0hGHXsp6redUEd8T5', []);
and then add expand: ['lines.data.taxes']
$invoice = $stripe->invoices->retrieve(
$stripe_invoice_id_given,
['expand' => ['lines.data.price'],
'expand' => ['lines.data.taxes']]
);
i have that
how can i see the result?
can i see the result in the stripe dashboard?
or do i not need price
However you log stuff out in php. In node I'd do something like console.log(JSON.stringify(invoice, null, 2));
oh
You shouldn't need the price if you are concerned with the tax amounts on an Invoice's individual lines
i only need:
quantitym price, taxes, item total
can that be retrieved from just lines.data.taxes?
I imagine if you're already expanding lines you should get all of that, but the best way to know is to test
ok, i removed price
just have it expanding taxes
$invoice = $stripe->invoices->retrieve(
$stripe_invoice_id_given,
[
//'expand' => ['lines.data.price'],
'expand' => ['lines.data.taxes']
]
);
let me see if I can pull taxes out of the lineitem
would this be it?
$tax_amount_in_cents = $lineitem->taxes->amount;
$invoice = $stripe->invoices->retrieve(
$stripe_invoice_id_given,
[
//'expand' => ['lines.data.price'],
'expand' => ['lines.data.taxes']
]
);
with that retrieve?
im confused on the structure
I'm sorry, I'm a little confusd on what you're asking. Could you give me the JSON you're getting back from Stripe?
ok, so this isnt correct
$tax_amount_in_cents = $lineitem->taxes->amount;
it causes an errror as I assume that array isnt correct
This is what creates lineitem
foreach ($invoice->lines->data as $lineitem) {
and I expanded lines.data.taxes
in the retrieve call
{
"expand": {
"0": "lines.data.taxes"
}
}
thats the JSON from the dev portal in stripe
That's the body of your request
I'm asking for the body response from stripe, which is an Invoice object in JSON
so with this code:
$invoice = $stripe->invoices->retrieve(
$stripe_invoice_id_given,
[
//'expand' => ['lines.data.price'],
'expand' => ['lines.data.taxes']
]
);
You still need to do something to log $invoice to the console
its fine - I have it formatted
So this is the way I would recommend doing it, but be aware that you can also go and look at webhook events in the dashboard that have the most recent copies of the objects in question. For example this invoice.updated event: https://dashboard.stripe.com/test/events/evt_1SLVXIH0hGHXsp6rtTk8KBgz
so it looks like, in that Invoice, only one of the items has taxes applied:
{ "id": "il_1SLVWpH0hGHXsp6rjZ193YEl", "object": "line_item", "amount": 2500, "currency": "usd", "description": "test", "discount_amounts": [], "discountable": true, "discounts": [], "invoice": "in_1SLPWvH0hGHXsp6redUEd8T5", "livemode": false, "metadata": {}, "parent": { "invoice_item_details": { "invoice_item": "ii_1SLVWpH0hGHXsp6rUJe5loVH", "proration": false, "proration_details": { "credited_items": null }, "subscription": null }, "subscription_item_details": null, "type": "invoice_item_details" }, "period": { "end": 1761252875, "start": 1761252875 }, "pretax_credit_amounts": [], "pricing": { "price_details": { "price": "price_1SLVWpH0hGHXsp6rAHTkdBer", "product": "prod_SP3N4Vo3Xjr3qq" }, "type": "price_details", "unit_amount_decimal": "2500" }, "quantity": 1, "taxes": [ { "amount": 213, "tax_behavior": "exclusive", "tax_rate_details": { "tax_rate": "txr_1SLS6YH0hGHXsp6rrhDNamB4" }, "taxability_reason": "not_available", "taxable_amount": 2500, "type": "tax_rate_details" } ] }
its the last item in the array
yes thats probably correct
so, how do I get that amount?
i have one other question too when we finish this
its probably much simplier
but related
What do you mean by "how do I get that amount?"? you retrieved the Invoice and it is there in the JSON response from Stripe
so i created a new invoice
i was looking in events
i added one taxable item
i looked at the json and I do not see any taxable info
What's the Invoice id?
nm
i see it now
its in the invgoice updated event
"total_taxes": [
{
"amount": 85,
"tax_behavior": "exclusive",
"tax_rate_details": {
"tax_rate": "txr_1SLS6YH0hGHXsp6rrhDNamB4"
},
"taxability_reason": "not_available",
"taxable_amount": 1000,
"type": "tax_rate_details"
}
Yep, you can also look at total_taxes on the Invoice, especially if you know you are only adding a single taxable item. https://docs.stripe.com/api/invoices/object#invoice_object-total_taxes
got it
I had to make another look to go through the tax array
here is the code in php if you care:
$tax_array = $lineitem->taxes;
$tax_amount_in_cents = 0;
foreach ($tax_array as $tax_detail) {
$tax_amount_in_cents = $tax_detail->amount;
}
I can make it better in case there are more than 1 tax, but that is how you pull the taxes
i had to go through the array!
works in my app too!
can i ask a related but different question now?
thanks for the jelp on that
on that
sure, fire away
cool
ok
so right now, when I add an item, i create a product, the price
is it possible to just add an item to a created invoice without creating a product and price?
some things just are one off items to add and dont need to be saved in stripe products
i have it create a Product, then a Price
$price = \Stripe\Price::create([
$product = \Stripe\Product::create([
then the actual item
$invoiceItem = \Stripe\InvoiceItem::create($ItemCreateArray);
thats backwards it creates product first then price
Hi there,
taking over for me colleague. Let me quickly catch up
When creating an InvoiceItem, you can either pass in a pricing parameter (which requires an existing product and price) https://docs.stripe.com/api/invoiceitems/create#create_invoiceitem-pricing
or you can pass in price_data, which creates a prices but requires a product https://docs.stripe.com/api/invoiceitems/create#create_invoiceitem-price_data
That is correct
then if its a manual addition of something I dont want stored in stripe foreever, can it be deleted or removed once its added to invoice?
From a technical standpoint, I think so. Haven't actually tried it yet. But you might need to keep it for documentation puposes / potential legal reasons but we here wouldn't know about that.