Plugin Directory

Changeset 2106139


Ignore:
Timestamp:
06/14/2019 10:35:51 AM (7 years ago)
Author:
xunhuweb
Message:

new api

File:
1 edited

Legend:

Unmodified
Added
Removed
  • xunhu-wechat-payment-for-woocommerce/trunk/class-wechat-wc-payment-gateway.php

    r2035631 r2106139  
    88        $this->icon               = XH_Wechat_Payment_URL . '/images/logo/wechat.png';
    99        $this->has_fields         = false;
    10        
     10
    1111        $this->method_title       = __('Wechat Payment',XH_Wechat_Payment);
    1212        $this->method_description = __('Helps to add Wechat payment gateway that supports the features including QR code payment, OA native payment, exchange rate.',XH_Wechat_Payment);
    13        
     13
    1414        $this->title              = $this->get_option ( 'title' );
    1515        $this->description        = $this->get_option ( 'description' );
    16        
     16
    1717        $this->enabled            = $this->get_option ( 'enabled' );
    1818        $this->instructions       = $this->get_option('instructions');
    19        
     19
    2020        $this->init_form_fields ();
    2121        $this->init_settings ();
    22        
    23        
     22
     23
    2424        add_filter ( 'woocommerce_payment_gateways', array($this,'woocommerce_add_gateway') );
    2525        add_action ( 'woocommerce_update_options_payment_gateways_' .$this->id, array ($this,'process_admin_options') );
     
    2828        add_action ( 'woocommerce_thankyou_'.$this->id, array( $this, 'thankyou_page' ) );
    2929    }
    30    
     30
    3131    public function notify(){
    3232        global $XH_Wechat_Payment_WC_Payment_Gateway;
    33          
     33
    3434        $data = $_POST;
    3535        if(!isset($data['hash'])
     
    4040            return;
    4141        }
    42    
     42
    4343        $appkey =$XH_Wechat_Payment_WC_Payment_Gateway->get_option('appsecret');
    4444        $hash =$XH_Wechat_Payment_WC_Payment_Gateway->generate_xh_hash($data,$appkey);
     
    4646            return;
    4747        }
    48    
     48
    4949        $order = wc_get_order($data['trade_order_id']);
    5050        try{
     
    5252                throw new Exception('Unknow Order (id:'.$data['trade_order_id'].')');
    5353            }
    54    
     54
    5555            if(!(method_exists($order, 'is_paid')?$order->is_paid():in_array($order->get_status(),  array( 'processing', 'completed' )))&&$data['status']=='OD'){
    5656                $order->payment_complete(isset($data['transacton_id'])?$data['transacton_id']:'');
     
    6060            $logger = new WC_Logger();
    6161            $logger->add( 'xh_wedchat_payment', $e->getMessage() );
    62    
     62
    6363            $params = array(
    6464                'action'=>'fail',
     
    6767                'errmsg'=>$e->getMessage()
    6868            );
    69    
     69
    7070            $params['hash']=$XH_Wechat_Payment_WC_Payment_Gateway->generate_xh_hash($params, $appkey);
    7171            ob_clean();
     
    7373            exit;
    7474        }
    75    
     75
    7676        $params = array(
    7777            'action'=>'success',
    7878            'appid'=>$XH_Wechat_Payment_WC_Payment_Gateway->get_option('appid')
    7979        );
    80    
     80
    8181        $params['hash']=$XH_Wechat_Payment_WC_Payment_Gateway->generate_xh_hash($params, $appkey);
    8282        ob_clean();
     
    8888        return $methods;
    8989    }
    90    
     90
    9191    public function process_payment($order_id) {
    9292        $turl = $this->get_option('tranasction_url');
     
    107107//          );
    108108//      }
    109      
     109
    110110        $order            = wc_get_order ( $order_id );
    111111        if(!$order||(method_exists($order, 'is_paid')?$order->is_paid():in_array($order->get_status(),  array( 'processing', 'completed' )))){
     
    115115            );
    116116        }
    117        
     117
    118118        $expire_rate      = floatval($this->get_option('exchange_rate',1));
    119119        if($expire_rate<=0){
    120120            $expire_rate=1;
    121121        }
    122        
     122
    123123        $siteurl = rtrim(home_url(),'/');
    124124        $posi =strripos($siteurl, '/');
     
    127127            $siteurl.='/';
    128128        }
    129        
    130         $total_amount     = round($order->get_total()*$expire_rate,2);     
     129
     130        $total_amount     = round($order->get_total()*$expire_rate,2);
    131131        $data=array(
    132132              'version'   => '1.1',//api version
    133               'lang'       => get_option('WPLANG','zh-cn'),   
     133              'lang'       => get_option('WPLANG','zh-cn'),
    134134              'plugins'   => 'woo-wechat',
    135135              'appid'     => $this->get_option('appid'),
     
    146146              'nonce_str' => str_shuffle(time())
    147147        );
    148        
     148
    149149        $hashkey          = $this->get_option('appsecret');
    150150        $data['hash']     = $this->generate_xh_hash($data,$hashkey);
    151151        $url              = $turl.'/payment/do.html';
    152        
     152
    153153        try {
    154154            $response     = $this->http_post($url, json_encode($data));
     
    157157                throw new Exception('Internal server error',500);
    158158            }
    159              
     159
    160160            $hash         = $this->generate_xh_hash($result,$hashkey);
    161161            if(!isset( $result['hash'])|| $hash!=$result['hash']){
    162162                throw new Exception(__('Invalid sign!',XH_Wechat_Payment),40029);
    163163            }
    164            
     164
    165165            if($result['errcode']!=0){
    166166                throw new Exception($result['errmsg'],$result['errcode']);
    167167            }
    168            
    169            
     168
     169
    170170            return array(
    171171                'result'  => 'success',
     
    180180        }
    181181    }
    182    
     182
    183183    private function http_post($url,$data){
    184184        if(!function_exists('curl_init')){
     
    202202            throw new Exception("invalid httpstatus:{$httpStatusCode} ,response:$response,detail_error:".$error,$httpStatusCode);
    203203        }
    204        
     204
    205205        return $response;
    206206    }
    207    
     207
    208208    public function generate_xh_hash(array $datas,$hashkey){
    209209        ksort($datas);
    210210        reset($datas);
    211        
     211
    212212        $pre =array();
    213213        foreach ($datas as $key => $data){
     
    218218            $pre[$key]=$data;
    219219        }
    220        
     220
    221221        $arg  = '';
    222222        $qty = count($pre);
    223223        $index=0;
    224        
     224
    225225        foreach ($pre as $key=>$val){
    226226            $arg.="$key=$val";
     
    229229            }
    230230        }
    231        
     231
    232232        return md5($arg.$hashkey);
    233233    }
    234    
     234
    235235    private function is_wechat_app(){
    236236        return strripos($_SERVER['HTTP_USER_AGENT'],'micromessenger');
    237237    }
    238    
     238
    239239    public function thankyou_page() {
    240240        if ( $this->instructions ) {
     
    242242        }
    243243    }
    244    
     244
    245245    /**
    246246     * Add content to the WC emails.
     
    300300                        'type'        => 'text',
    301301                        'css'         => 'width:400px',
    302                          'default'=>'20146123713',
     302                         'default'=>'2147483647',
    303303                        'section'     => 'default',
    304                         'description'=>'app下载:https://www.xunhupay.com/app-v2.html<br/>
    305                                                                                                          帮助文档:https://www.xunhupay.com/19.html'
     304                        'description'=>'帮助文档:https://www.xunhupay.com/114.html'
    306305                ),
    307306                'appsecret' => array(
     
    309308                        'type'        => 'text',
    310309                        'css'         => 'width:400px',
    311                          'default'=>'6D7B025B8DD098C485F0805193136FB9',
     310                         'default'=>'160130736b1ac0d54ed7abe51e44840b',
    312311                        'section'     => 'default'
    313312                ),
     
    316315                        'type'        => 'text',
    317316                        'css'         => 'width:400px',
    318                          'default'=>'https://pay2.xunhupay.com/v2',
     317                         'default'=>'https://api.xunhupay.com',
    319318                        'section'     => 'default',
    320319                    'description'=>''
     
    330329        );
    331330    }
    332        
     331
    333332    public function get_order_title($order, $limit = 98) {
    334333        $order_id = method_exists($order, 'get_id')? $order->get_id():$order->id;
    335334        $title ="#{$order_id}";
    336        
     335
    337336        $order_items = $order->get_items();
    338337        if($order_items){
     
    346345            }
    347346        }
    348        
     347
    349348        $title = mb_strimwidth($title, 0, $limit,'utf-8');
    350349        return apply_filters('xh-payment-get-order-title', $title,$order);
    351350    }
    352    
     351
    353352}
    354353
Note: See TracChangeset for help on using the changeset viewer.