Jens Van den Eede
Jens Van den Eede

Reputation: 95

Why is Stripe not working in this example?

I am trying to make a website and I am looking into payment gateways. Stripe was the first I came across and I am trying this. Before I ask my question about it, I would like to know if it's any good or are there better alternatives?

The problem I have is the following. I can't make a payment with the following code. Firstly I use this form the submit the info to the js file and get a token.

<form  class="pay" action="payment.php" method="POST">
          <script
            src="https://checkout.stripe.com/checkout.js" class="stripe-button"
            data-key=" filled in ... "
            data-amount="999"
            data-name="Demo Site"
            data-description="Widget"
            data-image="https://stripe.com/img/documentation/checkout/marketplace.png"
            data-locale="auto"
            data-zip-code="true"
            data-currency="eur">
          </script>
</form>

Then I use this php where it goed to via action.

<?php

// 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
\Stripe\Stripe::setApiKey(" filled in ... ");

// Token is created using Stripe.js or Checkout!
// Get the payment token submitted by the form:
$token = $_POST['stripeToken'];

// Charge the user's card:
$charge = \Stripe\Charge::create(array(
  "amount" => 1000,
  "currency" => "eur",
  "description" => "Example charge",
  "source" => $token,
));

?>

Trust me the codes are filled in correctly in my real code.

If you could help me that would be awesome!

Thanks in advance, Jens Van den Eede.

Upvotes: 1

Views: 3136

Answers (2)

hpar
hpar

Reputation: 748

First, I'd put print_r($token); right after the line that says:

$token = $_POST['stripeToken'];

This should show you the value of the token you're getting from Stripe.js.

If the value is null, this tells you that something's happening on your frontend and the hidden stripeToken field is not getting injected. At that point, I'd use Web Inspector to debug the code on the page and make sure that the request getting sent to your PHP server contains the serverToken field.

As far as the 500 error, my guess is that because the value of $token there is null or missing. The best way to determine this would be to implement the error handling code shown in the Stripe API docs here:

https://stripe.com/docs/api/php#errors

This will trap any error and print it out, rather than allow it to cascade and cause a 500 error. Finally, there's my go-to for whenever I can't find a problem with a PHP program, which is to put these two lines at the top of the script:

ini_set('error_reporting', E_ALL);
ini_set('display_errors', true);

ALWAYS remove those before putting your code in production, as it can leak sensitive info. However, in development and testing that will cause PHP to print out any errors or warnings along the way, which can help track down subtle issues.

The other possibility is that your code isn't able to load the Stripe PHP library for some version; you may need to download this library or use Composer to fetch it so that it's available in the 'vendor/' folder as your PHP code says.

If you haven't used Composer, I'd recommend following the manual installation steps instead:

https://github.com/stripe/stripe-php#manual-installation

Upvotes: 0

Anil M
Anil M

Reputation: 100

I see that you have not included autoload.php, are you using composer? Then write this before using stripe.

require_once('vendor/autoload.php');

And about the payment solution choices, it depends on what countries you want to target, alternatives are Braintree, Mangopay.

Upvotes: 1

Related Questions