APP 调微信支付接口 PHP代码 APP端 调用时报-100 什么原因?
下面是代码,那个大神帮忙看看
<?php namespace Api\Controller; use Think\Controller; class ApppayController extends Controller { /* public function index(){ $appid = "wx8ef4a596503a9dca"; $secret="4b382d4f81b825e6d55d50778b389f19"; $code = $_GET['code']; $url_format = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code"; $json = file_get_contents($url_format); // $data = json_decode($json,true); echo $json; }*/ public function wxpay() { $random = $this -> randomkeys(); $appid = 'wxeac548ffb*******'; $body = "支付测试"; $mch_id = "1496284****"; $KEY = "61bddsa93f7f2fa1dd1234a069b*****"; $nonce_str = $random; $notify_url = "https://wx.goutianmi.com/Api/loadsing/paySu"; $out_trade_no = time() . mt_rand(10000, 99999); //商户订单号,不能重复 $spbill_create_ip = $_SERVER["REMOTE_ADDR"]; $trade_type = 'APP'; $total_fee = I('post.price', 1) * 100; // $openid = I('post.openid'); $post['appid'] = $appid; $post['body'] = $body; $post['mch_id'] = $mch_id; $post['nonce_str'] = $nonce_str; $post['notify_url'] = $notify_url; // $post['openid'] = $openid; $post['out_trade_no'] = $out_trade_no; $post['spbill_create_ip'] = $spbill_create_ip; $post['total_fee'] = intval($total_fee); $post['trade_type'] = $trade_type; $sign = $this -> MakeSign($post, $KEY); $this -> sign = $sign; $post_xml = '<xml> <appid>' . $appid . '</appid> <body>' . $body . '</body> <mch_id>' . $mch_id . '</mch_id> <nonce_str>' . $nonce_str . '</nonce_str> <notify_url>' . $notify_url . '</notify_url> <out_trade_no>' . $out_trade_no . '</out_trade_no> <spbill_create_ip>' . $spbill_create_ip . '</spbill_create_ip> <total_fee>' . $total_fee . '</total_fee> <trade_type>' . $trade_type . '</trade_type> <sign>' . $sign . '</sign> </xml> '; $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; $xml = $this -> http_request($url, $post_xml); $array = $this -> xml2array($xml); if ($array['RETURN_CODE'] == 'SUCCESS' && $array['RESULT_CODE'] == 'SUCCESS') { /* $time = time(); $tmp=''; $tmp['appId'] = $appid; $tmp['nonceStr'] = $nonce_str; $tmp['package'] = 'Sign=WXPay'; $tmp['signType'] = 'MD5'; $tmp['timeStamp'] = "$time"; $data['appId'] = $appid; $data['state'] = 1; $data['timeStamp'] = "$time"; $data['nonceStr'] = $nonce_str; $data['signType'] = 'MD5';*/ //$data['package'] = 'prepay_id='.$array['PREPAY_ID']; $data = array('prepayid' => $array['prepay_id'], 'appid' => 'wxeac548ff******', 'partnerid' => '1496284052', 'package' => 'Sign=WXPay', 'noncestr' => $nonce_str, 'timestamp' => time() ); $data['sign'] = $this -> MakeSign($data, $KEY); $data['out_trade_no'] = $out_trade_no; } else { $data['state'] = 0; $data['text'] = "错误"; $data['RETURN_CODE'] = $array['RETURN_CODE']; $data['RETURN_MSG'] = $array['RETURN_MSG']; } exit( json_encode($data,JSON_UNESCAPED_UNICODE )); } public function randomkeys($length = 32) { $pattern = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; for ($i = 0; $i < $length; $i++) { $key .= $pattern[mt_rand(0, strlen($pattern) - 1)]; } return $key; } public function paySu() { $post = post_data(); $post_data = $this -> xml_to_array($post); $postSign = $post_data['sign']; unset($post_data['sign']); ksort($post_data); $str = $this -> ToUrlParams($post_data); $user_sign = strtoupper(md5($post_data)); $where['crsNo'] = $post_data['out_trade_no']; //$order_status = M('home_order','xxf_witkey_')->where($where)->find(); if ($post_data['return_code'] == 'SUCCESS' && $postSign) { /* * 首先判断,订单是否已经更新为ok,因为微信会总共发送8次回调确认 * 其次,订单已经为ok的,直接返回SUCCESS * 最后,订单没有为ok的,更新状态为ok,返回SUCCESS */ /* if($order_status['order_status']=='ok'){ $this->return_success(); }else{ $updata['order_status'] = 'ok'; if(M('home_order','xxf_witkey_')->where($where)->save($updata)){ $this->return_success(); } }*/ $ddid = $post_data['out_trade_no']; $save_data["zhuangtai"] = 1; $save_data["pay_time"] = date("Y-m-d H:i:s", time()); if ( M("dingdan") -> where(array("ddid" => $ddid)) -> save($save_data)) { $this -> return_success(); } } else { echo '微信支付失败'; } } public function payStatus() { $ddid = $_POST['orderNum']; $save_data["zhuangtai"] = 1; $save_data["pay_time"] = date("Y-m-d H:i:s", time()); if ( M("dingdan") -> where(array("ddid" => $ddid)) -> save($save_data)) { $this -> return_success(); } } private function return_success() { $return['return_code'] = 'SUCCESS'; $return['return_msg'] = 'OK'; $xml_post = '<xml> <return_code>' . $return['return_code'] . '</return_code> <return_msg>' . $return['return_msg'] . '</return_msg> </xml>'; echo $xml_post; exit ; } public function MakeSign($params, $KEY) { ksort($params); $string = $this -> ToUrlParams($params); $string = $string . "&key=" . $KEY; $string = md5($string); $result = strtoupper($string); return $result; } public function ToUrlParams($params) { $string = ''; if (!empty($params)) { $array = array(); foreach ($params as $key => $value) { $array[] = $key . '=' . $value; } $string = implode("&", $array); } return $string; } public function http_request($url, $data = null, $headers = array()) { $curl = curl_init(); if (count($headers) >= 1) { curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); } curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); if (!empty($data)) { curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); return $output; } //获取xml里面数据,转换成array private function xml2array($xml) { $p = xml_parser_create(); xml_parse_into_struct($p, $xml, $vals, $index); xml_parser_free($p); $data = ""; foreach ($index as $key => $value) { if ($key == 'xml' || $key == 'XML') continue; $tag = $vals[$value[0]]['tag']; $value = $vals[$value[0]]['value']; $data[$tag] = $value; } return $data; } } ?>
phpcn_u397472018-08-09 16:26:45
<return_code>' . $return['return_code'] . '</return_code>
<return_msg>' . $return['return_msg'] . '</return_msg>
这里,单引号的拼接没看懂