Search code examples
phpwordpressbraintree

Braintree Webhooks and Wordpress


I am a little new to web hooks, please go easy :)

I am developing a plugin for WordPress, with a payment processor of Braintree using their API.

I have the forms setup to submit transactions and that appears to be working well. However, I am in need of monitoring the status of a Sub Merchant account authorization.

The Dev Docs specify to set up a webhook in the control panel of Braintree with the domain.com/hook.php location.

In Wordpress, you do not have direct access to the plugin file location via URL.

How do I point the webhook at the correct file with the function to get the sub-merchant information?

I currently process payments using ajax and

get_admin_url() . 'admin-ajax.php?action=icfloevents_do_payment';

to get the admin url, and direct it to the function.

How do you do this externally?


Solution

  • Figured this out after some searching and experimentation.

    The obvious (after the fact) solution is to use the WordPress rewrite URL function to direct a URL path to an individual file in the Plugin Directory.

    function rewrite_braintree_hook(){
    
        global $wp_rewrite;
    
        $plugin_url = plugins_url( 'brainhook.php', __FILE__ );
        $plugin_url = substr( $plugin_url, strlen( home_url() ) + 1 );
    
        add_rewrite_rule('brainhook', $plugin_url ,'top');
    
    
        $wp_rewrite->flush_rules(true);
    }
    

    This will aloow the Url something.com/brainhook to be directed towards brainhook.php thus allowing me to work with the API callback directly in my plugin.

    Make sure you flush your rewrite "cache" by going into settings->Permalinks and clicking save without changing anything.

    Hope this helps someone else.