直接上代码把没有用官方DEMO
我们直接一个方法来支付:
里面会遇到几个问题
1:金钱记得是数字类型
2:最后返回跳转链接的时候:记得尾巴上加&redirect_url=地址 不然不会吊起
3:对于百度APP这样浏览器,支付成功后是不会回调的。值能获取到ID值,用这个来判断是否支付。
4:其他商户号APPid等信息自己去微信获取即可!
$key = ;//appkey
$appid = ;//appid
$mch_id = ;//商户号
$total_amount = 1; //总金额
$subject = '商品描述'; //商品描述
$additional = '附加数据'; //附加数据
$order_id = ; //订单号
$nonce_str =MD5($order_id);//随机字符串
$spbill_create_ip =$this->getClientIp(); //终端ip //以上参数接收不必纠结,按照正常接收就行,相信大家都看得懂
$trade_type = 'MWEB';//交易类型 具体看API 里面有详细介绍
$notify_url = '回调地址'; //回调地址
$scene_info ='{"h5_info":{"type":"Wap","wap_url":"https://mtxqg.mtxqg.com/","wap_name":"商品购买"}}'; //场景信息
//对参数按照key=value的格式,并按照参数名ASCII字典序排序生成字符串
$signA = "appid=$appid&body=$subject&mch_id=$mch_id&nonce_str=$nonce_str¬ify_url=$notify_url&out_trade_no=$order_id&scene_info=$scene_info&spbill_create_ip=$spbill_create_ip&total_fee=$total_amount&trade_type=$trade_type";
//dump($signA);
$strSignTmp = $signA."&key=$key"; //拼接字符串
$sign = strtoupper(MD5($strSignTmp)); // MD5 后转换成大写
$post_data = "<xml>
<appid>$appid</appid>
<body>$subject</body>
<mch_id>$mch_id</mch_id>
<nonce_str>$nonce_str</nonce_str>
<notify_url>$notify_url</notify_url>
<out_trade_no>$order_id</out_trade_no>
<scene_info>$scene_info</scene_info>
<spbill_create_ip>$spbill_create_ip</spbill_create_ip>
<total_fee>$total_amount</total_fee>
<trade_type>$trade_type</trade_type>
<sign>$sign</sign>
</xml>";
$url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
$dataxml = $this->http_post($url,$post_data);
$objectxml = (array)simplexml_load_string($dataxml, 'SimpleXMLElement', LIBXML_NOCDATA);
if($objectxml['return_code'] == 'SUCCESS') {
if($objectxml['result_code'] == 'SUCCESS'){
Log::write($objectxml);
}
}