Search code examples
codeigniterstripe-payments

Error in Codeigniter PHP STRIPE lib payment (using a card number for a declined response_


I'm trying to catch errors in codeigniter 3 by using stripe php library (last version) So, when I use a correct testing card number the payment goes ok, but when I use a test declined card number for testing purposes I'm getting the error: Type: Stripe\Exception\CardException

Message: Your card was declined.enter image description here

Filename: /opt/lampp/htdocs/plataforma_asociaciones/application/third_party/stripe-php-old/lib/Exception/ApiErrorException.php

Line Number: 38

Backtrace:

File: /opt/lampp/htdocs/plataforma_asociaciones/application/third_party/stripe-php-old/lib/Exception/CardException.php Line: 38 Function: factory

File: /opt/lampp/htdocs/plataforma_asociaciones/application/third_party/stripe-php-old/lib/ApiRequestor.php Line: 195 Function: factory


Solution

  • It looks like your code is only catching a subset of all possible errors. Have a look at the PHP error handling sample snippet in the Stripe API reference:

    try {
      // Use Stripe's library to make requests...
    } 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
    }
    

    ApiErrorException is only one type of exception among many. If you want to handle other types of exceptions you need to add code to do so.