I have a form that is being used on a donation page. I am using the "gform_validation" hook to test the payment information with a third party processor and if the payment is declined then I want to display a custom validation message (via the "gform_validation_message" function). If the payment goes through and is accepted then I would like the text confirmation message that is attached to the form to display. I am having issues getting this to work the way I'd like. Here is the code I've got thus far:
add_filter('gform_validation_9', 'validate_donation');
function validate_donation($validation_result) {
// Make a call to process_payment function to process payment and check validity
$is_valid = process_payment();
// If the payment is valid don't do anything, return $validation_result
if($is_valid){
// Return the validation result
return $validation_result;
}
else{
// Fail the validation for the entire form
$validation_result['is_valid'] = false;
// Return the validation result
return $validation_result;
}
}
// This function runs the Donation script for third party processor
function process_payment() {
include("donate.php");
}
Currently I have the donate.php file processing of the donation and returning true if it goes through and false if there is a problem. The custom message is being called like this:
add_filter("gform_validation_message_9", "change_message", 10, 2);
function change_message($message, $form){
global $donate_error;
return '<div id="donate_error">' . "An error occurred while processing your submission.<br/>Please modify your information based on the error below and try again.<br/><br/>" . '<span>' . "Error: {$donate_error}" . '</span>' . '</div>';
}
The $donate_error is a global variable that is being called/set from within the donate.php file and displays the error (if any) returned from the third party processor. The problem that I am experiencing is that if the payment is declined the custom validation message shows up as it should and the included error message ($donate_error) is displayed correctly but if the payment goes through and is accepted the custom validation message still shows up and the included error message ($donate_error) is displayed but as an empty string. What am I doing wrong here? It appears that the validation is getting stuck on false or something. Here is link to my form page: https://www.biglifeonlinetest.org/donate