原创 2017年08月07日 21:51:25
//获取支付必备参数prepay_id 以及进行支付返回paysignpublic function doPagegetprepayid(){ //进行查询数据库获得支付参数, $resinfo = "SELECT * FROM " . tablename('abcshopping_pay') . " WHERE `id` = 1 "; $app_info = pdo_fetch($resinfo); foreach ($app_info as $k => &$v) { $v = trim($v); }; $app_info['shopidshop']=$app_info['shopstart']; header("Content-type:text/html;charset=utf-8"); //此处进行字符集初始化, global $_GPC, $_W; $order_id = $_GPC['orderid']; $sql = "SELECT * FROM " . tablename('abcshopping_order') . " WHERE `id` = '" . $order_id . "'"; $res = pdo_fetch($sql); function http_request_curl($url, $rawData) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_POSTFIELDS, $rawData); curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Content-Type: text' ) ); $data = curl_exec($ch); curl_close($ch); return ($data); } $data = array( //'appid' => 'wxf0d27e41678c566c', //小程序appid 'appid' => $app_info['appid'], 'body' => '腾讯-游戏', //'mch_id' => '1484855592', //微信支付商户号 'mch_id' => $app_info['shopid'], 'nonce_str' => md5(date('YmdHis') . time() . rand(1000, 9999)), //随机字符串 'notify_url' => 'http://www.weixin.qq.com/wxpay/pay.php', //异步回调地址 'openid' => $_GPC['openid'], //用户登录时获取的code中含有的值 'out_trade_no' => $res['ordersn'], //商家订单号 'spbill_create_ip' => $_GPC['ip'], //APP和网页支付提交用户端ip 'total_fee' => $res['price'] * 100, //订单总额 'trade_type' => 'JSAPI' //交易类型 ); //$key = 'z4hgl4cnf5ac2wl3msiek5p0x3aiy2yc'; //商户秘钥 $key = $app_info['shopidshop']; //制作签名 //签名步骤一:按字典序排序参数 ksort($data); $buff = ""; foreach ($data as $k => $v) { if ($k != "sign" && $v != "" && !is_array($v)) { $buff .= $k . "=" . $v . "&"; } } $buff = trim($buff, "&"); //签名步骤二:在string后加入KEY $string = $buff . "&key=" . $key; //签名步骤三:MD5加密 $string = md5($string); //签名步骤四:所有字符转为大写 $sign = strtoupper($string); $data['sign'] = $sign; ksort($data); //进行拼接数据 $abc_xml = "<xml>"; foreach ($data as $key => $val) { if (is_numeric($val)) { $abc_xml .= "<" . $key . ">" . $val . "</" . $key . ">"; } else { $abc_xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">"; } } $abc_xml .= "</xml>"; //统一下单接口prepay_id $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; $xml = http_request_curl($url, $abc_xml); //POST方式请求http //将XMl转化为数组 function xmlToArray($xml) { //禁止引用外部xml实体 libxml_disable_entity_loader(true); $xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA); $val = json_decode(json_encode($xmlstring), true); return $val; } $info = xml2array($xml); //进行拼接sign function MakeSign($params, $KEY) { //签名步骤一:按字典序排序数组参数 ksort($params); $buff1 = ''; foreach ($params as $k => $v) { if ($k != "sign" && $v != "" && !is_array($v)) { $buff1 .= $k . "=" . $v . "&"; } } $buff1 = trim($buff1, "&"); //签名步骤二:在string后加入KEY $string = $buff1 . "&key=" . $KEY; //签名步骤三:MD5加密 $string = md5($string); //签名步骤四:所有字符转为大写 $result = strtoupper($string); return $result; } $params = array( 'appId' => $data['appid'], 'nonceStr' => $data['nonce_str'], 'package' => 'Sign=WXPay&prepay_id=' . $info['prepay_id'], 'signType' => 'MD5', 'timeStamp' => time() ); //$key = 'z4hgl4cnf5ac2wl3msiek5p0x3aiy2yc'; //商户秘钥 $key = $app_info['shopidshop']; $info['paySign'] = MakeSign($params, $key); $info['timeStamp'] = "" . $params['timeStamp'] . ""; $info['nonceStr'] = $params['nonceStr']; $info['package'] = $params['package']; $info = array_merge($info, $app_info); //请求成功后进行返回数据信息 if ($info['return_code'] == 'SUCCESS' || $info['result_code'] == 'SUCCESS') { $errno = 0; $message = '请求成功'; return $this->result($errno, $message, $info); } else { $errno = -1; $message = '请求失败'; return $this->result($errno, $message, $info); }}