Java微信支付之公众号支付、扫码支付实例
程序员文章站
2024-03-11 18:43:13
微信支付现在已经变得越来越流行了,随之也出现了很多以可以快速接入微信支付为噱头的产品,不过方便之余也使得我们做东西慢慢依赖第三方,丧失了独立思考的能力,这次打算分享下我之前...
微信支付现在已经变得越来越流行了,随之也出现了很多以可以快速接入微信支付为噱头的产品,不过方便之余也使得我们做东西慢慢依赖第三方,丧失了独立思考的能力,这次打算分享下我之前开发过的微信支付。
一 、h5公众号支付
要点:正确获取openid以及统一下单接口,正确处理支付结果通知,正确配置支付授权目录
h5的支付方式是使用较为广泛的方式,这种支付方式主要用于微信内自定义菜单的网页,依赖手机上安装的微信客户端,高版本的微信才支持微信支付,下面按我的流程注意说明
1 编写用于支付的页面,由于是测试用就写的简单了点
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> <% string path = request.getcontextpath(); string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/"; %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <base href="<%=basepath%>"> <title>微信支付样例</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="oauthservlet" method="post"> 订单号:<input type="text" name="orderno" /> <input type="submit" value="h5支付"/> </form> </br></br> <form action="scancodepayservlet?flag=createcode" method="post"> 订单号:<input type="text" name="orderno" /> <input type="submit" value="扫码支付"/> </form> </body> </html>
2 编写一个servlet用于通过oauth获取code
package com.debug.weixin.servlet; import java.io.ioexception; import java.io.printwriter; import javax.servlet.requestdispatcher; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import com.debug.weixin.util.commonutil; import com.debug.weixin.util.serverconfig; public class oauthservlet extends httpservlet { public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { this.dopost(request, response); } public void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { string orderno=request.getparameter("orderno"); //调用微信oauth2.0获取openid string redirecturl=serverconfig.serverdomain+"/basicweixin/payservletforh5?orderno="+orderno; string redirecturi=""; try { redirecturi=commonutil.initopenid(redirecturl); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } //system.out.println(redirecturi); //requestdispatcher dis= request.getrequestdispatcher(redirecturi); //dis.forward(request, response); response.sendredirect(redirecturi); } }
3 获取到code后,通过redirecturi获取openid,调用统一下单接口
package com.debug.weixin.servlet; import java.io.ioexception; import java.io.printwriter; import java.util.sortedmap; import java.util.treemap; import javax.servlet.requestdispatcher; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import com.debug.weixin.pojo.weixinoauth2token; import com.debug.weixin.pojo.weixinqrcode; import com.debug.weixin.util.advancedutil; import com.debug.weixin.util.commonutil; import com.debug.weixin.util.configutil; import com.debug.weixin.util.paycommonutil; public class payservletforh5 extends httpservlet { public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { this.dopost(request, response); } public void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { string orderno=request.getparameter("orderno"); string code=request.getparameter("code"); //获取accesstoken weixinoauth2token token=advancedutil.getoauth2accesstoken(configutil.appid, configutil.app_secrect, code); string openid=token.getopenid(); //调用微信统一支付接口 sortedmap<object, object> parameters = new treemap<object, object>(); parameters.put("appid", configutil.appid); parameters.put("mch_id", configutil.mch_id); parameters.put("device_info", "1000"); parameters.put("body", "我的测试订单"); parameters.put("nonce_str", paycommonutil.createnoncestr()); parameters.put("out_trade_no", orderno); //parameters.put("total_fee", string.valueof(total)); parameters.put("total_fee", "1"); parameters.put("spbill_create_ip", request.getremoteaddr()); parameters.put("notify_url", configutil.notify_url); parameters.put("trade_type", "jsapi"); parameters.put("openid", openid); string sign = paycommonutil.createsign("utf-8", parameters); parameters.put("sign", sign); string requestxml = paycommonutil.getrequestxml(parameters); string result = commonutil.httpsrequestforstr(configutil.unified_order_url,"post", requestxml); system.out.println("----------------------------------"); system.out.println(result); system.out.println("----------------------------------"); request.setattribute("orderno", orderno); request.setattribute("totalprice", "0.01"); string payjson=""; try { payjson=commonutil.geth5paystr(result,request); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } //system.out.println(payjson); request.setattribute("unifiedorder",payjson); requestdispatcher dis= request.getrequestdispatcher("h5pay.jsp"); dis.forward(request, response); } }
调用微信统一下单接口,需要注意签名算法,只有签名计算正确才能顺利支付
public static string geth5paystr(string result,httpservletrequest request) throws exception{ map<string, string> map = xmlutil.doxmlparse(result); sortedmap<object,object> params = new treemap<object,object>(); params.put("appid", configutil.appid); params.put("timestamp", long.tostring(new date().gettime())); params.put("noncestr", paycommonutil.createnoncestr()); params.put("package", "prepay_id="+map.get("prepay_id")); params.put("signtype", configutil.sign_type); string paysign = paycommonutil.createsign("utf-8", params); params.put("paysign", paysign); //paysign的生成规则和sign的生成规则一致 string json = jsonobject.fromobject(params).tostring(); return json; }
4 编写最终的支付界面调起微信h5支付
<%@ page language="java" import="java.util.*" pageencoding="utf-8"%> <% string path = request.getcontextpath(); string basepath = request.getscheme()+"://"+request.getservername()+":"+request.getserverport()+path+"/"; %> <!doctype html public "-//w3c//dtd html 4.01 transitional//en"> <html> <head> <base href="<%=basepath%>"> <title>微信h5支付</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0"> <script type="text/javascript"> function jsapicall(){ weixinjsbridge.invoke( 'getbrandwcpayrequest',<%=(string)request.getattribute("unifiedorder")%>, function(res){ weixinjsbridge.log(res.err_msg); //alert(res.err_code+res.err_desc+res.err_msg); if(res.err_msg == "get_brand_wcpay_request:ok" ) { alert("恭喜你,支付成功!"); }else{ alert(res.err_code+res.err_desc+res.err_msg); } } ); } function callpay(){ if (typeof weixinjsbridge == "undefined"){ if( document.addeventlistener ){ document.addeventlistener('weixinjsbridgeready', jsapicall, false); }else if (document.attachevent){ document.attachevent('weixinjsbridgeready', jsapicall); document.attachevent('onweixinjsbridgeready', jsapicall); } }else{ jsapicall(); } } </script> </head> <body> <input type="button" value="支付" onclick="callpay()"/> </body> </html>
5 处理微信支付结果通知
package com.debug.weixin.servlet; import java.io.bytearrayoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.printwriter; import java.util.map; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import org.jdom.jdomexception; import com.debug.weixin.util.paycommonutil; import com.debug.weixin.util.xmlutil; public class payhandlerservlet extends httpservlet { public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { this.dopost(request, response); } public void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { inputstream instream = request.getinputstream(); bytearrayoutputstream outsteam = new bytearrayoutputstream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = instream.read(buffer)) != -1) { outsteam.write(buffer, 0, len); } outsteam.close(); instream.close(); string result = new string(outsteam.tobytearray(),"utf-8");//获取微信调用我们notify_url的返回信息 map<object, object> map=null; try { map = xmlutil.doxmlparse(result); } catch (jdomexception e) { // todo auto-generated catch block e.printstacktrace(); } for(object keyvalue : map.keyset()){ system.out.println(keyvalue+"="+map.get(keyvalue)); } if (map.get("result_code").tostring().equalsignorecase("success")) { //对订单进行业务操作 system.out.println("-------------ok"); response.getwriter().write(paycommonutil.setxml("success", "")); //告诉微信服务器,我收到信息了,不要在调用回调action了 } } }
对于上面的代码,有很多都是参考,因此这部分的代码就不贴出来了,需要的话看这个博客就知道了。
二 微信扫码支付(模式一)
要点:必须调用长链接转短链接接口、正确配置扫码支付回调url
1 根据订单号生成微信支付二维码
下面是几个生成二维码的方法:
package com.debug.weixin.util; import com.google.zxing.common.bitmatrix; import javax.imageio.imageio; import java.io.file; import java.io.outputstream; import java.io.ioexception; import java.awt.image.bufferedimage; public final class matrixtoimagewriter { private static final int black = 0xff000000; private static final int white = 0xffffffff; private matrixtoimagewriter() {} public static bufferedimage tobufferedimage(bitmatrix matrix) { int width = matrix.getwidth(); int height = matrix.getheight(); bufferedimage image = new bufferedimage(width, height, bufferedimage.type_int_rgb); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { image.setrgb(x, y, matrix.get(x, y) ? black : white); } } return image; } public static void writetofile(bitmatrix matrix, string format, file file) throws ioexception { bufferedimage image = tobufferedimage(matrix); if (!imageio.write(image, format, file)) { throw new ioexception("could not write an image of format " + format + " to " + file); } } public static void writetostream(bitmatrix matrix, string format, outputstream stream) throws ioexception { bufferedimage image = tobufferedimage(matrix); if (!imageio.write(image, format, stream)) { throw new ioexception("could not write an image of format " + format); } } }
这个算是工具类,还有一个就是把二维码显示在界面上的方法,createqrcode主要用到代码块:
public static void createcodestream(string text,httpservletresponse response) throws exception{ // response.setcontenttype("image/jpeg"); servletoutputstream sos = response.getoutputstream(); int width = 500; int height = 500; //二维码的图片格式 string format = "jpg"; multiformatwriter multiformatwriter = new multiformatwriter(); map hints = new hashmap(); //内容所使用编码 hints.put(encodehinttype.character_set, "utf-8"); bitmatrix bitmatrix = multiformatwriter.encode(text, barcodeformat.qr_code, width, height, hints); //生成二维码 matrixtoimagewriter.writetostream(bitmatrix, format,sos); sos.close(); }
2 长链接转短链接生成二维码,编写扫码支付回调方法并调用统一下单接口
package com.debug.weixin.servlet; import java.io.bytearrayoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.printwriter; import java.util.date; import java.util.map; import java.util.sortedmap; import java.util.treemap; import javax.servlet.servletexception; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import org.jdom.jdomexception; import com.debug.weixin.util.commonutil; import com.debug.weixin.util.configutil; import com.debug.weixin.util.createqrcode; import com.debug.weixin.util.paycommonutil; import com.debug.weixin.util.xmlutil; import com.mongodb.dbobject; public class scancodepayservlet extends httpservlet { public void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { this.dopost(request, response); } public void dopost(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { string flag=request.getparameter("flag"); if("createcode".equals(flag)){ createpaycode(request,response); }else{ try { wxscancodehandler(request,response); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } } public void createpaycode(httpservletrequest request,httpservletresponse response){ string orderno=request.getparameter("orderno"); sortedmap<object,object> paras = new treemap<object,object>(); paras.put("appid", configutil.appid); paras.put("mch_id", configutil.mch_id); paras.put("time_stamp", long.tostring(new date().gettime())); paras.put("nonce_str", paycommonutil.createnoncestr()); paras.put("product_id", orderno);//商品号要唯一 string sign = paycommonutil.createsign("utf-8", paras); paras.put("sign", sign); string url = "weixin://wxpay/bizpayurl?sign=sign&appid=appid&mch_id=mchid&product_id=productid&time_stamp=timestamp&nonce_str=nocestr"; string nativeurl = url.replace("sign", sign).replace("appid", configutil.appid).replace("mchid", configutil.mch_id).replace("productid", (string)paras.get("product_id")).replace("timestamp", (string)paras.get("time_stamp")).replace("nocestr", (string)paras.get("nonce_str")); sortedmap<object,object> parameters = new treemap<object,object>(); parameters.put("appid", configutil.appid); parameters.put("mch_id", configutil.mch_id); parameters.put("nonce_str", paycommonutil.createnoncestr()); parameters.put("long_url", commonutil.urlencodeutf8(nativeurl)); string sign2 = paycommonutil.createsign("utf-8", parameters); parameters.put("sign", sign2); string requestxml = paycommonutil.getrequestxml(parameters); string result =commonutil.httpsrequestforstr(configutil.short_url, "post", requestxml); map<string, string> map=null; try { map = xmlutil.doxmlparse(result); } catch (jdomexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } string returncode = map.get("return_code"); string resultcode = map.get("result_code"); if(returncode.equalsignorecase("success")&&resultcode.equalsignorecase("success")){ string shorturl = map.get("short_url"); //todo 拿到shorturl,写代码生成二维码 system.out.println("shorturl="+shorturl); try { createqrcode.createcodestream(shorturl,response); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } } public void wxscancodehandler(httpservletrequest request,httpservletresponse response) throws exception { inputstream instream = request.getinputstream(); bytearrayoutputstream outsteam = new bytearrayoutputstream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = instream.read(buffer)) != -1) { outsteam.write(buffer, 0, len); } outsteam.close(); instream.close(); string result = new string(outsteam.tobytearray(),"utf-8");//获取微信调用我们notify_url的返回信息 map<object, object> map=null; try { map = xmlutil.doxmlparse(result); } catch (jdomexception e) { // todo auto-generated catch block e.printstacktrace(); } for(object keyvalue : map.keyset()){ system.out.println(keyvalue+"="+map.get(keyvalue)); } string orderno=map.get("product_id").tostring(); //接收到请求参数后调用统一下单接口 sortedmap<object, object> parameters = new treemap<object, object>(); parameters.put("appid", configutil.appid); parameters.put("mch_id", configutil.mch_id); parameters.put("device_info", "1000"); parameters.put("body", "测试扫码支付订单"); parameters.put("nonce_str", paycommonutil.createnoncestr()); parameters.put("out_trade_no", map.get("product_id")); //parameters.put("total_fee", string.valueof(totalprice)); parameters.put("total_fee", "1"); parameters.put("spbill_create_ip", request.getremoteaddr()); parameters.put("notify_url", configutil.notify_url); parameters.put("trade_type", "native"); parameters.put("openid", map.get("openid")); string sign = paycommonutil.createsign("utf-8", parameters); parameters.put("sign", sign); string requestxml = paycommonutil.getrequestxml(parameters); string result2 = commonutil.httpsrequestforstr(configutil.unified_order_url,"post", requestxml); system.out.println("-----------------------------统一下单结果---------------------------"); system.out.println(result2); map<string, string> mm=null; try { mm=geth5paymap(result2,request); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } //string prepayid=getprepayid(result2,request); //string returnnonestr=getreturnnonestr(result2,request); string prepayid=mm.get("prepay_id"); string returnnonestr=mm.get("nonce_str");; sortedmap<object, object> lastsign = new treemap<object, object>(); lastsign.put("return_code", "success"); lastsign.put("appid", configutil.appid); lastsign.put("mch_id", configutil.mch_id); lastsign.put("nonce_str", returnnonestr); lastsign.put("prepay_id", prepayid); lastsign.put("result_code", "success"); lastsign.put("key", configutil.api_key); string lastsignpara = paycommonutil.createsign("utf-8", lastsign); stringbuffer buf=new stringbuffer(); buf.append("<xml>"); buf.append("<return_code>success</return_code>"); buf.append("<appid>"+configutil.appid+"</appid>"); buf.append("<mch_id>"+configutil.mch_id+"</mch_id>"); buf.append("<nonce_str>"+returnnonestr+"</nonce_str>"); buf.append("<prepay_id>"+prepayid+"</prepay_id>"); buf.append("<result_code>success</result_code>"); buf.append("<sign>"+lastsignpara+"</sign>"); buf.append("</xml>"); response.getwriter().print(buf.tostring()); } public map<string, string> geth5paymap(string result,httpservletrequest request) throws exception{ map<string, string> map = xmlutil.doxmlparse(result); return map; } }
最终看下公众号支付和扫码支付的微信配置:
希望通过这篇文章,大家能明白就算通过java来做微信公众号、微信支付而不借助github提供的那些坑人的代码也可以开发出另自己和客户满意的微信应用。虽然微信给出的demo都是php的,但这些都是浮云,开发语言是其次,理解接口调用需具备的底层只是才是程序员的必修课。