Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Callback_handler won't fire WooCommerce

I am building a payment Gateway for WooCommerce where the payment takes place in an offsite URL. I need that page to be able to message back to the WooCommerce plugin, and a "callback" URL is really all I need.

WooCommerce seems to have this, but I can't get it to work. You're supposed to be able to ping:

http://yoursite/wc-api/WC_your_gateway

And then you're supposed to add add_action( 'woocommerce_api_callback', 'callback_handler' ); And then it's supposed to fire a function like this public function callback_handler() {}

But when I go to that URL, all I see is a 1 on my page - my handler should be redirecting to another page (that's what I set it to do to make it obvious). What I'd LOVE is if anyone has an example of this working. I've tried placing the add_action and the handler function lots of places, no luck.

like image 551
Tommy Nicholas Avatar asked Jun 05 '14 17:06

Tommy Nicholas


2 Answers

I have the same problem. Try to add exit; or wp_die(); in the end of your callback function.

This works for me.

like image 69
Stanley Wang Avatar answered Sep 30 '22 03:09

Stanley Wang


I had the same problem, so, this is what worked for me:

class WC_mygateway extends WC_Payment_Gateway {
  public function __construct() {
    //'woocommerce_api_'.strtolower(get_class($this)) will result in 'woocommerce_api_wc_mygateway'
    add_action('woocommerce_api_'.strtolower(get_class($this)), array(&$this, 'handle_callback'));
  }
  function handle_callback() {
    //Handle the thing here!
  }
}

function woocommerce_mygateway_add_gateway( $methods ) {
  $methods[] = 'WC_mygateway';
  return $methods
}
add_filter( 'woocommerce_payment_gateways', 'woocommerce_mygateway_add_gateway');

Make sure you are not missing any of those details, other wise it wont work. Also you can call it using http://example.com/?wc-api=wc_mygateway or http://example.com/wc-api/wc_mygateway

Hope this work for everyone getting stuck with this issue!

like image 38
Jhuliano Moreno Avatar answered Sep 30 '22 01:09

Jhuliano Moreno