当前位置: 首页 > 工具软件 > wxpay-sdk > 使用案例 >

微信支付学习笔记之调用com.github.binarywang.wxpay.service接口

商振
2023-12-01
接口链接:https://github.com/binarywang

使用步骤:

  1. 将HttpServletRequest转成 ip
            /**
    	 * 获得请求地址ip
    	 * @param httpservletrequest
    	 * @return
    	 */
    	public static String getClientIP(HttpServletRequest httpservletrequest) {
    	    if (httpservletrequest == null)
    	        return null;
    	    String s = httpservletrequest.getHeader("X-Forwarded-For");
    	    if (s == null || s.length() == 0 || "unknown".equalsIgnoreCase(s))
    	        s = httpservletrequest.getHeader("Proxy-Client-IP");
    	    if (s == null || s.length() == 0 || "unknown".equalsIgnoreCase(s))
    	        s = httpservletrequest.getHeader("WL-Proxy-Client-IP");
    	    if (s == null || s.length() == 0 || "unknown".equalsIgnoreCase(s))
    	        s = httpservletrequest.getHeader("HTTP_CLIENT_IP");
    	    if (s == null || s.length() == 0 || "unknown".equalsIgnoreCase(s))
    	        s = httpservletrequest.getHeader("HTTP_X_FORWARDED_FOR");
    	    if (s == null || s.length() == 0 || "unknown".equalsIgnoreCase(s))
    	        s = httpservletrequest.getRemoteAddr();
    	    return s;
    	}
  2. 封装微信统一下单接口,获取"预支付交易会话标识"返回的结果 WxPayUnifiedOrderResult
    
        public WxPayUnifiedOrderRequest toPayInfo(String clientIp) {
            WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
            request.setDeviceInfo("WEB");//设备号
            request.setBody(this.getProductName());//商品描述
            request.setOutTradeNo(this.getOrderNo());//商户订单号
            request.setDetail(this.getProductName());//商品详情
            request.setTotalFee(this.getPrice());//总金额|分计
            request.setSpbillCreateIp(clientIp);//终端IP
            request.setNotifyUrl(this.getNotifyUrl());//设置回调路径
            request.setTradeType("JSAPI");//交易类型
            request.setProductId(this.getProductId().toString());//商品id
            request.setOpenid(this.getOpenId());
            return request;
        }

     

  3. 调用WxPayService接口的createOrder
      /**
       * 调用统一下单接口,并组装生成支付所需参数对象.
       *
       * @param <T>     请使用{@link com.github.binarywang.wxpay.bean.order}包下的类
       * @param request 统一下单请求参数
       * @return 返回 {@link com.github.binarywang.wxpay.bean.order}包下的类对象
       * @throws WxPayException the wx pay exception
       */
      <T> T createOrder(WxPayUnifiedOrderRequest request) throws WxPayException;
    //将上面封装的request传进去,调用支付,返回结果
    WxPayMpOrderResult result = payService.createOrder(wxPayUnifiedOrderRequest);

     

  4. 将返回的结果进行处理返回给前端 
  5. 对回调函数进行处理 调用WxPayService 的 parseOrderNotifyResult方法
    WxPayOrderNotifyResult parseOrderNotifyResult(String xmlData) throws WxPayException;

     

     

 

 类似资料: