zoukankan      html  css  js  c++  java
  • Java微信支付开发之查询订单

    官方文档

    该接口提供所有微信支付订单的查询,商户可以通过查询订单接口主动查询订单状态,完成下一步的业务逻辑。

    一、应用场景

    • 当商户后台、网络、服务器等出现异常,商户系统最终未接收到支付通知
    •  调用支付接口后,返回系统错误或未知交易状态情况
    •  调用刷卡支付API,返回USERPAYING的状态
    •  调用关单或撤销接口API之前,需确认支付状态

    二、接口地址

    https://api.mch.weixin.qq.com/pay/orderquery

    三、请求参数

    可以根据微信订单号(优先使用)或者商户订单号进行查询
    package com.phil.wechatpay.model.rep;
    
    import java.io.Serializable;
    
    /**
     * 查询订单请求参数(正常XML)
     * @author phil
     * @date  2017年7月25日
     *
     */
    public class OrderQueryParams extends AbstractPayParams implements Serializable{
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = -168458096490563992L;
    	
    	private String transaction_id; //微信订单号,优先
    	
    	private String out_trade_no; //商户订单号    二选一
    
    	public String getTransaction_id() {
    		return transaction_id;
    	}
    
    	public void setTransaction_id(String transaction_id) {
    		this.transaction_id = transaction_id;
    	}
    
    	public String getOut_trade_no() {
    		return out_trade_no;
    	}
    
    	public void setOut_trade_no(String out_trade_no) {
    		this.out_trade_no = out_trade_no;
    	}
    }

    四、返回结果

    自己酌情封装
    package com.phil.wechatpay.model.resp;
    
    import java.io.Serializable;
    
    import com.phil.common.annotation.NotRequire;
    
    /**
     * 查询订单返回参数(带<![CDATA[]]>XML格式)
     * 
     * @author phil
     * @date 2017年7月25日
     *
     */
    public class OrderQueryResult extends AbstractPayResult implements Serializable {
    
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = -1996103742747816922L;
    
    	private String return_code; // 返回状态码SUCCESS/FAIL
    
    	@NotRequire
    	private String err_code;// 错误返回的信息描述
    	@NotRequire
    	private String err_code_des;// 错误返回的信息描述
    	/**** return_code 、result_code、trade_state ***为SUCCESS ****/
    	
    	.....
    }

    五、查询订单

    package com.phil.wechatpay.controller;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import com.phil.common.config.WechatConfig;
    import com.phil.common.util.HttpReqUtil;
    import com.phil.common.util.PayUtil;
    import com.phil.common.util.SignatureUtil;
    import com.phil.common.util.XmlUtil;
    import com.phil.wechatpay.model.rep.OrderQueryParams;
    import com.phil.wechatpay.model.resp.OrderQueryResult;
    import com.phil.wechatpay.service.WechatPayService;
    
    /**
     * 查询订单
     * @author phil
     * @date  2017年7月25日
     *
     */
    @Controller
    @RequestMapping("/wxpay/")
    public class WechatPayOrderQueryController {
    	
    	@Autowired
    	private WechatPayService wechatPayService;
    	
    	@ResponseBody
    	@RequestMapping("orderQuery")
    	public OrderQueryResult orderQuery(HttpServletRequest request, HttpServletResponse response) throws Exception {
    		OrderQueryResult orderQueryResult = null;
    		OrderQueryParams orderQueryParams = new OrderQueryParams();
    		orderQueryParams.setAppid(WechatConfig.APP_ID);
    		orderQueryParams.setMch_id(WechatConfig.MCH_ID);
    		orderQueryParams.setNonce_str(PayUtil.createNonceStr());
    		orderQueryParams.setTransaction_id(""); //二者选其一,推荐transaction_id
    		//orderQueryParams.setOut_trade_no("");
    		//请求的xml
    		String orderQueryXml = wechatPayService.abstractPayToXml(orderQueryParams);//签名合并到service
    		// 返回<![CDATA[SUCCESS]]>格式的XML
    		String orderQueryResultXmL = HttpReqUtil.HttpsDefaultExecute(HttpReqUtil.POST_METHOD,WechatConfig.ORDER_QUERY_URL, null, orderQueryXml);
    		// 进行签名校验
    		if (SignatureUtil.checkIsSignValidFromWeiXin(orderQueryResultXmL)) {
    			orderQueryResult = XmlUtil.getObjectFromXML(orderQueryResultXmL, OrderQueryResult.class);
    		}
    		return orderQueryResult;
    	}
    }

    本文为Phil Jing原创文章,未经博主允许不得转载,如有问题请直接回复或者加群。
  • 相关阅读:
    mybatis typeAlias (别名)说明
    jsp路径
    Jutil 单元测试
    SpringMVC数据绑定
    EL表达式(Exprission language)
    JSTL (标准标签库)
    Introduce Null Object
    Replace Type Code With Class和Replace Type Code With Subclass和Replace Type Code With State/Strategy
    利用QBuffer和QLinkedList做数据块存储队列
    Duplicate Observed Data
  • 原文地址:https://www.cnblogs.com/phil_jing/p/15615873.html
Copyright © 2011-2022 走看看