WooCommerce wc-api返回错误的类

时间:2015-08-30 14:30:54

标签: wordpress woocommerce payment-gateway

我有两个Woocommerce支付网关类叫做#34; WC_banklink_seb"和#34; WC_banklink_swedbank"最后一个在将Woocommerce 2.2更新到2.4时停止工作

集成和付款仍然有效,但使用wc-api的返回网址却没有。

当我使用" WC_banklink_swedbank"支付网关并从支付提供商返回到网址http://my-domain.com/wc-api/WC_banklink_swedbank/我收到以下错误:

致命错误:第304行的/ wp-content / plugins / banklink_seb / banklink.php中的签名无效

班级" WC_banklink_swedbank" actualy位于文件夹... / wp-content / plugins / banklink_swedbank /

我的任务是为什么wc-api从另一个支付网关返回一个班级?类WC_banklink_seb和WC_banklink_swedbank之间唯一的共同点是它们都扩展了" WC_Payment_Gateway"

这两个代码都是来自类的appart和插件名称几乎相同,因为"设置"设置在adin面板上。

让我感到困惑的是,当我禁用该类的插件" WC_banklink_seb"时," WC_banklink_swedbank"再次开始工作?

<?php
/*
Plugin Name:banklink_swedbank
Plugin URI: 
Description:
Version: 1.0
Author: Meelis Karulin
Author URI: http://www.paleo.ee
License: GPL2
Text Domain: banklink_swedbank  
*/

if (!defined('ABSPATH')) exit; // Exit if accessed directly
if (!in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) return;

add_action('plugins_loaded', 'woocommerce_banklink_swedbank_init', 0);
function woocommerce_banklink_swedbank_init()
{
    add_action('init', 'translations_banklink_swedbank');
    function translations_banklink_swedbank()
    {
        load_plugin_textdomain('banklink_swedbank', FALSE, dirname(plugin_basename(__FILE__)) . '/languages/');
    }

    class WC_banklink_swedbank extends WC_Payment_Gateway
    {
        var $notify_url;

        function __construct()
        {
            global $woocommerce;

            $this->id = 'banklink_swedbank';
            $this->medthod_title = 'Pangalink (SEB)';
            $this->has_fields = false;
            $this->VK_CURR = 'EUR';
            $this->return_url = $this->get_return_url($order);
            $this->VK_RETURN = str_replace('https:', 'http:', home_url('/') . 'wc-api/WC_banklink_swedbank/');

            $this->init_form_fields();
            $this->init_settings();
            $this->icon = $this->settings['bank_logo'];
            $this->title = $this->settings['title'];
            $this->description = $this->settings['description'];
            $this->VK_SND_ID = $this->settings['VK_SND_ID'];
            $this->liveurl = $this->settings['liveurl'];
            $this->my_private_key = $this->settings['my_private_key'];
            $this->my_private_key_password = $this->settings['my_private_key_password'];
            $this->bank_certificate = $this->settings['bank_certificate'];
            $this->VK_ACC = $this->settings['VK_ACC'];
            $this->VK_NAME = $this->settings['VK_NAME'];
            $this->bank_name = $this->settings['bank_name'];
            $this->bank_charset = $this->settings['bank_charset'];
            $this->bank_charset_param = $this->settings['bank_charset_param'];
            $this->my_support_email = $this->settings['my_support_email'];


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

            add_action('woocommerce_api_wc_banklink_swedbank', array(&$this, 'check_bank_response'));
            add_action('valid_request', array($this, 'successful_request'));
            add_action('woocommerce_thankyou_banklink_swedbank', array(&$this, 'thankyou_page'));
        }

        function init_form_fields()
        {
            // removed from sample because not relevant to the problem
        }


        function admin_options()
        {
            // removed from sample because not relevant to the problem
        }

        function process_payment($order_id)
        {
            $order = new WC_Order($order_id);
            return array(
                'result' => 'success',
                'redirect' => add_query_arg('order', $order->id, add_query_arg('key', $order->order_key, get_permalink(woocommerce_get_page_id('pay'))))
            );
        }

        function receipt_page($order)
        {
            echo '<p>' . __('Pay through your bank', 'banklink_swedbank') . '</p>';
            echo $this->generate_bank_form($order);
        }

        function generate_bank_form($order_id)
        {
            global $woocommerce;
            $order = new WC_Order ($order_id);

            foreach ($order->get_items() as $item) {
                $item_names[] = $item['name'] . ' x ' . $item['qty'];
            }

            $mac_fields = array(
                'VK_SERVICE' => '1001',
                'VK_VERSION' => '008',
                'VK_SND_ID' => $this->to_bank_ch($this->VK_SND_ID),
                'VK_STAMP' => $this->to_bank_ch(str_replace('#', '', $order->get_order_number())),
                'VK_AMOUNT' => $this->to_bank_ch($order->get_total()),
                'VK_CURR' => $this->to_bank_ch($this->VK_CURR),
                'VK_ACC' => $this->to_bank_ch($this->VK_ACC),
                'VK_NAME' => $this->to_bank_ch($this->VK_NAME),
                'VK_REF' => $this->to_bank_ch($this->calculate_ref(str_replace('#', '', $order->get_order_number()))),
                'VK_MSG' => $this->to_bank_ch('Arve number: ' . $order->get_order_number()),
                'VK_RETURN' => $this->to_bank_ch($this->VK_RETURN));

            $p = $this->bank_charset_param;
            if ($p != '') {
                $mac_fields[$p] = $this->bank_charset;
            }

            $key = openssl_pkey_get_private(
                file_get_contents($this->my_private_key),
                $this->my_private_key_password);

            if (!openssl_sign($this->generate_mac_strings($mac_fields), $signature, $key)) {
                trigger_error("Unable to generate signature", E_USER_ERROR);
            }

            $mac_fields['VK_MAC'] = base64_encode($signature);
            $bank_args_array = array();
            foreach ($mac_fields as $key => $value) {
                $bank_args_array[] = '<input type="hidden" name="' . esc_attr($key) . '" value="' . esc_attr($value) . '" />';
            }

            return '<form action="' . esc_url($this->liveurl) . '" method="post" id="bank_payment_form" target="_top">
                ' . implode('', $bank_args_array) . '
                <input type="submit" class="button alt" id="submit_bank_payment_form" value="' . __('Pay', 'banklink_swedbank') . '" />
                <a class="button cancel" href="' . esc_url($order->get_cancel_order_url()) . '">' . __('Cancel order', 'banklink_swedbank') . '</a>
                </form>';
        }

        function check_bank_response()
        {
            @ob_clean();

            if (!empty($_REQUEST)) {
                header('HTTP/1.1 200 OK');
                do_action("valid_request", $_REQUEST);

            } else {
                wp_die("No response from the bank");
            }
        }

        function successful_request($posted)
        {

            $posted = stripslashes_deep($posted);
            $macFieldsBefore = array();

            foreach ((array)$posted as $f => $v) {
                if (substr($f, 0, 3) == 'VK_') {
                    $macFieldsBefore[$f] = $v;
                }
            }

            $p = $this->bank_charset_param;
            $bankCharset = '';
            if ($p != '') {
                $bankCharset = $macFieldsBefore[$p];
            }

            if ($bankCharset == '') {
                $bankCharset = 'iso-8859-1';
            }

            $macFields = array();
            foreach ($macFieldsBefore as $f => $v) {
                $macFields[$f] = $this->from_bank_ch($v, $bankCharset);
            }

            $key = openssl_pkey_get_public(file_get_contents($this->bank_certificate));

            if (!openssl_verify($this->generate_mac_strings($macFields), base64_decode($macFields['VK_MAC']), $key)) {
                trigger_error("Invalid signature", E_USER_ERROR);
            }

            $order_id = str_replace('#', '', $macFields['VK_STAMP']);
            $order = $this->get_order($order_id);

            if ($macFields['VK_SERVICE'] == '1901') {
                $macFields['message'] = __("Payment canceled!\n", 'banklink_swedbank');
                $macFields['status'] = 'pending';
                $macFields['return'] = $this->get_return_url($order);
            } elseif ($macFields['VK_SERVICE'] == '1902') {
                $macFields['message'] = sprintf(__("Payment unsuccessful!\nBank returned error %s!", 'banklink_swedbank'), $macFields('VK_ERROR_CODE'));
                $macFields['status'] = 'pending';
                $macFields['return'] = $this->get_return_url($order);
            } elseif ($macFields['VK_SERVICE'] == '1101') {
                if ($macFields['VK_REC_ID'] != $this->VK_SND_ID) {
                    $macFields['message'] = __("Payment unsuccessful!.\nBank returned unknown merchant ID!\n", 'banklink_swedbank');
                    $macFields['status'] = 'pending';
                    $macFields['return'] = $this->get_return_url($order);

                } elseif ($macFields['VK_AMOUNT'] != $order->get_total()) {
                    $macFields['message'] = __("Payd ammount and order ammount do not match!\nOrder has been set to on-hold.\nPlease contact us: ", 'banklink_swedbank') . $this->my_support_email . "\n";
                    $macFields['status'] = "on-hold";
                    $macFields['return'] = $this->get_return_url($order);
                } else {
                    $macFields['message'] = __("Payment recieved!\n", 'banklink_swedbank');
                    $macFields['status'] = "completed";
                    $macFields['return'] = $this->get_return_url($order);
                }
            } else {
                $macFields['message'] = __("Payment unsuccessful!\nUnknown reply: ", 'banklink_swedbank') . $macFields['VK_SERVICE'] . "\n";
                $macFields['status'] = 'pending';
                $macFields['return'] = $this->get_return_url($order);
            }

            if ($macFields['status'] == 'completed') {
                $order->add_order_note($macFields['message']);
                $order->payment_complete();
            } else {
                $order->update_status($macFields['status'], sprintf(__('Payment on-hold: %s', 'banklink_swedbank'), $macFields['message']));
                $order->add_order_note($macFields['message']);

            }

            wp_safe_redirect(add_query_arg('status', urlencode($macFields['status']), add_query_arg('msg', urlencode($macFields['message']), $macFields['return'])));
        }

        function thankyou_page()
        {
            $order = $this->get_order($_GET['order']);
            $color = $_GET['status'] != 'completed' ? 'orange' : 'green';
            $msg = $_GET['msg'];
            echo '<h2 style="color:' . $color . ';">' . $msg . '</h2>';
        }


        function get_order($order_id)
        {
            $order = new WC_Order($order_id);
            if (!isset($order->id)) {
                $order_id = woocommerce_get_order_id_by_order_key($order_key);
            }
            return $order;
        }

        function to_bank_ch($v)
        {
            $bankCharset = $this->bank_charset;
            if ($bankCharset != 'utf-8') {
                return mb_convert_encoding($v, $bankCharset, 'utf-8');
            } else {
                return $v;
            }
        }

        function from_bank_ch($v, $bankCharset)
        {
            return mb_convert_encoding($v, 'utf-8', $bankCharset);
        }

        function generate_mac_strings($mac_fields)
        {
            $n = new WC_banklink_swedbank();
            $VK_variableOrder = array(
                1001 => array(
                    'VK_SERVICE', 'VK_VERSION', 'VK_SND_ID',
                    'VK_STAMP', 'VK_AMOUNT', 'VK_CURR',
                    'VK_ACC', 'VK_NAME', 'VK_REF', 'VK_MSG'),
                1101 => array(
                    'VK_SERVICE', 'VK_VERSION', 'VK_SND_ID',
                    'VK_REC_ID', 'VK_STAMP', 'VK_T_NO', 'VK_AMOUNT', 'VK_CURR',
                    'VK_REC_ACC', 'VK_REC_NAME', 'VK_SND_ACC', 'VK_SND_NAME',
                    'VK_REF', 'VK_MSG', 'VK_T_DATE'),
                1901 => array(
                    'VK_SERVICE', 'VK_VERSION', 'VK_SND_ID',
                    'VK_REC_ID', 'VK_STAMP', 'VK_REF', 'VK_MSG'));

            $requestNum = $mac_fields['VK_SERVICE'];
            $data = '';

            foreach ((array)$VK_variableOrder[$requestNum] as $key) {
                $v = $mac_fields[$key];
                $l = ($n->bank_name == 'swedbank' ? mb_strlen($v, $n->bank_charset) : strlen($v));
                $data .= str_pad($l, 3, '0', STR_PAD_LEFT) . $v;
            }
            return $data;
        }

        function calculate_ref($nr)
        {
            $nr = (string)$nr;
            $kaal = array(7, 3, 1);
            $sl = $st = strlen($nr);
            $total = 0;
            while ($sl > 0 and substr($nr, --$sl, 1) >= '0') {
                $total += substr($nr, ($st - 1) - $sl, 1) * $kaal[($sl % 3)];
            }
            $kontrollnr = ((ceil(($total / 10)) * 10) - $total);
            return $nr . $kontrollnr;
        }


    }

    function woocommerce_add_banklink_swedbank_gateway($methods)
    {
        $methods[] = 'WC_banklink_swedbank';
        return $methods;
    }

    add_filter('woocommerce_payment_gateways', 'woocommerce_add_banklink_swedbank_gateway');
}

?>

1 个答案:

答案 0 :(得分:0)

问题列在动作名称“valid_request”中,并不是唯一的。两种付款方式都注册了相同的操作,因此只有第一个付款方式才真正注册。

add_action('valid_request', array($this, 'successful_request'));

应使用唯一名称替换操作的名称,例如:

add_action('valid_request_swedbank', array($this, 'successful_request'));

当然在相应的do_action()

中更改操作名称
do_action( "valid_request_swedbank", $_REQUEST );