Send amount to Stripe

621 Views Asked by At

I would like know how send a custom amount to the final stripe process without input type hidden, in another words I would get the data-amount from the form

this is my form:

$Total = 1000000;

<form action="stripe.php" method="POST">
   <script
      src="https://checkout.stripe.com/checkout.js" class="stripe-button"
      data-key= '.$stripe["publishable"].'
      data-amount='.$Total.'
      data-name="My Web Site"
      data-description="Reservation"
      data-email='.$_POST["email"].'
      data-image="img/logo2.png"
      data-locale="auto">
   </script>
</form>

and this would be my process

if(isset($_POST['stripeToken']))
{
    $toke = $_POST['stripeToken'];
    $Amount = $_POST["stripeAmount"];   //  I don't know how to get this value
    $email = $_POST["stripeEmail"];     //  Email works good

    try
    {
        Stripe_Charge::create(array(
          "amount" => $Amount, //  this doesn't work
          "currency" => "usd",
          "source" => $toke, // obtained with Stripe.js
          "description" => $email
        ));
    }
    catch(Stripe_CardError $e)
    {
        alert("Error");
    }
    header('Location: index.php');
    exit();
}

with print_r($_POST) I get

Array ( [stripeToken] => tok_1Ax6UbIUY34Xblablablabla [stripeTokenType] => card [stripeEmail] => [email protected] )

so, I know $_POST["stripeAmount"] doesn't exist but I believe exist one way to do that without a input type hidden

1

There are 1 best solutions below

1
On BEST ANSWER

Why not store the total in the session?

$_SESSION['grandtotal'] = $total;

Then in the file that gets posted to, you can pull that value and change it to cents (Stripe requirement) before adding to the Stripe_Charge object:

if(isset($_POST['stripeToken']))
{
    $toke = $_POST['stripeToken'];
    $amount = $_SESSION["grandtotal"];  
    $amount *= 100; //convert from dollars to cents.
    $email = $_POST["stripeEmail"];

    try
    {
        Stripe_Charge::create(array(
          "amount" => $amount, 
          "currency" => "usd",
          "source" => $toke, // obtained with Stripe.js
          "description" => $email
        ));
    }
    catch(Stripe_CardError $e)
    {
        alert("Error");
    }
    header('Location: index.php');
    exit();
}