赞
踩
上手一个小案例,实现Java版本的微信支付。
第一步: 从微信平台下载微信支付demo。
链接: 到官方下载
这里我们下载Java版的。
注意微信提供的demo是myclipse版本的。
我们要用myclipse打开。
第二步:把demo导入到myclipse中去。
ps:过程中爆红的可以选择
(1)硬着头皮跟着做下去。
(2)拉到文章最后导入jar包。
第三步:配置代码。
(1) MyConfig.java
package com.github.wxpay.sdk; import com.github.wxpay.sdk.WXPayConfig; import java.io.*; public class MyConfig extends WXPayConfig{ private byte[] certData; public MyConfig() throws Exception { //String certPath = "d:/tomcat/ssqc/webapps/wxpay-sdk/cert/apiclient_cert.p12"; /*String certPath = "C:/Tomcat 8.5/webapps/wxpay-sdk/cert/apiclient_cert.p12"; File file = new File(certPath); InputStream certStream = new FileInputStream(file); this.certData = new byte[(int) file.length()]; certStream.read(this.certData); certStream.close();*/ //上面六行注释,要修改WXPay.java,注释掉63行 } public String getAppID() { return "wx8397f8696b538317"; } public String getMchID() { return "1473426802"; } public String getKey() { return "T6m9iK73b0kn9g5v426MKfHQH7X8rKwb"; } public InputStream getCertStream() { ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData); return certBis; } public int getHttpConnectTimeoutMs() { return 8000; } public int getHttpReadTimeoutMs() { return 10000; } // @Override // IWXPayDomain getWXPayDomain() { // // TODO Auto-generated method stub // return null; // } //README.MD漏了这个类型: public IWXPayDomain getWXPayDomain() { // 这个方法需要这样实现, 否则无法正常初始化WXPay IWXPayDomain iwxPayDomain = new IWXPayDomain() { public void report(String domain, long elapsedTimeMillis, Exception ex) { } public DomainInfo getDomain(WXPayConfig config) { return new IWXPayDomain.DomainInfo(WXPayConstants.DOMAIN_API, true); } }; return iwxPayDomain; }//public IWXPayDomain getWXPayDomain }//public class MyConfig extends WXPayConfig
(2) WXPayExample.java
// A code block var foo = 'bar';package com.github.wxpay.sdk; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.HashMap; import java.util.Hashtable; import java.util.Map; //import com.google.zxing.*; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.common.BitMatrix; public class WXPayExample { public static void main(String[] args) throws Exception { MyConfig config = new MyConfig(); WXPay wxpay = new WXPay(config); Map<String, String> data = new HashMap<String, String>(); data.put("body", "QQ会员充值"); data.put("out_trade_no", "2020113010595900000012"); data.put("device_info", ""); data.put("fee_type", "CNY"); data.put("total_fee", "1"); data.put("spbill_create_ip", "123.12.12.123"); data.put("notify_url", "http://www.gzchenlijun.com/notify.php"); data.put("trade_type", "NATIVE"); // 此处指定为扫码支付//APP data.put("product_id", "12"); try { Map<String, String> resp = wxpay.unifiedOrder(data); System.out.println(resp); } catch (Exception e) { e.printStackTrace(); } }//public static void main public void dopay(String order_id,String price,String jdpath) throws Exception { MyConfig config = new MyConfig(); WXPay wxpay = new WXPay(config); Map<String, String> data = new HashMap<String, String>(); data.put("body", "广州软件研究所"); data.put("out_trade_no", order_id); data.put("device_info", ""); data.put("fee_type", "CNY"); data.put("total_fee", price); data.put("spbill_create_ip", "123.12.12.123"); data.put("notify_url", "http://www.gzchenlijun.com/notify.php"); data.put("trade_type", "NATIVE"); // 此处指定为扫码支付 data.put("product_id", "12"); try { //发起支付 Map<String, String> resp = wxpay.unifiedOrder(data); System.out.println(resp); System.out.println("jdpath2="+jdpath); //获取二维码URL String code_url = resp.get("code_url"); //根据url生成二维码 String text = code_url; int width = 300; int height = 300; //二维码的图片格式 String format = "gif"; Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(); //内容所使用编码 hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); BitMatrix bitMatrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints); //生成二维码 //File outputFile = new File("D:/Tomcat 7.0/webapps/wxpay-sdk"+"/img/"+"/"+order_id+".gif"); System.out.println("jdpath="+jdpath); File outputFile = new File(jdpath+"/img/"+"/"+order_id+".gif"); //二维码图片放置的目录jdpath是首页index.jsp生成的 if(!outputFile.exists()){ QRUtil.writeToFile(bitMatrix, format, outputFile); }//if(!outputFile.exists } catch (Exception e) { e.printStackTrace(); }// try }// public Map<String, String> dopay ///订单查询 public String chaxun(String order_id) throws Exception { MyConfig config = new MyConfig(); WXPay wxpay = new WXPay(config); Map<String, String> data = new HashMap<String, String>(); data.put("out_trade_no", order_id); String trade_state = ""; try { Map<String, String> resp = wxpay.orderQuery(data); trade_state = resp.get("trade_state").toString(); //System.out.println(resp); } catch (Exception e) { e.printStackTrace(); } return trade_state; } public static void getTXT() throws Exception { String filePath = "C:\\a.txt"; String content="SUCCESS"; FileWriter fwriter = null; try { // true表示不覆盖原来的内容,而是加到文件的后面。若要覆盖原来的内容,直接省略这个参数就好 fwriter = new FileWriter(filePath); fwriter.write(content); } catch (IOException ex) { ex.printStackTrace(); } finally { try { fwriter.flush(); fwriter.close(); } catch (IOException ex) { ex.printStackTrace(); } } //return content; } }//public class WXPayExample
(3) 在src/main/java的代码窗口添加一个新文件MatrixToImageWriter.java
package com.github.wxpay.sdk; import java.awt.Graphics2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.Hashtable; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import javax.imageio.ImageIO; import java.awt.*; import java.awt.geom.AffineTransform; import java.awt.image.AffineTransformOp; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map; /** * 生成二维码图片 */ public final class MatrixToImageWriter { /** * 生成二维码图片文件 * @param content 二维码图片中所嵌入的内容 * @param format 图片文件格式(后缀名) * @param imgPath 二维码图片生成的位置 * @param logoPath logo图片的路径 * @param width 二维码图片的宽 * @param height 二维码图片的高 * @return * @return boolean 生成二维码成功返回true * @throws WriterException * @throws IOException */ public static BufferedImage makeMatrixImg(String content, String logoPath,int width,int height) throws WriterException, IOException{ /* * 1.将内容写入矩阵 */ // 设置矩阵所使用的编码格式 Hashtable<EncodeHintType,String> hints = new Hashtable<EncodeHintType, String>() ; hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // 初始化矩阵 BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints); // 将矩阵图形化 BufferedImage matrixImage = toBufferredImage(bitMatrix); /* * 2.往即将生成的‘二维码图+logo’图片中载入logo */ // 2.1 读取logo图片 BufferedImage logoImage = ImageIO.read(new File(logoPath)); /* * 3。使用二维码图形创建图形工具 */ Graphics2D g2d = matrixImage.createGraphics() ; /* * 4. 画出中间含有logo的二维码图形对象 */ int posW = (matrixImage.getWidth()-logoImage.getWidth())/2 ; int posH = (matrixImage.getHeight()-logoImage.getHeight())/2 ; g2d.drawImage(logoImage, posW, posH, null); g2d.dispose(); logoImage.flush(); /* * 5. 将图片读出到文件中 */ //boolean writed = ImageIO.write(matrixImage, format, os) ; return matrixImage; } private static BufferedImage toBufferredImage(BitMatrix bitMatrix){ int width = bitMatrix.getWidth(); int height = bitMatrix.getHeight(); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); for (int i=0; i<width; i++) { for(int j=0; j<height; j++){ image.setRGB(i, j, bitMatrix.get(i, j)?0XFF000000:0XFFFFFF); } } return image; } }
(4) 在WebRoot下添加一个新文件index.jsp,代码如下
<%@ page pageEncoding="utf-8" contentType="text/html; charset=utf-8" language="java" import="java.sql.*,java.util.*,java.text.*" errorPage="" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>电子商场</title> </head> <style type="text/css"> <!-- body { background-color: #99CCFF; } --> </style> <BODY> <font color="#FFFFFF"> <center> <% String user = "guest";顾客注册账号 Connection con; Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://172.16.252.48:3306/goods?useUnicode=true&characterEncoding=utf-8","root","chenlijun"); Statement sql; ResultSet rs; try { request.setCharacterEncoding("utf-8"); sql=con.createStatement(); rs=sql.executeQuery("SELECT * FROM goods.goods"); out.print("<Table Border style='font-size: 10pt'>"); out.print("<TR><td colspan=5 align=center height='40'>电子商场</td></tr>"); out.print("<TR><td colspan=5 align=center height='40'><a href='addgoods.html' target='_self'>添加商品</a></td></tr>"); out.print("<TR>"); out.print("<Td width='45%' height='30'>"+"标题"); out.print("<Td width='5%' height='30'>"+"价格"); out.print("<Td width='25%' colspan=2 height='30' align='center'>"+"操作"); out.print("</TR>"); while(rs.next()) { out.print("<TR>"); out.print("<TD height='30'>"+rs.getString("title1")+"</TD>"); out.print("<TD height='30'>"+rs.getString("price")+"</TD>"); String id=rs.getString("id");//商品的ID String order_id=(String)(new SimpleDateFormat("yyyyMMdd").format(Calendar.getInstance().getTime())); int rmd=(int)(Math.floor(Math.random()*10000000)); order_id=order_id+rmd; //order_id = "202012211261053";//20201221647394 //order_id = "20201221647394";202012226830336 //order_id = "202012228246206"; String price1 = (String)rs.getString("price"); float price2= Float.parseFloat(price1)*100;//字符串转float; int price= (int)price2; String title1 = rs.getString("price");//最好不要传输中文,传id过去,再读取商品的title String slt = rs.getString("slt"); out.print("<TD align='center'><a href='addorder.jsp?id="+id+"&user="+user+"&order_id="+order_id+"&price="+price+"&title1="+title1+"&slt="+slt+"'>我要购买</a></TD>"); out.print("</TR>") ; } out.print("</Table>"); con.close(); } catch(SQLException e1) { out.print("SQL异常!!!!"); } %> </center> </BODY> </HTML>
(5) 在WebRoot下添加一个新文件addorder.jsp,代码如下:
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*,java.util.*,java.text.*" errorPage="" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>去支付中心</title> <script src="/wxpay-sdk/js/jquery-3.5.1.min.js"></script> <script> function chenlijun(){ document.form1.submit(); return true; } </script> </head> <body onload="return chenlijun();"> <% //从首页index.jsp获取订单编号,商品价格,商品的ID号; String order_id = request.getParameter("order_id"); String price = request.getParameter("price"); String real_price = price; String order_info = request.getParameter("order_info"); String pay_type = "1";//1:微信支付,2:支付宝支付 String pay_time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime()); String status = null; String id = request.getParameter("id"); String user = request.getParameter("user"); Connection con=null; try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://172.16.252.48:3306/goods","root","chenlijun"); Statement stat; stat=con.createStatement(); //检查是否已经添加 Statement sql1; ResultSet rs; sql1=con.createStatement(); rs=sql1.executeQuery("SELECT * FROM goods.order1 where order_id="+order_id); int row = 0; while(rs.next()){ row= rs.getRow(); } if(row == 0){ String sql2="insert into order1(order_id,price,real_price,order_info,pay_type,pay_time,status,goodsid,user) values('"+order_id+"','"+price+"','"+real_price+"','"+order_info+"','"+pay_type+"','"+pay_time+"','"+status+"','"+id+"','"+user+"')"; int s=stat.executeUpdate(sql2); } } catch(Exception e){ out.println(e); } con.close(); %> <!-- 下面提交表单的目的:让价格不出现在地址栏,避免修改价格 --> <form name="form1" method="post" action="/wxpay-sdk/pay.jsp"> <input type="hidden" name="order_id" value="<%=order_id%>" /> <input type="hidden" name="price" value="<%=price%>" /> <input type="hidden" name="user" value="<%=user%>" /> </form> </body> </html>
(6) 在WebRoot下添加一个新文件addorder2.jsp,代码如下:
<%@ page pageEncoding="GBK" contentType="text/html; charset=GBK" language="java" import="java.sql.*" errorPage="" %> <%@ page import="java.sql.*" %> <% request.setCharacterEncoding("GBK"); String title1=request.getParameter("title1"); String price=request.getParameter("price"); String content1=request.getParameter("content1"); String slt=request.getParameter("slt"); Connection con=null; try { request.setCharacterEncoding("GBK"); Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://172.16.252.48:3306/goods?useUnicode=true&characterEncoding=GBK","root","chenlijun"); Statement sql; sql=con.createStatement(); String sql2="insert into goods(title1,price,content1,slt) values('"+title1+"','"+price+"','"+content1+"','"+slt+"')"; //out.print(sql2); int s=sql.executeUpdate(sql2); } catch(Exception e){ out.println(e); } %><title>添加商品到数据库</title> 添加商品到数据库<br /> <a href="index.jsp">??</a>
(7)在WebRoot下添加一个新文件pay.jsp,代码如下:
<%@ page language="java" import="java.util.*,com.github.wxpay.sdk.*" pageEncoding="utf-8" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ page import="java.text.*"%> <%@page import="java.sql.*"%> <%@ page import="java.io.OutputStream"%> <%@ page import="java.io.*"%> <%//@ page import="org.apache.commons.io.IOUtils"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=path%>"> <title>微信支付</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> <script src="/wxpay-sdk/js/jquery-3.5.1.min.js"></script> </head> <body> <% //下面是获取项目或网站的绝对地址,以便把微信支付的二维码放在此目录下 String realpath = application.getRealPath("/")+"img/"; if (!(new java.io.File(realpath).isDirectory())) //如果文件夹不存在 { new java.io.File(realpath).mkdir(); //不存在 文件夹,则建立此文件夹 } String jdpath = getServletContext().getRealPath("/");//获取网站的绝对路径,包括虚拟目录 jdpath = jdpath.replaceAll("\\\\", "\\/");//"\"换成"/" %> <br> <div align="center" style="font-size:30px;color:#ff0000">微信支付</div> <% String order_id = request.getParameter("order_id"); String price = request.getParameter("price"); String user = request.getParameter("user"); String id = request.getParameter("id"); WXPayExample wxpay = new WXPayExample();//类的实例化对象wxpay; wxpay.dopay(order_id,price,jdpath);//wxpay对象执行微信支付,出现二维码; %> <!-- 下面是显示二维码,二维码图片的文件名称,使用订单编号.gif --> <p align="center"><img src="<%=basePath%>/img/<%=order_id%>.gif"></p> <!-- <p align="center"><img src="<%=basePath%>/img/2020111254974.gif"></p> --> <p align="center">二维码过期还剩:<div style="color:red" id = "time" align="center"></div></p> <p> <script type="text/javascript"> $(document).ready(function () { var timeobj1 = window.setInterval("tijiao()", 6000000); //每隔6秒钟执行一次; 6000 }); function tijiao() { $.post("<%=path%>/chaxun.jsp",{ order_id:<%=order_id%> }, //上面是以POST方式向chaxun.jsp提交订单编号order_id的值,可以提交多个键值对; function(data,status){ //alert("数据: \n" + data + "\n状态: " + status); //data数据是chaxun.jsp中的所有超文本标记内容; //status是向chaxun.jsp提交后,返回的状态,如果chaxun.jsp存在,能打开,status就为success; data = data.replace(/\s/g,""); //上面是去年所有的空格 var data1 = data.replace(/<\/?.+?>/g,""); //上面是去掉hmtl var data2 = data1.replace(/ /g,"");//去掉hmtl var datalen = data2.length; //alert("data2="+data2); var qcsuccess = data2.substring(datalen-7,datalen);//取出"SUCCESS"或取出";FAIL" //alert("qcsuccess="+qcsuccess); if(qcsuccess == "SUCCESS"){ //alert("qcsuccess=SUCCESS"); document.write("<iframe src='<%=path%>/redirect.jsp?order_id=<%=order_id%>&user=<%=user%>&id=<%=id%>' rel='nofollow' scrolling='no' frameborder='0' width='100%' height='1000'></iframe>"); }else{ //alert("qcsuccess=FAIL"); }//if(qcsuccess = "SUCCESS") }//function(data,status) );//$(document).ready( }// $(document).ready(function () { </script> <script> var times = 5 * 60;//剩余时间,单位秒 var timeDiv = document.getElementById("time"); var timeObj2 = null; function timer(){ if(times == 0){ //document.form1.form1.submit(); window.clearInterval(timeObj1); window.clearInterval(timeObj2); document.write("<iframe src='<%=path%>/index.jsp' rel='nofollow' scrolling='no' frameborder='0' width='100%' height='1000'></iframe>"); return; } var t = Math.floor(times/60) +"分"+times%60+"秒" timeDiv.innerHTML = t; times --; } timeObj2 = window.setInterval(timer,1000); </script> </p> </body> </html>
(8)在WebRoot下添加jquery-3.5.1.min.js
最后
代码编写完成,选择Tomcat运行,进行测试:http://localhost:8080/wxpay
结果:
点击我要购买,调起支付界面
ps:整个过程中会涉及到很多jar包,爆红找不到的可以自行百度下载。
以下是我的整个工程目录和涉及到的其他jar包:
Webroot下:
希望大家也能成功。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。