David Hope
David Hope

Reputation: 1536

PHP: try/catch Stripe bank account verification?

I'm using Stripe API and trying to verify the bank account details.

So I tried using this:

try {
$bank = \Stripe\Token::create(array(
  "bank_account" => array(
            "object" => "bank_account",
            "country" => "US",
            "currency" => "usd",
            "account_holder_name" => 'SOme name',
            "account_holder_type" => 'individual',
            "routing_number" => "1100300",
            "account_number" => "000123456"
  )
));

$bankID = $bank['id'];

echo $bankID;

} catch (\Stripe\Error\Card $e) {
        // handle errors
        $error1 = $e->getMessage();
        echo $error1;
}catch (Stripe_Error $e) {
  // Invalid parameters were supplied to Stripe's API
  $error2 = $e->getMessage();
  echo $error2;
} 

But this doesn't really catch the errors!

Could someone please let me know how I can catch the bank account verification errors?

Thanks in advance.

Upvotes: 0

Views: 225

Answers (1)

Wee Zel
Wee Zel

Reputation: 1324

here's your code with the error handling:

<?php
include 'vendor/autoload.php';

try {
    \Stripe\Stripe::setApiKey("sk_test_BQokikJOvBiI2HlWgH4olfQ2");

    // Use Stripe's library to make requests...
    $bank = \Stripe\Token::create(
        array(
            "bank_account" => array(
                "object" => "bank_account",
                "country" => "US",
                "currency" => "usd",
                "account_holder_name" => 'Some name',
                "account_holder_type" => 'individual',
                "routing_number" => "1100300",
                "account_number" => "000123456"
            )
        )
    );
} catch(\Stripe\Error\Card $e) {
    // Since it's a decline, \Stripe\Error\Card will be caught
    echo 'Decline' . "\n";

    $body = $e->getJsonBody();
    $err  = $body['error'];

    print('Status is:' . $e->getHttpStatus() . "\n");
    print('Type is:' . $err['type'] . "\n");
    print('Code is:' . $err['code'] . "\n");
    // param is '' in this case
    print('Param is:' . $err['param'] . "\n");
    print('Message is:' . $err['message'] . "\n");
} catch (\Stripe\Error\RateLimit $e) {
    // Too many requests made to the API too quickly
    echo 'Too many requests' . "\n";
} catch (\Stripe\Error\InvalidRequest $e) {
    // Invalid parameters were supplied to Stripe's API
    echo 'Invalid parameters were supplied' . "\n";

    $body = $e->getJsonBody();
    $err  = $body['error'];

    print('Status is:' . $e->getHttpStatus() . "\n");
    print('Type is:' . $err['type'] . "\n");
    // param is '' in this case
    print('Param is:' . $err['param'] . "\n");
    print('Message is:' . $err['message'] . "\n");
} catch (\Stripe\Error\Authentication $e) {
    // Authentication with Stripe's API failed
    // (maybe you changed API keys recently)
    echo 'Authentication failed' . "\n";

    $message = $e->getMessage();
    print('Message is:' .$message);
} catch (\Stripe\Error\ApiConnection $e) {
    // Network communication with Stripe failed
    echo 'Network communication failed' . "\n";
} catch (\Stripe\Error\Base $e) {
    // Display a very generic error to the user, and maybe send
    // yourself an email
    echo 'Display a very generic error to the user' . "\n";
} catch (Exception $e) {
    // Something else happened, completely unrelated to Stripe
    echo 'Something else happened' . "\n";
}

when you run the code it throws an InvalidRequest exception:

Invalid parameters were supplied
Status is:400
Type is:invalid_request_error
Param is:bank_account[routing_number]
Message is:Routing number must have 9 digits

so it looks like your routing_number (1100300) is invalid!

Upvotes: 1

Related Questions