- 导入maven依赖
<!--微信支付--> <dependency> <groupId>com.github.wxpay</groupId> <artifactId>wxpay-sdk</artifactId> <version>0.0.3</version> </dependency>
- 微信支付参数配置
-
import com.github.wxpay.sdk.WXPayConfig; import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; /** * 微信支付配置(单例) */ public class WXConfigUtil implements WXPayConfig { private byte[] certData; private static WXConfigUtil INSTANCE; public static final String APP_ID = "*****";//应用AppID public static final String KEY = "******";//商户密钥 public static final String MCH_ID = "******";//商户号 public WXConfigUtil() throws Exception { String certPath = WXConfigUtil.class.getClassLoader().getResource("").getPath();//从微信商户平台下载的安全证书存放的路径 File file = new File(certPath+ "apiclient_cert.p12"); InputStream certStream = new FileInputStream(file); this.certData = new byte[(int) file.length()]; certStream.read(this.certData); certStream.close(); } //双重检查加锁 public static WXConfigUtil getInstance() throws Exception { if (INSTANCE == null) { synchronized (WXConfigUtil.class) { if (INSTANCE == null) { INSTANCE = new WXConfigUtil(); } } } return INSTANCE; } @Override public String getAppID() { return APP_ID; } //parnerid,商户号 @Override public String getMchID() { return MCH_ID; } @Override public String getKey() { return KEY; } @Override public InputStream getCertStream() { ByteArrayInputStream certBis = new ByteArrayInputStream(this.certData); return certBis; } @Override public int getHttpConnectTimeoutMs() { return 8000; } @Override public int getHttpReadTimeoutMs() { return 10000; } }
- 业务层统一下单以及异步通知后的XML数据处理
private HashMap<String, String> wxpay(PayDto payDto,HttpServletRequest request) throws Exception { String request_ip = IpUtils.getIpAddr(request); Map<String, String> data = new HashMap<>(); WxPayConfig wxPayConfig =new WxPayConfig(); WXPay wxpay = new WXPay(wxPayConfig); data.put("appid", wxPayConfig.getAppID()); data.put("mch_id", wxPayConfig.getMchID()); data.put("body", payDto.getBody()); data.put("out_trade_no", payDto.getOrderSn()); data.put("fee_type", "CNY"); data.put("total_fee", String.valueOf(payDto.getTotalFee().multiply(new BigDecimal(100)).intValue())); data.put("spbill_create_ip", request_ip); data.put("notify_url", WxPayConfig.notifyUrl); data.put("trade_type", "APP"); data.put("nonce_str", WXPayUtil.generateNonceStr()); String sign = WXPayUtil.generateSignature(data, wxPayConfig.getKey()); data.put("sign", sign); Map<String, String> resp = wxpay.unifiedOrder(data); HashMap hashMap =new HashMap(); if ("SUCCESS".equals(resp.get("return_code"))) { Map<String, String> reData = new HashMap<>(); //主要返回以下5个参数(必须按照顺序,否则APP报错:-1) reData.put("appid", wxPayConfig.getAppID()); reData.put("noncestr", resp.get("nonce_str")); reData.put("package", "Sign=WXPay"); reData.put("partnerid", wxPayConfig.getMchID()); reData.put("prepayid", resp.get("prepay_id")); reData.put("timestamp", String.valueOf(System.currentTimeMillis() / 1000)); String newSign = WXPayUtil.generateSignature(reData, wxPayConfig.getKey());//二次签名 hashMap.put("appid", resp.get("appid")); hashMap.put("noncestr", resp.get("nonce_str")); hashMap.put("prepayid", resp.get("prepay_id")); hashMap.put("partnerid", wxPayConfig.getMchID()); hashMap.put("sign", newSign); hashMap.put("package", "Sign=WXPay"); hashMap.put("timestamp", reData.get("timestamp")); return hashMap; } }
public void wxNotify(HttpServletRequest request, HttpServletResponse response) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream())); String line = null; StringBuilder sb = new StringBuilder(); while ((line = br.readLine()) != null) { sb.append(line); } br.close(); String notityXml = sb.toString(); String resXml = ""; Map map = PayUtil.doXMLParse(notityXml); String returnCode = (String) map.get("return_code"); if ("SUCCESS".equals(returnCode)) { //验证签名是否正确 Map<String, String> validParams = PayUtil.paraFilter(map); String prestr = PayUtil.createLinkString(validParams); String orderSn = (String) map.get("out_trade_no"); String mch_id = (String) map.get("mch_id"); String appid = (String) map.get("appid"); String transactionId = (String) map.get("transaction_id"); //注意要判断微信支付重复回调,支付成功后微信会重复的进行回调 //通知微信服务器已经支付成功 resXml = "<xml>" + "<return_code><![CDATA[SUCCESS]]></return_code>" + "<return_msg><![CDATA[OK]]></return_msg>" + "</xml> "; } else { resXml = "<xml>" + "<return_code><![CDATA[FAIL]]></return_code>" + "<return_msg><![CDATA[报文为空]]></return_msg>" + "</xml> "; } } } } BufferedOutputStream out = new BufferedOutputStream( response.getOutputStream()); out.write(resXml.getBytes()); out.flush(); out.close(); }
07-21
6067