joedoe150
joedoe150

Reputation: 103

Action hook 'parse_request' fires twice for single GET request to Wordpress

I have a problem with receiving a GET request in Wordpress.

I am writing a payment plugin for (amongst others) 2Checkout hosted checkout. Once the customer submits a form, he is redirected to 2Checkout, where they can complete the payment request. On successful payment, the customer is directed back to my wordpress site with the payment details as GET parameters (http://example.org/success/?key1=value1&key2=value2...).

My wordpress plugin hooks into the 'parse_request' hook to intercept this request, check if a certain key is present to see if it is a 2Checkout reply and then validates and stores the request in a table.

In addition, 2Checkout has something called 'INS' - Instant Merchant Notifications. Basically these are separate POST request messages for the payment that are sent in the background - I also set these up and pointed them at my wordpress site, where they are intercepted by the same hook to 'parse_request' and also validated and stored in the same table.

When I test the payment, I receive the direct GET request and the INS requests as intended and store them in the table. However, the GET request is always stored twice as the function to handle the request is called twice for the GET request. This is only true for the GET request however - the INS POST requests only appear once and are handled accordingly.

The only thing that is different between the two is that the GET request actually redirects to a page where the GET parameters are used to display a custom thank you message. The POST request are processed in the background (not user-facing).

I already checked with 2Checkout and tested sending the redirect to requestb.in and 2Checkout only sends the GET request once. Yet somehow it gets processed twice by my plugin.

Short of checking the database for an already existing entry for the GET request (which is an ugly hack in my opinion as it does not address the issue), I don't know what to do to prevent the duplicate GET request.

As I am not that experienced in the wordpress "flow" to figure this out and could not find an answer to this anywhere using google or stack overflow, I am looking for any help you may provide. Also, if there is a better way of doing this than to hook into 'parse_request', any information is appreciated.

Sample Code (simplified to show the principle):

class MyController {

    public function __construct() {
        $this->register_hooks();
    }

    private function register_hooks() {
        add_action('parse_request', array($this, 'handle_request'));
    }

    public function handle_request( $wp ) {
        if( !isset($_REQUEST['2Checkout_variable'] ) {
            return;
        }
        $request_handler = new MyPluginRequestHandler($_REQUEST);
        $request_handler->handle_request();
    }
}

Upvotes: 0

Views: 2072

Answers (1)

joedoe150
joedoe150

Reputation: 103

So after some further debugging with the hosting provider, the cause was quickly identified (and it was not a Wordpress problem as such):

The return URL set in the 2Checkout account was missing the trailing '/', causing the server to add it when the request arrived and then re-requesting the page with the trailing slash - hence the 2 GET requests.

Leaving this here in case anyone else ever runs into this problem.

Upvotes: 1

Related Questions