#nugax80_invoiceitems-and-tax

1 messages ยท Page 1 of 1 (latest)

rotund fiberBOT
#

๐Ÿ‘‹ 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.

valid lark
#

Hi Pal!

vivid matrix
#

Howdy!

#

what guide are you following? Could I have a look at the Invoice that you're working with?

valid lark
#

sure

#

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

vivid matrix
#

Is that when you're retrieving the Invoice? Or in the response to a request?

valid lark
#

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

vivid matrix
#

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

valid lark
#

to show in the Taxes: col

vivid matrix
valid lark
#

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!

vivid matrix
#

what does the first entry in lines.data look like in the response that you get from this code?

valid lark
#

how do i tell?

vivid matrix
#

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']

valid lark
#

$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

vivid matrix
#

However you log stuff out in php. In node I'd do something like console.log(JSON.stringify(invoice, null, 2));

valid lark
#

oh

vivid matrix
#

You shouldn't need the price if you are concerned with the tax amounts on an Invoice's individual lines

valid lark
#

i only need:

#

quantitym price, taxes, item total

#

can that be retrieved from just lines.data.taxes?

vivid matrix
#

I imagine if you're already expanding lines you should get all of that, but the best way to know is to test

valid lark
#

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

vivid matrix
#

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?

valid lark
#

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

vivid matrix
#

That's the body of your request

#

I'm asking for the body response from stripe, which is an Invoice object in JSON

valid lark
#

hrm

#

one sec

vivid matrix
#

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

valid lark
#

i am

#

vardump invoice object

#

one sec

#

let me make that json readable

vivid matrix
#

its fine - I have it formatted

valid lark
vivid matrix
#

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

valid lark
#

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

vivid matrix
#

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

valid lark
#

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

vivid matrix
#

What's the Invoice id?

valid lark
#

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

vivid matrix
valid lark
#

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

rotund fiberBOT
vivid matrix
#

sure, fire away

valid lark
#

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

brazen sierra
#

Hi there,
taking over for me colleague. Let me quickly catch up

valid lark
#

cool

#

thanks pal! appreciate it

brazen sierra
valid lark
#

so you cannot just create an item without a product

#

invoice item

brazen sierra
#

That is correct

valid lark
#

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?

brazen sierra
#

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.