PHP实现微信小程序在线支付功能(代码实例)(2)
小程序页面请求处理:
wx.request({
url: 'https://yourhost.com/wxpay/payfee.php',//改成你自己的链接
data:{
id: app.globalData.openid,//获取用户openid
fee:100 //商品价格
},
header: {
'Content-Type': 'application/x-www-form-urlencoded'
},
method: 'POST',
success: function (res) {
console.log(res.data);
console.log('调起支付');
wx.requestPayment({
'timeStamp': res.data.timeStamp,
'nonceStr': res.data.nonceStr,
'package': res.data.package,
'signType': 'MD5',
'paySign': res.data.paySign,
'success': function (res) {
console.log('success');
wx.showToast({
title: '支付成功',
icon: 'success',
duration: 3000
});
},
'fail': function (res) {
console.log(res);
},
'complete': function (res) {
console.log('complete');
}
});
},
fail: function (res) {
console.log(res.data)
}
});
回调URL:notify.php
$postXml = $GLOBALS["HTTP_RAW_POST_DATA"]; //接收微信参数
// 接受不到参数可以使用file_get_contents("php://input"); PHP高版本中$GLOBALS好像已经被废弃了
if (empty($postXml)) {
return false;
}
//将xml格式转换成数组
function xmlToArray($xml) {
//禁止引用外部xml实体
libxml_disable_entity_loader(true);
$xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$val = json_decode(json_encode($xmlstring), true);
return $val;
}
$attr = xmlToArray($postXml);
$total_fee = $attr['total_fee'];
$open_id = $attr['openid'];
$out_trade_no = $attr['out_trade_no'];
$time = $attr['time_end'];
So:在微信的异步通知后,也需要给微信服务器,返回一个信息,只不过微信的所有数据格式都是xml的,所以我们在返回一个数据给微信即可。
echo exit('<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>');
总结
到此这篇关于PHP微信小程序在线支付功能的文章就介绍到这了,更多相关php 微信小程序在线支付内容请搜索黑区网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持黑区网络!
