0
votes

hey guys I am fairly new to programming. Would really appreciate your help. I am trying to setup Stripe payment but upon charging the card I am getting this very long error message again and again.

Fatal error: Uncaught exception 'Stripe\Error\InvalidRequest' with message 'No such token: false' in C:\xampp\htdocs\vendor\stripe\stripe-php\lib\ApiRequestor.php:124 from API request 'req_MN1aLPlF6OMPIv' Stack trace: #0 C:\xampp\htdocs\vendor\stripe\stripe-php\lib\ApiRequestor.php(102): Stripe\ApiRequestor::_specificAPIError('{\n "error": {\n...', 400, Array, Array, Array) #1 C:\xampp\htdocs\vendor\stripe\stripe-php\lib\ApiRequestor.php(309): Stripe\ApiRequestor->handleErrorResponse('{\n "error": {\n...', 400, Array, Array) #2 C:\xampp\htdocs\vendor\stripe\stripe-php\lib\ApiRequestor.php(65): Stripe\ApiRequestor->_interpretResponse('{\n "error": {\n...', 400, Array) #3 C:\xampp\htdocs\vendor\stripe\stripe-php\lib\ApiResource.php(119): Stripe\ApiRequestor->request('post', '/v1/charges', Array, Array) #4 C:\xampp\htdocs\vendor\stripe\stripe-php\lib\ApiResource.php(158): Stripe\ApiResource::_staticRequest('post', '/v1/charges', Array, NULL) #5 C:\xampp\htdocs\vendor\stripe\stripe-php\lib\Charge.php(74): Stripe\ApiResourc in C:\xampp\htdocs\vendor\stripe\stripe-php\lib\ApiRequestor.php on line 124

I am absolutely clueless what does this mean. I have literally copy/pasted every thing from stripe. Below are my codes

Stripe.setPublishableKey('<?=STRIPE_PUBLIC;?>');

function stripeTokenHandler(token) {

Insert the token ID into the form so it gets submitted to the server

var form = document.getElementById('payment-form');
var hiddenInput = document.createElement('input');
hiddenInput.setAttribute('type', 'hidden');
hiddenInput.setAttribute('name', 'stripeToken');
hiddenInput.setAttribute('value', token.id);
form.appendChild(hiddenInput);

Submit the form

form.submit();
  }

Create a token or display an error when the form is submitted.

  var form = document.getElementById('payment-form');
  form.addEventListener('submit', function(event) {
   event.preventDefault();

  stripe.createToken(card).then(function(result) {
   if (result.error) {

Inform the customer that there was an error'

  var errorElement = document.getElementById('card-errors');
  errorElement.textContent = result.error.message;
  } else {

Send the token to your server

      stripeTokenHandler(result.token);
       }
    });
   });

After this I am taking my customer to ThankYou.php with following code

<?php
require_once '/init.php';

Set your secret key: remember to change this to your live secret key in production

\Stripe\Stripe::setApiKey(STRIPE_PRIVATE);

Token is created using Checkout or Elements! Get the payment token ID submitted by the form:

$token = isset($_POST['stripeToken']);

Charge the user's card:

$charge = \Stripe\Charge::create(array(
 "amount" => 1000,
 "currency" => "usd",
 "description" => "Example charge",
 "source" => $token,
 ));
1

1 Answers

0
votes

This line:

$token = isset($_POST['stripeToken']);

will assign true or false to the variable $token depending on whether there is stripeToken POST parameter or not.

So when you pass $token in the charge creation request here:

$charge = \Stripe\Charge::create(array(
  "amount" => 1000,
  "currency" => "usd",
  "description" => "Example charge",
  "source" => $token,
 ));

the source parameter will have either true or false as its value. This is not what you want -- rather, you should simply assign the value of the stripeToken POST parameter to your $token variable:

$token = $_POST['stripeToken'];

That said, according to the error message: "No such token: false", your $token variable had a value of false which indicates your backend PHP script did not receive a stripeToken POST parameter. This indicates that there is another issue, probably in your frontend code where you create the token.

If you are new to web development, I advise that you look at Checkout for collecting card information and creating tokens -- it's a prebuilt payment form that does most of the work for you.

You can find a tutorial for using Checkout with PHP here: https://stripe.com/docs/checkout/php