程式碼備份錄——微信企業付款
程式碼
$partner_trade_no = strtoupper(Date("YmdHis").str_random(14).rand(1111,9999)); // 商戶訂單號
$nonce_str = substr(md5(time().rand(1,9999)),1,32);
$openid = "";//
$amount = 100; // 單位為分,付款金額
$url = "https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
$data = [
"mch_appid"=>___wx_appid,
"mchid"=>___wx_mch_id,
"nonce_str"=>$nonce_str,
"partner_trade_no"=>$partner_trade_no,
"openid"=>$openid,
"check_name"=>"NO_CHECK",
"amount"=>$amount,
"desc"=>"付款原因",
"spbill_create_ip"=>$request->getClientIp(),
];
ksort($data);
$sign = strtoupper(md5(ToUrlParams($data)."&key=".___wx_pay_key));
$data = array_merge($data,array("sign"=>$sign));
$data = ToXml($data);
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,"{$url}");
curl_setopt($curl,CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl,CURLOPT_POST,true);
curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl,CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($curl,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($curl,CURLOPT_SSLCERT,app_path('Cert/apiclient_cert.pem'));
curl_setopt($curl,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt($curl,CURLOPT_SSLKEY,app_path('Cert/apiclient_key.pem'));
$string = curl_exec($curl);
curl_close($curl);
$strpostData = simplexml_load_string($string);
$stringData = [];
foreach ($strpostData->children() as $child){
$stringData = array_merge($stringData,array($child->getName()=>(string)$child));
}
Log::info(["付款資料",$stringData]);
if($stringData["return_code"] == "SUCCESS" && $stringData["result_code"] == "SUCCESS") {
// 付款成功...
}
return;
方法內的引數,請參考另一篇文章 微信商戶 API 發紅包!
本作品採用《CC 協議》,轉載必須註明作者和本文連結