I am try to simplify this as much as possible. When a user hit the submit button the following happens:
- Validate if the credit card/expiry date/ cvc value are valid
- A token is generated as an input
- The value of the token is posted in the php code
- 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:
- I cannot run the query if no token value is found or available
- the php script cannot be run until the input token is generated
- 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.