kallangreen
kallangreen

Reputation: 21

How Do I Handle Stripe Errors PHP

Sorry for being a bit wet behind the ears when it comes to PHP and Stripe, but i guess we all have to start somewhere.

I have set a payment page up with stripe all working fine when the card details are correct and it is authorised. This issue i have got is when the card is declined or there are issues with the details. All that i get is a blank white screen and i don't know how to handle to the errors.

so i guess would someone be so kind as to help.

Thanks and code below;

Main Index.php page

<!-- STRIPE -->


<div class="payment">

<div class="title">
  <div class="copy">
    <h2>Deposit Payment</h2>
    <p>Please fill out the following form.</p>
  </div>
</div>

  <?php require_once('inc/config.php'); ?>
  <form action="inc/charge.php" method="post">
  <input type="text" name="business" placeholder="Advertiser Name">
  <input type="text" name="email" placeholder="Advertiser Email">
  <input type="text" name="hotel" placeholder="Hotel Name">
  <div class="two-blocks">
  <select class="small" id="currency">
  <option value="gbp">Great British Pounds</option>
  </select>
  <input type="text" class="small" id="price" name="price" placeholder="£500.00">
  </div>
  <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
        data-key="<?php echo $stripe['publishable_key']; ?>"
        data-currency="GBP"
        data-label="Submit"
        data-name="Guest Services Worldwide"
        data-image="http://www.itsjoeturner.com/pay/wallet.png"
        data-description="Advertising Deposit Payment"
        data-allow-remember-me="false">
  </script>
  </form>

</div>

<!-- // STRIPE -->

charge.php

<?php
  require_once('config.php');

  $token = $_POST['stripeToken'];
  $business = $_POST['business'];
  $email = $_POST['email'];
  $hotel = $_POST['hotel'];
  $currency = $_POST['currency'];
  $price =  $_POST['price'];

 $price .= "00";

  $customer = Stripe_Customer::create(array(
      'email' => $email,
      'card'  => $token,
      'description' => "Business Name: ".$business.""
  ));

  $charge = Stripe_Charge::create(array(
      'customer' => $customer->id,
      'amount'   => $price,
      'currency' => 'gbp',
  ));

  header( 'Location: http://gswportal.co.uk/payments/thanks.html' ) ;

?>

Upvotes: 2

Views: 845

Answers (2)

shivam
shivam

Reputation: 16506

Here's a solution from Google.

 try {
    $charge = Stripe_Charge::create(array(
    "amount" => $clientPriceStripe, // amount in cents
    "currency" => "usd",
    "customer" => $customer->id,
    "description" => $description));
    $success = 1;
    $paymentProcessor="Credit card (www.stripe.com)";
} catch(Stripe_CardError $e) {
  $error1 = $e->getMessage();
} catch (Stripe_InvalidRequestError $e) {
  // Invalid parameters were supplied to Stripe's API
  $error2 = $e->getMessage();
} catch (Stripe_AuthenticationError $e) {
  // Authentication with Stripe's API failed
  $error3 = $e->getMessage();
} catch (Stripe_ApiConnectionError $e) {
  // Network communication with Stripe failed
  $error4 = $e->getMessage();
} catch (Stripe_Error $e) {
  // Display a very generic error to the user, and maybe send
  // yourself an email
  $error5 = $e->getMessage();
} catch (Exception $e) {
  // Something else happened, completely unrelated to Stripe
  $error6 = $e->getMessage();
}

if ($success!=1)
{
    $_SESSION['error1'] = $error1;
    $_SESSION['error2'] = $error2;
    $_SESSION['error3'] = $error3;
    $_SESSION['error4'] = $error4;
    $_SESSION['error5'] = $error5;
    $_SESSION['error6'] = $error6;
    header('Location: checkout.php');
    exit();
}

Upvotes: 0

FuzzyTree
FuzzyTree

Reputation: 32402

Stripe throws a Stripe_CardError exception when a card is declined.

try {
    $charge = Stripe_Charge::create(array(
      'customer' => $customer->id,
      'amount'   => $price,
      'currency' => 'gbp',
    ));
} catch (Stripe_CardError $e) {
    //card could not be charged
    $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");
}

See https://stripe.com/docs/api?lang=php#errors for more info and a full list of exceptions that could be thrown.

Upvotes: 1

Related Questions