接口链接:https://github.com/binarywang
使用步骤:
/**
* 获得请求地址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;
}
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;
}
/**
* 调用统一下单接口,并组装生成支付所需参数对象.
*
* @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);
WxPayOrderNotifyResult parseOrderNotifyResult(String xmlData) throws WxPayException;