Hi zaus,
I have a similar issue as this one. When my 3rd party return an error response, I need to be able to skip sending email and stop the redirection to the thank-you page. My implementation is below but it’s not working. Could you please review and let me know if you see anything wrong with it. Thanks!
My response has a json string in the body and an 400 error code in the response as following:
[body] => {“status”:”error”, “response”:{“error message”,”error.header”:”There was a problem. Please check the errors below:”}}
[response] => Array
(
[code] => 400
[message] => Bad Request
)
I tried your suggestion of the filter and action as below but neither one works, could you take a look and see if I may have done something incorrectly:
---Using filter to process the error status from the body ---
class MyPlugin {
public function MyPlugin() {
add_filter('Forms3rdPartyIntegration_service', array(&$this, 'adjust_response'), 10, 2);
}
public function adjust_response($body, $refs) {
$response_array = json_decode($body);
try{
if( false !== strpos($response_array->status, 'error') ) :
{$refs['message'] = 'Error message';
add_filter('wpcf7_before_send_mail', 'wpcf7_custom_form_action_url');}
else:
$refs['message'] = 'Success message';
endif;
} catch(Exception $ex){
$refs['errors'] = array($ex->getMessage());
}
}
}
new MyPlugin();
---Using action to process response code from the response ---
function service1_action_callback($response, $results){
try {
if( false !== strpos($response['code'], '400') ) {
$results['message'] = 'Error message';
add_filter('wpcf7_before_send_mail', 'wpcf7_custom_form_action_url');
} else {
$results['message'] = 'Success message';
}
} catch(Exception $ex){
$results['errors'] = array($ex->getMessage());
}
}
add_action('Forms3rdPartyIntegration_service_a0', array(&$this, 'service1_action_callback'), 10, 2);
--- for skipping email, I tried to implement the following, but it did not seem to work either - please take a look to see if you see anything wrong with it.
The filter to skip email is added to your function after processing the error response:
add_filter('wpcf7_before_send_mail', 'wpcf7_custom_form_action_url');
The function for this filter is:
function wpcf7_custom_form_action_url( $form) { $submission = WPCF7_Submission::get_instance();
$submission->skip_mail = true;
}
In contact form 7 submission.php, I changed the $skip-mail to public:
public $skip_mail = false;