title: How can I invoke an error in my Stripe.card.createToken function?
body: <p>I'm using stripe in React and processing the charge through an AJAX call.
  I've tried to strip down the <strong>Stripe.card.createToken</strong> function to the bare essentials for this question. </p>
  
  <p>The <strong>if else</strong> statement checks if the <strong>response</strong> has any errors. </p>
  
  <p>I use the <strong>4100000000000019</strong> card number to ensure a card declined error but the else statement(the successful charge) fires regardless which card number is entered.</p>
  
  <p>Entering the <strong>4100000000000019</strong> card number results in a blocked charge in the Stripe dashboard. An error definitely gets generated:</p>
  
  <pre><code>{
    "error": {
      "message": "Your card was declined.",
      "type": "card_error",
      "code": "card_declined",
      "decline_code": "generic_decline",
      "charge": "ch_19gMBaIWHxnqld7LCdbCtdNz"
    }
  } 
  </code></pre>
  
  <p>But the <strong>if(response.error)</strong> is ignored and runs the else statement.</p>
  
  <pre><code>    Stripe.card.createToken({
      number: $('.card-number').val(),
      cvc: $('.card-cvc').val(),
      exp_month: $('.card-expiry-month').val(),
      exp_year: $('.card-expiry-year').val(),
      name: $('.first-name').val()
  }, function(status, response){
      if (response.error) {
          this.reportError(response.error.message);
      } else { // No errors, submit the form.
        var token = response.id;
          $.ajax({
             type: 'POST',
             url: 'components/charge.php',
             data : {
               stripeToken: token
             },
             success: function(data,response) {
               paymentSuccessful();
             },
             error: function(data,textStatus) {
               console.log("Ajax Error!");
             }
           });//$.ajax
      }//else
  });//Stripe.card.createToken
  </code></pre>
  
  <p>Any help is much appreciated.</p>
  
  <p>Moe</p>
  
  <p><strong>UPDATE</strong>: Thanks to this awesome tutorial by Larry Ullman and it's section on stripe error handling, I came up with a fairly good solution. 
  <a href="http://www.larryullman.com/2013/01/30/handling-stripe-errors/" rel="nofollow noreferrer">http://www.larryullman.com/2013/01/30/handling-stripe-errors/</a></p>
  
  <p>So I added the if else statment inside the AJAX success function.</p>
  
  <pre><code>success: function(data,response) {
                   if(data == "success"){
                     $('#payment-error-copy').text("Your payment was successful. Thank you for ordering!");
                     paymentSuccessful();
                   } else {
                     $('#payment-error-copy').text(data);
                   }
      };//success function
  </code></pre>
  
  <p>Inside the charge we can return the error response and the precise reason for the error. </p>
  
  <p>my charge.php file</p>
  
  <pre><code>&lt;?php
  require_once('vendor/autoload.php');
  
  // Get the payment token submitted by the form:
  $token = $_POST['stripeToken'];
  try{
    \Stripe\Stripe::setApiKey("&lt;secret_KEY&gt;");
  
    $customer = \Stripe\Customer::create(array(
      "source" =&gt; $token,
      "email" =&gt; $email
      )
    );
    // Charge the Customer instead of the card
    \Stripe\Charge::create(array(
        "amount" =&gt; $price, // amount in cents, again
        "currency" =&gt; "aud",
        "description" =&gt; $email." ".$first_name,
        "customer" =&gt; $customer-&gt;id
      )
    );
    echo "success";
  } catch (\Stripe\Error\ApiConnection $e) {
      // Network problem, perhaps try again.
      $e_json = $e-&gt;getJsonBody();
      $error = $e_json['error'];
      echo "Sorry, your charge couldn't be processed. Reason: ".$error['message'];
  } catch (\Stripe\Error\InvalidRequest $e) {
      // You screwed up in your programming. Shouldn't happen!
      $e_json = $e-&gt;getJsonBody();
      $error = $e_json['error'];
      echo "Sorry, your charge couldn't be processed. Reason: ".$error['message'];
  } catch (\Stripe\Error\Api $e) {
      // Stripe's servers are down!
      $e_json = $e-&gt;getJsonBody();
      $error = $e_json['error'];
      echo "Sorry, your charge couldn't be processed. Reason: ".$error['message'];
  } catch (\Stripe\Error\Card $e) {
    // Card was declined.
    $e_json = $e-&gt;getJsonBody();
    $error = $e_json['error'];
    echo "Sorry, your charge couldn't be processed. Reason: ".$error['message'];
  }
  
  ?&gt;
  </code></pre>
  
  <p>The try catch statement returns a precise message about the charge error and returns it to our success function, so if the charge is anything other than success it runs the error function.</p>
  
  <p>Although my question remains unanswered, the build works as expected and still remains safe for the user to enter their billing info.</p>
  
  <p>Thanks to the other SO users for their input, it is really appreciated. </p>