mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-02-18 22:26:22 +08:00
#319 增加“退款结果通知“处理方法,并优化调整微信支付相关代码
This commit is contained in:
@@ -2,6 +2,8 @@ package com.github.binarywang.wxpay.service;
|
||||
|
||||
import com.github.binarywang.wxpay.bean.WxPayApiData;
|
||||
import com.github.binarywang.wxpay.bean.coupon.*;
|
||||
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
|
||||
import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyResult;
|
||||
import com.github.binarywang.wxpay.bean.request.*;
|
||||
import com.github.binarywang.wxpay.bean.result.*;
|
||||
import com.github.binarywang.wxpay.config.WxPayConfig;
|
||||
@@ -22,7 +24,7 @@ public interface WxPayService {
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 查询订单(详见https://com.github.binarywang.wechat.pay.bean.pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_2)
|
||||
* 查询订单(详见https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_2)
|
||||
* 该接口提供所有微信支付订单的查询,商户可以通过查询订单接口主动查询订单状态,完成下一步的业务逻辑。
|
||||
* 需要调用查询接口的情况:
|
||||
* ◆ 当商户后台、网络、服务器等出现异常,商户系统最终未接收到支付通知;
|
||||
@@ -54,7 +56,7 @@ public interface WxPayService {
|
||||
WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxPayException;
|
||||
|
||||
/**
|
||||
* 统一下单(详见http://com.github.binarywang.wechat.pay.bean.pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1)
|
||||
* 统一下单(详见https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1)
|
||||
* 在发起微信支付前,需要调用统一下单接口,获取"预支付交易会话标识"
|
||||
* 接口地址:https://api.mch.weixin.qq.com/pay/unifiedorder
|
||||
*
|
||||
@@ -64,7 +66,7 @@ public interface WxPayService {
|
||||
|
||||
/**
|
||||
* 该接口调用“统一下单”接口,并拼装发起支付请求需要的参数
|
||||
* 详见http://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421141115&token=&lang=zh_CN
|
||||
* 详见https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_5
|
||||
*
|
||||
* @param request 请求对象,注意一些参数如appid、mchid等不用设置,方法内会自动从配置对象中获取到(前提是对应配置中已经设置)
|
||||
*/
|
||||
@@ -113,10 +115,23 @@ public interface WxPayService {
|
||||
throws WxPayException;
|
||||
|
||||
/**
|
||||
* 读取支付结果通知
|
||||
* @deprecated use WxPayService#parseOrderNotifyResult(String) instead
|
||||
* @see WxPayService#parseOrderNotifyResult(String)
|
||||
*/
|
||||
@Deprecated
|
||||
WxPayOrderNotifyResult getOrderNotifyResult(String xmlData) throws WxPayException;
|
||||
|
||||
/**
|
||||
* 解析支付结果通知
|
||||
* 详见https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7
|
||||
*/
|
||||
WxPayOrderNotifyResult getOrderNotifyResult(String xmlData) throws WxPayException;
|
||||
WxPayOrderNotifyResult parseOrderNotifyResult(String xmlData) throws WxPayException;
|
||||
|
||||
/**
|
||||
* 解析退款结果通知
|
||||
* 详见https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_16&index=9
|
||||
*/
|
||||
WxPayRefundNotifyResult parseRefundNotifyResult(String xmlData) throws WxPayException;
|
||||
|
||||
/**
|
||||
* 发送微信红包给个人用户
|
||||
|
||||
@@ -3,6 +3,11 @@ package com.github.binarywang.wxpay.service.impl;
|
||||
import com.github.binarywang.utils.qrcode.QrcodeUtils;
|
||||
import com.github.binarywang.wxpay.bean.WxPayApiData;
|
||||
import com.github.binarywang.wxpay.bean.coupon.*;
|
||||
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
|
||||
import com.github.binarywang.wxpay.bean.notify.WxPayRefundNotifyResult;
|
||||
import com.github.binarywang.wxpay.bean.order.WxPayAppOrderResult;
|
||||
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
|
||||
import com.github.binarywang.wxpay.bean.order.WxPayNativeOrderResult;
|
||||
import com.github.binarywang.wxpay.bean.request.*;
|
||||
import com.github.binarywang.wxpay.bean.result.*;
|
||||
import com.github.binarywang.wxpay.config.WxPayConfig;
|
||||
@@ -97,11 +102,17 @@ public abstract class WxPayServiceAbstractImpl implements WxPayService {
|
||||
}
|
||||
|
||||
@Override
|
||||
@Deprecated
|
||||
public WxPayOrderNotifyResult getOrderNotifyResult(String xmlData) throws WxPayException {
|
||||
return this.parseOrderNotifyResult(xmlData);
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxPayOrderNotifyResult parseOrderNotifyResult(String xmlData) throws WxPayException {
|
||||
try {
|
||||
log.debug("微信支付回调参数详细:{}", xmlData);
|
||||
log.debug("微信支付异步通知请求参数:{}", xmlData);
|
||||
WxPayOrderNotifyResult result = WxPayOrderNotifyResult.fromXML(xmlData);
|
||||
log.debug("微信支付回调结果对象:{}", result);
|
||||
log.debug("微信支付异步通知请求解析后的对象:{}", result);
|
||||
result.checkResult(this);
|
||||
return result;
|
||||
} catch (WxPayException e) {
|
||||
@@ -113,6 +124,19 @@ public abstract class WxPayServiceAbstractImpl implements WxPayService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxPayRefundNotifyResult parseRefundNotifyResult(String xmlData) throws WxPayException {
|
||||
try {
|
||||
log.debug("微信支付退款异步通知参数:{}", xmlData);
|
||||
WxPayRefundNotifyResult result = WxPayRefundNotifyResult.fromXML(xmlData, this.getConfig().getMchKey());
|
||||
log.debug("微信支付退款异步通知解析后的对象:{}", result);
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
throw new WxPayException("发生异常," + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxPaySendRedpackResult sendRedpack(WxPaySendRedpackRequest request) throws WxPayException {
|
||||
request.checkAndSign(this.getConfig());
|
||||
@@ -181,6 +205,65 @@ public abstract class WxPayServiceAbstractImpl implements WxPayService {
|
||||
return result;
|
||||
}
|
||||
|
||||
public <T> T createOrder(WxPayUnifiedOrderRequest request) throws WxPayException {
|
||||
WxPayUnifiedOrderResult unifiedOrderResult = this.unifiedOrder(request);
|
||||
String prepayId = unifiedOrderResult.getPrepayId();
|
||||
if (StringUtils.isBlank(prepayId)) {
|
||||
throw new RuntimeException(String.format("无法获取prepay id,错误代码: '%s',信息:%s。",
|
||||
unifiedOrderResult.getErrCode(), unifiedOrderResult.getErrCodeDes()));
|
||||
}
|
||||
|
||||
String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
|
||||
String nonceStr = String.valueOf(System.currentTimeMillis());
|
||||
Object payResult = null;
|
||||
switch (request.getTradeType()) {
|
||||
case TradeType.NATIVE: {
|
||||
payResult = WxPayNativeOrderResult.newBuilder().codeUrl(unifiedOrderResult.getCodeURL())
|
||||
.build();
|
||||
break;
|
||||
}
|
||||
case TradeType.APP: {
|
||||
// APP支付绑定的是微信开放平台上的账号,APPID为开放平台上绑定APP后发放的参数
|
||||
String appId = this.getConfig().getAppId();
|
||||
Map<String, String> configMap = new HashMap<>();
|
||||
// 此map用于参与调起sdk支付的二次签名,格式全小写,timestamp只能是10位,格式固定,切勿修改
|
||||
String partnerId = getConfig().getMchId();
|
||||
configMap.put("prepayid", prepayId);
|
||||
configMap.put("partnerid", partnerId);
|
||||
String packageValue = "Sign=WXPay";
|
||||
configMap.put("package", packageValue);
|
||||
configMap.put("timestamp", timestamp);
|
||||
configMap.put("noncestr", nonceStr);
|
||||
configMap.put("appid", appId);
|
||||
|
||||
payResult = WxPayAppOrderResult.newBuilder()
|
||||
.sign(SignUtils.createSign(configMap, this.getConfig().getMchKey()))
|
||||
.prepayId(prepayId)
|
||||
.partnerId(partnerId)
|
||||
.appId(appId)
|
||||
.packageValue(packageValue)
|
||||
.timeStamp(timestamp)
|
||||
.nonceStr(nonceStr)
|
||||
.build();
|
||||
break;
|
||||
}
|
||||
case TradeType.JSAPI: {
|
||||
payResult = WxPayMpOrderResult.newBuilder()
|
||||
.appId(unifiedOrderResult.getAppid())
|
||||
.timeStamp(timestamp)
|
||||
.nonceStr(nonceStr)
|
||||
.packageValue("prepay_id=" + prepayId)
|
||||
.signType(SignType.MD5)
|
||||
.build();
|
||||
((WxPayMpOrderResult) payResult)
|
||||
.setPaySign(SignUtils.createSign(payResult, this.getConfig().getMchKey()));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return (T) payResult;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request) throws WxPayException {
|
||||
request.checkAndSign(this.getConfig());
|
||||
@@ -508,7 +591,7 @@ public abstract class WxPayServiceAbstractImpl implements WxPayService {
|
||||
public WxPayApiData getWxApiData() {
|
||||
try {
|
||||
return wxApiData.get();
|
||||
}finally {
|
||||
} finally {
|
||||
//一般来说,接口请求会在一个线程内进行,这种情况下,每个线程get的会是之前所存入的数据,
|
||||
// 但以防万一有同一线程多次请求的问题,所以每次获取完数据后移除对应数据
|
||||
wxApiData.remove();
|
||||
|
||||
Reference in New Issue
Block a user