[MKT_923]:fix/prevent checkout crash when Stripe cannot geolocate the user IP - #391
Conversation
| usingIpAddress && | ||
| !customerId && | ||
| error instanceof Stripe.errors.StripeError && | ||
| error.message?.includes('insufficient to determine'); |
There was a problem hiding this comment.
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.
| 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 () => { |
| } catch (error) { | ||
| const isInsufficientLocationError = | ||
| usingIpAddress && | ||
| !customerId && |
There was a problem hiding this comment.
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.
| } | ||
|
|
||
| if (userAddress || (postalCode && country) || user?.customerId) { | ||
| if (currency === 'eur' && (userAddress || (postalCode && country) || user?.customerId)) { |
There was a problem hiding this comment.
Extract this if to a constant. Both the currency check and the other part.
| ): Promise<Stripe.Tax.Calculation> { | ||
| ): Promise<Stripe.Tax.Calculation | undefined> { | ||
| const customerDetails: Stripe.Tax.CalculationCreateParams.CustomerDetails = {}; | ||
| const usingIpAddress = !(postalCode && country); |
There was a problem hiding this comment.
Use conventional names for booleans. E.g.:
isUsingIpAddressshouldUseIpAddress
| error.message?.includes('insufficient to determine'); | ||
|
|
||
| if (isInsufficientLocationError) { | ||
| Logger.warn(`Tax calculation skipped: IP ${ipAddress} is insufficient to determine tax location`); |
There was a problem hiding this comment.
If I'm not mistaken, warn logs don't appear in Graylog. Use either info or error.
There was a problem hiding this comment.
Okay, info better than error??
There was a problem hiding this comment.
As you are throwing and printing an error, maybe error is better.
|



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:
calculateTaxfunction to allow it to returnundefinedif we encounter the specific error we’re discussing. To do this, we created theisInsufficientLocationErrorexception.1. IP cannot be geolocated → returns
undefined2. another Stripe error → propagates.