v3删除分账接收方请求url地址错误 #88

订单关闭根据状态码进行判断成功失败
移除异步加载证书方式
This commit is contained in:
egan
2022-03-20 23:48:47 +08:00
parent be3b66e025
commit 80ce0950ea
8 changed files with 57 additions and 41 deletions

View File

@@ -2,25 +2,19 @@ package com.egzosn.pay.wx.v3.api;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
import java.security.Signature;
import java.security.cert.Certificate;
import java.util.Map;
import java.util.TreeMap;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.message.BasicHeader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.apache.http.entity.ContentType.APPLICATION_JSON;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.egzosn.pay.common.bean.MethodType;
import com.egzosn.pay.common.bean.NoticeParams;
import com.egzosn.pay.common.bean.PayOrder;
import com.egzosn.pay.common.bean.TransactionType;
import com.egzosn.pay.common.exception.PayErrorException;
@@ -30,9 +24,6 @@ import com.egzosn.pay.common.http.ResponseEntity;
import com.egzosn.pay.common.http.UriVariables;
import com.egzosn.pay.common.util.DateUtils;
import com.egzosn.pay.common.util.sign.SignTextUtils;
import com.egzosn.pay.common.util.sign.encrypt.Base64;
import com.egzosn.pay.common.util.sign.encrypt.RSA;
import com.egzosn.pay.common.util.sign.encrypt.RSA2;
import com.egzosn.pay.common.util.str.StringUtils;
import com.egzosn.pay.wx.bean.WxPayError;
import com.egzosn.pay.wx.v3.bean.WxTransactionType;
@@ -84,7 +75,7 @@ public class DefaultWxPayAssistService implements WxPayAssistService {
* @param uriVariables 用于匹配表达式
* @return 响应内容体
*/
public JSONObject doExecute(String body, TransactionType transactionType, Object... uriVariables) {
public ResponseEntity<JSONObject> doExecuteEntity(String body, TransactionType transactionType, Object... uriVariables) {
String reqUrl = UriVariables.getUri(wxPayService.getReqUrl(transactionType), uriVariables);
MethodType method = MethodType.valueOf(transactionType.getMethod());
if (MethodType.GET == method && StringUtils.isNotEmpty(body)) {
@@ -93,6 +84,19 @@ public class DefaultWxPayAssistService implements WxPayAssistService {
}
HttpEntity entity = buildHttpEntity(reqUrl, body, transactionType.getMethod());
ResponseEntity<JSONObject> responseEntity = requestTemplate.doExecuteEntity(reqUrl, entity, JSONObject.class, method);
return responseEntity;
}
/**
* 发起请求
*
* @param body 请求内容
* @param transactionType 交易类型
* @param uriVariables 用于匹配表达式
* @return 响应内容体
*/
public JSONObject doExecute(String body, TransactionType transactionType, Object... uriVariables) {
final ResponseEntity<JSONObject> responseEntity = doExecuteEntity(body, transactionType, uriVariables);
int statusCode = responseEntity.getStatusCode();
JSONObject responseBody = responseEntity.getBody();
if (statusCode >= 400) {

View File

@@ -4,10 +4,9 @@ import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import com.egzosn.pay.common.bean.Order;
import com.egzosn.pay.common.bean.AssistOrder;
import com.egzosn.pay.common.bean.OrderParaStructure;
import com.egzosn.pay.common.bean.PayOrder;
import com.egzosn.pay.common.http.UriVariables;
import com.egzosn.pay.common.util.MapGen;
import com.egzosn.pay.wx.v3.utils.WxConst;
@@ -71,8 +70,9 @@ public class WxParameterStructure {
* @param parameters 订单参数
* @param order 订单信息
*/
public void initNotifyUrl(Map<String, Object> parameters, Order order) {
public void initNotifyUrl(Map<String, Object> parameters, AssistOrder order) {
OrderParaStructure.loadParameters(parameters, WxConst.NOTIFY_URL, payConfigStorage.getNotifyUrl());
OrderParaStructure.loadParameters(parameters, WxConst.NOTIFY_URL, order.getNotifyUrl());
OrderParaStructure.loadParameters(parameters, WxConst.NOTIFY_URL, order);
}

View File

@@ -8,7 +8,7 @@ import org.apache.http.HttpEntity;
import com.alibaba.fastjson.JSONObject;
import com.egzosn.pay.common.bean.PayOrder;
import com.egzosn.pay.common.bean.TransactionType;
import com.egzosn.pay.common.http.HttpStringEntity;
import com.egzosn.pay.common.http.ResponseEntity;
/**
* 微信支付辅助服务
@@ -40,6 +40,16 @@ public interface WxPayAssistService {
*/
JSONObject doExecute(String body, TransactionType transactionType, Object... uriVariables);
/**
* 发起请求
*
* @param body 请求内容
* @param transactionType 交易类型
* @param uriVariables 用于匹配表达式
* @return 响应内容体
*/
ResponseEntity<JSONObject> doExecuteEntity(String body, TransactionType transactionType, Object... uriVariables);
/**
* 发起请求
*

View File

@@ -72,7 +72,7 @@ public class WxPayConfigStorage extends BasePayConfigStorage {
/**
* 证书信息
*/
private CertEnvironment certEnvironment;
private volatile CertEnvironment certEnvironment;
/**
* 是否为服务商模式, 默认为false
@@ -233,6 +233,7 @@ public class WxPayConfigStorage extends BasePayConfigStorage {
}
public CertEnvironment getCertEnvironment() {
loadCertEnvironment();
return certEnvironment;
}

View File

@@ -135,11 +135,11 @@ public class WxPayService extends BasePayService<WxPayConfigStorage> {
@Override
protected void initAfter() {
payConfigStorage.setPartner(StringUtils.isNotEmpty(payConfigStorage.getSubMchId()));
new Thread(() -> {
payConfigStorage.loadCertEnvironment();
wxParameterStructure = new WxParameterStructure(payConfigStorage);
getAssistService();
}).start();
// new Thread(() -> {
payConfigStorage.loadCertEnvironment();
wxParameterStructure = new WxParameterStructure(payConfigStorage);
getAssistService();
// }).start();
}
@@ -235,8 +235,8 @@ public class WxPayService extends BasePayService<WxPayConfigStorage> {
public JSONObject unifiedOrder(PayOrder order) {
//统一下单
Map<String, Object> parameters = wxParameterStructure.initPartner(null);
;
Map<String, Object> parameters = wxParameterStructure.initPartner(null);
;
// wxParameterStructure.getPublicParameters(parameters);
// 商品描述
OrderParaStructure.loadParameters(parameters, WxConst.DESCRIPTION, order.getSubject());
@@ -261,7 +261,8 @@ public class WxPayService extends BasePayService<WxPayConfigStorage> {
TransactionType transactionType = order.getTransactionType();
((WxTransactionType) transactionType).setAttribute(parameters, order);
// 订单附加信息,可用于预设未提供的参数,这里会覆盖以上所有的订单信息,
parameters.putAll(order.getAttrs());
return getAssistService().doExecute(parameters, order);
}
@@ -285,7 +286,7 @@ public class WxPayService extends BasePayService<WxPayConfigStorage> {
Map<String, Object> params = new LinkedHashMap<>();
String appId = payConfigStorage.getAppId();
if (payConfigStorage.isPartner() && StringUtils.isNotEmpty(payConfigStorage.getSubAppId())){
if (payConfigStorage.isPartner() && StringUtils.isNotEmpty(payConfigStorage.getSubAppId())) {
appId = payConfigStorage.getSubAppId();
}
String timeStamp = String.valueOf(DateUtils.toEpochSecond());
@@ -496,7 +497,11 @@ public class WxPayService extends BasePayService<WxPayConfigStorage> {
@Override
public Map<String, Object> close(AssistOrder assistOrder) {
String parameters = JSON.toJSONString(wxParameterStructure.getMchParameters());
return getAssistService().doExecute(parameters, WxTransactionType.CLOSE, assistOrder.getOutTradeNo());
final ResponseEntity<JSONObject> responseEntity = getAssistService().doExecuteEntity(parameters, WxTransactionType.CLOSE, assistOrder.getOutTradeNo());
if (responseEntity.getStatusCode() == 204) {
return new MapGen<String, Object>("statusCode", responseEntity.getStatusCode()).getAttr();
}
return responseEntity.getBody();
}
@@ -530,8 +535,6 @@ public class WxPayService extends BasePayService<WxPayConfigStorage> {
}
/**
* 查询退款
*

View File

@@ -16,15 +16,15 @@ public enum WxProfitSharingTransactionType implements TransactionType {
/**
* 请求分账
*/
ORDERS("/v3/profitsharing/orders ", MethodType.POST),
ORDERS("/v3/profitsharing/orders", MethodType.POST),
/**
* 查询分账结果
*/
ORDERS_RESULT("/v3/profitsharing/orders/{out_order_no} ", MethodType.POST),
ORDERS_RESULT("/v3/profitsharing/orders/{out_order_no}", MethodType.GET),
/**
* 请求分账回退
*/
RETURN_ORDERS("/v3/profitsharing/return-orders ", MethodType.POST),
RETURN_ORDERS("/v3/profitsharing/return-orders", MethodType.POST),
/**
* 查询分账回退结果
*/
@@ -32,19 +32,19 @@ public enum WxProfitSharingTransactionType implements TransactionType {
/**
* 解冻剩余资金
*/
ORDERS_UNFREEZE("/v3/profitsharing/orders/unfreeze ", MethodType.POST),
ORDERS_UNFREEZE("/v3/profitsharing/orders/unfreeze", MethodType.POST),
/**
* 查询剩余待分金额
*/
AMOUNTS("/v3/profitsharing/transactions/{transaction_id}/amounts ", MethodType.GET),
AMOUNTS("/v3/profitsharing/transactions/{transaction_id}/amounts", MethodType.GET),
/**
* 服务商专用-查询最大分账比例
*/
MCH_CONFIG("/v3/profitsharing/merchant-configs/{sub_mchid} ", MethodType.GET),
MCH_CONFIG("/v3/profitsharing/merchant-configs/{sub_mchid}", MethodType.GET),
/**
* 添加分账接收方
*/
RECEIVERS_ADD("/v3/profitsharing/receivers/add ", MethodType.POST),
RECEIVERS_ADD("/v3/profitsharing/receivers/add", MethodType.POST),
/**
* 删除分账接收方
*/

View File

@@ -101,6 +101,6 @@ public class ProfitSharingOrder extends PayOrder {
public void setUnfreezeUnsplit(Boolean unfreezeUnsplit) {
this.unfreezeUnsplit = unfreezeUnsplit;
addAttr(WxConst.UNFREEZE_UNSPLIT, receivers);
addAttr(WxConst.UNFREEZE_UNSPLIT, unfreezeUnsplit);
}
}

View File

@@ -21,8 +21,7 @@ import java.util.concurrent.ConcurrentHashMap;
import javax.crypto.Cipher;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.management.openmbean.InvalidKeyException;
import com.egzosn.pay.common.exception.PayErrorException;
import com.egzosn.pay.common.util.sign.encrypt.Base64;
@@ -57,10 +56,6 @@ public final class AntCertificationUtil {
}
try {
if (null == Security.getProvider("BC")) {
Security.removeProvider("SunEC");
Security.addProvider(new BouncyCastleProvider());
}
PKCS12_KEY_STORE = KeyStore.getInstance("PKCS12");
}
catch (KeyStoreException e) {
@@ -127,6 +122,9 @@ public final class AntCertificationUtil {
PrivateKey privateKey = (PrivateKey) PKCS12_KEY_STORE.getKey(keyAlias, pem);
return new CertEnvironment(privateKey, publicKey, serialNumber);
}
catch (InvalidKeyException e) {
throw new PayErrorException(new WxPayError(WxConst.FAILURE, "获取公私钥失败, 解决方式替换jre包local_policy.jarUS_export_policy.jar"), e);
}
catch (GeneralSecurityException e) {
throw new PayErrorException(new WxPayError(WxConst.FAILURE, "获取公私钥失败"), e);
}