Best approach to control confirmation message from ajax submission

I have created a Gravity Form that allows users to make payments. The form contains regular Gravity Form fields, as well as an embedded iframe that is hosted cross domain. This iframe contains input text boxes for credit card, expiry date, and CSV. I’m using the ‘gform_submit_button’ filter to add an onclick attribute that points to a JavaScript function. This JavaScript function contains code that handles the form submission.

When the form is submitted, an iframe postMessage is executed and it’s returned message is handled by another JavaScript function. This JavaScript function contains code to test if the returned message data contains a valid token, and if so, makes an ajax call to a specific function within functions.php to process the token.

During the processing of the token, various API call are made to validate the credit card/customer details. I would like to use the ajax success/error callback functions to determine what confirmation message should be displayed to the user. For example, if an error was encountered because the credit card doesn’t have enough funds to cover the payment amount, I would like to send back this corresponding error message and have it displayed as the form’s confirmation message. Because the error message is being set within the called ajax function, and now being sent to JavaScript, I’m unsure how I can replace the default confirmation message with an updated one.

Perhaps this is simple, but I’m stumped. I’ve been looking at this all day and can’t find a solution.


Have you looked at the gform_confirmation filter:

That is normally where you would change the confirmation message when using code. I am not sure how that works with your AJAX calls, but that would be the time to do it.

Otherwise, maybe your AJAX call can populate a field in the $_POST which is submitted with the form, and then you include that message in the confirmation message using merge tags for that field.