I'm trying to implement the method for using a custom payment in Stripe provided in the following question, stripe checkout custom button not charging. The user inputs a value for the amount which is displayed on the submit button.
Index.php
<form action="charge.php" method="post">
<input class="form-control" type="number" id="donation-amount" placeholder="20.00" min="0" step="5.00"/>
<script src="https://checkout.stripe.com/v2/checkout.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.js"></script>
<button id="customButton">Donate</button>
<script>
$('#customButton').click(function(){
var token = function(res){
var $input = $('<input type=hidden name=stripeToken />').val(res.id);
$('form').append($input).submit();
};
var amount = $("#donation-amount").val() * 100;
StripeCheckout.open({
key: 'pk_test_*************************',
address: false,
amount: amount,
currency: 'usd',
name: 'Test Customer',
description: 'Demo Description',
panelLabel: 'Checkout',
token: token
});
return false;
});
</script>
<input type="hidden" name="chargeamount" val=<?php amount ?>/>
</form>
The submit button displays the correct amount, but when clicking the submit button a white screen with no charge occurring.
Charge.php
<?php
require_once(dirname(__FILE__) . '/config.php');
$token = $_POST['stripeToken'];
$amount = $_POST['chargeamount'];
$customer = Stripe_Customer::create(array(
'email' => 'test@example.com',
'card' => $token
));
$charge = Stripe_Charge::create(array(
'customer' => $customer->id,
'amount' => $amount,
'currency' => 'usd'
));
echo '<h1>Successfully charged '. $amount. '!</h1>';
?>
Config.php
<?php
require_once('vendor/stripe/lib/Stripe.php');
$stripe = array(
"secret_key" => "sk_test_************************",
"publishable_key" => "pk_test_************************"
);
Stripe::setApiKey($stripe['secret_key']);
?>
$charge = Stripe_Charge::create(array(
'customer' => $customer->id,
'amount' => $amount
'currency' => 'usd'
));
echo '<h1>Successfully charged '. $amount .'!</h1>';
?>
I'm thinking my implementation of the input
at the bottom of my form is a bit shakey, considering that if I insert a fixed number into my $amount
variable in the charge.php, it DOES actually charge that payment.
Any assistance would be greatly appreciated.