jcadima
5/11/2017 - 6:19 PM

Stripe - Declined Card and Errors

Stripe - Declined Card and Errors


https://stripe.com/docs/api?lang=php#expanding_objects

http://www.larryullman.com/2013/01/30/handling-stripe-errors/

http://stackoverflow.com/questions/17750143/catching-stripe-errors-with-try-catch-php-method

<?php

try {
  // Use Stripe's library to make requests...

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

  \Stripe\Charge::create(array(
    "amount" => 2000,
    "currency" => "usd",
    "source" => "tok_189gDP2eZvKYlo2CrxrFoLDU", // obtained with Stripe.js
    "metadata" => array("order_id" => "6735")
  ));
  
} catch(\Stripe\Error\Card $e) {
  // Since it's a decline, \Stripe\Error\Card will be caught
  $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
} catch (\Stripe\Error\InvalidRequest $e) {
  // Invalid parameters were supplied to Stripe's API
} catch (\Stripe\Error\Authentication $e) {
  // Authentication with Stripe's API failed
  // (maybe you changed API keys recently)
} catch (\Stripe\Error\ApiConnection $e) {
  // Network communication with Stripe failed
} catch (\Stripe\Error\Base $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
}