0

I am try to simplify this as much as possible. When a user hit the submit button the following happens:

  1. Validate if the credit card/expiry date/ cvc value are valid
  2. A token is generated as an input
  3. The value of the token is posted in the php code
  4. the query runs and stores the value and page is relocated

I have been able to manage up until number 2. I am unable to post the value of the token, and hence the token value is not stored.

Constraints:

  1. I cannot run the query if no token value is found or available
  2. the php script cannot be run until the input token is generated
  3. Once the php query has been executed, the token input needs to be destroyed

Below is the js function that generates the token:

 <script type="text/javascript">
    // This identifies your website in the createToken call below
  Stripe.setPublishableKey('CODE');

  var stripeResponseHandler = function(status, response) {
      var $form = $('#payment-form');

      if (response.error) {
        // Show the errors on the form
        $form.find('.payment-errors').text(response.error.message);
        $form.find('button').prop('disabled', false);
      } else {
        // token contains id, last4, and card type
        var token = response.id;
        // Insert the token into the form so it gets submitted to the server
        $form.append($('<input type="text" name="stripeToken" />').val(token));
        // and re-submit
      }
    };

    jQuery(function($) {
      $('#payment-form').submit(function(e) {
        var $form = $(this);

        // Disable the submit button to prevent repeated clicks
        $form.find('button').prop('disabled', true);

        Stripe.card.createToken($form, stripeResponseHandler);

        // Prevent the form from submitting with the default action
        return false;
      });
    });

  </script>

Below is the php code

 if(isset($_POST['paid'])){
          $course_token = $_POST['stripeToken'];

if (!empty($course_token)) {

    $course_price_final = $_POST['course_price_final'];
    $course_provider = $_POST['course_provider'];
    $user_email = $_POST['user_email'];
    $course_delivery = $_POST['course_delivery'];
    $order_date = date("Y-m-d");
    $insert_c = "insert into orders (course_title,course_price_final,course_provider,user_email,course_date,course_delivery,order_date,course_token) 
             values ('$crs_title','$course_price_final','$course_provider','$user_email','$course_date1','$course_delivery','$order_date','$course_token')";
    $run_c = mysqli_query($con, $insert_c);

ob_start();

}
}
?>

Thanks in advance, and for any clarification, let me know.

code_legend
  • 3,547
  • 15
  • 51
  • 95
  • You are or aren't getting a token back from stripe? – Jack Apr 17 '15 at 16:51
  • I am retrieving the token from stripe and it does output into an input so visually i can see it, my problem lies in grabbing that value once the input is populated and posting into the php as a variable to that the query can execute – code_legend Apr 17 '15 at 16:51

2 Answers2

1

It looks like you're missing a submit in the stripeResponseHandler:

$form.get(0).submit();

The return false in your submit function, as the comment indicates, prevents the form from being submitted. This is necessary to allow Stripe to process the request. You provide Stripe with the stripeResponseHandler function which it will call once it has had a chance to satisfy your request and return a valid token or fail with an error. Do not remove the

return false;

statement as that will interfere with the work-flow allowing Stripe to process the request properly.

Your stripeResponseHandler needs to handle any errors from Stripe and if none are found then it needs to submit the form. The code I have above is what I use on my web site to submit the form.

get(0) returns the DOM element and the submit issues the submit directly.

Dave D
  • 430
  • 3
  • 8
  • Thanks. With that the token gets generated but just refreshes the page. Also i would like to put a criteria where when the token is generated it cannot be generated again, where now if you hit the button 5 times, than 5 inputs are generated. – code_legend Apr 17 '15 at 17:07
  • my objective is not to use stripe to submit the form, but to just grab the value of the input that is generated so that can be used in the php code above. when i add $form.get(0).submit(); it just refreshes the page – code_legend Apr 17 '15 at 17:11
  • This is more of a workflow question. My site is written in python or else I would give you sample code. But the workflow in PHP is: - accept the token - call the Stripe library to charge the card - store the information appropriately - direct the user to a new page with a receipt or clear the form I allow multiple registrations and charges so I simply clear the form with a message at the top indicating they've been charged. – Dave D Apr 17 '15 at 17:13
  • thanks for your response. the problem is that the user not charged immediatetly rather afterwards. so i am trying to store the token value to use it at a later time – code_legend Apr 17 '15 at 17:18
  • how about i active the php code once the input has been generated – code_legend Apr 17 '15 at 17:19
  • That's fine, simply store the token for charge later. It's still in your workflow though that you will need to give the do some processing so the user is aware that their credit card was or will be charged. See here:http://stackoverflow.com/questions/17157685/php-redirect-to-another-page-after-form-submit – Dave D Apr 17 '15 at 17:22
  • but thats where my problem lies. in storing the token – code_legend Apr 17 '15 at 17:25
  • Sorry, I thought you were trying to get past step 2, the token being posted to the PHP code. Can you edit your question and add the error message you're getting when you attempt to store the token? I should have paid more attention, use $_GET instead of $_POST – Dave D Apr 17 '15 at 17:29
  • i am now using _get but i dont see any errors, but yet the php code is not executed – code_legend Apr 17 '15 at 17:58
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/75539/discussion-between-dave-d-and-user3907211). – Dave D Apr 17 '15 at 17:59
1

Remove the return false statement at the end of submit handler. It prevents the form from being posted.

You can actually append the input before submit and submit your form later.

jQuery(function($) {
  $('#payment-form').click(function(e) {
    var $form = $(this);

    // Disable the submit button to prevent repeated clicks
    $form.find('button').prop('disabled', true);

    Stripe.card.createToken($form, stripeResponseHandler);

    // Prevent the form from submitting with the default action
    $form.submit();
  });
});
bipen
  • 36,319
  • 9
  • 49
  • 62
  • Thanks. With that the token gets generated but just refreshes the page. Also i would like to put a criteria where when the token is generated it cannot be generated again, where now if you hit the button 5 times, than 5 inputs are generated. – code_legend Apr 17 '15 at 17:07
  • Removing the return false here will prevent Stripe from calling the stripeResponseHandler with the token. The Stripe.card.createToken() function receives a function name that is to be called upon Stripe's completion. Submitting the form in this function will cause the form to be submitted before that processing is complete. – Dave D Apr 17 '15 at 17:19