🆕 #2130 【微信支付】增加微信支付部分v3接口

This commit is contained in:
thinsstar
2021-05-21 22:07:02 +08:00
committed by GitHub
parent fa5d892176
commit efb2c52011
18 changed files with 3959 additions and 6 deletions

View File

@@ -0,0 +1,24 @@
package com.github.binarywang.wxpay.service;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderV3Request;
import com.github.binarywang.wxpay.exception.WxPayException;
/**
* <pre>
* 微信基础支付v3相关服务类.
* </pre>
*
* @author thinsstar
*/
public interface BasePayV3Service {
/**
* 调用统一下单接口,并组装生成支付所需参数对象.
*
* @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(WxPayUnifiedOrderV3Request request) throws WxPayException;
}

View File

@@ -2,11 +2,10 @@ 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.notify.WxScanPayNotifyResult;
import com.github.binarywang.wxpay.bean.notify.*;
import com.github.binarywang.wxpay.bean.request.*;
import com.github.binarywang.wxpay.bean.result.*;
import com.github.binarywang.wxpay.bean.result.enums.TradeTypeEnum;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.constant.WxPayConstants;
import com.github.binarywang.wxpay.exception.WxPayException;
@@ -291,6 +290,53 @@ public interface WxPayService {
*/
WxPayOrderQueryResult queryOrder(WxPayOrderQueryRequest request) throws WxPayException;
/**
* <pre>
* 查询订单
* 详见 https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_2.shtml
* 商户可以通过查询订单接口主动查询订单状态,完成下一步的业务逻辑。查询订单状态可通过微信支付订单号或商户订单号两种方式查询
* 注意:
* 查询订单可通过微信支付订单号和商户订单号两种方式查询,两种查询方式返回结果相同
* 需要调用查询接口的情况:
* ◆ 当商户后台、网络、服务器等出现异常,商户系统最终未接收到支付通知。
* ◆ 调用支付接口后,返回系统错误或未知交易状态情况。
* ◆ 调用付款码支付API返回USERPAYING的状态。
* ◆ 调用关单或撤销接口API之前需确认支付状态。
* 接口地址:
* https://api.mch.weixin.qq.com/v3/pay/transactions/id/{transaction_id}
* https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/{out_trade_no}
* </pre>
*
* @param transactionId 微信订单号
* @param outTradeNo 商户系统内部的订单号当没提供transactionId时需要传这个。
* @return the wx pay order query result
* @throws WxPayException the wx pay exception
*/
WxPayOrderQueryV3Result queryOrderV3(String transactionId, String outTradeNo) throws WxPayException;
/**
* <pre>
* 查询订单
* 详见 https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_2.shtml
* 商户可以通过查询订单接口主动查询订单状态,完成下一步的业务逻辑。查询订单状态可通过微信支付订单号或商户订单号两种方式查询
* 注意:
* 查询订单可通过微信支付订单号和商户订单号两种方式查询,两种查询方式返回结果相同
* 需要调用查询接口的情况:
* ◆ 当商户后台、网络、服务器等出现异常,商户系统最终未接收到支付通知。
* ◆ 调用支付接口后,返回系统错误或未知交易状态情况。
* ◆ 调用付款码支付API返回USERPAYING的状态。
* ◆ 调用关单或撤销接口API之前需确认支付状态。
* 接口地址:
* https://api.mch.weixin.qq.com/v3/pay/transactions/id/{transaction_id}
* https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/{out_trade_no}
* </pre>
*
* @param request 查询订单请求对象
* @return the wx pay order query result
* @throws WxPayException the wx pay exception
*/
WxPayOrderQueryV3Result queryOrderV3(WxPayOrderQueryV3Request request) throws WxPayException;
/**
* <pre>
* 关闭订单.
@@ -327,6 +373,40 @@ public interface WxPayService {
*/
WxPayOrderCloseResult closeOrder(WxPayOrderCloseRequest request) throws WxPayException;
/**
* <pre>
* 关闭订单
* 应用场景
* 以下情况需要调用关单接口:
* 1、商户订单支付失败需要生成新单号重新发起支付要对原订单号调用关单避免重复支付
* 2、系统下单后用户支付超时系统退出不再受理避免用户继续请调用关单接口。
* 注意关单没有时间限制建议在订单生成后间隔几分钟最短5分钟再调用关单接口避免出现订单状态同步不及时导致关单失败。
* 接口地址https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_3.shtml
* </pre>
*
* @param outTradeNo 商户系统内部的订单号
* @return the wx pay order close result
* @throws WxPayException the wx pay exception
*/
void closeOrderV3(String outTradeNo) throws WxPayException;
/**
* <pre>
* 关闭订单
* 应用场景
* 以下情况需要调用关单接口:
* 1、商户订单支付失败需要生成新单号重新发起支付要对原订单号调用关单避免重复支付
* 2、系统下单后用户支付超时系统退出不再受理避免用户继续请调用关单接口。
* 注意关单没有时间限制建议在订单生成后间隔几分钟最短5分钟再调用关单接口避免出现订单状态同步不及时导致关单失败。
* 接口地址https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_3.shtml
* </pre>
*
* @param request 关闭订单请求对象
* @return the wx pay order close result
* @throws WxPayException the wx pay exception
*/
void closeOrderV3(WxPayOrderCloseV3Request request) throws WxPayException;
/**
* 调用统一下单接口,并组装生成支付所需参数对象.
*
@@ -360,6 +440,25 @@ public interface WxPayService {
*/
WxPayUnifiedOrderResult unifiedOrder(WxPayUnifiedOrderRequest request) throws WxPayException;
/**
* 调用统一下单接口,并组装生成支付所需参数对象.
*
* @param <T> 请使用{@link com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderV3Result}里的内部类或字段
* @param request 统一下单请求参数
* @return 返回 {@link com.github.binarywang.wxpay.bean.result.WxPayUnifiedOrderV3Result}里的内部类或字段
* @throws WxPayException the wx pay exception
*/
<T> T createOrderV3(TradeTypeEnum tradeType, WxPayUnifiedOrderV3Request request) throws WxPayException;
/**
* 在发起微信支付前,需要调用统一下单接口,获取"预支付交易会话标识"
*
* @param request 请求对象注意一些参数如appid、mchid等不用设置方法内会自动从配置对象中获取到前提是对应配置中已经设置
* @return the wx pay unified order result
* @throws WxPayException the wx pay exception
*/
WxPayUnifiedOrderV3Result unifiedOrderV3(TradeTypeEnum tradeType, WxPayUnifiedOrderV3Request request) throws WxPayException;
/**
* 该接口调用“统一下单”接口,并拼装发起支付请求需要的参数.
* 详见https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_5
@@ -426,6 +525,33 @@ public interface WxPayService {
*/
WxPayRefundResult refundV2(WxPayRefundRequest request) throws WxPayException;
/**
* <pre>
* 申请退款API支持单品.
* 详见 https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_9.shtml
*
* 应用场景
* 当交易发生之后一年内,由于买家或者卖家的原因需要退款时,卖家可以通过退款接口将支付金额退还给买家,微信支付将在收到退款请求并且验证成功之后,将支付款按原路退还至买家账号上。
*
* 注意:
* 1、交易时间超过一年的订单无法提交退款
* 2、微信支付退款支持单笔交易分多次退款不超50次多次退款需要提交原支付订单的商户订单号和设置不同的退款单号。申请退款总金额不能超过订单金额。 一笔退款失败后重新提交,请不要更换退款单号,请使用原商户退款单号
* 3、错误或无效请求频率限制6qps即每秒钟异常或错误的退款申请请求不超过6次
* 4、每个支付订单的部分退款次数不能超过50次
* 5、如果同一个用户有多笔退款建议分不同批次进行退款避免并发退款导致退款失败
* 6、申请退款接口的返回仅代表业务的受理情况具体退款是否成功需要通过退款查询接口获取结果
* 7、一个月之前的订单申请退款频率限制为5000/min
*
* 接口地址
* https://api.mch.weixin.qq.com/v3/refund/domestic/refunds
* </pre>
*
* @param request 请求对象
* @return 退款操作结果 wx pay refund result
* @throws WxPayException the wx pay exception
*/
WxPayRefundV3Result refundV3(WxPayRefundV3Request request) throws WxPayException;
/**
* <pre>
* 微信支付-查询退款.
@@ -486,6 +612,36 @@ public interface WxPayService {
*/
WxPayRefundQueryResult refundQueryV2(WxPayRefundQueryRequest request) throws WxPayException;
/**
* <pre>
* 微信支付-查询退款
* 应用场景:
* 提交退款申请后通过调用该接口查询退款状态。退款有一定延时建议在提交退款申请后1分钟发起查询退款状态一般来说零钱支付的退款5分钟内到账银行卡支付的退款1-3个工作日到账。
* 详见 https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_10.shtml
* 接口链接https://api.mch.weixin.qq.com/v3/refund/domestic/refunds/{out_refund_no}
* </pre>
*
* @param outTradeNo 商户订单号
* @return 退款信息 wx pay refund query result
* @throws WxPayException the wx pay exception
*/
WxPayRefundQueryV3Result refundQueryV3(String outTradeNo) throws WxPayException;
/**
* <pre>
* 微信支付-查询退款
* 应用场景:
* 提交退款申请后通过调用该接口查询退款状态。退款有一定延时建议在提交退款申请后1分钟发起查询退款状态一般来说零钱支付的退款5分钟内到账银行卡支付的退款1-3个工作日到账。
* 详见 https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_10.shtml
* 接口链接https://api.mch.weixin.qq.com/v3/refund/domestic/refunds/{out_refund_no}
* </pre>
*
* @param request 微信退款单号
* @return 退款信息 wx pay refund query result
* @throws WxPayException the wx pay exception
*/
WxPayRefundQueryV3Result refundQueryV3(WxPayRefundQueryV3Request request) throws WxPayException;
/**
* 解析支付结果通知.
* 详见https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_7
@@ -507,6 +663,17 @@ public interface WxPayService {
*/
WxPayOrderNotifyResult parseOrderNotifyResult(String xmlData, String signType) throws WxPayException;
/**
* 解析支付结果v3通知.
* 详见https://pay.weixin.qq.com/wiki/doc/apiv3/apis/chapter3_1_5.shtml
*
* @param notifyData 通知数据
* @param header 通知头部数据,不传则表示不校验头
* @return the wx pay order notify result
* @throws WxPayException the wx pay exception
*/
WxPayOrderNotifyV3Result parseOrderNotifyV3Result(String notifyData, SignatureHeader header) throws WxPayException;
/**
* 解析退款结果通知
* 详见https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_16&index=9
@@ -517,6 +684,17 @@ public interface WxPayService {
*/
WxPayRefundNotifyResult parseRefundNotifyResult(String xmlData) throws WxPayException;
/**
* 解析退款结果通知
* 详见https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_16&index=9
*
* @param notifyData 通知数据
* @param header 通知头部数据,不传则表示不校验头
* @return the wx pay refund notify result
* @throws WxPayException the wx pay exception
*/
WxPayRefundNotifyV3Result parseRefundNotifyV3Result(String notifyData, SignatureHeader header) throws WxPayException;
/**
* 解析扫码支付回调通知
* 详见https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_4

View File

@@ -3,15 +3,14 @@ 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.notify.WxScanPayNotifyResult;
import com.github.binarywang.wxpay.bean.notify.*;
import com.github.binarywang.wxpay.bean.order.WxPayAppOrderResult;
import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.github.binarywang.wxpay.bean.order.WxPayMwebOrderResult;
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.bean.result.enums.TradeTypeEnum;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.config.WxPayConfigHolder;
import com.github.binarywang.wxpay.constant.WxPayConstants;
@@ -21,9 +20,12 @@ import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.*;
import com.github.binarywang.wxpay.util.SignUtils;
import com.github.binarywang.wxpay.util.XmlConfig;
import com.github.binarywang.wxpay.v3.util.AesUtils;
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import jodd.io.ZipUtil;
import me.chanjar.weixin.common.error.WxRuntimeException;
import org.apache.commons.lang3.StringUtils;
@@ -31,10 +33,12 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.GeneralSecurityException;
import java.util.*;
import java.util.zip.ZipException;
@@ -52,6 +56,7 @@ import static com.github.binarywang.wxpay.constant.WxPayConstants.TarType;
public abstract class BaseWxPayServiceImpl implements WxPayService {
private static final String TOTAL_FUND_COUNT = "资金流水总笔数";
private static final Gson GSON = new GsonBuilder().create();
final Logger log = LoggerFactory.getLogger(this.getClass());
@@ -242,6 +247,13 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
return result;
}
@Override
public WxPayRefundV3Result refundV3(WxPayRefundV3Request request) throws WxPayException {
String url = String.format("%s/v3/refund/domestic/refunds", this.getPayBaseUrl());
String response = this.postV3(url, GSON.toJson(request));
return GSON.fromJson(response, WxPayRefundV3Result.class);
}
@Override
public WxPayRefundQueryResult refundQuery(String transactionId, String outTradeNo, String outRefundNo, String refundId)
throws WxPayException {
@@ -278,6 +290,20 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
return result;
}
@Override
public WxPayRefundQueryV3Result refundQueryV3(String outTradeNo) throws WxPayException {
String url = String.format("%s/v3/refund/domestic/refunds/%s", this.getPayBaseUrl(), outTradeNo);
String response = this.getV3(url);
return GSON.fromJson(response, WxPayRefundQueryV3Result.class);
}
@Override
public WxPayRefundQueryV3Result refundQueryV3(WxPayRefundQueryV3Request request) throws WxPayException {
String url = String.format("%s/v3/refund/domestic/refunds/%s", this.getPayBaseUrl(), request.getOutTradeNo());
String response = this.getV3(url);
return GSON.fromJson(response, WxPayRefundQueryV3Result.class);
}
@Override
public WxPayOrderNotifyResult parseOrderNotifyResult(String xmlData) throws WxPayException {
return this.parseOrderNotifyResult(xmlData, null);
@@ -308,6 +334,44 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
}
}
/**
* 校验通知签名
* @param header 通知头信息
* @param data 通知数据
* @return true:校验通过 false:校验不通过
*/
private boolean verifyNotifySign(SignatureHeader header, String data) {
String beforeSign = String.format("%s\n%s\n%s\n",
header.getTimeStamp(),
header.getNonce(),
data);
return this.getConfig().getVerifier().verify(header.getSerial(),
beforeSign.getBytes(StandardCharsets.UTF_8), header.getSignature());
}
@Override
public WxPayOrderNotifyV3Result parseOrderNotifyV3Result(String notifyData, SignatureHeader header) throws WxPayException {
if (Objects.nonNull(header) && !this.verifyNotifySign(header, notifyData)) {
throw new WxPayException("非法请求,头部信息验证失败");
}
OriginNotifyResponse response = GSON.fromJson(notifyData, OriginNotifyResponse.class);
OriginNotifyResponse.Resource resource = response.getResource();
String cipherText = resource.getCiphertext();
String associatedData = resource.getAssociatedData();
String nonce = resource.getNonce();
String apiV3Key = this.getConfig().getApiV3Key();
try {
String result = AesUtils.decryptToString(associatedData, nonce, cipherText, apiV3Key);
WxPayOrderNotifyV3Result.DecryptNotifyResult decryptNotifyResult = GSON.fromJson(result, WxPayOrderNotifyV3Result.DecryptNotifyResult.class);
WxPayOrderNotifyV3Result notifyResult = new WxPayOrderNotifyV3Result();
notifyResult.setRawData(response);
notifyResult.setResult(decryptNotifyResult);
return notifyResult;
} catch (GeneralSecurityException | IOException e) {
throw new WxPayException("解析报文异常!", e);
}
}
@Override
public WxPayRefundNotifyResult parseRefundNotifyResult(String xmlData) throws WxPayException {
try {
@@ -326,6 +390,29 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
}
}
@Override
public WxPayRefundNotifyV3Result parseRefundNotifyV3Result(String notifyData, SignatureHeader header) throws WxPayException {
if (Objects.nonNull(header) && !this.verifyNotifySign(header, notifyData)) {
throw new WxPayException("非法请求,头部信息验证失败");
}
OriginNotifyResponse response = GSON.fromJson(notifyData, OriginNotifyResponse.class);
OriginNotifyResponse.Resource resource = response.getResource();
String cipherText = resource.getCiphertext();
String associatedData = resource.getAssociatedData();
String nonce = resource.getNonce();
String apiV3Key = this.getConfig().getApiV3Key();
try {
String result = AesUtils.decryptToString(associatedData, nonce, cipherText, apiV3Key);
WxPayRefundNotifyV3Result.DecryptNotifyResult decryptNotifyResult = GSON.fromJson(result, WxPayRefundNotifyV3Result.DecryptNotifyResult.class);
WxPayRefundNotifyV3Result notifyResult = new WxPayRefundNotifyV3Result();
notifyResult.setRawData(response);
notifyResult.setResult(decryptNotifyResult);
return notifyResult;
} catch (GeneralSecurityException | IOException e) {
throw new WxPayException("解析报文异常!", e);
}
}
@Override
public WxScanPayNotifyResult parseScanPayNotifyResult(String xmlData, String signType) throws WxPayException {
try {
@@ -372,6 +459,28 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
return result;
}
@Override
public WxPayOrderQueryV3Result queryOrderV3(String transactionId, String outTradeNo) throws WxPayException {
WxPayOrderQueryV3Request request = new WxPayOrderQueryV3Request();
request.setOutTradeNo(StringUtils.trimToNull(outTradeNo));
request.setTransactionId(StringUtils.trimToNull(transactionId));
return this.queryOrderV3(request);
}
@Override
public WxPayOrderQueryV3Result queryOrderV3(WxPayOrderQueryV3Request request) throws WxPayException {
if (StringUtils.isBlank(request.getMchid())) {
request.setMchid(this.getConfig().getMchId());
}
String url = String.format("%s/v3/pay/transactions/out-trade-no/%s", this.getPayBaseUrl(), request.getOutTradeNo());
if (Objects.isNull(request.getOutTradeNo())) {
url = String.format("%s/v3/pay/transactions/id/%s", this.getPayBaseUrl(), request.getTransactionId());
}
String query = String.format("?mchid=%s", request.getMchid());
String response = this.getV3(url + query);
return GSON.fromJson(response, WxPayOrderQueryV3Result.class);
}
@Override
public WxPayOrderCloseResult closeOrder(String outTradeNo) throws WxPayException {
if (StringUtils.isBlank(outTradeNo)) {
@@ -396,6 +505,25 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
return result;
}
@Override
public void closeOrderV3(String outTradeNo) throws WxPayException {
if (StringUtils.isBlank(outTradeNo)) {
throw new WxPayException("out_trade_no不能为空");
}
WxPayOrderCloseV3Request request = new WxPayOrderCloseV3Request();
request.setOutTradeNo(StringUtils.trimToNull(outTradeNo));
this.closeOrderV3(request);
}
@Override
public void closeOrderV3(WxPayOrderCloseV3Request request) throws WxPayException {
if (StringUtils.isBlank(request.getMchid())) {
request.setMchid(this.getConfig().getMchId());
}
String url = String.format("%s/v3/pay/transactions/out-trade-no/%s/close", this.getPayBaseUrl(), request.getOutTradeNo());
this.postV3(url, GSON.toJson(request));
}
@Override
public <T> T createOrder(WxPayUnifiedOrderRequest request) throws WxPayException {
WxPayUnifiedOrderResult unifiedOrderResult = this.unifiedOrder(request);
@@ -500,6 +628,25 @@ public abstract class BaseWxPayServiceImpl implements WxPayService {
return result;
}
@Override
public <T> T createOrderV3(TradeTypeEnum tradeType, WxPayUnifiedOrderV3Request request) throws WxPayException {
WxPayUnifiedOrderV3Result result = this.unifiedOrderV3(tradeType, request);
return result.getPayInfo(tradeType, request.getAppid(), request.getMchid(), this.getConfig().getPrivateKey());
}
@Override
public WxPayUnifiedOrderV3Result unifiedOrderV3(TradeTypeEnum tradeType, WxPayUnifiedOrderV3Request request) throws WxPayException {
if (StringUtils.isBlank(request.getAppid())) {
request.setAppid(this.getConfig().getAppId());
}
if (StringUtils.isBlank(request.getMchid())) {
request.setMchid(this.getConfig().getMchId());
}
String url = this.getPayBaseUrl() + tradeType.getPartnerUrl();
String response = this.postV3(url, GSON.toJson(request));
return GSON.fromJson(response, WxPayUnifiedOrderV3Result.class);
}
@Override
@Deprecated
public Map<String, String> getPayInfo(WxPayUnifiedOrderRequest request) throws WxPayException {