微信提现
2018-11-01 15:33 wlf123457 阅读(366) 评论(0) 编辑 收藏 举报不多BB直接粘代码
<?php
class tixian{
/** 提现
* @paramstring $money 单位分(接口中已 *100)
* @paramstring $appid appid
* @paramstring $secret app_secret
* @paramstring $mch_id 商户号
* @paramstring $key 商户api秘钥
* @param $openid 用户openid
* @paramstring $order_trade_no 订单号
* @param $desc 备注信息
* @returnstring|\think\response\Json
*/
public function wxTiXian($money, $appid, $secret, $mch_id, $key, $openid, $order_trade_no, $desc){ //单位为分,最低为100
if(empty($money) || empty($appid) || empty($secret) || empty($mch_id) || empty($key) || empty($openid) || empty($order_trade_no) || empty($desc)){
return json($this->return_data('','提现失败',1) );
}
$arr = array();
$arr['mch_appid'] = $appid;
$arr['mchid'] = $mch_id;
$arr['nonce_str'] = $this->rand_str(32);//随机字符串,不长于32位
$arr['partner_trade_no'] = '151' . date("Ymd") . rand(10000, 90000) . rand(10000, 90000);//商户订单号
// $arr['partner_trade_no'] = $order_trade_no;
$arr['openid'] = $openid;
$arr['check_name'] = 'NO_CHECK';//是否验证用户真实姓名,这里不验证
$arr['amount'] = $money*100;//付款金额,
$arr['desc'] = $desc;//描述信息
$arr['spbill_create_ip'] = $_SERVER['SERVER_ADDR'];//获取服务器的ip
$sign = $this->MakeSign($arr,$key);//封装的关于签名的算法
$arr['sign'] = $sign;
$var = $this->array_to_xml($arr);
$xml = $this->curl_post_ssl1('https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers', $var, 30, array(), 1);
$rdata = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$return_code = (string)$rdata->return_code;
$result_code = (string)$rdata->result_code;
$return_code = trim(strtoupper($return_code));
$result_code = trim(strtoupper($result_code));
$err_code = (string)$rdata->err_code;
$err_code = trim(strtoupper($err_code));
if ($return_code == 'SUCCESS' && $result_code == 'SUCCESS') {
$cert = array(
'con'=>'ok',
'error' => 0,
'return_code'=>$rdata
);
} else {
$returnmsg = (string)$rdata->return_msg;
$cert = array(
'error' => 1,
'errmsg' => $err_code,
'return_code'=>$rdata
);
}
return json_encode($cert);
}
//上个方法中用到的curl_post_ssl()
function curl_post_ssl1($url, $vars, $second = 30, $aHeader = array())
{
//证书文件夹 的地址
$cert = ' ';
$ch = curl_init();//初始化curl
curl_setopt($ch, CURLOPT_TIMEOUT, $second);//设置执行最长秒数
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 终止从服务端进行验证
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//
curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');//证书类型
curl_setopt($ch, CURLOPT_SSLCERT, $cert . 'apiclient_cert.pem');//证书位置
curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');//CURLOPT_SSLKEY中规定的私钥的加密类型
curl_setopt($ch, CURLOPT_SSLKEY, $cert . 'apiclient_key.pem');//证书位置
if (count($aHeader) >= 1) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);//设置头部
}
curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);//全部数据使用HTTP协议中的"POST"操作来发送
$data = curl_exec($ch);//执行回话
if ($data) {
curl_close($ch);
return $data;
} else {
$error = curl_errno($ch);
echo "call faild, errorCode:$error\n";
curl_close($ch);
return false;
}
}
//遍历数组方法
function arraytoxml($data){
$str='<xml>';
foreach($data as $k=>$v) {
$str.='<'.$k.'>'.$v.'</'.$k.'>';
}
$str.='</xml>';
return $str;
}
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;
}
public function MakeSign( $params,$KEY){
//签名步骤一:按字典序排序数组参数
ksort($params);
$string = $this->ToUrlParams($params); //参数进行拼接key=value&k=v
//签名步骤二:在string后加入KEY
$string = $string . "&key=".$KEY;
Log::write("String:".$string);
//签名步骤三:MD5加密
$string = md5($string);
Log::write("StringMD5加密:".$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 array_to_xml($arr){
$xml = "<xml>";
foreach ($arr as $key => $val) {
if (is_numeric($val)) {
$xml .= "<" .$key.">".$val."</".$key.">";
} else
$xml .= "<".$key."><![CDATA[".$val."]]></".$key.">";
}
$xml .= "</xml>";
return $xml;
}
// 返回code信息
public function return_data($data,$msg = '',$status = 0)
{
$return_data = [
'Msg' => "{$msg}",
'status' => "{$status}",
'detail'=> $data
];
return $return_data;
}
// 随机字符串
function rand_str( $length = 32 ) {
// 密码字符集,可任意添加你需要的字符
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$password = "";
for ( $i = 0; $i < $length; $i++ )
{
// 这里提供两种字符获取方式
// 第一种是使用 substr 截取$chars中的任意一位字符;
// 第二种是取字符数组 $chars 的任意元素
// $password .= substr($chars, mt_rand(0, strlen($chars) – 1), 1);
$password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
}
return $password;
}
}
/** 提现
* @paramstring $money 单位分(接口中已 *100)
* @paramstring $appid appid
* @paramstring $secret app_secret
* @paramstring $mch_id 商户号
* @paramstring $key 商户api秘钥
* @param $openid 用户openid
* @paramstring $order_trade_no 订单号
* @param $desc 备注信息
* @returnstring|\think\response\Json
*/
public function wxTiXian($money='', $appid='wxf9c21dcb810f315c', $secret='f1d24926d920e5a1945a32e25a71e54f', $mch_id='1510782461', $key='tianshengtech12345quanmianshidai', $openid, $order_trade_no='123', $desc){ //单位为分,最低为100
if(empty($money) || empty($appid) || empty($secret) || empty($mch_id) || empty($key) || empty($openid) || empty($order_trade_no) || empty($desc)){
return json($this->return_data('','提现失败',1) );
}
$arr = array();
$arr['mch_appid'] = $appid;
$arr['mchid'] = $mch_id;
$arr['nonce_str'] = $this->rand_str(32);//随机字符串,不长于32位
$arr['partner_trade_no'] = '151' . date("Ymd") . rand(10000, 90000) . rand(10000, 90000);//商户订单号
// $arr['partner_trade_no'] = $order_trade_no;
$arr['openid'] = $openid;
$arr['check_name'] = 'NO_CHECK';//是否验证用户真实姓名,这里不验证
$arr['amount'] = $money*100;//付款金额,
$arr['desc'] = $desc;//描述信息
$arr['spbill_create_ip'] = $_SERVER['SERVER_ADDR'];//获取服务器的ip
$sign = $this->MakeSign($arr,$key);//封装的关于签名的算法
$arr['sign'] = $sign;
$var = $this->array_to_xml($arr);
$xml = $this->curl_post_ssl1('https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers', $var, 30, array(), 1);
$rdata = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$return_code = (string)$rdata->return_code;
$result_code = (string)$rdata->result_code;
$return_code = trim(strtoupper($return_code));
$result_code = trim(strtoupper($result_code));
$err_code = (string)$rdata->err_code;
$err_code = trim(strtoupper($err_code));
if ($return_code == 'SUCCESS' && $result_code == 'SUCCESS') {
$cert = array(
'con'=>'ok',
'error' => 0,
'return_code'=>$rdata
);
} else {
$returnmsg = (string)$rdata->return_msg;
$cert = array(
'error' => 1,
'errmsg' => $err_code,
'return_code'=>$rdata
);
}
return json_encode($cert);
}
//上个方法中用到的curl_post_ssl()
function curl_post_ssl1($url, $vars, $second = 30, $aHeader = array())
{
// $cert = APP_PATH.$this->appwxcert;
$cert = APP_PATH.'wxqmns123/cert/';
// $isdir = APP_PATH.'wxqmns123/cert/';//证书位置
$ch = curl_init();//初始化curl
curl_setopt($ch, CURLOPT_TIMEOUT, $second);//设置执行最长秒数
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_URL, $url);//抓取指定网页
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 终止从服务端进行验证
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//
curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');//证书类型
curl_setopt($ch, CURLOPT_SSLCERT, $cert . 'apiclient_cert.pem');//证书位置
curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');//CURLOPT_SSLKEY中规定的私钥的加密类型
curl_setopt($ch, CURLOPT_SSLKEY, $cert . 'apiclient_key.pem');//证书位置
if (count($aHeader) >= 1) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);//设置头部
}
curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);//全部数据使用HTTP协议中的"POST"操作来发送
$data = curl_exec($ch);//执行回话
if ($data) {
curl_close($ch);
return $data;
} else {
$error = curl_errno($ch);
echo "call faild, errorCode:$error\n";
curl_close($ch);
return false;
}
}
//遍历数组方法
function arraytoxml($data){
$str='<xml>';
foreach($data as $k=>$v) {
$str.='<'.$k.'>'.$v.'</'.$k.'>';
}
$str.='</xml>';
return $str;
}
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;
}
public function MakeSign( $params,$KEY){
//签名步骤一:按字典序排序数组参数
ksort($params);
$string = $this->ToUrlParams($params); //参数进行拼接key=value&k=v
//签名步骤二:在string后加入KEY
$string = $string . "&key=".$KEY;
Log::write("String:".$string);
//签名步骤三:MD5加密
$string = md5($string);
Log::write("StringMD5加密:".$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 array_to_xml($arr){
$xml = "<xml>";
foreach ($arr as $key => $val) {
if (is_numeric($val)) {
$xml .= "<" .$key.">".$val."</".$key.">";
} else
$xml .= "<".$key."><![CDATA[".$val."]]></".$key.">";
}
$xml .= "</xml>";
return $xml;
}
// 返回code信息
public function return_data($data,$msg = '',$status = 0)
{
$return_data = [
'Msg' => "{$msg}",
'status' => "{$status}",
'detail'=> $data
];
return $return_data;
}
// 随机字符串
function rand_str( $length = 32 ) {
// 密码字符集,可任意添加你需要的字符
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$password = "";
for ( $i = 0; $i < $length; $i++ )
{
// 这里提供两种字符获取方式
// 第一种是使用 substr 截取$chars中的任意一位字符;
// 第二种是取字符数组 $chars 的任意元素
// $password .= substr($chars, mt_rand(0, strlen($chars) – 1), 1);
$password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
}
return $password;
}