zaus/forms-3rdparty-integration

How to check for response result before showing success confirmation on CF7

Closed this issue · 6 comments

Hello Zaus, Thank you for writing such a great plugin.

I want to show users custom message from the response I get back after submitting the form to the forms-3rdparty-integration URL.

I use forms-3rdparty-integration to submit to third part URL, It check if email exists or not, if it does, I get the response to my email that user already exist, so instead of telling the user that your message has submitted successfully, I want to tell the user that they have already signed up and they should use a different email.

I have written this hook and added it to my functions.php but it doesnt work, anything that I am doing wrong?

if(!class_exists('Cf73rdParty_custom_message')){

class Cf73rdParty_custom_message {

public function __construct(){
//actions require 2 parameters: 3rd-party response, results placeholders
///NOTE: customize this hook name to match your Service (in the admin settings)
add_action('Forms3rdPartyIntegration_service_a7', array(&$this, 'adjust_response'), 10, 2);
}//-- function __construct

public function adjust_response($response, &$results) {


    if(!empty($response['body'])){
            if($response['body'] = 'CustomerExists');
            {       $response['success'] = 'false';
                    $results['errors'] = array('User already exists');
                    $results['message'] = 'A trial for this customer already exists';
            }

    }

}
}


}

$Cf73rdParty_custom_message_ins = new Cf73rdParty_custom_message(); // attach hook
zaus commented

Hm...looks like the example I wrote a while ago. Updating $response won't affect anything though.

But, having just looked at the relevant line of sourcecode, I think your conditional check is the problem -- $response is already the body portion of the wp_remote_post result array.

Yes... This is an example that you wrote while ago. Below is the response I get from the server

SUBMISSION
Array
(
[timeout] => 10
[body] => Array
(
[name] => John Doe
[company] =>
[email] => xxx@yyy.com
[phone] =>
[researchField] => Water
[internalProductName] => Trial
[quantity] => 1
[countryCode] => United States
[organizationType] => Unknown
)

)

RAW RESPONSE
Array
(
[reason] => Could not locate success clause within response
[safe_message] => Success Clause not found
[clause] => EmailSent
[response] => CustomerTrialAlreadyExists
A trial for this customer already exists
UserName: xxx@yyy.com
)

How do I get this response [response] => CustomerTrialAlreadyExists and use it as a message
$results['message'] = 'A trial for this customer already exists';

Thank you.

zaus commented

Sorry if I wasn't clear -- you have a couple bugs in your if statement / hook:

public function adjust_response($response, & $results) {
    if (!empty($response['body'])) {    //     <-- $response is already the 'body' portion
        if ($response['body'] = 'CustomerExists'); {      //    <-- should be '=='
            $response['success'] = 'false'; // this does nothing
            $results['errors'] = array('User already exists');
            $results['message'] = 'A trial for this customer already exists';
        }
    }
}

should look like:

public function adjust_response($response, & $results) {
    if ($response == 'CustomerExists'); {   // <-- may want to check with `strpos` instead
        $results['errors'] = array('User already exists');
        $results['message'] = 'A trial for this customer already exists';
    }
}

Thank you for your reply.

I dont know what I am doing wrong.

I have:

  1. Checked the plugin to allow hooks.

  2. Used WP Action callback as it shows on plugin settings

add_action('Forms3rdPartyIntegration_service_a2', array(&$this, 'YOUR_CALLBACK'), 10, 2);
  1. Added this function to functions.php
if(!class_exists('Cf73rdParty_custom_message')){

class Cf73rdParty_custom_message {

    public function __construct(){
    //actions require 2 parameters: 3rd-party response, results placeholders
    ///NOTE: customize this hook name to match your Service (in the admin settings)
    add_action('Forms3rdPartyIntegration_service_a2', array(&$this, 'adjust_response'), 10, 2);
    }//-- function __construct

public function adjust_response($response, & $results) {
    if ($response == 'CustomerExists'); {   // <-- may want to check with `strpos` instead
        $results['errors'] = array('User already exists');
        $results['message'] = 'A trial for this customer already exists';
    }
}


}

$Cf73rdParty_custom_message_instance = new Cf73rdParty_custom_message(); // attach hook

When I go back to contact form 7 and click submit, it doesn't change the message to A trial for this customer already exists.

Any ideas what I am doing wrong?

zaus commented

Sorry I never got back to you -- I think the problem is that you missed my comment

<-- may want to check with strpos instead

I wasn't exactly clear on what the $response body actually looked like (and was probably replying from my phone, so was difficult to get the entire thing); from your submission email (almost the same as the debug email) it seems that it's actually a paragraph of text containing the phrase "CustomerTrialAlreadyExists", so you'd want to check if that value is present within the response using strpos.

if(strpos($response, 'CustomerTrialAlreadyExists') !== FALSE) { ...

You can also get fancy and use a regex to pull out that username to display in the message, etc

zaus commented

Can I assume Resolved? Will reopen if not...