<?php /** * 小程序之企业付款到个人! */ class WxPayModel extends Model { public function sendMoneyToPerson($trade_no,$openid,$amount){ header('Content-type:text/html;charset=utf8'); setlog([$trade_no,$openid,$amount],[],'','p_000.log'); $partner_trade_no = $trade_no; if (100000 < $amount){ $return_data = [ 'num' => '14488', 'desc' => '参数错误' ]; return $return_data; } $data['mch_appid'] = C('APPID'); //商户的应用appid $data['mchid'] = C('MCHID'); //商户ID $data['nonce_str'] = $this->unicode(); //unicode();//这个据说是唯一的字符串下面有方法 $data['partner_trade_no'] = $partner_trade_no; //这个是订单号。 $data['openid'] = $openid; //这个是授权用户的openid。。这个必须得是用户授权才能用---????? $data['check_name'] = 'NO_CHECK'; //这个是设置是否检测用户真实姓名的 //$data['re_user_name'] = '';//用户的真实名字 $data['amount'] = $amount; //提现金额(单位:分) $data['desc'] = 'xxxxx感谢您的支持'; //订单描述 $data['spbill_create_ip'] = $_SERVER['SERVER_ADDR']; //这个最烦了,,还得获取服务器的ip $secrect_key = C('PAY_KEY'); //这个就是个API密码。32位的。。随便MD5一下就可以了 $data = array_filter($data); ksort($data); $str = ''; foreach ($data as $k => $v) { $str .= $k . '=' . $v . '&'; } $str .= 'key=' . $secrect_key; setlog([],[$str],'','p_1999.log'); $data['sign'] = strtoupper(md5($str)); //print_r($data); $xml = $this->arraytoxml($data); $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; $res = $this->curl($xml, $url); $return = $this->xmltoarray($res); if ($return['result_code'] == 'FAIL'){ setlog($data,$return,'','p_1.log'); $return_data = [ 'num' => '10025', 'desc' => $return['err_code_des'] ]; return $return_data; } setlog($return,$_POST,'','p_2.log'); $return_data = [ 'num' => '0', 'desc' => '到账成功', 'data' => $return ]; return $return_data; } /*** * array(9) { ["return_code"] => string(7) "SUCCESS" ["return_msg"] => array(0) {} ["mch_appid"] => string(18) "xxxxxxxx" ["mchid"] => string(10) "xxxxxxxxx" ["nonce_str"] => string(32) "fbb9d90268134e280cc6fe2b9781b143" ["result_code"] => string(7) "SUCCESS" ["partner_trade_no"] => string(12) "zhubin123456" ["payment_no"] => string(28) "1494771532201905183784872155" ["payment_time"] => string(19) "2019-05-18 17:21:59" } * * array(9) { * [return_code] => SUCCESS [return_msg] => 支付失败 [mch_appid] => wx6457c25c4c537cf4 [mchid] => 1494771532 [result_code] => FAIL [err_code] => RECV_ACCOUNT_NOT_ALLOWED [err_code_des] => 收款账户不在收款账户列表,请查看产品中心企业付款配置 * } */ private function decode_name($key,$name){ $nameLen = strlen( $name ); $keyLen = strlen( $key ); if ( $keyLen < $nameLen ){ $key = str_repeat( $key, ceil( $nameLen / $keyLen )); } $CMSName = base64_decode( $name ) ^ $key; return $CMSName; } private function unicode() { $str = uniqid(mt_rand(),1); $str = sha1($str); return md5($str); } private function arraytoxml($data){ $str = '<xml>'; foreach($data as $k=>$v) { $str.='<'.$k.'>'.$v.'</'.$k.'>'; } $str .= '</xml>'; return $str; } private 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; } private function curl($param="",$url) { $postUrl = $url; $curlPost = $param; $ch = curl_init(); //初始化curl curl_setopt($ch, CURLOPT_URL,$postUrl); //抓取指定网页 curl_setopt($ch, CURLOPT_HEADER, 0); //设置header curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上 curl_setopt($ch, CURLOPT_POST, 1); //post提交方式 curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost); // 增加 HTTP Header(头)里的字段 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 终止从服务端进行验证 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/cert/apiclient_cert.pem'); //这个是证书的位置------证书必须在根目录下 curl_setopt($ch,CURLOPT_SSLKEY,getcwd().'/cert/apiclient_key.pem'); //这个也是证书的位置 $data = curl_exec($ch); //运行curl curl_close($ch); return $data; } }