在wordpress上接收和显示来自网关的响应通知

在wordpress上接收和显示来自网关的响应通知,wordpress,woocommerce,Wordpress,Woocommerce,我有一个插件,用于将支付网关集成到wordpress woocommerce中。 它的工作正常,但当响应来自网关时,我不知道如何获取和恢复 在wordpress中显示它。以下是插件代码: function sb_gtpay_init() { if (!class_exists('WC_Payment_Gateway')) { return; } class WC_GTPay extends WC_Payment_Gateway {

我有一个插件,用于将支付网关集成到wordpress woocommerce中。 它的工作正常,但当响应来自网关时,我不知道如何获取和恢复 在wordpress中显示它。以下是插件代码:

    function sb_gtpay_init() {
    if (!class_exists('WC_Payment_Gateway')) {
        return;
    }

    class WC_GTPay extends WC_Payment_Gateway { 

        public function __construct() { 
            global $woocommerce;

            $this->id       = 'gtpay';
            $this -> icon  =  plugins_url( 'GTPay.png' , __FILE__ );
            $this->has_fields   = false;
            $this->liveurl      = 'https://ibank.gtbank.com/GTPay/Tranx.aspx';
            $this->method_title     = __( 'GTPay', 'woocommerce' );

            // Load the form fields.
            $this->init_form_fields();

            // Load the settings.
            $this->init_settings();

            // Define user set variables
            $this->title        = $this->settings['title'];
            $this->description  = $this->settings['description'];
            $this->marchant_id  = $this->settings['marchant_id'];   
            $this->hash_key     = $this->settings['hash_key'];  
            $this->thanks_message   = $this->settings['thanks_message'];    
            $this->error_message    = $this->settings['error_message']; 
            $this->feedback_message = '';
            $this->response_codes   = $this->get_response_codes();

            // Actions
            add_action( 'init', array(&$this, 'check_ipn_response') );
            add_action('valid-gtpay-ipn-request', array(&$this, 'successful_request') );
            add_action('woocommerce_receipt_gtpay', array(&$this, 'receipt_page'));
            //add_action('woocommerce_update_options_payment_gateways', array(&$this, 'process_admin_options'));

            //add_action('woocommerce_thankyou_' . $this->id, array(&$this, 'thankyou_page'));


            // Actions
            if ( version_compare( WOOCOMMERCE_VERSION, '2.0', '<' ) ) {
                // Pre 2.0
                add_action('woocommerce_update_options_payment_gateways', array(&$this, 'process_admin_options'));
            } else {
                // 2.0
                add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
            }           

            //Filters
            add_filter('woocommerce_currencies', array($this, 'add_ngn_currency'));
            add_filter('woocommerce_currency_symbol', array($this, 'add_ngn_currency_symbol'), 10, 2);

            if ( !$this->is_valid_for_use() ) $this->enabled = false;

        }

        function add_ngn_currency($currencies) {
             $currencies['NGN'] = __( 'Nigerian Naira (NGN)', 'woocommerce' );
             return $currencies;
        }

        function add_ngn_currency_symbol($currency_symbol, $currency) {
            switch( $currency ) {
                case 'NGN':
                    $currency_symbol = '₦';
                    break;
            }

            return $currency_symbol;
        }    

        function is_valid_for_use() {
            $return = true;

            if (!in_array(get_option('woocommerce_currency'), array('NGN'))) {
                $return = false;
            }

            return $return;
        }

        function admin_options() {

            echo '<h3>' . __('GTPay', 'woocommerce') . '</h3>';
            echo '<p>' . __('GTPay works by sending the user to secured GTBank portal to enter their payment information.', 'woocommerce') . '</p>';
            echo '<table class="form-table">';

            if ( $this->is_valid_for_use() ) {
                $this->generate_settings_html();
            } else {
                echo '<div class="inline error"><p><strong>' . __( 'Gateway Disabled', 'woocommerce' ) . '</strong>: ' . __( 'GTPay does not support your store currency.', 'woocommerce' ) . '</p></div>';
            }

            echo '</table>';

        }

           function init_form_fields() {
            $this->form_fields = array(
                'enabled' => array(
                                'title' => __( 'Enable/Disable', 'woocommerce' ), 
                                'type' => 'checkbox', 
                                'label' => __( 'Enable GTPay', 'woocommerce' ), 
                                'default' => 'yes'
                            ), 
                'title' => array(
                                'title' => __( 'Title', 'woocommerce' ), 
                                'type' => 'text', 
                                'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ), 
                                'default' => __( 'GTPay', 'woocommerce' )
                            ),
                'description' => array(
                                'title' => __( 'Description', 'woocommerce' ), 
                                'type' => 'textarea', 
                                'description' => __( 'This controls the description which the user sees during checkout.', 'woocommerce' ), 
                                'default' => __("Pay via GTPay;", 'woocommerce')
                            ),
                'thanks_message' => array(
                                'title' => __( 'Thanks message', 'woocommerce' ), 
                                'type' => 'textarea', 
                                'description' => __( 'The message to show on a successful payment', 'woocommerce' ), 
                                'default' => __("Thank you. Your order has been received", 'woocommerce')
                            ),
                'error_message' => array(
                                'title' => __( 'Failure message', 'woocommerce' ), 
                                'type' => 'textarea', 
                                'description' => __( 'The message to show when a payment has failed', 'woocommerce' ), 
                                'default' => __("Sorry. There was a problem with your order", 'woocommerce')
                            ),
                'marchant_id' => array(
                                'title' => __( 'Marchant ID', 'woocommerce' ), 
                                'type' => 'text', 
                                'description' => __( 'This is the GTPay-wide unique identifier of merchant, assigned by GTPay and communicated to merchant by GTBank.', 'woocommerce' ), 
                                'default' => ''
                            ),
                'hash_key' => array(
                                'title' => __( 'Hash Key', 'woocommerce' ), 
                                'type' => 'text', 
                                'description' => __( 'Hash Key is a hash which would have been given to you when you opened your account with GTPay', 'woocommerce' ), 
                                'default' => ''
                            )
            );

        }

        function payment_fields() {
            if ($this->description) {
            echo wpautop(wptexturize($this->description));
            }
        }

        function get_gtpay_args( $order ) {
            global $woocommerce;

            $order_id = wp_rand(1, 9999977);
            $redirect_url = $this->get_return_url($order);
            $order_total = round(((number_format($order->get_order_total() + $order->get_order_discount(), 2, '.', ''))*100),0);
            $hash_string = $order_id . $order_total  . $redirect_url . $this->hash_key;
            $hash = hash('sha512', $hash_string);

            $gtpay_args = array(
                'gtpay_mert_id'             => $this->marchant_id,
                'gtpay_tranx_id'            => $order_id,
                'gtpay_tranx_amt'           => $order_total,
                'gtpay_tranx_curr'          => 566,
                'gtpay_tranx_memo'                       =>"Mobow",

                'gtpay_tranx_hash'          => $hash,


                'gtpay_tranx_noti_url'          => $redirect_url,
                'gtpay_cust_id'         => $order_id . wp_rand(1, 999999),
                'gtpay_cust_name'           => trim($order->billing_first_name . ' ' . $order->billing_last_name)


            );

            if (isset($order->user_id)) {
                $gtpay_args['gtpay_cust_id'] = $order->user_id;
            }

            $gtpay_args = apply_filters('woocommerce_gtpay_args', $gtpay_args);

            return $gtpay_args;
        }

        function generate_gtpay_form( $order_id ) {
            global $woocommerce;

            $order = new WC_Order( $order_id );
            $gtpay_args = $this->get_gtpay_args( $order );
            $gtpay_args_array = array();

            $gtpay_adr = $this->liveurl;

            foreach ($gtpay_args as $key => $value) {
                $gtpay_args_array[] = '<input type="hidden" name="' . esc_attr($key) . '" value="' . esc_attr($value) . '" />';
            }


            $woocommerce->add_inline_js('
                jQuery("body").block({ 
                    message: "<img src=\"'.esc_url( $woocommerce->plugin_url() ).'/assets/images/ajax-loader.gif\" alt=\"Redirecting...\" style=\"float:left; margin-right: 10px;\" />'.__('Thank you for your order. We are now redirecting you to GTPay to make payment.', 'woocommerce').'", 
                    overlayCSS: { 
                        background: "#fff", 
                        opacity: 0.6 
                    },
                    css: { 
                        padding:        20, 
                        textAlign:      "center", 
                        color:          "#555", 
                        border:         "3px solid #aaa", 
                        backgroundColor:"#fff", 
                        cursor:         "wait",
                        lineHeight: "32px"
                    } 
                });
                jQuery("#submit_gtpay_payment_form").click();
            ');

            $form = '<form action="'.esc_url( $gtpay_adr ).'" method="post" id="gtpay_payment_form">
                    ' . implode('', $gtpay_args_array) . '
                    <input type="submit" class="button-alt" id="submit_gtpay_payment_form" value="'.__('Pay via GTPay', 'woocommerce').'" /> <a class="button cancel" href="'.esc_url( $order->get_cancel_order_url() ).'">'.__('Cancel order &amp; restore cart', 'woocommerce').'</a>
                </form>';
                echo $form; exit;

            return $form;
        }

        function generate_gtpay_try_again_form( $order_id ) {
            global $woocommerce;

            $order = new WC_Order( $order_id );
            $gtpay_args = $this->get_gtpay_args( $order );
            $gtpay_args_array = array();

            $gtpay_adr = $this->liveurl;

            foreach ($gtpay_args as $key => $value) {
                $gtpay_args_array[] = '<input type="hidden" name="' . esc_attr($key) . '" value="' . esc_attr($value) . '" />';
            }

            $form = '<form action="'.esc_url( $gtpay_adr ).'" method="post" id="gtpay_payment_form">
                    ' . implode('', $gtpay_args_array) . '
                    <input type="submit" class="button-alt" id="submit_gtpay_payment_form" value="'.__('Try Again', 'woocommerce').'" />
                </form>';

            return $form;
        }

        function successful_request( $posted ) {
            global $woocommerce;
            print_r($_GET); exit;
            $order_ref = $posted['gtpay_tranx_id'];
            $order = new WC_Order( (int) $order_ref );



            //fool the thanks page into working?
            $_GET['key'] = $order->order_key;
            $_GET['order'] - $order->id;

            if ($this->get_transaction_status($order_ref)) {
                // Check order not already completed
                if ($order->status == 'completed') :
                     return false;
                endif;

                // Payment completed
                $order->add_order_note( __('IPN payment completed', 'woocommerce') );
                $order->payment_complete();
                $woocommerce->cart->empty_cart();

                foreach ($_GET as $k=>$v) {
                    update_post_meta((int)$order_ref, $k, $v);
                }

                update_post_meta( (int) $order_ref, 'Payment Method', 'GTPay');

                $this->feedback_message = $this->thanks_message;
            } else {
                $error_code = '';
                if (@$_GET['resp']) {
                    $error_code = $this->response_codes[$_GET['resp']];
                }

                $try_again = $this->generate_gtpay_try_again_form($this->parse_txn_ref_order_id($order_ref));

                $order->add_order_note(__('Payment Failed - ' . $error_code, 'woocommerce'));
                $order->update_status('failed');

                $woocommerce->add_error('Transaction Failed: ' . $error_code . ' ' . $try_again); // Transaction Failed Notice on Checkout

                $this->feedback_message = $this->failed_message . $error_code . ' ' . $try_again;

            }
        }

        function thankyou_page() {
            echo wpautop($this->feedback_message);
        }

        public function get_response_codes() {
            return array(
                '00'=>'Approved by Financial Institution'
                ,'01'=>'Refer to Financial Institution'
                ,'02'=>'Refer to Financial Institution, Special Condition'
                ,'03'=>'Invalid Merchant'
                ,'04'=>'Pick-up card'
                ,'05'=>'Do Not Honor'
                ,'06'=>'Error'
                ,'07'=>'Pick-Up Card, Special Condition'
                ,'08'=>'Honor with Identification'
                ,'09'=>'Request in Progress'
                ,'10'=>'Approved by Financial Institution, Partial'
                ,'11'=>'Approved by Financial Institution, VIP'
                ,'12'=>'Invalid Transaction'
                ,'13'=>'Invalid Amount'
                ,'14'=>'Invalid Card Number'
                ,'15'=>'No Such Financial Institution'
                ,'16'=>'Approved by Financial Institution, Update Track 3'
                ,'17'=>'Customer Cancellation'
                ,'18'=>'Customer Dispute'
                ,'19'=>'Re-enter Transaction'
                ,'20'=>'Invalid Response from Financial Institution'
                ,'21'=>'No Action Taken by Financial Institution'
                ,'22'=>'Suspected Malfunction'
                ,'23'=>'Unacceptable Transaction Fee'
                ,'24'=>'File Update not Supported'
                ,'25'=>'Unable to Locate Record'
                ,'26'=>'Duplicate Record'
                ,'27'=>'File Update Field Edit Error'
                ,'28'=>'File Update File Locked'
                ,'29'=>'File Update Failed'
                ,'30'=>'Format Error'
                ,'31'=>'Bank Not Supported'
                ,'32'=>'Completed Partially by Financial Institution'
                ,'33'=>'Expired Card, Pick-Up'
                ,'34'=>'Suspected Fraud, Pick-Up'
                ,'35'=>'Contact Acquirer, Pick-Up'
                ,'36'=>'Restricted Card, Pick-Up'
                ,'37'=>'Call Acquirer Security, Pick-Up'
                ,'38'=>'PIN Tries Exceeded, Pick-Up'
                ,'39'=>'No Credit Account'
                ,'40'=>'Function not Supported'
                ,'41'=>'Lost Card, Pick-Up'
                ,'42'=>'No Universal Account'
                ,'43'=>'Stolen Card, Pick-Up'
                ,'44'=>'No Investment Account'
                ,'51'=>'Insufficient Funds'
                ,'52'=>'No Check Account'
                ,'53'=>'No Savings Account'
                ,'54'=>'Expired Card'
                ,'55'=>'Incorrect PIN'
                ,'56'=>'No Card Record'
                ,'57'=>'Transaction not permitted to Cardholder'
                ,'58'=>'Transaction not permitted on Terminal'
                ,'59'=>'Suspected Fraud'
                ,'60'=>'Contact Acquirer'
                ,'61'=>'Exceeds Withdrawal Limit'
                ,'62'=>'Restricted Card'
                ,'63'=>'Security Violation'
                ,'64'=>'Original Amount Incorrect'
                ,'65'=>'Exceeds withdrawal frequency'
                ,'66'=>'Call Acquirer Security'
                ,'67'=>'Hard Capture'
                ,'68'=>'Response Received Too Late'
                ,'75'=>'PIN tries exceeded'
                ,'76'=>'Reserved for Future Postilion Use'
                ,'77'=>'Intervene, Bank Approval Required'
                ,'78'=>'Intervene, Bank Approval Required for Partial Amount'
                ,'90'=>'Cut-off in Progress'
                ,'91'=>'Issuer or Switch Inoperative'
                ,'92'=>'Routing Error'
                ,'93'=>'Violation of law'
                ,'94'=>'Duplicate Transaction'
                ,'95'=>'Reconcile Error'
                ,'96'=>'System Malfunction'
                ,'98'=>'Exceeds Cash Limit'
                ,'A0'=>'Unexpected error'
                ,'A4'=>'Transaction not permitted to card holder, via channels'
                ,'Z0'=>'Transaction Status Unconfirmed'
                ,'Z1'=>'Transaction Error'
                ,'Z2'=>'Bank account error'
                ,'Z3'=>'Bank collections account error'
                ,'Z4'=>'Interface Integration Error'
                ,'Z5'=>'Duplicate Reference Error'
                ,'Z6'=>'Incomplete Transaction'
                ,'Z7'=>'Transaction Split Pre-processing Error'
                ,'Z8'=>'Invalid Card Number, via channels'
                ,'Z9'=>'Transaction not permitted to card holder, via channels'
            );
        }   

        function gtpay_thanks() {

            echo $this->response_codes(urldecode($_GET['desc']));



            if ($GET['desc'] != '00') {
                $this->generate_gtpay_form($_POST['txnref']);
            }
        }

        function parse_txn_ref_order_id($txnref) {
            $txnref = htmlentities($txnref);
            $txn_details = explode('_', $txnref);
            $marchant_id = $txn_details[1];
            $order_id = $txn_details[0];

            return $order_id;
        }   

        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>'.__('Thank you for your order, please click the button below to pay with GTPay.', 'woocommerce').'</p>';

            echo $this->generate_gtpay_form( $order );
        }

        function check_ipn_response() {
            if (isset($_POST['retRef'])) {
                @ob_clean();

                $_POST = stripslashes_deep($_POST);

                header('HTTP/1.1 200 OK');
                do_action("valid-gtpay-ipn-request", $_POST);
            }
        }

        function get_transaction_status($txnref) { 
            $return = false;
            $txnref = htmlentities($txnref);
            $txn_details = explode('a', $txnref);
            $marchant_id = $txn_details[1];
            //$hash_string = $marchant_id . $txnref . $this->hash_key;
            //$hash = hash('sha512', $hash_string);

            $endpoint = "https://ibank.gtbank.com/GTPay/Tranx.aspx";

            $endpoint .= '?mertid=' . $marchant_id . '&amount=' . $order_total . '&tranxid=' . $order_id . '&hash='.$hash;

            set_time_limit(0);

            $ch = curl_init($endpoint);

            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
            curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT ,120);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER ,false);
            curl_setopt($ch, CURLOPT_TIMEOUT, 120);

            $output = curl_exec($ch);
            $last_url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);

            curl_close($ch);

            if ($last_url) {
                if ($url = parse_url($last_url)) {
                    if ($pairs = explode('&', $url['query'])) {
                    print_r($pairs); exit;
                        foreach ($pairs as $i=>$pair) {
                            $kv = explode('=', $pair);

                            $_GET[$kv[0]] = $kv[1];
                            $_REQUEST[$kv[0]] = $kv[1];

                            if ($kv[0] == 'resp') {
                                if ($kv[1] == '00') {
                                    $return = true;
                                }
                            }
                        }
                    }
                }
            }

            return $return;
        }   

    }

    /**
     * Add the gateway to WooCommerce
     **/
    function add_gtpay_gateway( $methods ) {
        $methods[] = 'WC_GTPay'; return $methods;
    }

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


add_filter('plugins_loaded', 'sb_gtpay_init' );

?>
函数sbgtpay_init(){
如果(!class_存在('WC_支付网关')){
回来
}
类WC_GTPay扩展WC_支付网关{
公共函数uu构造(){
全球商业;
$this->id='gtpay';
$this->icon=plugins\u url('GTPay.png',文件);
$this->has_fields=false;
$this->liveurl=https://ibank.gtbank.com/GTPay/Tranx.aspx';
$this->method_title=uuu('GTPay','woocommerce');
//加载表单字段。
$this->init_form_fields();
//加载设置。
$this->init_settings();
//定义用户集变量
$this->title=$this->settings['title'];
$this->description=$this->settings['description'];
$this->marchant_id=$this->settings['marchant_id'];
$this->hash_key=$this->settings['hash_key'];
$this->Thank_message=$this->设置['Thank_message'];
$this->error_message=$this->settings['error_message'];
$this->feedback\u message='';
$this->response_code=$this->get_response_code();
//行动
添加操作('init',数组(&$this,'check_-ipn_-response');
添加_操作('valid-gtpay-ipn-request',数组(&$this,'successful_request');
添加操作('woocommerce'u receipt'gtpay',数组(&$this,'receipt'u page');
//添加操作('woocmerce\u update\u options\u payment\u gateways',数组(&$this,'process\u admin\u options');
//添加动作('woocommerce'u thankyou'.$this->id,数组(&$this,'thankyou'page');
//行动
如果(版本比较(WOOCOMMERCE版本,'2.0','。。。。。'('感谢您的订单。我们现在将您重定向到GTPay进行支付。','WOOCOMMERCE')。“,
覆盖区:{
背景:“fff”,
不透明度:0.6
},
css:{
填充:20,
textAlign:“居中”,
颜色:#555“,
边框:“3px实心#aaa”,
背景颜色:“fff”,
光标:“等待”,
线宽:“32px”
} 
});
jQuery(“提交支付表单”)。单击();
');
$form='1
“.内爆('$gtpay_args_数组)。”
';
退出;
返回$表格;
}
函数生成支付重试表单($order\u id){
全球商业;
$order=新WC\U订单($order\U id);
$gtpay_args=$this->getgtpay_args($order);
$gtpay_args_array=array();
$gtpay_adr=$this->liveurl;
foreach($gtpay_参数为$key=>$value){
$gtpay_args_数组[]='';
}
$form='1
“.内爆('$gtpay_args_数组)。”
';
返回$表格;
}
函数成功请求($posted){
全球商业;
打印(获取);退出;
$order_ref=$posted['gtpay_tranx_id'];
$order=新WC_订单((int)$order_ref);
//愚弄感谢页面使其工作?
$\u GET['key']=$order->order\u key;
$\u获取['order']-$order->id;
如果($this->get_transaction_status($order_ref)){
//检查订单尚未完成
如果($order->status=='completed'):
返回false;
endif;
//付款完成
$order->add_order_note(uuuu('IPN付款完成,'woocommerce'));
$order->payment_complete();
$woocommerce->cart->empty_cart();
foreach($\u获取为$k=>$v){
更新后元((int)$order\u ref,$k,$v);
}
更新后元((int)$order\u ref,'Payment Method','GTPay');
$this->feedback\u message=$this->thank\u message;
}否则{
$error_code='';
如果(@$\u获取['resp']){
$error\u code=$this->response\u code[$\u GET['resp'];
}
$try_reach=$this->generategtpay_try_reach_form($this->parse_txn_ref_order_id($order_ref));
$order->add_order_note('Payment Failed-'。$error_code,'woocommerce');
$order->update_status('failed');
$woocommerce->add_error('Transaction Failed:'.$error_code.'.$try_重试);//签出时通知事务失败
$this->feedback\u message=$this->failed\u message.$error\u code.''.$try\u;
}
}
函数谢谢(第页){
回显wpautop($this->feedback_message);
}
公共函数获取\响应\代码(){
返回数组(
“00”=>“金融机构批准”
,'01'=>“指金融机构”
,'02'=>“指金融机构,特殊条件”
,'03'=>“无效商户”
,'04'=>“收件卡”
,'05'=>“不尊重”
,'06'=>“错误”
,'07'=>“提货卡,特殊条件”
,'08'=>“荣誉与身份”
,'09'=>'程序中的请求
$this->notify_url         = WC()->api_request_url( 'WC_Gateway_Paypal' );
// Payment listener/API hook
add_action( 'woocommerce_api_wc_gateway_paypal', array( $this, 'check_ipn_response' ) );