#Handling Invalid VAT Numbers and Preventing 500 Error Page

72 messages · Page 1 of 1 (latest)

white pasture
#

When I use below method, and customer enters '1234' as their VAT id number:

$taxId = $customer->createTaxId("eu_vat", "DK1234");

Customer are redirected to a 500 error page, with the following error message:
ERROR: Invalid value for EU VAT.

How can I prevent the default validation for EU VAT and avoid redirecting users to a 500 error page, allowing them to enter a VAT company ID number even if it is wrong?

Should I maybe use a custom Stripe ID field for company VAT number which do not have a default validation?

winter roost
winter roost
#

@white pasture If you’re getting an exception from Stripe when trying to create a tax ID for a customer, then you need to handle that exception.

Errors thrown by the Stripe SDK will have methods that you can use to check the type of error. So you can check the type of error in your controller and convert it to a validation error message if the tax ID was submitted via a form for example:

try {
    $taxId = $customer->createTaxId('eu_vat', 'DK1234');
} catch (\Stripe\Exception\InvalidRequestException $e) {
    switch ($e->getStripeCode()) {
        case 'tax_id_invalid':
            throw \Illuminate\Validation\ValidationException::withMessages([
                'tax_id' => [
                    'Invalid value for eu_vat.',
                ],
            ]);

        default:
            throw $e; // Just re-throw original exception
    }
}

// Rest of your method...
white pasture
winter roost
#

I don’t really know what you’re expecting. If a user types in an incorrect tax ID, then you need to communicate that to the user.

#

Hence, validation error.

white pasture
winter roost
winter roost
#

Then what’s the problem?

white pasture
#
try {
$taxId = $stripeCustomer->createTaxId("eu_vat", "DK1234");
} catch (\Stripe\Exception\InvalidRequestException $e) {
return Inertia::render("Error", [
                    "customError" => $e->getMessage(),
"status" => 422,
]);
}
winter roost
#

User enters invalid tax ID. They get validation error saying tax ID is invalid.

white pasture
winter roost
#

Yes. You get a 500 if you don’t handle the error from Stripe. But my code sample does handle the code from Stripe, and instead turns it into a validation error that tells the user specifically what they did wrong.

white pasture
winter roost
#

Why are you trying to set the tax ID to a hard-coded value (DK1234) in the first place?

#

Is this not in a form?

white pasture
winter roost
#

So just accept the user-supplied tax ID?

$taxId = $stripeCustomer->createTaxId('eu_vat', $request->input('tax_id'));
#

My code above would catch any exceptions using an incorrect tax ID and redirect back to the form, with a validation error for the tax_id field saying “Invalid valid for eu_vat.”

white pasture
white pasture
winter roost
#

How else is the user going to know if they entered an incorrect tax ID?

white pasture
winter roost
#

I’ve literally given you the code you need.

white pasture
winter roost
#

It’s not working because instead of using the code I wrote for you, you instead decided to piss about and use Inertia::render instead.

white pasture
white pasture
# winter roost It’s not working because instead of using the code I wrote for you, you instead ...

No, I am using it. But user is being directed to 422 Error page.

I am not getting a validation error in the form.

try {
$taxId = $stripeCustomer->createTaxId("eu_vat", "DK1234");
} catch (\Stripe\Exception\InvalidRequestException $e) {
switch ($e->getStripeCode()) {
case "tax_id_invalid":
throw \Illuminate\Validation\ValidationException::withMessages(
[
                                "tax_id" => ["Invalid value for eu_vat."],
]
);

default:
throw $e; // Just re-throw original exception
}
}

Seems like you gave an stupid code example

winter roost
#

Seems like you gave an stupid code example
Yeah, f— off then.

#

Asshole.

white pasture
tall snow
#

No need to be rude, Martin tried helping you.

But why would you support invalid VAT? You just need to catch the exception. 😅

white pasture
# tall snow No need to be rude, Martin tried helping you. But why would you support invali...

I don't want to support invalid Vat number, but rather than sending user to a 422 or 500 error page and away from the form, it will make more sense to return a Vat input field validation error.

But I think I found a solution, I will just use the StoreSubscriptionRequest class I have created and using try catch.

I found it bad user experienced to send user to 500 error page, cause they entered a wrong Vat number.

tall snow
white pasture
#

Good thing is that we have chatgpt, so we don't have to deal with people like him anymore.

winter roost
#

It’s not my fault you don’t know how validation works in Laravel and Inertia.

#

You also messed around with my code example, and then moaned it didn’t work.

#

Inertia automatically converts Laravel validation exceptions to errors if you’re using Inertia’s form helper.

white pasture
winter roost
white pasture
#

Nobody forced you to.

winter roost
#

That’s the thing: I do like to help others. But I don’t like spending time helping others, and for them to go “Your code’s stupid” when you’re the one that messed about with the code sample I gave you.

white pasture
#

Good for you

white pasture
tall snow
#

I for instance do not have a VAT no

white pasture
tall snow
#

And tbf I think Martin just got upset because you said stupid code.

And you can't rely on ChatGPT for complex things, it doesn't have full context and will still provide old code samples 😅

tall snow
white pasture
white pasture
tall snow
white pasture
#

Ok, thanks. But do I need to deduct tax, if I do not required tax in the first place for a listing

tall snow
#

In your case the VAT should really only be used to confirm it's a business so you don't just let someone type 1234 and pass through

tall snow
#

But for businesses they might need the info for their accounting or whatnot

white pasture
tall snow
#

Yeah I'd collect it personally, it's not gonna hold them off. If you're buying as a business, informing a VAT no is a given

tall snow
#

No problem

white pasture
# tall snow No problem

Below is amazing. I am able to throw form input error based on try catch:

try {
$taxId = $stripeCustomer->createTaxId("eu_vat", "DK1234");
} catch (Exception $e) {
throw \Illuminate\Validation\ValidationException::withMessages([
"tax_id" => [$e->getMessage()],
]);
}
tall snow
white pasture
snow rampart
#

Laravel form requests with inertia form helper does all of this - check it out - you can also include in the form request some check on whether VAT is required or not but i think that's a different direction this conversation went in.
If you simply want some text under the input box to say tax id invalid or whatever with inertia form then i suggest you use the things i mentioned here - if you want to do it manually and have stripe validate then the code is above (this could also be in a Laravel form request class and return form error in the same way)
https://inertiajs.com/forms
https://laravel.com/docs/10.x/validation#form-request-validation

Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.

#

The second link here answers your last question directly BTW