每次支付都要被迫下载一堆东西;而且下载完也不一定能用;所以照着官方文档写个简易的;
public static function pay($openId,$outNo,$fee){
$wx = config('wechat');
$key = $wx["key"];
$fee = floor($fee*100);
$nonceStr = md5(microtime().mt_rand(1,1000000000000));
$sign = strtoupper(md5("appid=$wx[appid]&attach=支付测试&body=JSAPI支付测试&mch_id=$wx[mch_id]&nonce_str=$nonceStr¬ify_url=$wx[notify_url]&openid=$openId&out_trade_no=$outNo&spbill_create_ip=14.23.150.211&total_fee=$fee&trade_type=JSAPI&key=$key"));
$xml = "
<xml>
<appid>$wx[appid]</appid>
<attach>支付测试</attach>
<body>JSAPI支付测试</body>
<mch_id>$wx[mch_id]</mch_id>
<nonce_str>$nonceStr</nonce_str>
<notify_url>$wx[notify_url]</notify_url>
<openid>$openId</openid>
<out_trade_no>$outNo</out_trade_no>
<spbill_create_ip>14.23.150.211</spbill_create_ip>
<total_fee>$fee</total_fee>
<trade_type>JSAPI</trade_type>
<sign>$sign</sign>
</xml>
";
$body = Http::post("https://api.mch.weixin.qq.com/pay/unifiedorder",$xml);
$obj = simplexml_load_string($body,'SimpleXMLElement',LIBXML_NOCDATA);
$tmp = get_object_vars($obj);
$arr = [];
if($tmp['return_code']=="SUCCESS"&&$tmp['result_code']=="SUCCESS"){
$arr["provider"]="wxpay";
$arr["timeStamp"]=(string)time();
$arr["nonceStr"]=$nonceStr;
$arr["package"]="prepay_id=$tmp[prepay_id]";
$arr["signType"]="MD5";
$paySign = strtoupper(md5("&appid=$wx[appid]&nonceStr=$nonceStr&package=$arr[package]&signType=$arr[signType]&key=$key"));
$arr["paySign"]=$paySign;
}
return $arr;
}
返回结果
{
"code": 200,
"data": {
"provider": "wxpay",
"timeStamp": "1640228441",
"nonceStr": "8edc6d6534f71676e277d961dbd826bc",
"package": "prepay_id=wx231100416525631467baf7da5304ea0000",
"signType": "MD5",
"paySign": "2C00B6C377RD2115DF5E69B23FF20610"
}
}
|