I'm following the basic example from https://stripe.com/docs/checkout/guides/php and having problems seeing how the amount can be determine and use in the creation of the charge.
I would have thought/hoped that using the token I created I would be able to retrieve the amount associated with it and pass this amount into \Stripe\Charge::create()?
For example I have some PHP/template code that generates a form for a range of products
# index.php
<?php require_once('./config.php'); ?>
<form action="charge.php" method="post">
<script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="<?php echo $stripe['publishable_key']; ?>"
data-amount="{$MY_AMOUNT}" data-description="One year's subscription"></script>
</form>
$MY_AMOUNT will change according to the product that is being displayed
Now charge.php receives the post and creates a charge but what is the correct way to specify the value (currently 5000) of the 'amount' index of the array?
<?php
require_once('./config.php');
$token = $_POST['stripeToken'];
$customer = \Stripe\Customer::create(array(
'email' => 'customer@example.com',
'card' => $token
));
$charge = \Stripe\Charge::create(array(
'customer' => $customer->id,
'amount' => 5000,
'currency' => 'usd'
));
echo '<h1>Successfully charged $50.00!</h1>';
?>
So, there are 2 approaches I can think of here
Passing the key and amount as hidden variables, these doesn't seem "right" as it can easily be changed, e.g. below
<form action="charge.php" method="POST">
<script
src="https://checkout.stripe.com/checkout.js" class="stripe-button"
data-key="{$key}"
data-amount="{$amount}"
data-currency="GBP"
data-address="true"
data-name="{$name}"
data-description="{$description}"
data-image="logo.png">
</script>
<input name="myHiddenKey" type="hidden" value="{$amount}" />
<input name="myHiddenName" type="hidden" value="{$name}" />
</form>
No the token is just a precheck to allow you to make a charge, the amount being passed through to Stripe in the Javascript is only used by Stripe for display purposes. Stripe_Charge::create
is where you actually pass through the amount.