您好,登錄后才能下訂單哦!
<?php
namespace app\common\library;
class Cash
{
public function wxcash($openId,$money)
{
$pub = config('keys.payConfig');
$appid = $pub['app_id'];//商戶賬號appid
$mch_id = $pub['mch_id'];//商戶號
$key = $pub['key'];
$openid = $openId;//授權用戶openid
$arr = array();
$arr['mch_appid'] = $appid;
$arr['mchid'] = $mch_id;
$arr['nonce_str'] = md5(uniqid(microtime(true),true));//隨機字符串,不長于32位
$arr['partner_trade_no'] = '123456789' . date("Ymd") . rand(10000, 90000) . rand(10000, 90000);//商戶訂單號
$arr['openid'] = $openid;
$arr['check_name'] = 'NO_CHECK';//是否驗證用戶真實姓名,這里不驗證
$arr['amount'] = $money * 100;//付款金額,單位為分
$arr['desc'] = "零錢提現";//描述信息
$arr['spbill_create_ip'] = 'xx.xx.xx.xx';//獲取服務器的ip
//封裝的關于簽名的算法
$arr['sign'] = $this->makeSign($arr,$key);//簽名
$var = $this->arrayToXml($arr);
// dump($arr['sign'] );exit;
$xml = $this->curl_post_ssl('https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers',$var,30);
libxml_disable_entity_loader(true);
$rdata = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)),true);
eblog('cash_xmldata',$rdata);
// dump($rdata);exit;
$return_code = trim(strtoupper($rdata['return_code']));
$result_code = trim(strtoupper($rdata['result_code']));
if ($return_code == 'SUCCESS' && $result_code == 'SUCCESS') {
$isrr = array(
'status'=>1,
'msg' => '',
);
} else {
// $returnmsg = $rdata['return_msg'];
$err_code_des = $rdata['err_code_des'];
$isrr = array(
'status' => 0,
'msg' => $err_code_des,
);
}
return $isrr;
}
protected function makesign($data,$key)
{
//獲取微信支付秘鑰
$data = array_filter($data);
//簽名步驟一:按字典序排序參數
ksort($data);
$string_a = http_build_query($data);
$string_a = urldecode($string_a);
//簽名步驟二:在string后加入KEY
//$config=$this->config;
$string_sign_temp = $string_a."&key=".$key;
//簽名步驟三:MD5加密
$sign = md5($string_sign_temp);
// 簽名步驟四:所有字符轉為大寫
$result = strtoupper($sign);
// $result = strtoupper(hash_hmac("sha256",$string_sign_temp,$key));
return $result;
}
protected function arraytoxml($data){
$str='<xml>';
foreach($data as $k=>$v) {
$str.='<'.$k.'>'.$v.'</'.$k.'>';
}
$str.='</xml>';
return $str;
}
protected function curl_post_ssl($url, $vars, $second = 30, $aHeader = array())
{
$isdir = APP_PATH."/common/library/php_sdk/lib/";//證書位置
$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, $isdir . 'apiclient_cert.pem');//證書位置
curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');//CURLOPT_SSLKEY中規定的私鑰的加密類型
curl_setopt($ch, CURLOPT_SSLKEY, $isdir . 'apiclient_key.pem');//證書位置
curl_setopt($ch, CURLOPT_CAINFO, 'PEM');
curl_setopt($ch, CURLOPT_CAINFO, $isdir . 'rootca.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;
}
}
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。