本文實(shí)例為大家分享了php微信企業(yè)付款到個(gè)人零錢(qián)的具體代碼,供大家參考,具體內(nèi)容如下
1.基本配置
//公眾賬號(hào)appid
$data["mch_appid"] = 'appid';
//商戶(hù)號(hào)
$data["mchid"] = '';
//隨機(jī)字符串
$data["nonce_str"] = 'suiji'.mt_rand(100,999);
//商戶(hù)訂單號(hào)
$data["partner_trade_no"]=date('YmdHis').mt_rand(1000,9999);
//金額 用戶(hù)輸入的提現(xiàn)金額需要乘以100
$data["amount"] = $money;
//企業(yè)付款描述
$data["desc"] = '企業(yè)付款到個(gè)人零錢(qián)';
//用戶(hù)openid
$data["openid"] = $openid;
//不檢驗(yàn)用戶(hù)姓名
$data["check_name"] = 'NO_CHECK';
//獲取IP
$data['spbill_create_ip']=$_SERVER['SERVER_ADDR'];
//商戶(hù)密鑰
$data['key']='';
//商戶(hù)證書(shū) 商戶(hù)平臺(tái)的API安全證書(shū)下載
$data['apiclient_cert.pem']
$data['apiclient_key.pem']
2.PHP代碼
/**
**開(kāi)始支付
/
public function userpay(){
$money = ‘用戶(hù)輸入提現(xiàn)金額';
$info['money'] = ‘用戶(hù)余額';
if ($this->openid $money){
if ($money>$info['money'] ){
echo json_encode([
'status' => 1,
'message' => '余額不足,不能提現(xiàn)!',
'code'=>'余額不足,不能提現(xiàn)!'
]);
}elseif ($money1){
echo json_encode([
'status' => 2,
'message' => '提現(xiàn)金額不能小于1元',
'code'=>'提現(xiàn)金額太低'
]);
}else{
$openid = $this->openid;
$trade_no = date('YmdHis').mt_rand(1000,9999);
$res = $this->pay($openid,$trade_no,$money*100,'微信提現(xiàn)');
//結(jié)果打印
if($res['result_code']=="SUCCESS"){
echo json_encode([
'status' => 3,
'message' => '提現(xiàn)成功!',
]);
}elseif ($res['err_code']=="SENDNUM_LIMIT"){
echo json_encode([
'status' => 4,
'message' => '提現(xiàn)失??!',
'code'=>'每日僅能提現(xiàn)一次',
]);
}else{
echo json_encode([
'status' => 5,
'message' => '提現(xiàn)失?。?,
'code'=>$res['err_code'],
]);
}
}
}else{
echo json_encode([
'status' => 5,
'message' => '未檢測(cè)到您當(dāng)前微信賬號(hào)~',
]);
}
}
/**
*支付方法
/
public function pay($openid,$trade_no,$money,$desc){
$params["mch_appid"]='';
$params["mchid"] = '';
$params["nonce_str"]= 'suiji'.mt_rand(100,999);
$params["partner_trade_no"] = $trade_no;
$params["amount"]= $money;
$params["desc"]= $desc;
$params["openid"]= $openid;
$params["check_name"]= 'NO_CHECK';
$params['spbill_create_ip'] = $_SERVER['SERVER_ADDR'];
//生成簽名
$str = 'amount='.$params["amount"].'check_name='.$params["check_name"].'desc='.$params["desc"].'mch_appid='.$params["mch_appid"].'mchid='.$params["mchid"].'nonce_str='.$params["nonce_str"].'openid='.$params["openid"].'partner_trade_no='.$params["partner_trade_no"].'spbill_create_ip='.$params['spbill_create_ip'].'key=商戶(hù)密鑰';
//md5加密 轉(zhuǎn)換成大寫(xiě)
$sign = strtoupper(md5($str));
//生成簽名
$params['sign'] = $sign;
//構(gòu)造XML數(shù)據(jù)
$xmldata = $this->array_to_xml($params); //數(shù)組轉(zhuǎn)XML
$url='https://api.mch.weixin.qq.com/mmpaymkttransfers/prom otion/transfers';
//發(fā)送post請(qǐng)求
$res = $this->curl_post_ssl($url, $xmldata); //curl請(qǐng)求
if(!$res){
return array('status'=>1,
'msg'=>"服務(wù)器連接失敗" );
}
//付款結(jié)果分析
$content = $this->xml_to_array($res); //xml轉(zhuǎn)數(shù)組
return $content;
}
/**
* curl請(qǐng)求
/
public function curl_post_ssl($url, $xmldata, $second=30,$aHeader=array()){
$ch = curl_init();
//超時(shí)時(shí)間
curl_setopt($ch,CURLOPT_TIMEOUT,$second);
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
//這里設(shè)置代理,如果有的話(huà)
//curl_setopt($ch,CURLOPT_PROXY, '10.206.30.98');
//curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
//默認(rèn)格式為PEM,可以注釋
curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
//絕對(duì)地址可使用 dirname(__DIR__)打印,如果不是絕對(duì)地址會(huì)報(bào) 58 錯(cuò)誤
curl_setopt($ch,CURLOPT_SSLCERT,' 絕對(duì)地址/apiclient_cert.pem');
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLKEY,'絕對(duì)地址/apiclient_key.pem');
if( count($aHeader) >= 1 ){
curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
}
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$xmldata);
$data = curl_exec($ch);
if($data){
curl_close($ch);
return $data;
}
else {
$error = curl_errno($ch);
echo "call faild, errorCode:$error\n";
die();
curl_close($ch);
return false;
}
}
/**
* array 轉(zhuǎn) xml
* 用于生成簽名
*/
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;
}
/**
* xml 轉(zhuǎn)化為array
*/
public function xml_to_array($xml){
//禁止引用外部xml實(shí)體
libxml_disable_entity_loader(true);
$values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $values;
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:- PHP實(shí)現(xiàn)微信支付(jsapi支付)和退款(無(wú)需集成支付SDK)流程教程詳解
- PHP開(kāi)發(fā)實(shí)現(xiàn)微信退款功能示例
- PHP實(shí)現(xiàn)微信申請(qǐng)退款流程實(shí)例代碼
- php實(shí)現(xiàn)微信支付之退款功能
- PHP實(shí)現(xiàn)微信退款功能
- PHP實(shí)現(xiàn)微信申請(qǐng)退款功能
- PHP實(shí)現(xiàn)QQ、微信和支付寶三合一收款碼實(shí)例代碼
- PHP編程實(shí)現(xiàn)微信企業(yè)向用戶(hù)付款的方法示例
- php實(shí)現(xiàn)微信支付之企業(yè)付款
- php微信公眾號(hào)開(kāi)發(fā)之微信企業(yè)付款給個(gè)人
- PHP實(shí)現(xiàn)微信退款的方法示例