java微信支付接入流程详解
程序员文章站
2024-02-25 09:11:22
背景
由于项目是采用java编写的,微信包括微信支付大都是php相关,于是微信支付官方文档对java的支持就不是很友好,在网上找了很多文章,基本上没有一篇是真正跑的通的,...
背景
由于项目是采用java编写的,微信包括微信支付大都是php相关,于是微信支付官方文档对java的支持就不是很友好,在网上找了很多文章,基本上没有一篇是真正跑的通的,经过一番整理,先将java接入微信支付详细流程总结出来以便后续使用。
步骤一
准备阶段:已认证微信号,且通过微信支付认证,这个可以看微信文档,很详细,这里就不再重复。
步骤二
配置授权目录,官方推荐使用https类型的url,不知道http能不能行,个人也推荐使用https的保证不会错。
配置授权域名
步骤三
微信支付二次开发所需要的参数:
app_id,app_key,partner,partner_key(appsecret)
app_id和partner_key(appsecret)
partner
app_key(自行设置32位字符)
步骤四
4.1通过页面跳转到确认支付页面,其中的redirect_uri必须是配置授权目录下的
<html> <head> <title>支付测试</title> </head> <body> <a href="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx619890d997071358&redirect_uri=https%3a%2f%2fm.yangji.com%2fpay%2fpaytest.jsp&response_type=code&scope=snsapi_base&state=123#wechat_redirect">支付测试</a> </body> </html>
4.2 获取到openid,再经服务器向微信请求获取prepay_id,封装字段并进行签名后通过jsapi调起微信支付
网页端
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/tr/html4/loose.dtd"> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" /> <meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="apple-mobile-web-app-status-bar-style" content="black" /> <meta name="format-detection" content="telephone=no" /> <title>测试支付</title> <link href="../css/css.css?v=1.0" rel="external nofollow" rel="stylesheet" type="text/css"> </head> <body> <div class="index_box"> <div class="apply_name">微信js支付测试</div> <div class="branch_con"> <ul> <li><span class="name">测试支付信息[]</span></li> </ul> <p class="cz_btn"><a href="javascript:pay();" rel="external nofollow" class="btn_1">立即支付</a></p> </div> </div> <% string code = request.getparameter("code"); %> <input type="text" id="code" value="<%out.print(code); %>"/> <input type="text" id="path" value="${pagecontext.request.contextpath}"/> <div><hr/> code:<%out.print(code); %> </div> <script type="text/javascript"> var appid,timestamp,noncestr,pg,signtype,paysign; function onbridgeready(){ weixinjsbridge.invoke( 'getbrandwcpayrequest', { "appid" : appid, //公众号名称,由商户传入 "timestamp": timestamp, //时间戳,自1970年以来的秒数 "noncestr" : noncestr, //随机串 "package" : "prepay_id=" + pg, "signtype" : signtype, //微信签名方式: "paysign" : paysign //微信签名 }, function(res){ if(res.err_msg == "get_brand_wcpay_request:ok" ) { alert("支付成功"); } } ); } function pay(){ var code = document.getelementbyid("code").value; var path = document.getelementbyid("path").value; send_request(function(value){ var json = eval("(" + value + ")"); if(json.length > 0){ appid = json[0].appid; timestamp = json[0].timestamp; noncestr = json[0].noncestr; pg = json[0].pg; signtype = json[0].signtype; paysign = json[0].paysign; if (typeof weixinjsbridge == "undefined"){ if( document.addeventlistener ){ document.addeventlistener('weixinjsbridgeready', onbridgeready, false); }else if (document.attachevent){ document.attachevent('weixinjsbridgeready', onbridgeready); document.attachevent('onweixinjsbridgeready', onbridgeready); } }else{ onbridgeready(); } } },path+"/pay/payparm.htm?openid="+code, true); } function send_request(callback, urladdress,isreturndata){ var xmlhttp = getxmlhttprequest(); xmlhttp.onreadystatechange = function(){ if (xmlhttp.readystate == 4) { try{ if(xmlhttp.status == 200){ if(isreturndata && isreturndata==true){ callback(xmlhttp.responsetext); } }else{ callback("页面找不到!"+ urladdress +""); } } catch(e){ callback("请求发送失败,请重试!" + e); } } } xmlhttp.open("post", urladdress, true); xmlhttp.send(null); } function getxmlhttprequest() { var xmlhttp; if (window.xmlhttprequest) { try { xmlhttp = new xmlhttprequest(); xmlhttp.overridemimetype("text/html;charset=utf-8"); } catch (e) {} } else if (window.activexobject) { try { xmlhttp = new activexobject("microsoft.xmlhttp"); } catch (e) { try { xmlhttp = new activexobject("msxml2.xmlhttp"); } catch (e) { try { xmlhttp = new activexobject("msxml3.xmlhttp"); } catch (e) {} } } } return xmlhttp; } </script> </body> </html>
服务器端
@requestmapping("/pay/payparm") public void payparm(httpservletrequest request, httpservletresponse response){ try { // 获取openid string openid = (string) request.getsession().getattribute("openid"); if (openid == null) { openid = getuseropenid(request); } string appid = wxconfig.app_id; string paternerkey = wxconfig.pertner_key; string out_trade_no = gettradeno(); map<string, string> paramap = new hashmap<string, string>(); paramap.put("appid", appid); paramap.put("attach", "测试"); paramap.put("body", "测试购买支付"); paramap.put("mch_id", wxconfig.partner); paramap.put("nonce_str", create_nonce_str()); paramap.put("openid", openid); paramap.put("out_trade_no", out_trade_no); paramap.put("spbill_create_ip", getaddrip(request)); paramap.put("total_fee", "1"); paramap.put("trade_type", "jsapi"); paramap.put("notify_url", "http://m.ebiaotong.com/wxpay/notify");// 此路径是微信服务器调用支付结果通知路径 string sign = getsign(paramap, paternerkey); paramap.put("sign", sign); // 统一下单 https://api.mch.weixin.qq.com/pay/unifiedorder string url = "https://api.mch.weixin.qq.com/pay/unifiedorder"; string xml = arraytoxml(paramap); string xmlstr = httpkit.post(url, xml); // 预付商品id string prepay_id = ""; if (xmlstr.indexof("success") != -1) { map<string, string> map = doxmlparse(xmlstr); prepay_id = (string) map.get("prepay_id"); } string timestamp = create_timestamp(); string noncestr = create_nonce_str(); map<string, string> paymap = new hashmap<string, string>(); paymap.put("appid", appid); paymap.put("timestamp", timestamp); paymap.put("noncestr", noncestr); paymap.put("signtype", "md5"); paymap.put("package", "prepay_id=" + prepay_id); string paysign = getsign(paymap, paternerkey); paymap.put("pg", prepay_id); paymap.put("paysign", paysign); // 拼接并返回json stringbuilder sbuilder = new stringbuilder("[{"); sbuilder.append("appid:'").append(appid).append("',") .append("timestamp:'").append(timestamp).append("',") .append("noncestr:'").append(noncestr).append("',") .append("pg:'").append(prepay_id).append("',") .append("signtype:'md5',") .append("paysign:'").append(paysign).append("'"); sbuilder.append("}]"); response.getwriter().print(sbuilder.tostring()); response.getwriter().close(); } catch (exception e) { e.printstacktrace(); } }
测试结果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。