
小程序支付 和 回调处理
��������������� ���������������
发布日期:2021-05-13 20:53:21
浏览次数:18
分类:精选文章
本文共 7818 字,大约阅读时间需要 26 分钟。
������������������������������������������������
���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
���������������������������������
���������
���������������������������������������������������������������������������������������������������������������ID���prepay_id���������������������������������������������������������������������������������������������������������������������������������- ���������������������������������������
- ������ID���������������������������������������
- ������������������������������
trade_type
���JSAPI
���������������������������������������������������������
���������������������
������������������������������������prepay_id
������������������������������������������������������������������������������������������������������������������������������ appId
������������������ID���package
������������ID���timeStamp
���������������������nonceStr
������������������������signType
������������������paySign
������������������
������������������������
������������������������������wx.requestPayment
��������������������������������������������������������������������������������������������� timeStamp
������������������������������nonceStr
������������������������package
������������������ID���������������signType
���������������������������MD5������paySign
���������������
������������
������������������������������URL���������notify_url
��������������������������������������������������������������������������������������������������������� - ������������������������������
- ������������������������������������������������
- ���������������������������������������
������������������������
���������������������������������������������������������������������
static $wxpayconf = [ 'wx_smallprogram_appid' => 'wx13212312313123', // ���������������ID 'mch_id' => '12312345454', // ��������������������������� 'key' => 'Gjanuarygaoqingsong', // ������ 'appsecret' => '1231231212daseqe123213', // ��������������������������� 'notify_url' => 'http://www.gaoqingsong.com/index.php?' // ������������������];
���������������������������������
1. ���������������������
public function unifiedorder($paydata, $orderSn) { // ������������������������������ $wxpayconf = self::$wxpayconf; // ������������IP������ $userIP = $_SERVER['REMOTE_ADDR']; $sign = [ 'appid' => $wxpayconf['wx_smallprogram_appid'], 'mch_id' => $wxpayconf['mch_id'], 'nonce_str' => $this->nonceStr(), 'body' => 'Gjanury������������', 'out_trade_no' => $orderSn, 'total_fee' => 1, 'spbill_create_ip' => $userIP, 'trade_type' => 'JSAPI', 'openid' => $paydata['openid'], 'notify_url' => $wxpayconf['notify_url'] ]; // ������������������ $sign['sign'] = $this->getSign($sign, $wxpayconf['key']); // ������������������������ $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; $data = $this->arrayToXml($sign); $res = $this->postXmlCurl($data, $url); if ($res['errNum'] != 0) { echo json_encode($res); die; } else { $res = $res['info']; if ($res['return_code'] == 'SUCCESS' && $res['result_code'] == 'SUCCESS') { $prepay_id = $res['prepay_id']; // ������������������������������ $yuzhifudata = [ 'appId' => $wxpayconf['wx_smallprogram_appid'], 'package' => 'prepay_id='.$prepay_id, 'timeStamp' => (string)time(), 'nonceStr' => $this->nonceStr(), 'signType' => 'MD5', 'paySign' => $this->getSign($yuzhifudata, $wxpayconf['key']) ]; exit(json_encode($yuzhifudata)); } else { // ������������������������ if ($res['return_code'] == 'FAIL') { $info['return_msg'] = $res['return_msg']; } if ($res['result_code'] == 'FAIL') { $info['err_code'] = $res['err_code']; $info['err_code_des'] = $res['err_code_des']; } exit($info); } }}
2. ������������������
private function getSign($params, $key) { ksort($params); $singstring = ''; foreach ($params as $key => $value) { $singstring .= '&'.$key . '=' . $value; } $string = $singstring . "&key=" . $key; $string = ltrim($string, '&'); $string = md5($string); $result = strtoupper($string); return $result;}
3. XML������������
public function arrayToXml($arr, $is_array = false) { if (!$is_array) { $xml = ''; } foreach ($arr as $key => $val) { if (is_array($val)) { $xml .= "<".$key.">".($this->arrayToXml($val, true))+"<"; } else { $xml .= "<".$key.">".$val."<"; } } if (!$is_array) { $xml .= " "; } return $xml;}
������������������������������
wx.request({ url: 'http://www.gaoqingsong.com/index.php?m=xxxx&a=payment', data: { tablenumber: tablenumber, orderdetail: JSON.stringify(that.data.goods), amount: that.data.totalprice, actualpayment: that.data.totalprice, openid: useropenid, wxheadimg: wxinfo.avatarUrl, wxname: wxinfo.nickName }, method: "POST", header: { "content-type": "application/x-www-form-urlencoded" }, success: function (res) { wx.navigateTo({ url: '../paysuccess/paysuccess' }); var timeStamp = res.data.timeStamp; wx.requestPayment({ timeStamp: timeStamp, nonceStr: res.data.nonceStr, package: res.data.package, signType: res.data.signType, paySign: res.data.paySign, success: function (res) { if (res.errMsg == 'requestPayment:ok') { wx.navigateTo({ url: '../paysuccess/paysuccess' }); } }, fail: function (res) { console.log(res); }, complete: function (res) { } }); }});
������������������������
$receipt = $_REQUEST;if ($receipt == null) { $receipt = file_get_contents("php://input");}if ($receipt == null) { $receipt = $GLOBALS['HTTP_RAW_POST_DATA'];}$post_data = $this->xml_to_array($receipt);$postSign = $post_data['sign'];unset($post_data['sign']);ksort($post_data);$str = $this->ToUrlParams($post_data);$user_sign = strtoupper(md5($post_data));$ordernumber = $post_data['out_trade_no'];if ($post_data['return_code'] == 'SUCCESS' && $postSign) { // ������������������ $result = M('userorder')->where('ordernumber = "'.$ordernumber.'"')->select(); if ($result) { if ($result[0]['paystatus'] == 0) { // ������������������ $obj = array( "paystatus" => 1, ); $res = M('userorder')->where('ordernumber = "'.$ordernumber.'"')->save($obj); file_put_contents('gg.txt', $res); if ($res) { $this->return_success(); } } else { $this->return_success(); } } else { echo '������������������,���������������������������'; }} else { // ������������������������ file_put_contents('wxpayerrorlog.txt', $post_data['return_code'].PHP_EOL, FILE_APPEND); echo '������������������';}
���������������������
���������������������������������������������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2025年04月06日 01时06分44秒
关于作者

喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Allegro中如何消除器件本身Pin间距报错
2021-05-14
linux--练习001-基础类型
2021-05-14
Flask--简介
2021-05-14
16 python基础-恺撒密码
2021-05-14
06.1 python基础--结构控制
2021-05-14
Frame--Api框架
2021-05-14
idea 在Debug 模式中运行语句中函数的方法
2021-05-14
Boostrap技能点整理之【网格系统】
2021-05-14
新闻发布项目——业务逻辑层(UserService)
2021-05-14
hibernate正向生成数据库表以及配置——hibernate.cfg.xml
2021-05-14
javaWeb服务详解(含源代码,测试通过,注释) ——Emp的Dao层
2021-05-14
java实现人脸识别源码【含测试效果图】——Dao层(IUserDao)
2021-05-14
使用ueditor实现多图片上传案例——前台数据层(Index.jsp)
2021-05-14
ssm(Spring+Spring mvc+mybatis)——saveDept.jsp
2021-05-14
解决Chrome播放视频闪屏黑屏无法播放
2021-05-14