#ofish0143

1 messages · Page 1 of 1 (latest)

robust vortexBOT
severe schooner
#

Hi
Did you delete that tax rate? txr_1NscVlL7Q22muElKO1Xi7UNg

mint zinc
#

Nope, I don't think I have even made any tax rates I thought they were created automatically with stripe tax

severe schooner
#

No stripe doesn't create tax rates if you are using Stripe Tax

#

This request was made from your backend

#

And you made a request GET /v1/tax_rates/txr_1NscVlL7Q22muElKO1Xi7UNg

#

let's start from the beginning, you want to use Stripe Tax or your own Tax rates ?

mint zinc
#

So I got that tax ID from the invoice preview, and then I tried to look it up directly.

#

I am already using Stripe tax and my approach is working, but sometimes that invoice preview is returning a tax ID that I can't lookup

#

This is being used to display a payment preview to customers, I only have to display tax to UK customers though and that is the only tax I have configured.

#

Weirdly the errors never seem to come from Uk customers though

severe schooner
#

You need to double check your integration how or from where it's fetching that tax rate Id, as far as I can tell that tax rate isn't found

mint zinc
#

Ye so if you look at that first code snippet I sent this is all happening within a few ms of each other

#

The tax ID comes from the invoice.Upcoming I get it from the Lines Data

#

So stripe returns that tax ID in the invoice and the the code goes on to immediatly lookup that tax id and it isn't found

#

I formatted the code so it's a bit easier to read ```//Generate a preview of the invoice for the given customer and price
newInvoice := &stripe.InvoiceUpcomingParams{
Customer: stripe.String(customerID),
SubscriptionItems: []*stripe.SubscriptionItemsParams{
{
Price: stripe.String(priceID),
Quantity: stripe.Int64(quantity),
},
},
AutomaticTax: &stripe.InvoiceAutomaticTaxParams{
Enabled: stripe.Bool(true),
},
}

in, err := invoice.Upcoming(newInvoice)
if err != nil {
return nil, fmt.Errorf("failed to get upcoming invoice: %w", err)
}

//Ensure there is data inside
if len(in.Lines.Data) == 0 {
return nil, fmt.Errorf(no lines in invoice for customer id %s, customerID)
}

//Get the last line of the invoice
invoiceLine := in.Lines.Data[len(in.Lines.Data)-1]

//Check if there is any tax inside the invoice
if invoiceLine.TaxAmounts != nil && len(invoiceLine.TaxAmounts) > 0 {
//Get the tax rate from the invoice
taxRates := invoiceLine.TaxAmounts[0]
//lookup the tax rate
tr, err := taxrate.Get(
taxRates.TaxRate.ID,
nil,
)
if err != nil {
zap.S().Errorw("failed to get tax rate",
zap.String("customer_id", customerID),
zap.String("tax_id", taxRates.TaxRate.ID),
zap.String("tax_display_name", taxRates.TaxRate.DisplayName),
zap.String("tax_country", taxRates.TaxRate.Country),
zap.Error(err),
)
// If we fail to get tax rate better to return the price without tax than hang forever
return &preview, nil
}
}```

#

This is all in the same endpoint as you can see, somehow, I get a tax ID and by the time I look it up it's either gone or was never existing in the first place.

severe schooner
#

However you said you are using Stripe Tax and you want to dispay the tax rate

#

Let me check something...

#

Did you try to expand tax_rate when getting the upcoming invoice? with the same API call ?

mint zinc
#

I will give that a try, maybe it will save me a tax rate lookup and will contain the information I need directly.

#

Thanks for your help

#

Have a great day!