php教程自学网 - 轻松零基础学php视频教程
条幅广告
当前位置: 主页 > PHP个人博客专栏 >

高蒙博客:PHP服务端微信扫码支付

时间:2019-06-26 20:23来源:未知 作者:admin 点击:
偶尔会遇到使用微信扫一扫二维码的支付功能,下面记录下基本的操作方法。 // 微信扫码支付生成二维码 public function wxcode() { $d[appid] = $this-wxappid; $d[mch_id] = $this-wxmch_id; $d[time_stamp] =
偶尔会遇到使用微信扫一扫二维码的支付功能,下面记录下基本的操作方法。
 
// 微信扫码支付生成二维码
public function wxcode()
{
$d['appid'] = $this->wxappid;
$d['mch_id'] = $this->wxmch_id;
$d['time_stamp'] = time();
$d['nonce_str'] = \Org\Util\String::randString(20);
$d['product_id'] = 'wxpay001';
$d['sign'] = $this->_getwxsign($d);
$url = 'weixin://wxpay/bizpayurl?' . http_build_query($d);
echo $url;
# ....生成二维码自己完成喽
}
 
// 微信扫码支付回调参数
public function wxcodereturn()
{
ob_clean();
$body = @file_get_contents('php://input');
if (!$body) {
die();
}
$body = $this->_xmldataparse($body);
ksort($body);
$user_sign = $this->_params_tourl($body);
if ($user_sign != $body['sign']) {
echo "<xml><return_code><![CDATA[FAIL]]></return_code></xml>";
die();
}
$d['appid'] = $body['appid'];
$d['mch_id'] = $body['mch_id'];
$d['nonce_str'] = \Org\Util\String::randString(20);
$d['body'] = '标题';
$d['product_id'] = $body['product_id'];
$d['out_trade_no'] = date('YmdHis') . rand(10000, 99999);
$d['total_fee'] = 100;
$d['spbill_create_ip'] = get_client_ip();
$d['notify_url'] = 'wxcodepay.php';
$d['trade_type'] = 'NATIVE';
// 获取签名
$d['sign'] = $this->_getwxsign($d);
// 拼装数据
$xml = $this->_toxml($d);
// 发送请求
$res = $this->_sendprePaycurl($xml);
if ($res['return_code'] == 'SUCCESS') {
if ($rerurn_code['code'] == 200) {
$t['return_code'] = 'SUCCESS';
$t['appid'] = $body['appid'];
$t['mch_id'] = $body['mch_id'];
$t['nonce_str'] = \Org\Util\String::randString(20);
$t['prepay_id'] = $res['prepay_id'];
$t['result_code'] = 'SUCCESS';
$t['sign'] = $this->_getwxsign($t);
echo $this->_toxml($t);
}
}
exit();
}
 
 
// 微信扫码支付成功的回调
public function wxcodepay()
{
ob_clean();
$body = @file_get_contents('php://input');
if (!$body) {
die();
}
$body = $this->_xmldataparse($body);
ksort($body);
$user_sign = $this->_params_tourl($body);
if ($user_sign != $body['sign']) {
die();
}
// 回调处理
......
$t['return_code'] = 'SUCCESS';
$t['return_msg'] = 'OK';
echo $this->_toxml($t);
}
 
// 生成xml数据格式
private function _toxml($values)
{
$xml = "<xml>";
foreach ($values as $key => $val) {
if (is_numeric($val)) {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
} else {
$xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
}
}
$xml .= "</xml>";
return $xml;
}
 
//xml格式数据解析函数
private function _xmldataparse($data)
{
$msg = array();
$msg = (array)simplexml_load_string($data, 'SimpleXMLElement', LIBXML_NOCDATA);
return $msg;
}
 
// 格式化参数格式化成url参数
private function _params_tourl($arr)
{
$buff = "";
foreach ($arr as $k => $v) {
if ($k != "sign" && $v != "" && !is_array($v)) {
$buff .= $k . "=" . $v . "&";
}
}
$buff = trim($buff, "&");
$buff .= '&key=' . $this->wxkey;
$sign = strtoupper(md5($buff));
return $sign;
}
 
// 生成微信签名
private function _getwxsign($data)
{
ksort($data);
$str = '';
foreach ($data as $key => $value) {
$str .= !$str ? $key . '=' . $value : '&' . $key . '=' . $value;
}
$str .= '&key=' . $this->wxkey;
$sign = strtoupper(md5($str));
return $sign;
}
 
(责任编辑:admin)
顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
相关推荐