Runningonjava
Runningonjava

Reputation: 1

Stripe Insufficient Funds: Redirect to new page

I am having trouble trying to catch the insufficient funds error after a payment is submitted and redirect the user. Right now, if a user has insufficient funds, the site gives a 500 error.

In PHP, the error log is:

PHP Fatal error: Uncaught Stripe\Error\InvalidRequest: Must provide source or customer. in /home/betheexe/public_html/_www/legalrideshare.com/stripe-php/lib/ApiRequestor.php:210 from API request 'req_o4ePJL1WVn611Y'

Here's my code to accept payments:

CHECKOUT PAGE:

<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"                                                 
data-key="pk_live_XXXXXXXXXXXXXXXXXXX"
data-amount="2499"
data-name="LR"
data-description="Deactivation"
data-email="<?php echo $_SESSION["email"]; ?>"
data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
data-locale="auto">
</script>

AFTER SUBMISSION:

\Stripe\Stripe::setApiKey("sk_live_XXXXXXXXXXXXXX");
// Token is created using Checkout or Elements!
// Get the payment token ID submitted by the form:
$token = $_POST['stripeToken'];

$charge = \Stripe\Charge::create([
    'amount' => '2499',
    'currency' => 'usd',
    'description' => 'Deactivation Letter',
    'source' => $token,
]);

I just want to catch any errors that occur so the page doesn't fail. I also must be missing a source?

Upvotes: 0

Views: 365

Answers (1)

ttmarek
ttmarek

Reputation: 3240

In order to catch any errors like the one you're seeing from Stripe when a charge fails you will need to wrap your charge request in a try/catch block. For example:

try {

  $charge = \Stripe\Charge::create([
    'amount' => '2499',
    'currency' => 'usd',
    'description' => 'Deactivation Letter',
    'source' => $token,
  ]);

} catch(\Stripe\Exception\CardException $e) {
  // Since it's a decline, \Stripe\Exception\CardException will be caught
  echo 'Status is:' . $e->getHttpStatus() . '\n';
  echo 'Type is:' . $e->getError()->type . '\n';
  echo 'Code is:' . $e->getError()->code . '\n';
  // param is '' in this case
  echo 'Param is:' . $e->getError()->param . '\n';
  echo 'Message is:' . $e->getError()->message . '\n';
} catch (\Stripe\Exception\RateLimitException $e) {
  // Too many requests made to the API too quickly
} catch (\Stripe\Exception\InvalidRequestException $e) {
  // Invalid parameters were supplied to Stripe's API
} catch (\Stripe\Exception\AuthenticationException $e) {
  // Authentication with Stripe's API failed
  // (maybe you changed API keys recently)
} catch (\Stripe\Exception\ApiConnectionException $e) {
  // Network communication with Stripe failed
} catch (\Stripe\Exception\ApiErrorException $e) {
  // Display a very generic error to the user, and maybe send
  // yourself an email
} catch (Exception $e) {
  // Something else happened, completely unrelated to Stripe
}

Within each catch block you can add logic to return a helpful error message to the user with relevant next steps. The Stripe docs provide a full example of these errors and how to handle them in this section:

https://stripe.com/docs/api/errors/handling?lang=php

You can also use the following test card/token to test for the case of insufficient funds:

  • raw card number 4000000000009995
  • test token string tok_chargeDeclinedInsufficientFunds

Upvotes: 0

Related Questions