Changeset 2106139
- Timestamp:
- 06/14/2019 10:35:51 AM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
xunhu-wechat-payment-for-woocommerce/trunk/class-wechat-wc-payment-gateway.php
r2035631 r2106139 8 8 $this->icon = XH_Wechat_Payment_URL . '/images/logo/wechat.png'; 9 9 $this->has_fields = false; 10 10 11 11 $this->method_title = __('Wechat Payment',XH_Wechat_Payment); 12 12 $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 14 14 $this->title = $this->get_option ( 'title' ); 15 15 $this->description = $this->get_option ( 'description' ); 16 16 17 17 $this->enabled = $this->get_option ( 'enabled' ); 18 18 $this->instructions = $this->get_option('instructions'); 19 19 20 20 $this->init_form_fields (); 21 21 $this->init_settings (); 22 23 22 23 24 24 add_filter ( 'woocommerce_payment_gateways', array($this,'woocommerce_add_gateway') ); 25 25 add_action ( 'woocommerce_update_options_payment_gateways_' .$this->id, array ($this,'process_admin_options') ); … … 28 28 add_action ( 'woocommerce_thankyou_'.$this->id, array( $this, 'thankyou_page' ) ); 29 29 } 30 30 31 31 public function notify(){ 32 32 global $XH_Wechat_Payment_WC_Payment_Gateway; 33 33 34 34 $data = $_POST; 35 35 if(!isset($data['hash']) … … 40 40 return; 41 41 } 42 42 43 43 $appkey =$XH_Wechat_Payment_WC_Payment_Gateway->get_option('appsecret'); 44 44 $hash =$XH_Wechat_Payment_WC_Payment_Gateway->generate_xh_hash($data,$appkey); … … 46 46 return; 47 47 } 48 48 49 49 $order = wc_get_order($data['trade_order_id']); 50 50 try{ … … 52 52 throw new Exception('Unknow Order (id:'.$data['trade_order_id'].')'); 53 53 } 54 54 55 55 if(!(method_exists($order, 'is_paid')?$order->is_paid():in_array($order->get_status(), array( 'processing', 'completed' )))&&$data['status']=='OD'){ 56 56 $order->payment_complete(isset($data['transacton_id'])?$data['transacton_id']:''); … … 60 60 $logger = new WC_Logger(); 61 61 $logger->add( 'xh_wedchat_payment', $e->getMessage() ); 62 62 63 63 $params = array( 64 64 'action'=>'fail', … … 67 67 'errmsg'=>$e->getMessage() 68 68 ); 69 69 70 70 $params['hash']=$XH_Wechat_Payment_WC_Payment_Gateway->generate_xh_hash($params, $appkey); 71 71 ob_clean(); … … 73 73 exit; 74 74 } 75 75 76 76 $params = array( 77 77 'action'=>'success', 78 78 'appid'=>$XH_Wechat_Payment_WC_Payment_Gateway->get_option('appid') 79 79 ); 80 80 81 81 $params['hash']=$XH_Wechat_Payment_WC_Payment_Gateway->generate_xh_hash($params, $appkey); 82 82 ob_clean(); … … 88 88 return $methods; 89 89 } 90 90 91 91 public function process_payment($order_id) { 92 92 $turl = $this->get_option('tranasction_url'); … … 107 107 // ); 108 108 // } 109 109 110 110 $order = wc_get_order ( $order_id ); 111 111 if(!$order||(method_exists($order, 'is_paid')?$order->is_paid():in_array($order->get_status(), array( 'processing', 'completed' )))){ … … 115 115 ); 116 116 } 117 117 118 118 $expire_rate = floatval($this->get_option('exchange_rate',1)); 119 119 if($expire_rate<=0){ 120 120 $expire_rate=1; 121 121 } 122 122 123 123 $siteurl = rtrim(home_url(),'/'); 124 124 $posi =strripos($siteurl, '/'); … … 127 127 $siteurl.='/'; 128 128 } 129 130 $total_amount = round($order->get_total()*$expire_rate,2); 129 130 $total_amount = round($order->get_total()*$expire_rate,2); 131 131 $data=array( 132 132 'version' => '1.1',//api version 133 'lang' => get_option('WPLANG','zh-cn'), 133 'lang' => get_option('WPLANG','zh-cn'), 134 134 'plugins' => 'woo-wechat', 135 135 'appid' => $this->get_option('appid'), … … 146 146 'nonce_str' => str_shuffle(time()) 147 147 ); 148 148 149 149 $hashkey = $this->get_option('appsecret'); 150 150 $data['hash'] = $this->generate_xh_hash($data,$hashkey); 151 151 $url = $turl.'/payment/do.html'; 152 152 153 153 try { 154 154 $response = $this->http_post($url, json_encode($data)); … … 157 157 throw new Exception('Internal server error',500); 158 158 } 159 159 160 160 $hash = $this->generate_xh_hash($result,$hashkey); 161 161 if(!isset( $result['hash'])|| $hash!=$result['hash']){ 162 162 throw new Exception(__('Invalid sign!',XH_Wechat_Payment),40029); 163 163 } 164 164 165 165 if($result['errcode']!=0){ 166 166 throw new Exception($result['errmsg'],$result['errcode']); 167 167 } 168 169 168 169 170 170 return array( 171 171 'result' => 'success', … … 180 180 } 181 181 } 182 182 183 183 private function http_post($url,$data){ 184 184 if(!function_exists('curl_init')){ … … 202 202 throw new Exception("invalid httpstatus:{$httpStatusCode} ,response:$response,detail_error:".$error,$httpStatusCode); 203 203 } 204 204 205 205 return $response; 206 206 } 207 207 208 208 public function generate_xh_hash(array $datas,$hashkey){ 209 209 ksort($datas); 210 210 reset($datas); 211 211 212 212 $pre =array(); 213 213 foreach ($datas as $key => $data){ … … 218 218 $pre[$key]=$data; 219 219 } 220 220 221 221 $arg = ''; 222 222 $qty = count($pre); 223 223 $index=0; 224 224 225 225 foreach ($pre as $key=>$val){ 226 226 $arg.="$key=$val"; … … 229 229 } 230 230 } 231 231 232 232 return md5($arg.$hashkey); 233 233 } 234 234 235 235 private function is_wechat_app(){ 236 236 return strripos($_SERVER['HTTP_USER_AGENT'],'micromessenger'); 237 237 } 238 238 239 239 public function thankyou_page() { 240 240 if ( $this->instructions ) { … … 242 242 } 243 243 } 244 244 245 245 /** 246 246 * Add content to the WC emails. … … 300 300 'type' => 'text', 301 301 'css' => 'width:400px', 302 'default'=>'2 0146123713',302 'default'=>'2147483647', 303 303 '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' 306 305 ), 307 306 'appsecret' => array( … … 309 308 'type' => 'text', 310 309 'css' => 'width:400px', 311 'default'=>' 6D7B025B8DD098C485F0805193136FB9',310 'default'=>'160130736b1ac0d54ed7abe51e44840b', 312 311 'section' => 'default' 313 312 ), … … 316 315 'type' => 'text', 317 316 'css' => 'width:400px', 318 'default'=>'https:// pay2.xunhupay.com/v2',317 'default'=>'https://api.xunhupay.com', 319 318 'section' => 'default', 320 319 'description'=>'' … … 330 329 ); 331 330 } 332 331 333 332 public function get_order_title($order, $limit = 98) { 334 333 $order_id = method_exists($order, 'get_id')? $order->get_id():$order->id; 335 334 $title ="#{$order_id}"; 336 335 337 336 $order_items = $order->get_items(); 338 337 if($order_items){ … … 346 345 } 347 346 } 348 347 349 348 $title = mb_strimwidth($title, 0, $limit,'utf-8'); 350 349 return apply_filters('xh-payment-get-order-title', $title,$order); 351 350 } 352 351 353 352 } 354 353
Note: See TracChangeset
for help on using the changeset viewer.