I am using JSF/Javascript/Jquery to build a creditcard payment form using Stripe. Following is the code:
JSF Form
<h:form id="payment-form">
:
:
Input text fields
:
<h:commandButton id="submitBtn" value="Submit Payment" styleClass="button round blue" action="#{userBean.submitPayment}">
</h:commandButton>
</h:form>
The submit event handler in Jquery is as follows -
function stripeResponseHandler(){
alert("in responseHandler");
var f = $("#payment-form");
// Token contains id, last4, and card type:
var token = response['id'];
// Insert the token into the form so it gets submitted to the server
f.append("<input type='hidden' name='stripeToken' value='" + token + "' />");
// re-enable the submit button
$("#payment-form\\:submitBtn").attr("disabled", false);
// Submit the form
//$("#payment-form")[0].submit();
return false;
}
function reportError(msg) {
// Show the error in the form:
$('#payment-errors').text(msg).addClass('alert alert-error');
// re-enable the submit button:
$("#payment-form\\:submitBtn").prop('disabled', false);
return false;
}
$(document).ready(function(){
$("#payment-form").submit(function(event) {
alert("in eventHandler");
// Flag variable:
var error = false;
// disable the submit button to prevent repeated clicks:
$("#payment-form\\:submitBtn").attr("disabled", "disabled");
// Get the values:
// Validate the fields
// Validate other form elements, if needed!
// Check for errors:
if (!error) {
//alert("Getting the token");
// Get the Stripe token:
Stripe.card.createToken({
number: ccNum,
cvc: cvcNum,
exp_month: expMonth,
exp_year: expYear
}, stripeResponseHandler);
}
event.preventDefault();
});
});
(Code taken from Stripe to integrate with JSF form)
The above code does not invoke the backing bean method mentioned in the action parameter of the h:commandButton when it is called from stripeResponseHandler, which is a callback for the asynchronous response from Stripe.
Can someone please shed light on what is wrong in the above code.