skip to Main Content

I’m actually writing a custom plugin to connect a payment gateway to Woocommerce, and I’ve been stuck on a problem for several hours:

I cannot get the callback of the payment processing.

I googled a lot to find an issue but no success yet.

The environment is as follows:

  • Site on a dedicated server – Ubuntu 12.04 ;

  • PHP 7.1 ;

  • WordPress 5.3.2 ;

  • Woocommerce 3.8.1 ;

  • A lot of plugins are loaded too ;

My class look like this:

    add_filter('woocommerce_payment_gateways', 'woogatewaypro_add_gateway_class');
    function woogatewaypro_add_gateway_class($gateways){
        $gateways[] = 'WC_Gateway_Pro';
        return $gateways;
    }

    add_action('plugins_loaded', 'woogatewaypro_init_gateway_class');

    function woogatewaypro_init_gateway_class(){

        class WC_Gateway_Pro extends WC_Payment_Gateway{

            public function __construct(){

                global $wpdb;

                global $woocommerce;

                $this->id                   = 'woogatewaypro';
                $this->icon                 = '';
                $this->has_fields           = true;
                $this->method_title         = 'Gateway Pro payment gateway';
                $this->method_description   = __('Allow payment from Gateway Pro gateway.','woogatewaypro');

                $this->supports = array(
            'products',
                    'subscriptions',
                    'subscription_cancellation',
                    'subscription_suspension',
                    'subscription_reactivation',
                    'subscription_amount_changes',
                    'subscription_date_changes',
                    'subscription_payment_method_change',
                    'subscription_payment_method_change_customer',
                    'subscription_payment_method_change_admin',
                    'multiple_subscriptions'
                );

                $this->init_form_fields();

                $this->init_settings();
                $this->title            = $this->get_option('title');
                $this->description      = $this->get_option('description');
                $this->enabled          = $this->get_option('enabled');
                $this->testmode         = 'yes' === $this->get_option('testmode');
                $this->private_key      = $this->testmode ? $this->get_option('test_private_key') : $this->get_option('private_key');
                $this->publishable_key  = $this->testmode ? $this->get_option('test_publishable_key') : $this->get_option('publishable_key');

                /* some code... */

                add_action('woocommerce_api_'.strtolower(get_class($this)), 'woogatewaypro_payment_callback');

                add_action('woocommerce_api_woogatewaypro_cancel_payment', array($this, 'woogatewaypro_cancel_payment'));

                add_action('woocommerce_api_woogatewaypro_payment_ko', array($this, 'woogatewaypro_payment_ko'));

                add_action('woocommerce_api_woogatewaypro_payment_ok', array($this, 'woogatewaypro_payment_ok'));

                add_action('woocommerce_update_options_payment_gateways_'.$this->id, array($this, 'process_admin_options'));

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

                /* some code... */

            }

            public function init_form_fields(){

                /* some code... */

            }

            public function payment_scripts(){

                /* some code... */


            }

            public function process_payment($order_id){

                /* some code... */

            }

            public function woogatewaypro_cancel_payment(){

                wp_redirect(get_permalink(get_option('woocommerce_checkout_page_id')).'?woogatewaypro=cancelled-payment');

            }

            public function woogatewaypro_payment_ko(){

                wp_redirect(get_permalink(get_option('woocommerce_checkout_page_id')).'?woogatewaypro=payment-ko');

            }

            public function woogatewaypro_payment_ok(){

                /* some code... */

                wp_redirect(get_permalink(get_option('woocommerce_checkout_page_id')).'?woogatewaypro=payment-ok');

            }

        }

    }

    function add_wc_gateway_pro_gateway($methods){

        $methods[] = 'WC_Gateway_Pro';

        return $methods;

    }

    add_filter('woocommerce_payment_gateways', 'add_wc_gateway_pro_gateway');

    function woogatewaypro_payment_callback(){

        mail('[email protected]','Check callback','Enter func');

        // wp_die();
        exit;

    }

What I’m doing wrong?

I tried to place my woogatewaypro_payment_callback function in my class, but it doesn’t work.

My URL to callback is like this:

    if('' == get_option('permalink_structure')){
        $callback = site_url().'/?wc-api=WC_Gateway_Pro';
    }else{
        $callback = site_url().'/wc-api/WC_Gateway_Pro/';
    }

I look the logs on my server, and the data seem to be posted: I get 200 status with POST.

When I try to access the function by GET, I still have a 1 on a blank page.

Could some plugin(s) create the problem?

Thanks in advance for your help ;).

3

Answers


  1. Chosen as BEST ANSWER

    I'm still on my problem (I'm going crazy...).

    I use WP Cerber Security to protect my site, and no problem with it: WP Cerber Security allows posting to wc-api/WC_Gateway_Pro/, and the request is in 200 status.

    The fact is that the callback function isn't executed: I don't receive any mail. I tried to save a string in the debug.log file of WordPress (debug set to true), and no log was saved.

    I have read and reread all the topics to build a custom payment gateway for Woocommerce, and I don't understand what's the problem with my code: everything seems to be good in the code construction, but no possibility to execute the callback.

    The WC_API execute correctly the cancel, ko and ok functions of my class, but not the callback function (why?).

    Must I activate any setting on Woocommerce?

    If someone has had the same problem or has an idea to solve this problem, I will be very grateful :)

    Thanks a lot for your replies ;)


  2. After reading again my cde, I detected I have missed something to correctly declare my add_action() on callback function. SO it look like this now:

        add_action('woocommerce_api_'.strtolower(get_class($this)), array($this,'woogatewaypro_payment_callback'));
    

    But it not works yet.

    I’ll try to install a new WordPress to test on a fresh environment, because I really don’t understand why it doesn’t work.

    I hope a “Woo’pro” will pass here to help me a little :).

    Login or Signup to reply.
  3. I had the same issue and this is what works for WooCommerce 2.0+

     //register the callback hooks which we will use to receive the payment response from the gateway
    add_action( 'woocommerce_api_custom_gateway', array( $this, 'payment_callback'));
    

    The callback link can be accessed as http://yoursite.com/wc-api/custom_gateway/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search