我的 WooCommerce 插件回调函数在 wc 更新后停止工作

My WooCommerce plugin callback function stop working after wc update

TL;DR WooCommerce callback url that run a class with the same name broke. Like I could access my valitorcallback class by going to http://mywebsite.com/wc-api/valitorcallback, is there way to enable that url or somehow access this class?

我开发了一个 WooCommerce 支付网关插件来连接到像 Valitor 公司这样的冰岛贝宝。

支付流程简单:

  1. 客户将商品添加到他的购物车并去结账。
  2. 使用请求参数中的订单信息将网上商店的客户重定向到 Valitor。
  3. 客户使用他的信用卡或借记卡付款。
  4. 客户被重定向到提供的感谢页面,Valitor 调用另一个 url(回调)以通知付款已完成并提供订单信息和验证 codes/method。

插件唯一做的是第 2 步和第 4 步。在第 4 步中,如果订单有效,插件会降低库存并将订单状态更改为表示付款已完成。

问题是在大约 2 个月前 WooCommerce 更新后回调 url 中断 ,可能是出于安全原因。我无法找到再次启用此 url 或解决此问题的代码。它认为它可以用 add_action 方法或一些钩子来完成,但我无法让它工作。

这是我认为是关键的指南,但我做错了:http://docs.woothemes.com/document/payment-gateway-api/#section-4

代码插件结构是这样的:

<?php

function initWooCommerceValitorGatewayPlugin()
{
    class WooCommerceValitorGateway extends WC_Payment_Gateway
    {
        public function __construct()
        {
            // ...Varible code...

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

        public function init_form_fields()
        {
            // ...Define settings code...
        }

        public function process_payment($orderId)
        {
            // ...Magic code...

            // Redirect to Valitor with all necessary data 
            return array(
                'result' => 'success',
                'redirect' => add_query_arg($valitorData, $gatewayUrl)
            );
        }

        // ...Helper functions code (like sending an email)...
    }
}

class valitorcallback
{
    public function __construct()
    {
        $this->verifyPayment();
    }

    public function verifyPayment()
    {
        // ...Verification code...
    }

    // ...Helper functions code (like sending an email)...

}

// Add plugin to wordpress/woocommerce
add_action('plugins_loaded', 'initWooCommerceValitorGatewayPlugin');

function addValitorGateway($methods)
{
    $methods[] = 'WooCommerceValitorGateway'; 
    return $methods;
}
// Add gateway method to woocommerce
add_filter('woocommerce_payment_gateways', 'addValitorGateway');

?>

现在你看到了函数名称,也许可以告诉我 add_action 应该放在哪里。

我知道我的网站。com/wc-api/v3/... REST API 但我希望我可以再次启用 url 这样我就不必编写代码了又是那个部分,我不确定如何从另一个文件中获取该插件设置。

谢谢,Sigurður

编辑:在我陈述问题时在顶部添加了 TL;DR 部分并以粗体显示。

我找到了,不知道为什么我没有找到它,除了我在 google 上没有使用正确的搜索词。

但就是这样:WooCommerce Custom Payment Gateway

我不知道为什么文档中没有提到这一点,但这是您需要的操作挂钩:

// Payment listener/API hook
add_action('woocommerce_api_wc_valitor_gateway', array($this, 'valitorcallback'));

我需要将求和函数从第二个 class 移到第一个,然后使用正确的操作名称使其工作。它仍然是 returns -1 但正确的代码是使用 url.

中的所有 GET 变量执行的

另外我把class名字改成了WC_Valitor_Gateway,插件结构是这样的:

<?php

function initWooCommerceValitorGatewayPlugin()
{
    class WC_Valitor_Gateway extends WC_Payment_Gateway
    {
        public function __construct()
        {
            // ...Varible code...

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

            // Payment listener/API hook
            add_action('woocommerce_api_wc_valitor_gateway', array($this, 'valitorcallback'));
        }

        public function init_form_fields()
        {
            // ...Define settings code...
        }

        public function process_payment($orderId)
        {
            // ...Magic code...
        }

        public function valitorcallback()
        {
            // ...Verification code...
        }

        // ...Helper functions code (like sending an email)...
    }
}

// Add plugin to wordpress/woocommerce
add_action('plugins_loaded', 'initWooCommerceValitorGatewayPlugin', 0);

function addValitorGateway($methods)
{
    $methods[] = 'WC_Valitor_Gateway'; 
    return $methods;
}
// Add gateway method to woocommerce
add_filter('woocommerce_payment_gateways', 'addValitorGateway');

?>