温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java开发微信Navicat支付的示例分析

发布时间:2021-09-08 11:29:18 来源:亿速云 阅读:90 作者:小新 栏目:编程语言

这篇文章主要为大家展示了“Java开发微信Navicat支付的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Java开发微信Navicat支付的示例分析”这篇文章吧。

一:准备工作

1:先去微信公众平台注册一个公众号,选择服务号

2:去微信商户平台注册一个商户号,用于收款

3:在商户号中配置对应的公众号的APPID

4:支付结果异步通知(需要重点注意)

注意:请先详细查看官方文档按步骤开发,一切以官方文档为主 微信Navicat支付官方文档

5:测试的时候一定要使用内网穿透软件,否则回调时会报错

 二:开发代码

WeChatPayConfig:

public class WeChatPayConfig {
  //公众号APPID
  private String APPID = "";
 
  //商户号KEY
  private String KEY = "";
 
  //商户号ID
  private String MCHID = "";
 
  //支付完成后微信回调地址,需要外网能访问要是域名,不能是127.0.0.1跟localhost
  private String NOTIFY_URL = "";
}

WeChatPayServcie:

public interface WeChatPayServcie {
 
  //微信支付下单
  public Map<String,String> getWxpayUrl(Map<String, String> sourceMap);
 
  //订单查询
  public String orderQuery(String out_trade_no);
}
@Service
public class WeChatPayServiceImpl implements WeChatPayServcie {
  
  /**
   * 微信支付请求
   * @param sourceMap
   * @return
   */
  public Map<String,String> getWxpayUrl(Map<String, String> sourceMap) {
    SortedMap<String, Object> signParams = new TreeMap<String, Object>();
    String nonce_str = UUID.randomUUID().toString().trim().replaceAll("-", "");
    signParams.put("appid", PayConfig.APPID);
    signParams.put("mch_id",PayConfig.MCHID);
    signParams.put("nonce_str",sourceMap.get("nonce_str"));
    signParams.put("product_id",sourceMap.get("prod_id"));
    signParams.put("body",sourceMap.get("body"));
    signParams.put("out_trade_no",sourceMap.get("out_trade_no"));
    signParams.put("total_fee",sourceMap.get("total_fee"));
    signParams.put("spbill_create_ip", WxUtil.getIp());
    signParams.put("notify_url",PayConfig.NOTYFLY_URL);
    signParams.put("trade_type","NATIVE");
    String sign = WxUtil.createSign(signParams,PayConfig.KEY);
    signParams.put("sign",sign);
    String xmlPackage = WxUtil.parseMapXML(signParams);
    Map<String, Object> resultMap = new HashMap();
    try {
      String result = HttpUtil.post("https://api.mch.weixin.qq.com/pay/unifiedorder",xmlPackage);
      resultMap = WxUtil.parseXmlMap(result);
    } catch (Exception e) {
      e.printStackTrace();
    }
    String returnCode = (String) resultMap.get("return_code");
    String returnMsg = (String) resultMap.get("return_msg");
    if (returnCode.equalsIgnoreCase("FAIL")) {
      throw new RuntimeException(returnMsg);
    }
    String result_code = (String) resultMap.get("result_code");
    if (result_code.equalsIgnoreCase("FAIL")) {
      throw new RuntimeException(resultMap.get("err_code_des"));
    }
    String code_url = (String) resultMap.get("code_url");
    Map<String,String> map = new HashMap<>();
    map.put("code_url",code_url);
    map.put("out_trade_no",sourceMap.get("out_trade_no"));
    return map;
  }
 
 
  /**
   * 微信支付订单查询
   * @param out_trade_no
   * @return
   */
  public String orderQuery(String out_trade_no) {
    String nonce_str = UUID.randomUUID().toString().trim().replaceAll("-", "");
    SortedMap<String, Object> signParams = new TreeMap<>();
    signParams.put("appid", payConfig.getWeChatPorpetties().getAppId());
    signParams.put("mch_id",payConfig.getWeChatPorpetties().getMchId());
    signParams.put("out_trade_no",out_trade_no);
    signParams.put("nonce_str",nonce_str);
    String sign = WxUtil.createSign(signParams,payConfig.getWeChatPorpetties().getApiKey());
    signParams.put("sign",sign);
    String xmlPackage = WxUtil.parseMapXML(signParams);
    Map<String, Object> resultMap = new HashMap();
    try {
      String result = HttpUtil.post("https://api.mch.weixin.qq.com/pay/orderquery",xmlPackage);
      resultMap = WxUtil.parseXmlMap(result);
    } catch (Exception e) {
      e.printStackTrace();
      throw new RuntimeException("渠道网络异常");
    }
    String returnCode = (String) resultMap.get("return_code");
    String returnMsg = (String) resultMap.get("return_msg");
    if (returnCode.equalsIgnoreCase(PayContants.FAIL)) {
      throw new RuntimeException(returnMsg);
    }
    String result_code = (String) resultMap.get("result_code");
    if (result_code.equalsIgnoreCase(PayContants.FAIL)) {
      throw new RuntimeException(resultMap.get("err_code_des"));
    }
    String trade_state = (String) resultMap.get("trade_state");
    return trade_state;
  }
}

WeChatPayController:

/**
 * 微信支付接口
 */
@Controller
public class WxPayController {
 
  @Autowired
  WeChatPayServcie weChatPayServcie;
 
  /**
  * 微信支付下单
  * payID可以不用传自己生成32位以内的随机数就好,要保证不重复
  * totalFee金额一定要做判断,判断这笔支付金额与数据库是否一致
  */
  @RequestMapping("pay")
  @ResponseBody
  public Map<String ,String> toWxpay(HttpServletRequest httpRequest,String payId, String totalFee, String body){
    System.out.println("开始微信支付...");
    Map<String, String> map = new HashMap<>();
    String nonce_str = UUID.randomUUID().toString().trim().replaceAll("-", "");
 
    //生成一笔支付记录
 
 
    //拼接支付参数
    Map<String, String> paramMap = new HashMap<>();
    paramMap.put("nonce_str", nonce_str);
    paramMap.put("prod_id", payId);
    paramMap.put("body", body);
    paramMap.put("total_fee", totalFee);
    paramMap.put("out_trade_no", payId);
 
    //发送支付请求并获取返回参数与二维码,用于支付与调用查询接口
    Map<String, String> returnMap = weChatPayServcie.getWxpayUrl(paramMap);
    httpRequest.setAttribute("out_trade_no", payId);
    httpRequest.setAttribute("total_fee", totalFee);
    
    //code_url就是二维码URL,可以把这个URL放到草料二维码中生成微信二维码
    httpRequest.setAttribute("code_url", returnMap.get("code_url"));
 
    map.put("out_trade_no", payId);
    map.put("total_fee", String.valueOf(bigDecimal));
    map.put("code_url", returnMap.get("code_url"));
    return map;
  }
 
  /**
  * 查询微信订单
  * ot_trade_no是支付
  */
  @RequestMapping("query")
  @ResponseBody
  public Root<String> orderQuery(String out_trade_no){
    return weixinPayServcie.orderQuery(queryCallbackForm.getOut_trade_no());
  }
 
  /**
  * 微信支付回调,这个地址就是Config中的NOTYFLY_URL
  */
  @RequestMapping("callback")
  public void notify_url(HttpServletRequest request, HttpServletResponse response) throws DocumentException, ServletException, IOException,Throwable {
    System.out.print("微信支付回调获取数据开始...");
    String inputLine;
    String notityXml = "";
    try {
      while ((inputLine = request.getReader().readLine()) != null) {
        notityXml += inputLine;
      }
      request.getReader().close();
    } catch (Exception e) {
      WxUtil.sendXmlMessage(request,response, PayContants.FAIL);
      throw new RuntimeException("回调数据xml获取失败!");
    }
    if(StringUtils.isEmpty(notityXml)){
      WxUtil.sendXmlMessage(request,response, PayContants.FAIL);
      throw new RuntimeException("回调数据xml为空!");
    }
    Map<String ,Object> resultMap = XMLParse.parseXmlMap(notityXml);
    String returnCode = (String) resultMap.get("return_code");
    String returnMsg = (String) resultMap.get("return_msg");
    if (returnCode.equalsIgnoreCase(PayContants.FAIL)) {
      WxUtil.sendXmlMessage(request,response, PayContants.FAIL);
      throw new RuntimeException(returnMsg);
    }
    String resultCode = (String) resultMap.get("result_code");
    if (resultCode.equalsIgnoreCase(PayContants.FAIL)) {
      WxUtil.sendXmlMessage(request,response, PayContants.FAIL);
      throw new RuntimeException(resultMap.get("err_code_des"));
    }
 
    SortedMap<String ,Object> paramMap = new TreeMap<>();
    paramMap.put("appid",resultMap.get("appid"));
    paramMap.put("mch_id",resultMap.get("mch_id"));
    paramMap.put("nonce_str",resultMap.get("nonce_str"));
    paramMap.put("body",resultMap.get("body"));
    paramMap.put("openid", resultMap.get("openid"));
    paramMap.put("is_subscribe",resultMap.get("is_subscribe"));
    paramMap.put("trade_type",resultMap.get("trade_type"));
    paramMap.put("bank_type",resultMap.get("bank_type"));
    paramMap.put("total_fee",resultMap.get("total_fee"));
    paramMap.put("fee_type",resultMap.get("fee_type"));
    paramMap.put("cash_fee",resultMap.get("cash_fee"));
    paramMap.put("transaction_id",resultMap.get("transaction_id"));
    paramMap.put("out_trade_no",resultMap.get("out_trade_no"));
    paramMap.put("time_end",resultMap.get("time_end"));
    paramMap.put("return_code",resultMap.get("return_code"));
    paramMap.put("return_msg",resultMap.get("return_msg"));
    paramMap.put("result_code",resultMap.get("result_code"));
 
    String out_trade_no = (String) resultMap.get("out_trade_no");
    String sign = SignUtil.createSign(paramMap,WxPayConfig.KEY);
    String mySign =(String) resultMap.get("sign");
 
    //回调一定要验证签名以防数据被篡改
    if(sign.equals(mySign)){
      System.out.println("回调签名验证成功!");
      
        //修改业务逻辑,将那笔支付状态改为已支付
      }
      WxUtil.sendXmlMessage(request,response, PayContants.SUCCESS);
    }else{
      WxUtil.sendXmlMessage(request,response, PayContants.FAIL);
      throw new RuntimeException("签名不一致!");
    }
  }
 
}

 WxUtil:

package com.ys.commons.utils.pay;
 
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
//微信工具类
public class WxUtil {
 
  //获取当前IP
  public static String getIp() {
    try {
      String spbill_create_ip = InetAddress.getLocalHost().getHostAddress();
      return spbill_create_ip;
    } catch (UnknownHostException var2) {
      var2.printStackTrace();
      return "获取IP失败...";
    }
  }
 
  //输出xml格式
  public static void sendXmlMessage(HttpServletRequest request, HttpServletResponse response, String content) {
    try {
      String contentXml = "<xml><return_code><![CDATA[" + content + "]]></return_code></xml>";
      OutputStream os = response.getOutputStream();
      BufferedWriter resBr = new BufferedWriter(new OutputStreamWriter(os));
      resBr.write(contentXml);
      resBr.flush();
      resBr.close();
    } catch (IOException var6) {
      var6.printStackTrace();
    }
 
  }
  
  //生成sign签名
  public static String createSign(SortedMap<String, Object> packageParams, String KEY) {
    StringBuffer sb = new StringBuffer();
    Set<Entry<String, Object>> es = packageParams.entrySet();
    Iterator it = es.iterator();
 
    while(it.hasNext()) {
      Entry<String, Object> entry = (Entry)it.next();
      String k = (String)entry.getKey();
      String v = (String)entry.getValue();
      if(null != v && !"".equals(v) && !"sign".equals(k) && !"key".equals(k)) {
        sb.append(k + "=" + v + "&");
      }
    }
 
    sb.append("key=" + KEY);
    String sign = MD5Util.MD5Encode(sb.toString(), "UTF-8").toUpperCase();
    return sign;
  }
 
  //将map转为xml
  public static String parseMapXML(SortedMap<String, Object> map) {
    String xmlResult = "";
    StringBuffer sb = new StringBuffer();
    sb.append("<xml>");
    Iterator var3 = map.keySet().iterator();
 
    while(var3.hasNext()) {
      String key = (String)var3.next();
      String value = "<![CDATA[" + map.get(key) + "]]>";
      sb.append("<" + key + ">" + value + "</" + key + ">");
      System.out.println();
    }
 
    sb.append("</xml>");
    xmlResult = sb.toString();
    return xmlResult;
  }
 
  //将xml转为map
  public static Map<String, Object> parseXmlMap(String xml) throws DocumentException {
    Document document = DocumentHelper.parseText(xml);
    Element root = document.getRootElement();
    List<Element> elementList = root.elements();
    Map<String, Object> map = new HashMap();
    Iterator var5 = elementList.iterator();
 
    while(var5.hasNext()) {
      Element e = (Element)var5.next();
      map.put(e.getName(), e.getText());
    }
 
    return map;
  }
}

发送请求需要推荐一个非常好用的工具,里面各种常用的工具都封装好了hutool,如果想直接复制代码使用也需要引入此工具的maven库

以上是“Java开发微信Navicat支付的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI