How can I update my PHP script based on my user input on HTML form

478 Views Asked by At

This is how it is submitted, once you click pay with card a popup modal appears to enter payment detailsI am trying to create custom payments using stripe API.

I would like the user to be able to input any number for a payment within a form and have my php get the input to charge the card for the selected amount.

In my php code the "amount is usually set to a fixed number like 50.00" but I would like it to be dependent on the users input in the html form.

Here is what I have so far.

Thanks

<form action="testprocess.php" method="POST">
<h3> Enter Payment Amount</h3> 

<input type="number" name="pay" id="pay">

<script
   src="https://checkout.stripe.com/checkout.js" class="stripe-button"
   data-key="pk_test_00000000000"
   data-amount=""
   data-name="test"
   data-description="text">
</script>
</form>


     ___________________


 <?php

  // See your keys here: asgfcvbjh


  \Stripe\Stripe::setApiKey("sk_test_0000000000000");

   // Token is created using Stripe.js or Checkout!
   // Get the payment token submitted by the form:

   // Charge the user's card:

  $pay = $_POST['pay'];


  $charge = \Stripe\Charge::create(array(
    "amount" => $pay,
    "currency" => "usd",
    "description" => "test",
  ));


     ?>
1

There are 1 best solutions below

0
On

You can do it the following way, you have to create a custom checkout. Credit goes to this guy.

 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
 <script src="https://checkout.stripe.com/checkout.js"></script>

Amount: <input type="text" id="stripeAmount">
<button class="btn btn-success" id="stripe-button">
    Checkout
</button>

<script>
    $('#stripe-button').click(function () {
        var token = function (res) {
            var $id = $('<input type=hidden name=stripeToken />').val(res.id);
            var $email = $('<input type=hidden name=stripeEmail />').val(res.email);
            $('form').append($id).append($email).submit();
        };

        var amount = (Number($("#stripeAmount").val()) * 100);
        StripeCheckout.open({
            key: '<?= STRIPE_PUBLIC_KEY ?>',
            amount: amount,
            name: 'Serendipity Artisan Blends',
            //image: 'path to yoru image',
            description: 'Purchase Products',
            panelLabel: 'Checkout',
            token: token
        });

        return false;
    });
</script>

Output enter image description here