JAVA实现微信扫码支付回调Demo
扫码效果演示图
项目结构图:
微信实现JAVA扫码支付的Demo源码,做这个做的很辛苦,熬夜了几个晚上才把支付和回调做好的,在这里贴一下相关代码,以免更多开发者避免遇到这些坑,有不动的问题可以加我Q76949433,提供技术支持
package com.pay.common;
public class WxPayConfig {
public static String wxAppId="wsexbd6b2dgd23c841";
public static String wxappStrect="79ch22dsss71gbds5debe8d5c79e";
public static String wxMerchantNo="1523132d21";
public static String wxApiKey="ae12d0fgd3293df7aa1d606q228d91";
public static String notifyUrl ="http://test.pay.sucaiku.xin/wxPayNotifySevlet";
}
package com.pay.servlet;
import java.io.IOException;
import java.util.Map;
import java.util.Random;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jpay.ext.kit.IpKit;
import com.jpay.ext.kit.PaymentKit;
import com.jpay.ext.kit.StrKit;
import com.jpay.weixin.api.WxPayApi;
import com.jpay.weixin.api.WxPayApi.TradeType;
import com.jpay.weixin.api.WxPayApiConfig;
import com.jpay.weixin.api.WxPayApiConfig.PayModel;
import com.jpay.weixin.api.WxPayApiConfigKit;
import com.pay.common.WxPayConfig;
import com.pay.util.MoneyUtil;
/**
-
微信公众号支付
*/
public class WxPayMpServlet extends HttpServlet {
private static final long serialVersionUID = 1L;private ServletConfig config;
Random rand = new Random();
public void init(ServletConfig config) throws ServletException {
this.config = config;
}public WxPayMpServlet() {
super();
}protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {String openId = ""; // 订单金额 String total_fee = "0.01"; // 订单号 String outTradeNo = "D"; for (int i = 0; i < 10; i++) { outTradeNo += rand.nextInt(10); } if (StrKit.isBlank(total_fee)) { System.out.println("支付金额不能为空"); return; } String ip = IpKit.getRealIp(request); if (StrKit.isBlank(ip)) { ip = "127.0.0.1"; } WxPayApiConfig wxPayApiConfig = WxPayApiConfig.New(); // 微信公众号id wxPayApiConfig.setAppId(WxPayConfig.wxAppId); // 微信商户号 wxPayApiConfig.setMchId(WxPayConfig.wxMerchantNo); // 微信商户支付** wxPayApiConfig.setPaternerKey(WxPayConfig.wxApiKey); // IP wxPayApiConfig.setSpbillCreateIp(ip); // 交易类型 扫码支付 wxPayApiConfig.setTradeType(TradeType.JSAPI); wxPayApiConfig.setOpenId(openId); // 回调地址 wxPayApiConfig.setNotifyUrl(WxPayConfig.notifyUrl); // 订单号 不可重复 wxPayApiConfig.setOutTradeNo(outTradeNo); // 订单金额 wxPayApiConfig.setTotalFee(MoneyUtil.getMoney(total_fee)); // 备注 wxPayApiConfig.setBody("商品购买"); // 支付模式 商户模式(自签约不可更改) wxPayApiConfig.setPayModel(PayModel.BUSINESSMODEL); WxPayApiConfigKit.putApiConfig(wxPayApiConfig); Map<String, String> params = WxPayApiConfigKit.getWxPayApiConfig().build(); // ************************参数组装完毕开始发起支付*********************************// String xmlResult = WxPayApi.pushOrder(false, params); Map<String, String> result = PaymentKit.xmlToMap(xmlResult); String return_code = result.get("return_code"); String return_msg = result.get("return_msg"); if (!PaymentKit.codeIsOK(return_code)) { System.out.println(return_msg); return; } String result_code = result.get("result_code"); if (!PaymentKit.codeIsOK(result_code)) { System.out.println(return_msg); return; } System.out.println(result.toString());
}
}
package com.pay.servlet;
import java.io.IOException;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jpay.ext.kit.HttpKit;
import com.jpay.ext.kit.PaymentKit;
import com.jpay.weixin.api.WxPayApiConfigKit;
/**
- 微信回调
- @author Administrator
*/
public class WxPayNotifySevlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public WxPayNotifySevlet() {
super();
// TODO Auto-generated constructor stub
}
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 支付结果通用通知文档:https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=9_7&index=8
String xmlMsg = HttpKit.readData(request);
System.err.println("收到微信支付回调通知=" + xmlMsg);
Map<String, String> params = PaymentKit.xmlToMap(xmlMsg);
String appid = params.get("appid");
//商户号
String mch_id = params.get("mch_id");
String result_code = params.get("result_code");
String openId = params.get("openid");
//交易类型
String trade_type = params.get("trade_type");
//付款银行
String bank_type = params.get("bank_type");
// 总金额
String money = params.get("total_fee");
//现金支付金额
String cash_fee = params.get("cash_fee");
// 微信支付订单号
String transactionId = params.get("transaction_id");
// // 商户订单号
String outTradeNo = params.get("out_trade_no");
// // 支付完成时间,格式为yyyyMMddHHmmss
// String time_end = params.get("time_end");
///////////////////////////// 以下是附加参数///////////////////////////////////
String type = params.get("attach");
String fee_type = params.get("fee_type");
String is_subscribe = params.get("is_subscribe");
String err_code = params.get("err_code");
String err_code_des = params.get("err_code_des");
if (PaymentKit.verifyNotify(params, WxPayApiConfigKit.getWxPayApiConfig().getPaternerKey())) {
if (("SUCCESS").equals(result_code)) {
//自行实现
package com.pay.servlet;
import java.io.IOException;
import java.util.Map;
import java.util.Random;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jpay.ext.kit.IpKit;
import com.jpay.ext.kit.PaymentKit;
import com.jpay.ext.kit.StrKit;
import com.jpay.weixin.api.WxPayApi;
import com.jpay.weixin.api.WxPayApi.TradeType;
import com.jpay.weixin.api.WxPayApiConfig;
import com.jpay.weixin.api.WxPayApiConfig.PayModel;
import com.jpay.weixin.api.WxPayApiConfigKit;
import com.pay.common.WxPayConfig;
import com.pay.util.MoneyUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
/**
-
微信扫码支付
*/
@WebServlet("/WxPayServlet")
public class WxPayServlet extends HttpServlet {
private static final long serialVersionUID = 1L;private ServletConfig config;
Random rand = new Random();
public void init(ServletConfig config) throws ServletException {
this.config = config;
}public WxPayServlet() {
super();
}protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 网站根目录
String rootPath = config.getServletContext().getRealPath("/");//订单金额 String total_fee = "0.01"; //订单号 String outTradeNo = "D"; for (int i = 0; i < 10; i++) { outTradeNo += rand.nextInt(10); } if (StrKit.isBlank(total_fee)) { System.out.println("支付金额不能为空"); return; } String ip = IpKit.getRealIp(request); if (StrKit.isBlank(ip)) { ip = "127.0.0.1"; } WxPayApiConfig wxPayApiConfig = WxPayApiConfig.New(); // 微信公众号id wxPayApiConfig.setAppId(WxPayConfig.wxAppId); // 微信商户号 wxPayApiConfig.setMchId(WxPayConfig.wxMerchantNo); // 微信商户支付** wxPayApiConfig.setPaternerKey(WxPayConfig.wxApiKey); // IP wxPayApiConfig.setSpbillCreateIp(ip); // 交易类型 扫码支付 wxPayApiConfig.setTradeType(TradeType.NATIVE); // 回调地址 wxPayApiConfig.setNotifyUrl(WxPayConfig.notifyUrl); // 订单号 不可重复 wxPayApiConfig.setOutTradeNo(outTradeNo); // 订单金额 wxPayApiConfig.setTotalFee(MoneyUtil.getMoney(total_fee)); // 备注 wxPayApiConfig.setBody("商品购买"); // 支付模式 商户模式(自签约不可更改) wxPayApiConfig.setPayModel(PayModel.BUSINESSMODEL); WxPayApiConfigKit.putApiConfig(wxPayApiConfig); Map<String, String> params = WxPayApiConfigKit.getWxPayApiConfig().build(); // ************************参数组装完毕开始发起支付*********************************// String xmlResult = WxPayApi.pushOrder(false, params); Map<String, String> result = PaymentKit.xmlToMap(xmlResult); String return_code = result.get("return_code"); String return_msg = result.get("return_msg"); if (!PaymentKit.codeIsOK(return_code)) { System.out.println(return_msg); return; } String result_code = result.get("result_code"); if (!PaymentKit.codeIsOK(result_code)) { System.out.println(return_msg); return; } // 微信预支付订单生产成功 获取二维码url String qrCodeUrl = result.get("code_url"); QrCodeUtil.generate(qrCodeUrl, 300, 300, FileUtil.file(rootPath + "/wxPayCode/" + outTradeNo + ".png")); request.setAttribute("codeUrl", outTradeNo); request.getRequestDispatcher("/pay/wxpay.jsp").forward(request, response);
}
}
}
}
}
}
package com.pay.util;
public class MoneyUtil {
public static String getMoney(String amount) {
if (amount == null) {
return "";
}
// 金额转化为分为单位
String currency = amount.replaceAll("\\$|\\¥|\\,", ""); // 处理包含, ¥
// 或者$的金额
int index = currency.indexOf(".");
int length = currency.length();
Long amLong = 0l;
if (index == -1) {
amLong = Long.valueOf(currency + "00");
} else if (length - index >= 3) {
amLong = Long.valueOf((currency.substring(0, index + 3)).replace(".", ""));
} else if (length - index == 2) {
amLong = Long.valueOf((currency.substring(0, index + 2)).replace(".", "") + 0);
} else {
amLong = Long.valueOf((currency.substring(0, index + 1)).replace(".", "") + "00");
}
return amLong.toString();
}
}
<%@ page language=“java” import=“java.util.*” pageEncoding=“ISO-8859-1”%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"?/"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>