How to pass stripe token to the server, create a user account, charge the credit card and have it all show up on the dashboard

StackOverflow https://stackoverflow.com/questions/20641652

Question

So I'm trying to get stripe setup on my server but I'm having a problem. I'm using checkout v2 on my page and the form works perfectly, however the token never passes through to my php file. the card is charged but the information does not show up on the dashboard, it is in the log though.

here's what I have:

<form action="assets/php/slingshot.php" method="POST">
 <button id="customButton">Purchase</button>
 <script>
    $('#customButton').click(function(){
    var token = function(res){
    var $input = $('<input type=hidden name=stripeToken />').val(res.id);
    $('form').append($input).submit();
    };

  StripeCheckout.open({
    key:         'pk_live_*************************',
    address:     true,
    amount:      2500,
    currency:    'usd',
    name:        'test',
    description: 'A bag of test',
    panelLabel:  'Checkout',
    token:       token
  });

  return false;
  });
  </script>
</form>

and then for slingshot.php:

<?php
require_once(dirname(__FILE__) . '/config.php');

$token = $POST['stripeToken']; //get the creditcard details from the form
try {       
$charge = Stripe_Charge::create(array(
        'card' => $token,
        'amount'   => 2500,  //amount in cents
        'currency' => 'usd',
        'description' => 'Get Bacon Direct, LLC'
        ));

        } catch(Stripe_CardError $e) {
// The card has been declined
}
echo '<h1>Successfully charged $25.00 </h1>';
}
?>

and my config.php file:

<?php
require_once('stripe-php/lib/Stripe.php');
// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://manage.stripe.com/account

$stripe = array(
"secret_key"        => "sk_live_************************************",
"publishable_key"   => "pk_live_************************************"
);

Stripe::setApiKey($stripe['secret_key'] );

?>

could you please help me out?

Was it helpful?

Solution

I'm not entirely sure how the charge is being made, but I'm fairly confident that the reason you aren't getting the token, is because of this line

$token = $POST['stripeToken']; //get the creditcard details from the form

When accessing POST data in PHP, the global variable is prefixed with an underscore, so you'd need to do

$token = $_POST['stripeToken']; //get the creditcard details from the form

That should fix it.

-- UPDATE ---

Just for readability, in your javascript, I would specify the input field as follows

var $input = $('<input type="hidden" name="stripeToken" />').val(res.id);

I've also checked out the stripe documentation again, and it looks like the card attribute is optional, as is customer, but it does state that one must be provided. Chances are, it's not throwing an error because of an issue with their code that doesn't catch the eventuality of neither being provided.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top