Callback_handler won't fire WooCommerce
Asked Answered
A

3

6

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.

Adebayo answered 5/6, 2014 at 17:2 Comment(0)
S
5

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

This works for me.

Supermundane answered 27/6, 2015 at 8:13 Comment(0)
H
2

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!

Hanahanae answered 13/11, 2014 at 15:14 Comment(1)
Additionally, you must use the function name 'handle_callback', you cannot rename it. Waste me about five hours...Lampkin
A
0

Have you tried using http://yoursite/wc-api/WC_your_gateway/ (add slash at the end)?

Also the add_action should be "woocommerce_api_{class_name}" instead of" woocommerce_api_callback". So for your example, it should be "woocommerce_api_wc_your_gateway".

Alecalecia answered 3/5, 2016 at 3:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.