zaus / forms-3rdparty-integration

Wordpress Plugin - Forms 3rdparty Integration - a plugin to help integrate 3rdparty services with common WP Forms plugins like Contact Form 7 and Gravity Forms
http://wordpress.org/plugins/forms-3rdparty-integration/
47 stars 14 forks source link

Error With Assigning Error Message #79

Open daniel-byers opened 7 years ago

daniel-byers commented 7 years ago

So, I am trying to engineer the situations that occur when a request is successful or not. By a successful request, I simply mean the information has passed any additional validation at the API and the details are accepted. In this case I wish to attach the returned Quote to the email sent to the user, but if the validations fail I want to inform the user on screen and allow them to resubmit. To achieve this, I am trying to set the error message as below:

public function alter_response($response, $results){
  $xml = simplexml_load_string($response, "SimpleXMLElement", LIBXML_NOCDATA);
  $json = json_encode($xml);
  $array = json_decode($json,TRUE);

  $errs = array();
  if(array_key_exists('errors', $array['Response'])) {
    foreach ($array['Response']['errors'] as $error)
      $errs[] = $error['reason_description'];
  }

  if (sizeof($errs) > 0) {
    $results['success'] = "false";
    $results['errors']  = $errs;
    $results['message'] = "There was a problem with some fields.";
  } else {
    $results['success'] = "true";
    $results['message'] = "Your submission was a success";
    $results['attach'] = $response['RSFullApplicationResponse']['Quote']['result'];
  }

  error_log("Response:" . print_r($array, TRUE));
  error_log("Errors:" . print_r($errs, TRUE));
  error_log("Results:" . print_r($results, TRUE));
}

tl;dr Creates an array from the XML response, checks for errors and adds them to the separate errors array if they exist, then checks to see if the errors array is empty; if it is then the submission is a success, if not then the submission is a failure.

As you can see, I am printing out the results array at the end. Even on the cases when the array looks like this: Results:Array ( [success] => false [errors] => Array ( [0] => Internal Address Error ( Insufficient Address History: Must be at least 36 months). )

[attach] => [message] => There was a problem with some fields. )

because there was an error, I still receive a “Your message was sent successfully. Thanks.” with a green box.

I noticed in the 3rd-parties/mailchimp example it says: @param &$results the callback return results (passed by reference since function can’t return a value; also must be “constructed by reference”; see plugin)

but when I pass by reference, I get the following message: “PHP Warning: Parameter 2 to Cf73rdParty_RateSetterCallbacks::alter_response() expected to be a reference, value given in /var/www/wp/wp-includes/plugin.php on line 524”

Is this this issue, and if so how do I fix it? I can’t find this information anywhere.

Thanks in advance, Daniel

zaus commented 7 years ago

The $results should already be passed by reference (see here, probably should have just been a filter instead but way back then I thought it was an optimization), so just the act of updating it should take care of passing things through. Maybe this isn't the case anymore with newer PHPs, so I'll try to doublecheck.

Success might need to be a boolean (rather than a string as you've used, i.e. false vs "false"), but it should just expect a non-empty 'error' index. Otherwise, the 'errors' key wasn't meant to be shown to the user but instead included in the debug/failure email sent to the admin; that's why there's a 'safe_message' instead.

The 3rdparties examples are probably outdated, I haven't paid attention to them in a while...sorry...

Is Cf73rdParty_RateSetterCallbacks your plugin class? Seeing an explicit reference to CF7 makes me think you're using the an old version of this plugin -- are you on 1.7+? Otherwise, you're using CF7?

daniel-byers commented 7 years ago

Firstly, thanks for your response.

The code I supplied was just one attempt I made. I have tried boolean before with the same outcome. However, for a sanity check, I have changed it back to a boolean. The error log is showing:

// Array built from remote request-response
[08-Mar-2017 12:24:20 UTC] Response:Array
(
    [RequestDate] => 08/03/2017 12:24:20
    [APICall] => Full Application
    [Response] => Array
        (
            [errors] => Array
                (
                    [error] => Array
                        (
                            [reason_code] => 1040
                            [reason_description] => Internal Address Error ( Insufficient Address History: Must be at least 36 months).
                        )

                )

            [warnings] => Array
                (
                )

            [RSFullApplicationResponse] => Array
                (
                    [Quote] => Array
                        (
                            [result] => Error
                            [user_reference] => Array
                                (
                                )

                            [monthly_repayment] => Array
                                (
                                )

                            [total_repayment] => Array
                                (
                                )

                            [quote_reference] => Array
                                (
                                )

                        )

                )

        )

)
// Updated results Array
[08-Mar-2017 12:24:20 UTC] Results:Array
(
    [success] => 
    [errors] => Array
        (
            [0] => Internal Address Error ( Insufficient Address History: Must be at least 36 months).
        )

    [attach] => 
    [message] => There was a problem with some fields.
)

So, the $results array has been updated, yet the outcome remains the same:

Strangely, if the submission is valid, the green box displays "Your submission was a success", the quote result is attached to the email and the error log prints this for $results:

[08-Mar-2017 12:27:02 UTC] Results:Array
(
    [success] => 1
    [errors] => 
    [attach] => Declined
    [message] => Your submission was a success
)

I notice that 'success' is set to 1 when it's true, but not set to 0 when it's false. Could this be the cause of any issues?


Cf73rdParty_RateSetterCallbacks is indeed my plugin class and I am on version 1.7.1; the name was likely chosen because I was looking at an example whilst writing it. All of my Forms3rdPartyIntegration_service_filters are at the top of this class, so I assumed that would also be the place to put the action too. Is this not correct?

Sorry for my ignorance, this is the first time I've ventured into WordPress.

daniel-byers commented 7 years ago

@zaus, I was wondering if you had discovered anything regarding this issue?

ehausen commented 6 years ago

@daniel-byers did you manage to output the response somehow?