2

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
    Why dont you use `submit` button – Yash Parekh Jul 24 '17 at 08:18
  • how are you even submitting your form? There's no button or anything, unless that's what that stripe script creates? And also is that PHP in the same script as the HTML? Its not clear. If it is, you've got a problem because your PHP doesn't check to see if the form has been submitted before it tries to read the form values. You want to wrap that PHP in something like `if(isset($_POST["pay")) { ... }` otherwise it'll try to run that code when you first display the form, as well as when it's submitted. – ADyson Jul 24 '17 at 08:33

1 Answers1

0

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

Muhammad
  • 6,725
  • 5
  • 47
  • 54