Skip to content

[MKT_923]:fix/prevent checkout crash when Stripe cannot geolocate the user IP - #391

Merged
jaaaaavier merged 2 commits into
masterfrom
fix/tax-calculation-fallback-when-ip-geolocation-insufficient
Jul 23, 2026
Merged

[MKT_923]:fix/prevent checkout crash when Stripe cannot geolocate the user IP#391
jaaaaavier merged 2 commits into
masterfrom
fix/tax-calculation-fallback-when-ip-geolocation-insufficient

Conversation

@jaaaaavier

Copy link
Copy Markdown
Contributor

We were experiencing a problem where, when a user was geolocated in a region (for example Canada) that requires a sub-national jurisdiction (zip code and country) for tax calculations, we normally use the IP address, but in these cases that is insufficient. The error we encountered was: “The IP address provided is insufficient to determine the customer's tax location. Please provide an alternative address.”

To resolve this, we’ve made a couple of adjustments:

  • Calculate taxes only when working with euros. This was discussed yesterday during a call and proposed because it appears to be too costly for us, it’s an additional measure beyond the fix.
  • We updated the calculateTax function to allow it to return undefined if we encounter the specific error we’re discussing. To do this, we created the isInsufficientLocationError exception.
  • Tests: Added two cases :
    1. IP cannot be geolocated → returns undefined
    2. another Stripe error → propagates.

Comment thread src/services/payment.service.ts Outdated
usingIpAddress &&
!customerId &&
error instanceof Stripe.errors.StripeError &&
error.message?.includes('insufficient to determine');

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there another way to check that the error is about the invalid IP? matching it using includes is not a good idea. Maybe it has a Code error or smth. Maybe customer_tax_location_invalid can help you.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes better

expect(taxes).toStrictEqual(mockedTaxes);
});

it('When the IP cannot be geolocated to a tax jurisdiction, then no tax is returned so the price still loads', async () => {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use test, please

Comment thread src/services/payment.service.ts Outdated
} catch (error) {
const isInsufficientLocationError =
usingIpAddress &&
!customerId &&

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those checks have been removed. The workaround should be applied whenever Stripe cannot determine the tax jurisdiction, regardless of whether the information comes from an IP address or a stored customer address. I thought that perhaps if the customer had their address saved because they were a long-time user, but the truth is that the same error could still occur. The check is now simply:

error.code === STRIPE_TAX_LOCATION_INVALID_CODE.

Comment thread src/controller/checkout.controller.ts Outdated
}

if (userAddress || (postalCode && country) || user?.customerId) {
if (currency === 'eur' && (userAddress || (postalCode && country) || user?.customerId)) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extract this if to a constant. Both the currency check and the other part.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay👌

Comment thread src/services/payment.service.ts Outdated
): Promise<Stripe.Tax.Calculation> {
): Promise<Stripe.Tax.Calculation | undefined> {
const customerDetails: Stripe.Tax.CalculationCreateParams.CustomerDetails = {};
const usingIpAddress = !(postalCode && country);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use conventional names for booleans. E.g.:

  • isUsingIpAddress
  • shouldUseIpAddress

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay👌

Comment thread src/services/payment.service.ts Outdated
error.message?.includes('insufficient to determine');

if (isInsufficientLocationError) {
Logger.warn(`Tax calculation skipped: IP ${ipAddress} is insufficient to determine tax location`);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I'm not mistaken, warn logs don't appear in Graylog. Use either info or error.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, info better than error??

@xabg2 xabg2 Jul 23, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you are throwing and printing an error, maybe error is better.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect

sg-gs
sg-gs previously approved these changes Jul 23, 2026

@sg-gs sg-gs left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving this up to @xabg2. The changes requested by him are almost the same things I could mention in this case. Special mention to the constants, as some ifs are ununderstandable right now.

@sonarqubecloud

Copy link
Copy Markdown

@jaaaaavier
jaaaaavier requested a review from xabg2 July 23, 2026 08:50
@jaaaaavier
jaaaaavier merged commit 41082de into master Jul 23, 2026
10 of 11 checks passed
@jaaaaavier
jaaaaavier deleted the fix/tax-calculation-fallback-when-ip-geolocation-insufficient branch July 23, 2026 08:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request ready-for-preview

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants