0
votes

I am creating a custom payment gateway plugin for Woocommerce using latest wordpress version.

Once use clicks on Place order, 'process_payment' function get called and I open a popup for user to our payment gateway. Once the popup get closed, I get payment status at fronted javascript function, whether payment was success or failed.

From this javascript, I need to invoke a PHP function or some woocommerce action in my plugin class to update order status and redirect user to failed or success thank you page.

I have IPN also, but that http call will come later and we need to handle this frontend status first.

In short: Frontend checkout page have payment status in a javascript, need to send it to php function to update order and redirect user to thank you page.

1

1 Answers

0
votes

Step 1: First step is to write a php where you can redirect after popup close with status:

public function process_frontend_response() {
        if ( isset( $_GET['status'] ) ) {

            $status   = $_GET['status'];
            // LOGIC FOR YOUR STATUS

        //After logic, redirect to success
        $thanks_link = $this->get_return_url( $order );
        wp_redirect( $thanks_link );

        //OR, FAILURE PAGE
        wp_redirect( $order->get_cancel_endpoint() );
}

Now register this function in your constructor:

add_action( 'process_response', array( $this, 'process_frontend_response' ) );

Step 2: We register an endpoint to call this function:

add_action( 'init', 'register_custom_redirect' );
function register_custom_redirect() {
    if ( isset( $_GET['post-popup-redirect'] ) ) {
        do_action( 'process_response' );
    }
}

Step 3: We need to inject a custom javascript in order page to redirect to our above Url (post-popup-redirect) after popup close.

Here we call our popup and make a redirect to above php function:

public function custom_scripts() {
        $this->frontend_redirect_url = add_query_arg( 'post-popup-redirect', 'status', $this->get_return_url() );
        // Javascript for listening or hash change (it occurs after popup closes), once hash change detected, it will invoke showpopup and
        // on popup close, it will redirect to base url with status
        add_action( 'wp_head', function () {
            echo '<script>' . PHP_EOL;
            echo 'function locationHashChanged() {' . PHP_EOL;
            echo '    if (location.hash.includes(\'#open-popup\')) {' . PHP_EOL;
            echo '        somescript.openPopup().then(status => {' . PHP_EOL;
            echo '        console.log(status);' . PHP_EOL;
            echo '        window.location =\'' . $this->frontend_redirect_url . '&status=\'+status' . PHP_EOL;
            echo '        })' . PHP_EOL;
            echo '        .catch(err => {' . PHP_EOL;
            echo '        window.location =\'' . $this->frontend_redirect_url . '&status=failed\'' . PHP_EOL;
            echo '        })' . PHP_EOL;
            echo '    }' . PHP_EOL;
            echo '}' . PHP_EOL;
            echo 'window.onhashchange = locationHashChanged;' . PHP_EOL;
            echo '</script>' . PHP_EOL;
        } );
}

Register this function also in constructor:

add_action( 'wp_enqueue_scripts', array( $this, 'custom_scripts' ) );