0

Everything is working fine in Stripe - the token is generated, written in the "log" part in my dashboard, etc. However there is no charge done. I got no error from Stripe or my code even if I did all the error handling given by the stripe documentation. How can I correct this?

require_once ("vendor/autoload.php");

if ($_POST) {
 echo "catch if";

 // Set your secret key: remember to change this to your live secret key in production
 // See your keys here: https://dashboard.stripe.com/account/apikeys

 StripeStripe::setApiKey("myApyKey");

 // Get the credit card details submitted by the form

 $token = $_POST['stripeToken'];

 // Create a charge: this will charge the user's card

 try {
  echo "charging";
  $charge = StripeCharge::create(array(
   "amount" => 1000, // Amount in cents
   "currency" => "eur",
   "source" => $token,
   "description" => "Example charge"
  ));
 }

 catch(StripeErrorCard $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(StripeErrorRateLimit $e) {

  // Too many requests made to the API too quickly

 }

 catch(StripeErrorInvalidRequest $e) {

  // Invalid parameters were supplied to Stripe's API

 }

 catch(StripeErrorAuthentication $e) {

  // Authentication with Stripe's API failed
  // (maybe you changed API keys recently)

 }

 catch(StripeErrorApiConnection $e) {

  // Network communication with Stripe failed

 }

 catch(StripeErrorBase $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

 }
2
  • Your catches are mostly silently failing. Either remove them or have them do something obvious, like print "OH SHIT Stripe InvalidRequest";
    – ceejayoz
    Commented Dec 21, 2016 at 15:00
  • try after this line $err = $body['error']; print_r($err); and see error. Commented Dec 21, 2016 at 15:00

2 Answers 2

2

You are probably getting an error, and your code is handling that error correctly, but you error handling code doesn't actually do anything for many of the cases.

You should probably add something (such as a print call) in each catch block to see exactly what type of issue is being returned.

Alternatively, your Stripe Dashboard has a way to view your accounts Live Mode and Test Mode logs at https://dashboard.stripe.com/logs which will contain an entry for every request (successful or otherwise) that hit Stripe's servers.

3
  • I found the problem !! Commented Dec 21, 2016 at 15:23
  • Actually my API wasn't up to date ! Apparently Stripe didn't feel like telling me that it was that important :) Commented Dec 21, 2016 at 15:24
  • They probably were telling you. Your do-nothing catch statements probably hid that from you.
    – ceejayoz
    Commented Dec 22, 2016 at 3:30
0

Try to use below code find error issue

try {
  echo "charging";
  $charge = StripeCharge::create(array(
   "amount" => 1000, // Amount in cents
   "currency" => "eur",
   "source" => $token,
   "description" => "Example charge"
  ));
 }

 catch(StripeErrorCard $e) {
    $error = $e->getJsonBody();
    <pre>print_r($error);</pre>
    //then you can handle like that 
    if($error == "Your card was declined."){
      echo "Your credit card was declined. Please try again with an alternate card.";
     }
 }

Not the answer you're looking for? Browse other questions tagged or ask your own question.