2
votes

This is straight off the website. So I followed the tutorial but I obviously got something wrong. I have it on server with SSL.

If this is their code then where did I go wrong. jquery, v2.js and stripe are properly included.

<!DOCTYPE html>
<html lang="en">
    <head>
      <!-- Stripe.js used; fewer compliance requirements! -->
      <!-- Include Stripe.js, either in your <head> or as a direct descendant of <body>
      at the end of the page -->
      <script type="text/javascript" src="v2.js"></script>
      <script type="text/javascript" src="development-bundle"></script>
      <script type="text/javascript">
        Stripe.setPublishableKey('pk_test_thenthetestkeyidontwanttoshare');
      </script>
      <script>
        function stripeResponseHandler(status, response) {
        // Grab the form:
          var $form = $('#payment-form');

          if (response.error) { // Problem!

            // Show the errors on the form:
            $form.find('.payment-errors').text(response.error.message);
            $form.find('.submit').prop('disabled', false); // Re-enable submission

          } else { // Token was created!

            // Get the token ID:
            var token = response.id;

            // Insert the token ID into the form so it gets submitted to the server:
            $form.append($('<input type="hidden" name="stripeToken">').val(token));

            // Submit the form:
            $form.get(0).submit();
          }
        };
        $(function() {
          var $form = $('#payment-form');
          $form.submit(function(event) {
            // Disable the submit button to prevent repeated clicks:
            $form.find('.submit').prop('disabled', true);

            // Request a token from Stripe:
            Stripe.card.createToken($form, stripeResponseHandler);

            // Prevent the form from being submitted:
            return false;
          });
        });
      </script>
    </head>
    <body>
        <form action="charge.php" method="POST" id="payment-form">
          <span class="payment-errors"></span>

          <div class="form-row">
            <label>
              <span>Card Number</span>
              <input type="text" size="16" data-stripe="number">
            </label>
          </div>

          <div class="form-row">
            <label>
              <span>Expiration (MM/YY)</span>
              <input type="text" size="2" data-stripe="exp_month">
            </label>
            <span> / </span>
            <input type="text" size="4" data-stripe="exp_year">
          </div>

          <div class="form-row">
            <label>
              <span>CVC</span>
              <input type="text" size="3" data-stripe="cvc">
            </label>
          </div>

          <div class="form-row">
            <label>
              <span>Billing Zip</span>
              <input type="text" size="5" data-stripe="address_zip">
            </label>
          </div>

          <input type="submit" class="submit" value="Submit Payment">
        </form>
    </body>
</html>


<?php
    include('stripe/init.php');
    \Stripe\Stripe::setApiKey("sk_test_otherkeyidontwanttoshare");

    // Get the credit card details submitted by the form
    $token = $_POST['stripeToken'];

    // Create a Customer
    $customer = \Stripe\Customer::create(array(
      "source" => $token,
      "description" => "Donation customer")
    );

    // Charge the Customer instead of the card
    \Stripe\Charge::create(array(
      "amount" => 1000, // Amount in cents
      "currency" => "usd",
      "customer" => $customer->id)
    );
?>

It gives me this error:

Fatal error: Uncaught exception 'Stripe\Error\Card' with message 'Cannot charge a customer that has no active card' in /home3/blueman/public_html/assistforlife.com/stripe/lib/ApiRequestor.php:114 from API request 'req_9SlHeDVEeYFup4'
Stack trace:
#0 /home3/blueman/public_html/assistforlife.com/stripe/lib/ApiRequestor.php(266): Stripe\ApiRequestor->handleApiError('{\n "error": {\n...', 402, Array, Array)
#1 /home3/blueman/public_html/assistforlife.com/stripe/lib/ApiRequestor.php(65): Stripe\ApiRequestor->_interpretResponse('{\n "error": {\n...', 402, Array)
#2 /home3/blueman/public_html/assistforlife.com/stripe/lib/ApiResource.php(120): Stripe\ApiRequestor->request('post', '/v1/charges', Array, Array)
#3 /home3/blueman/public_html/assistforlife.com/stripe/lib/ApiResource.php(160): Stripe\ApiResource::_staticRequest('post', '/v1/charges', Array, NULL)
#4 /home3/blueman/public_html/assistforlife.com/stripe /lib/Charge.php(73): Stripe\ApiResource::_create(Array, NULL) #5 /home3/blueman/public_html/assistforlife.com/charge.ph in /home3/blueman/public_html/assistforlife.com/stripe/lib/ApiRequestor.php on line 114

I wrote my own code and it said that the value for my card was "null" I re-wrote it borrowing somebody else's code and it posted the customer but not any of the charge information.

1

1 Answers

1
votes

In your server-side PHP code, $token must have a null value, so the customer is created without a payment source. Then when you try to use it to create a charge, the request fails with "Cannot charge a customer that has no active card".

Your form's code looks correct so I'm not sure why $token is empty. I'd recommend using your browser's dev tools to make sure that the token ID ("tok_...") created by Stripe.js is really sent as the stripeToken POST parameter.

On an unrelated note, according to this line:

<script type="text/javascript" src="v2.js"></script>

it look like you're locally hosting Stripe.js. Note that in order to be PCI compliant, you must use the version that's hosted on Stripe's own servers, at https://js.stripe.com/v2/.