首页 > 作文

微信 JSAPI 支付

更新时间:2023-04-08 11:01:52 阅读: 评论:0

一、申请微信公众号、开通微信支付,通过【appid】将两者关联,具体操作步骤参考:点击查看

二、在设置【接收微信支付异步回调通知域名】,

三、在设置【支付授权域名】及【key】,支付授权域名与接收回调通知域名最好为同域名,

四、生成支付需要的配置文件

五、首次访问网站时静默获取用户 openid

六、用户点击支付时,调用微信【】,获取 prepay_id

七、生成 jsapi 支付需要的参数

八、用户输完支付密码,前台轮询订单状态,后台在 notify_url 中处理订单

九、php demo如下:

<?phpreturn $config = array('site_url' => '/d/file/titlepic/','weixinpay_config' => array('appid'      => 'wxf96fa703d64967cc', // 公众号后台获取'appcret'  => 'e2e87179cfe614dfa0ca16146b0cdfe3', // 公众号后台获取,用于获取用户openid'mchid'      => '1582427110', // 微信支付后台获取,'pay_key'    => 'a5f5764bc7905be3075c79d1ce216014', // 微信支付后台设置,用于参数签名'notify_url' => '/d/file/titlepic/', // 异步接收微信支付结果地址,不能有任何鉴权逻辑,能在浏览器中访问'trade_type' => 'jsapi', // 支付类型),);?><?phpclass homecontroller extends controller{public function __construct(){parent::__construct();}/*** 首页**/public function panel(){# code here...}/*** 引导页**/public function index(){// 微信静默授权if (empty($_ssion['wx_openid'])) {$appid = $config['weixinpay_config']['appid'];$jump_url = $config['site_url'] . 'home/index/wxoauth2/';$oauth2_url  = '/d/file/titlepic/authorize' . $appid;$oauth2_url .= '&redirect_uri=' . urlencode($jump_url);$oauth2_url .= '&respon_type=code';$oauth2_url .= '&scope=snsapi_ba';$oauth2_url .= '&state=state#wechat_redirect';redirect($oauth2_url);} el {redirect('/home/panel/');}}/*** 不弹出询问获取用户openid**/public function wxoauth2(){$url  = '/d/file/titlepic/access_token' . $config['weixinpay_config']['appid'];$url .= '&cret=' . $config['weixinpay_config']['appcret'];$url .= '&code=' . trim($_get['code']);$url .= '&grant_type=authorization_code';$ch = curl_init();curl_topt($ch, curlopt_url, $url);curl_topt($ch, curlopt_ssl_verifypeer, fal);curl_topt($ch, curlopt_ssl_verifyhost, fal);curl_topt($ch, curlopt_returntransfer, true);$output = curl_exec($ch);if (curl_error($ch)) {redirect('/home/index');}curl_clo($ch);$result = json_decode($output, true);if (!empty($result['openid'])) {$_ssion('wx_openid', $result['openid']);redirect('/home/panel/');}redirect('/home/index');}/*** 用户发请ajax支付请求** 请自先定义 api_error(), api_success(), array2xml(), xml2array(), write_log()**/public function wxpay(){is_weixin() or exit(api_error('请在微信中打开...'));if (empty($_ssion['ur_id'])) {redirect($config['site_url']);}if (empty($_post['total_fee'])) {exit(api_error('支付金额为0'));}if (empty($_post['goods_id'])) {exit(api_error('待支付商品不存在'));}$nonce_str = md5(uniqid(null, true) . mt_rand());$out_trade_no = crc32($nonce_str);// 调用统一下单接口获取prepay_id$unifiedorder_params = array('appid'             => $config['weixinpay_config']['appid'],'mch_id'            => $config['weixinpay_config']['mchid'],'trade_type'        => $config['weixinpay_config']['trade_type'],'notify_url'        => $config['weixinpay_config']['notify_url'],'openid'            => $_ssio大会致辞n['wx_openid'],'nonce_str'         => $nonce_str,'spbill_create_ip'  => $_rver['remote_addr'],'out_trade_no'      => $out_trade_no,'body'              => '微信支付后台商家名称-商品类目名' . rand(1, 100),'total_fee'         => $_post['total_fee'] * 100,'product_id'        => $_post['goods_id'],);ksort($unifiedorder_params);$tmp_str  = http_build_query($unifiedorder_params);$tmp_str .= '&key=' . $config['weixinpay_config']['pay_key'];$sign = md5($tmp_str);$unifiedorder_params['sign'] = strtoupper($sign);$ch = curl_init();curl_topt($ch, curlopt_url, '/d/file/titlepic/unifiedorder');curl_topt($ch, curlopt_ssl_verifypeer, fal);curl_topt($ch, curlopt_ssl_verifyhost, fal);curl_topt($ch, curlopt_returntransfer, true);curl_topt($ch, curlopt_post, true);curl_topt($ch, curlopt_httpheader, array('content-type: text/xml'));curl_topt($ch, curlopt_postfields, array2xml($unifiedorder_params));$output = curl_exec($ch);if ($errmsg = curl_error($ch)) {exit(api_error($errmsg));}curl_clo($ch);$unifiedorder = xml2array($output);if (empty($unifiedorder['prepay_id'])) {exit(api_error('微信预支付订单生成失败'));}write_log($unifiedorder);// 生成jsapi参数$jsapi_params = array('appid'  舞象之年是指多少岁   => $config['weixinpay_config']['appid'],'timestamp' => time(),'noncestr'  => $nonce_str,'package'   => 'prepay_id=' . $unifiedorder['prepay_id'],'signtype'  => 'md5',);ksort($jsapi_params);$tmp_str  = http_build_query($jsapi_params);$tmp_str .= '&key=' . $config['weixinpay_config']['pay_key'];$sign = md5($tmp_str);$jsapi_params['paysign'] = strtoupper($sign);$jsapi_params['order_no'] = $out_trade_no; // 用于前台轮询订单状态write_log($jsapi_params);// 商户订单入库$order = array('pay_state'   => 0, // 0待支付 1支付成功 2支付失败'pay_price'   => $_post['total_fee'],'pay_type'    => $config['weixinpay_config']['trade_type'],'pay_order'   => $out_trade_no,'ur_id'     => $_ssion['ur_id'],'goods_id'    => $_post['goods_id'],'create_time' => time(),);if (!(m('t_order')->add($order))) {$order['errmsg'] = '商户订单入库失败';write_log($order);exit(api_error('支付失败,请重新发起支付请求'));}exit(api_success($jsapi_params));}/*** 微信支付异步通知**/public function wxpay_sync_notice(){write_log('微信异步通知--start--');// 获取微信通知$xml = file_get_contents('php://input', 'r');$notify = xml2array($xml);if (!ist($notify['return_code'], 数字成语一到十大全$notify['result_c玫瑰茄的功效与作用ode'])|| $notify['return_code'] !== 'success'|| $notify['result_code'] !== 'success') {$log = array('errmsg' => '微信未返回return_code、result_code为success','wx_sync_notice' => $notify, );write_log($log);$pay_fail = '<xml><return_code><![cdata[fail]]></return_code><return_msg><![cdata[签名失败]]></return_msg></xml>';exit($pay_fail);}if (empty($notify['sign']) || $notify['out_trade_no']) {$log = array('errmsg' => '微信未返回签名或订单号','wx_sync_notice' => $notify, );write_log($log);$pay_fail = '<xml><return_code><![cdata[fail]]></return_code><return_msg><![cdata[签名失败]]></return_msg></xml>';exit($pay_fail);}// 验证签名$wx_sign = $notify['sign'];unt($notify['sign']);ksort($notify);$tmp_str  = http_build_query($notify);$tmp_str .= '&key=' . $config['weixinpay_config']['pay_key'];$valid_sign = strtoupper(md5($tmp_str));if ($wx_sign !== $valid_sign) {$log = array('errmsg' => '微信返回的签名未通过验证','wx_sync_notice' => $notify,'valid_sign' => $valid_sign,);write_log($log);$pay_fail = '<xml><return_code><![cdata[fail]]></return_code><return_msg><![cdata[签名失败]]></return_msg></xml>';exit($pay_fail);}// 验证订单金额及状态$where = "order_no = " . $notify['out_trade_no'];$order = m('t_order')->where($where)->find();if (empty($order) || $order['pay_price'] != $notify['total_fee'] / 100) {$log = array('errmsg' => '商户订单不存在或微信返回的订单金额与商户订单金额不一致','wx_sync_notice' => $notify,'order_info' => $order,);write_log($log);$pay_fail = '<xml><return_code><![cdata[fail]]></return_code><return_msg><![cdata[签名失败]]></return_msg></xml>';exit($pay_fail);}if ($order['pay_state'] == 1) {$log = array('errmsg' => '订单已被标记为‘支付成功’(重复异步通知)','wx_sync_notice' => $notify,'order_info' => $order,);write_log($log);$pay_success = '<xml><return_code><![cdata[success]]></return_code><return_msg><![cdata[ok]]></return_msg></xml>';exit($pay_success);}// 更新订单$data = array('pay_state' => 1,'pay_time' => time(),);$update = m('t_order')->where($where)->save($data);if ($update === fal) {$log = array('errmsg' => '商户更新订单状态为‘成功’时失败','wx_sync_notice' => $notify,'order_info' => $order,'update_order' => $data,);write_log($log);$pay_fail = '<xml><return_code><![cdata[fail]]></return_code><return_msg><![cdata[签名失败]]></return_msg></xml>';exit($pay_fail);}// 销量+1 库存-1// code here...write_log("支付成功.\n微信异步通知--end--\n\n");$pay_success = '<xml><return_code><![cdata[success]]></return_code><return_msg><![cdata[ok]]></return_msg></xml>';exit($pay_success);}/*** 检查订单支付状态**/public function order_state(){$order_no = $_post['order_no'];if (empty($order_no)) {exit('fail');}$map['pay_order'] = $order_no;$map['pay_state'] = 1;$order = m('t_order')->where($map)->find();if (empty($order)) {exit('fail');}ex文艺汇演it('success');}// end all}?>

<!doctype html><html><head><meta chart="utf-8"><title>微信支付</title><style type="text/css">input#pay_btn.disabled {pointer-events: none;background: #ccc;}input#pay_btn:link,input#pay_btn:visited,input#pay_btn:hover,input#pay_btn:active {outline: none;box-shadow: none;}</style></head><body><form id="pay_form"><input type="text" name="total_fee" value="5.9"><input type="hidden" name="goods_id" value="100256"><input type="button" name="" value="点击支付" id="pay_btn" class=""></form></body><script src="//layer-v3.0.3/layer/layer.js"></script><script type="text/javascript">function check_order_state(order_no){intvl = tinterval(function () {$.ajax({url: '<?= $config['site_url'] . 'home/order_state/' ?>',type: 'post',data: {order_no: order_no}}).done(function (msg) {if (msg === 'success') {clearinterval(intvl);alert('支付成功');} el {console.log('pay fail...');}});}, 1000);}function onbridgeready(data){weixinjsbridge.invoke('getbrandwcpayrequest', data, function (res) {layer.cloall();if (res.err_msg == "get_brand_wcpay_request:ok") {// 使用以上方式判断前端返回,微信团队郑重提示://res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。check_order_state(data.order_no);} el if (res.err_msg == "get_brand_wcpay_request:cancel") {alert('已取消支付');} el {alert('支付失败');}return fal;}); }$('#pay_btn').click(function () {$.ajax({url: '<?= $config['site_url'] . 'home/wxpay/' ?>',type: 'post',data: $('#pay_form').rialize(),datatype: 'json',beforend: function () {$('#pay_btn').addclass('disabled');layer.msg('支付中,请稍候...', {icon: 16, shade: 0.3});},}).done(function (data) {ttimeout(function () {layer.cloall();$('#pay_btn').removeclass('disabled');if (data.errmsg) {layer.msg(data.errmsg);return fal;}// 调起微信支付onbridgeready(data.data);}, 2000);}).fail(function () {layer.cloall();$('#pay_btn').removeclass('disabled');layer.msg('支付失败,请重新点击支付!');});});</script></html>

本文发布于:2023-04-08 11:01:48,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/07be5d2d21d657cf56805e8b16ea2030.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:微信 JSAPI 支付.doc

本文 PDF 下载地址:微信 JSAPI 支付.pdf

标签:订单   通知   商户   后台
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图