对接微信支付通道接口

对接微信支付通道接口

Scroll Down

微信的支付通道调起来比支付宝麻烦多了
首先引入工具包再把证书放到项目文件夹下
再把证书放到项目resources目录下的cert文件夹下
设置微信的配置参数
appid,appsecret,MchID,key这几个参数要注意,很容易出错,签名错误问题卡了好久
补一个获取秘钥的工具类

public class OurWxPayConfig implements WXPayConfig {
13 
14     /** 加载证书  这里证书需要到微信商户平台进行下载*/
15     private byte [] certData;
16 
17     public OurWxPayConfig() throws  Exception{
18         InputStream certStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("cert/wxpay/apiclient_cert.p12");
19         this.certData = IOUtils.toByteArray(certStream);
20         certStream.close();
21     }
22 
23     /** 设置我们自己的appid
24      * 商户号
25      * 秘钥
26      * */
27 
28     @Override
29     public String getAppID() {
30         return "wx7494893843843c";
31     }
32 
33     @Override
34     public String getMchID() {
35         return "4672984344";
36     }
37 
38     @Override
39     public String getKey() {
40         return "qbH5l4N3468798dfgK";
41     }
42 
43     @Override
44     public InputStream getCertStream() {
45         return new ByteArrayInputStream(this.certData);
46     }
47 
48     @Override
49     public int getHttpConnectTimeoutMs() {
50         return 0;
51     }
52 
53     @Override
54     public int getHttpReadTimeoutMs() {
55         return 0;
56     }
57 }

Native网页支付,注意签名顺序

  public String wxpayment (HttpServletRequest httpRequest,
    HttpServletResponse httpResponse,Model model) throws Exception{
        String notifyUrl = "";  //回调地址需要换成处理业务的回调接口
        String out_trade_no = httpRequest.getParameter("out_trade_no");//订单编号
        long total_fee = httpRequest.getParameter("total_amount");
        OurWxPayConfig ourWxPayConfig = new OurWxPayConfig();
        WXPay wxPay = new WXPay(ourWxPayConfig);
        //根据微信支付api来设置
        Map<String,String> data = new LinkedHashMap<String, String>();
        data.put("appid",ourWxPayConfig.getAppID());
        data.put("body","商品订单");
        data.put("mch_id",ourWxPayConfig.getMchID());         //商户号
        data.put("nonce_str",WXPayUtil.generateNonceStr());   // 随机字符串小于32位
        data.put("notify_url",notifyUrl);                     //回调地址
        /** wxPay.unifiedOrder 这个方法中调用微信统一下单接口 */
        data.put("out_trade_no",out_trade_no);   //交易号
        data.put("spbill_create_ip","10.200.25.59");             //终端ip
        data.put("sign_type","MD5");  						 //加密类型
        data.put("trade_type","NATIVE");                         //支付场景 APP 微信app支付 JSAPI 公众号支付  NATIVE 扫码支付
        data.put("total_fee",total_fee+"");       //订单总金额单位分
        String s = WXPayUtil.generateSignature(data, ourWxPayConfig.getKey());  //签名
        data.put("sign",s);
        //logger.info("请求map:"+data.toString());
        Map<String, String> respData = wxPay.unifiedOrder(data); 
        //logger.info("返回map:"+respData.toString());
        System.out.println("返回map:"+respData.toString());
        if (respData.get("return_code").equals("SUCCESS")){ 	
        	Map<String, String> map = new HashMap<String, String>();
	        String codeUrl = respData.get("code_url");   //获取二维码链接
	        map.put("retCode","0000");
	        map.put("retMsg","订单生成成功");
	        map.put("codeUrl", codeUrl);
	        map.put("out_trade_no", out_trade_no);
	        map.put("ordertype", ordertype+"");	
        	}
        }
        throw new Exception(respData.get("return_msg"));
     }

APP支付

public @ResponseBody String wxApppay(HttpServletRequest httpRequest,
            HttpServletResponse httpResponse) throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
        String notifyUrl = "http://testwww.appPayNotify";  //回调地址换成处理业务的回调接口
        OurWxPayConfig ourWxPayConfig = new OurWxPayConfig();
        WXPay wxPay = new WXPay(ourWxPayConfig);
        long total_fee = StringUtil.parseAmountStr2Long(httpRequest.getParameter("total_amount"));
        //根据微信支付api来设置
        Map<String,String> data = new LinkedHashMap<String, String>();
        data.put("appid",ourWxPayConfig.getAppID());
        data.put("body","商品订单");//商品描述
        data.put("mch_id",ourWxPayConfig.getMchID());         //商户号
        data.put("nonce_str",WXPayUtil.generateNonceStr());   // 随机字符串小于32位
        data.put("notify_url",notifyUrl);                     //回调地址
        /** wxPay.unifiedOrder 这个方法中调用微信统一下单接口 */
        data.put("out_trade_no",httpRequest.getParameter("out_trade_no"));   //交易号
        data.put("spbill_create_ip","10.200.25.59");             //终端ip
        data.put("sign_type","MD5");  						 //加密类型
        data.put("trade_type","APP");                         //支付场景 APP 微信app支付 JSAPI 公众号支付  NATIVE 扫码支付
        data.put("total_fee",total_fee+"");       //订单总金额单位分
        String s = WXPayUtil.generateSignature(data, ourWxPayConfig.getKey());  //签名
        data.put("sign",s);
        Map<String, String> respData = wxPay.unifiedOrder(data); 
        logger.info("返回map:"+respData.toString());
     }

支付回调接口

public  void  paymentNotify(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		logger.info("微信车贴订单支付异步回调");
		//读取参数
		InputStream inputStream ;
		StringBuffer sb = new StringBuffer();
		inputStream = request.getInputStream();
		String s ;
		BufferedReader in = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
		while ((s = in.readLine()) != null){
			sb.append(s);
		}
		in.close();
		inputStream.close();
		//解析xml成map
		Map<String, String> m = XMLUtil.readStringXmlOut(sb.toString());;
		//支付成功
		if("SUCCESS".equals(m.get("result_code"))){
			String carorderId = m.get("out_trade_no");
			//修改支付记录支付状态
		}
	}