3

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

  1. A PHP page per product with hardcoded amounts - No thanks
  2. 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>
    
David Nguyen
  • 8,368
  • 2
  • 33
  • 49
Carlton
  • 5,533
  • 4
  • 54
  • 73

1 Answers1

0

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.

David Nguyen
  • 8,368
  • 2
  • 33
  • 49
  • I understand the data attributes and the charge amount but if you work through any of their "checkout" examples they always seem to hard code the amount value in the script handling the "charge". This would be fine if you have one product but I have mutliple. – Carlton May 13 '15 at 15:01
  • Pass in the total, not sure what you are getting at - they are just samples. – David Nguyen May 13 '15 at 15:05