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


My WooCommerce plugin callback function stop working after wc update

TL;运行同名类的DR WooCommerce回调url已损坏。就像我可以通过访问http://mywebsite.com/wc-api/valitorcallback来访问我的valitorcallback类一样,有没有办法启用那个url或以某种方式访问这个类?

我开发了一个WooCommerce支付网关插件,可以连接到类似冰岛paypal的Valitor公司。

支付流程很简单:

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

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

问题是大约两个月前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应该放在哪里。

我知道mywebsite.com/wc-api/v3/…REST api,但我希望我可以再次启用url,这样我就不必再次对该部分进行编码,而且我不知道如何从另一个文件中获取插件设置。

谢谢,Sigurğur

编辑:添加TL;DR部分位于顶部,当我陈述问题时用粗体显示。

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

但它是:WooCommerce自定义支付网关

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

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

我需要将sum函数从第二个类移到第一个类,然后使用正确的操作名称使其工作。它仍然返回-1,但使用url中的所有GET变量执行正确的代码。

此外,我将类名更改为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');
?>