I am trying to get Stripe's Checkout Custom Button to charge a credit card but all it does is minimize after I enter the credit card details. I am using the code found in the documentation but I can't get it to work. The simple button is easy to use and I figured it would be as easy as just customizing that one but it's very different.
Here's the code:
Payment Page
<form action="charge.php" method="post">
<script src=".js"></script>
<input type="submit" value="Pay with card" id="customButton"/>
<?php require_once('./config.php'); ?>
<script>
var handler = StripeCheckout.configure({
key: '<?php echo $stripe['publishable_key']; ?>',
image: 'favicon.png',
token: function(token, args) {
// Use the token to create the charge with a server-side script.
// You can access the token ID with `token.id`
}
});
document.getElementById('customButton').addEventListener('click', function(e) {
// Open Checkout with further options
handler.open({
name: 'Imprintnation',
description: 'Decals',
amount: <?php echo $stripeTotal; ?>
});
e.preventDefault();
});
</script>
</form>
Charge Page(charge.php)
<?php
require_once(dirname(__FILE__) . '/config.php');
$token = $_POST['stripeToken'];
$customer = Stripe_Customer::create(array(
'email' => '[email protected]',
'card' => $token
));
$charge = Stripe_Charge::create(array(
'customer' => $customer->id,
'amount' => 5000,
'currency' => 'usd'
));
echo '<h1>Successfully charged $5!</h1>';
?>
Configuration Page(config.php)
<?php
require_once('./lib/Stripe.php');
$stripe = array(
secret_key => 'sk_test_************************',
publishable_key => 'pk_test_************************'
);
Stripe::setApiKey($stripe['secret_key']);
?>
What am I missing here? I can't even get it to retrieve a token.
How can I retrieve a token and charge a card?
I am trying to get Stripe's Checkout Custom Button to charge a credit card but all it does is minimize after I enter the credit card details. I am using the code found in the documentation but I can't get it to work. The simple button is easy to use and I figured it would be as easy as just customizing that one but it's very different.
Here's the code:
Payment Page
<form action="charge.php" method="post">
<script src="https://checkout.stripe./checkout.js"></script>
<input type="submit" value="Pay with card" id="customButton"/>
<?php require_once('./config.php'); ?>
<script>
var handler = StripeCheckout.configure({
key: '<?php echo $stripe['publishable_key']; ?>',
image: 'favicon.png',
token: function(token, args) {
// Use the token to create the charge with a server-side script.
// You can access the token ID with `token.id`
}
});
document.getElementById('customButton').addEventListener('click', function(e) {
// Open Checkout with further options
handler.open({
name: 'Imprintnation',
description: 'Decals',
amount: <?php echo $stripeTotal; ?>
});
e.preventDefault();
});
</script>
</form>
Charge Page(charge.php)
<?php
require_once(dirname(__FILE__) . '/config.php');
$token = $_POST['stripeToken'];
$customer = Stripe_Customer::create(array(
'email' => '[email protected]',
'card' => $token
));
$charge = Stripe_Charge::create(array(
'customer' => $customer->id,
'amount' => 5000,
'currency' => 'usd'
));
echo '<h1>Successfully charged $5!</h1>';
?>
Configuration Page(config.php)
<?php
require_once('./lib/Stripe.php');
$stripe = array(
secret_key => 'sk_test_************************',
publishable_key => 'pk_test_************************'
);
Stripe::setApiKey($stripe['secret_key']);
?>
What am I missing here? I can't even get it to retrieve a token.
How can I retrieve a token and charge a card?
Share Improve this question asked Mar 20, 2014 at 22:00 JJJJJJ 3,3325 gold badges30 silver badges45 bronze badges 4-
presumably you would have to set a callback page on stripe. and direct them to your
charge.php
that would get you the token, im assuming stripe works similar to paypal, i did not check – andrew Commented Mar 20, 2014 at 22:16 - This is directly from their documentation and it doesn't mention anything about setting up a callback page. – JJJ Commented Mar 20, 2014 at 22:22
-
you could try
var_dump($charge)
to see if anything got returned. also check your server has a curl extension installed for php, it will need it to interact with their server – andrew Commented Mar 20, 2014 at 22:59 - It appears they callBack to the same page from which you made the post so you're probably right, no need to configure that stripe./docs/tutorials/forms#create-a-single-use-token – andrew Commented Mar 20, 2014 at 23:05
2 Answers
Reset to default 12Finally got it to work. Had to change a bunch of it. Here is the code:
Payment page
<form action="charge.php" method="post">
<script src="https://checkout.stripe./checkout.js"></script>
<script src="http://code.jquery./jquery-latest.min.js"></script>
<button id="customButton">Pay with Card</button>
<style>
#customButton{
width:300px;
height:50px;
background-color:orange;
color:white;
border:2px solid green;
}
</style>
<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: '<?php echo $stripe['publishable_key']; ?>',
address: false,
amount: '<?php echo $price; ?>',
currency: 'usd',
name: 'test',
description: '<?php echo $desc; ?>',
panelLabel: 'Checkout',
token: token
});
return false;
});
</script>
<input type="hidden" name="desc" value="<?php echo $desc; ?>"/>
<input type="hidden" name="totalPrice" value="<?php echo $price; ?>"/>
</form>
charge.php
<?php
require_once(dirname(__FILE__) . '/config.php');
$token = $_POST['stripeToken'];
$amount = $_POST['totalPrice'];
$desc = $_POST['desc'];
$percent = "0.01";
$realAmount = $amount * $percent;
try {
$charge = Stripe_Charge::create(array(
'card' => $token,
'amount' => $amount,
'currency' => 'usd',
'description' => $desc
));
} catch(Stripe_CardError $e) {
// The card has been declined
}
echo "<h1>Successfully charged $$realAmount!</h1>";
?>
I wish Stripe's documentation was more straightforward but this code handles the charge and it logs it on your dashboard.
@BlueSix is right, why do you have:
<input type="hidden" name="totalPrice" value="<?php echo $price; ?>"/>
Just because the value is hidden, does not mean it cannot be edited by the end user.
It would be much better to set $amount = $price
directly, passing the variable through your application backend.