<?php
/**
 * [UQcms System] Copyright uqcms.com
 * QQ: 2200631718
 * Author: gj.name
 * This is NOT a freeware
*/
 class pay_wxpay { private static $instance; public static function init(){ if (is_null(self::$instance)){ self::$instance = new self(); } return self::$instance; } var $gateway = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; var $ordequery = 'https://api.mch.weixin.qq.com/pay/orderquery'; function __construct($result){ $this->appid = $result['appid']; $this->mch_id = $result['mch_id']; $this->pay_key = $result['pay_key']; $this->support = $result['support']; $this->notify_url = cfg('site_url').'member/api/wxpay_notify.php'; } function pay_url($param){ $openid = @$_SESSION['connect']['wchat']['openid']; $this->trade_no = $param['trade_no']; $param2 = array( 'appid'=>$this->appid, 'mch_id'=>$this->mch_id, 'notify_url'=>$this->notify_url, 'body'=> $param['title'], 'out_trade_no'=> $param['trade_no'], 'total_fee'=>$param['price']*100, 'device_info'=>'WEB', 'nonce_str'=>get_rand(32), 'limit_pay'=>'no_credit', 'fee_type'=>'CNY', 'spbill_create_ip'=> get_ip(), 'product_id'=>'12235413214070356458060', ); if($this->support == 'pc'){ $pay_arr_self = array( 'trade_type'=>'NATIVE' ); }elseif($this->support == 'wchat'){ $pay_arr_self = array( 'trade_type'=>'JSAPI', 'openid'=>$openid ); } $pay_arr = array_merge($pay_arr_self, $param2); $sys_sign = array('sign'=>$this->wx_sign($pay_arr)); $result = array_merge($pay_arr, $sys_sign); $post_data = $this->wx_xml($result); $result_data = curl::post($this->gateway, $post_data); $postObj = $this->wx_xml_to_array($result_data); if(isset($postObj['err_code'])){ if($postObj['err_code'] == 'ORDERPAID'){ exit('已经支付'); } }else{ if($this->support == 'pc') { $back_data['code'] = base64_encode($postObj['code_url']); $back_data['trade_no'] = $this->trade_no; $back_data['tpl'] = 'pay_wx_pc'; return $back_data; } elseif($this->support == 'wchat') { include UQCMS_PATH.'member/api/pay.class.php'; $back_data['jscfg'] = wchat::init()->js_config(); $back_data['paycfg'] = $this->js_query($postObj['prepay_id']); $back_data['trade_no'] = $this->trade_no; $back_data['tpl'] = 'pay_wx_wchat'; $back_data['jump_url'] = pay_jump(0, $this->trade_no); return $back_data; } } } function pay_status($param){ $pay_arr = array( 'nonce_str'=>get_rand(32) ); $pay_arr = array_merge($pay_arr, $param); $sys_sign = array('sign'=>$this->wx_sign($pay_arr)); $result = array_merge($pay_arr, $sys_sign); $post_data = $this->wx_xml($result); $result_data = curl::post($this->ordequery, $post_data); return $this->wx_xml_to_array($result_data); } function js_query($prepay_id){ $time = time(); $baseArr['appId'] = $this->appid; $baseArr['timeStamp'] = "$time"; $baseArr['nonceStr'] = get_rand(32); $baseArr['package'] = "prepay_id=".$prepay_id; $baseArr['signType'] = "MD5"; $baseArr['paySign'] = $this->wx_sign($baseArr); return $baseArr; } function wx_xml($array){ if(!is_array($array) || count($array) <= 0){ if(!$xml){ return; } return ; } $xml = "<xml>"; foreach ($array as $key=>$val){ if (is_numeric($val)){ $xml.="<".$key.">".$val."</".$key.">"; }else{ $xml.="<".$key."><![CDATA[".$val."]]></".$key.">"; } } $xml.="</xml>"; return $xml; } function wx_xml_to_array($xml){ if(!$xml){ return false; } libxml_disable_entity_loader(true); $result = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true); if($result['return_code'] == 'SUCCESS'){ return $result; }else{ return false; } } function wx_sign($arr){ ksort($arr); $buff = ""; foreach ($arr as $k => $v){ if($k != "sign" && $v != "" && !is_array($v)){ $buff .= $k . "=" . $v . "&"; } } $string = trim($buff, "&"); $string = $string . "&key=".$this->pay_key; $string = md5($string); return strtoupper($string); } } 