Reputation: 15
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' => '[email protected]',
'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.
Upvotes: 1
Views: 1007
Reputation: 1371
Yes, your method of assigning the amount to the form is incorrect.you have to do that using JavaScript
Note that anyone can change the amount. so you might wanna check the actual amount before processing the payment.
<form action="charge.php" method="post">
<!-- .... -->
<input class="form-control" type="number" id="donation-amount" placeholder="20.00" min="0" step="5.00"/>
<button id="customButton">Donate</button>
<!-- give an id to charge amount field -->
<input type="hidden" id="amount" name="chargeamount" val=""/>
<!-- .... -->
<script>
//....
var amount = $("#donation-amount").val() * 100;
//Assign the amount to the amount field.
$('input#amount').val(amount);
//....
</script>
</form>
Upvotes: 0