微信条码支付
This commit is contained in:
@@ -44,6 +44,7 @@
|
|||||||
<netty.version>4.1.128.Final</netty.version>
|
<netty.version>4.1.128.Final</netty.version>
|
||||||
<wechatpay.version>0.2.17</wechatpay.version>
|
<wechatpay.version>0.2.17</wechatpay.version>
|
||||||
<apipay-v3.version>3.1.65.ALL</apipay-v3.version>
|
<apipay-v3.version>3.1.65.ALL</apipay-v3.version>
|
||||||
|
<dom4j.version>2.2.0</dom4j.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
@@ -282,6 +283,13 @@
|
|||||||
<artifactId>alipay-sdk-java-v3</artifactId>
|
<artifactId>alipay-sdk-java-v3</artifactId>
|
||||||
<version>${apipay-v3.version}</version>
|
<version>${apipay-v3.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.dom4j</groupId>
|
||||||
|
<artifactId>dom4j</artifactId>
|
||||||
|
<version>${dom4j.version}</version>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
|
|
||||||
|
|||||||
@@ -35,6 +35,13 @@
|
|||||||
<artifactId>ocr_api20210707</artifactId>
|
<artifactId>ocr_api20210707</artifactId>
|
||||||
<version>3.1.2</version>
|
<version>3.1.2</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- Source: https://mvnrepository.com/artifact/org.dom4j/dom4j -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.dom4j</groupId>
|
||||||
|
<artifactId>dom4j</artifactId>
|
||||||
|
<scope>compile</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
@@ -385,8 +385,8 @@ public class EntryManager {
|
|||||||
// verifyEntryParam(merchantDto);
|
// verifyEntryParam(merchantDto);
|
||||||
// uploadParamImage(merchantDto);
|
// uploadParamImage(merchantDto);
|
||||||
//// System.out.println(merchantDto);
|
//// System.out.println(merchantDto);
|
||||||
EntryRespDto respDto = entryMerchant(merchantDto, PayCst.Platform.WECHAT);
|
// EntryRespDto respDto = entryMerchant(merchantDto, PayCst.Platform.WECHAT);
|
||||||
// entryMerchant(merchantDto, PayCst.Platform.ALIPAY);
|
EntryRespDto respDto = entryMerchant(merchantDto, PayCst.Platform.ALIPAY);
|
||||||
// entryMerchant(merchantDto, PayCst.Platform.WECHAT, PayCst.Platform.ALIPAY);
|
// entryMerchant(merchantDto, PayCst.Platform.WECHAT, PayCst.Platform.ALIPAY);
|
||||||
System.out.println(respDto);
|
System.out.println(respDto);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,22 +1,51 @@
|
|||||||
package com.czg;
|
package com.czg;
|
||||||
|
|
||||||
import com.czg.dto.req.PayParamsDto;
|
import com.czg.dto.req.PayParamsDto;
|
||||||
|
import com.czg.exception.CzgException;
|
||||||
import com.czg.third.alipay.AlipayIsvPayManager;
|
import com.czg.third.alipay.AlipayIsvPayManager;
|
||||||
import com.czg.third.wechat.WechatPayManager;
|
import com.czg.third.wechat.WechatPayManager;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author yjjie
|
* @author yjjie
|
||||||
* @date 2026/1/9 11:24
|
* @date 2026/1/9 11:24
|
||||||
*/
|
*/
|
||||||
public class PayManager {
|
public class PayManager {
|
||||||
|
|
||||||
public static void jsapiPay(PayParamsDto paramsDto) {
|
/**
|
||||||
|
* jsapi支付
|
||||||
|
*
|
||||||
|
* @param paramsDto 参数
|
||||||
|
* @return 结果
|
||||||
|
*/
|
||||||
|
public static Map<String, Object> jsapiPay(PayParamsDto paramsDto) {
|
||||||
paramsDto.verifyParams();
|
paramsDto.verifyParams();
|
||||||
|
|
||||||
if (PayCst.Platform.WECHAT.equals(paramsDto.getPlatform())) {
|
if (PayCst.Platform.WECHAT.equals(paramsDto.getPlatform())) {
|
||||||
WechatPayManager.jsapiPay(null, paramsDto);
|
return WechatPayManager.jsapiPay(null, paramsDto);
|
||||||
} else if (PayCst.Platform.ALIPAY.equals(paramsDto.getPlatform())) {
|
} else if (PayCst.Platform.ALIPAY.equals(paramsDto.getPlatform())) {
|
||||||
AlipayIsvPayManager.jsapiPay(null, paramsDto);
|
return AlipayIsvPayManager.jsapiPay(null, paramsDto);
|
||||||
|
} else {
|
||||||
|
throw new CzgException("不支持的支付平台");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 条码支付
|
||||||
|
*
|
||||||
|
* @param paramsDto 参数
|
||||||
|
* @return 结果
|
||||||
|
*/
|
||||||
|
public static Map<String, Object> barPay(PayParamsDto paramsDto) {
|
||||||
|
paramsDto.verifyParams();
|
||||||
|
|
||||||
|
if (PayCst.Platform.WECHAT.equals(paramsDto.getPlatform())) {
|
||||||
|
return WechatPayManager.barPay(null, paramsDto);
|
||||||
|
} else if (PayCst.Platform.ALIPAY.equals(paramsDto.getPlatform())) {
|
||||||
|
return AlipayIsvPayManager.barPay(null, paramsDto);
|
||||||
|
} else {
|
||||||
|
throw new CzgException("不支持的支付平台");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,10 +64,10 @@ public class PayManager {
|
|||||||
jsapiPay(new PayParamsDto()
|
jsapiPay(new PayParamsDto()
|
||||||
.setPlatform(PayCst.Platform.WECHAT)
|
.setPlatform(PayCst.Platform.WECHAT)
|
||||||
.setAppId("wxd88fffa983758a30")
|
.setAppId("wxd88fffa983758a30")
|
||||||
.setOpenId("123123123")
|
.setOpenId("or1l86yipGvwyfPhrKIAcQuSfAV8")
|
||||||
.setOrderNo("1111231231213")
|
.setOrderNo("1111231231213")
|
||||||
.setTitle("1213")
|
.setTitle("1213")
|
||||||
.setMerchantId("1665469114")
|
.setMerchantId("1738216504")
|
||||||
.setBody("1213")
|
.setBody("1213")
|
||||||
.setAmount(1000L)
|
.setAmount(1000L)
|
||||||
.setPayParams("{\"app_auth_token\": \"ssss\"}")
|
.setPayParams("{\"app_auth_token\": \"ssss\"}")
|
||||||
|
|||||||
@@ -77,6 +77,22 @@ public class PayParamsDto {
|
|||||||
*/
|
*/
|
||||||
private String body;
|
private String body;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 扩展数据
|
||||||
|
*/
|
||||||
|
private String extData;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设备 ip
|
||||||
|
*/
|
||||||
|
private String clientIp;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 支付条码
|
||||||
|
* 微信:18位纯数字,前缀以10、11、12、13、14、15开头
|
||||||
|
*/
|
||||||
|
private String barCode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 支付宝 授权信息解析,不用传
|
* 支付宝 授权信息解析,不用传
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -14,6 +14,8 @@ import com.czg.third.alipay.dto.config.AlipayConfigDto;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author yjjie
|
* @author yjjie
|
||||||
@@ -22,7 +24,12 @@ import java.math.BigDecimal;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class AlipayIsvPayManager {
|
public class AlipayIsvPayManager {
|
||||||
|
|
||||||
public static String jsapiPay(AlipayConfigDto configDto, PayParamsDto paramsDto) {
|
/**
|
||||||
|
* H5支付
|
||||||
|
* @param configDto 配置
|
||||||
|
* @param paramsDto 参数
|
||||||
|
*/
|
||||||
|
public static Map<String, Object> jsapiPay(AlipayConfigDto configDto, PayParamsDto paramsDto) {
|
||||||
try {
|
try {
|
||||||
AlipayClient.setApiClient(configDto);
|
AlipayClient.setApiClient(configDto);
|
||||||
AlipayTradeApi api = new AlipayTradeApi();
|
AlipayTradeApi api = new AlipayTradeApi();
|
||||||
@@ -41,7 +48,8 @@ public class AlipayIsvPayManager {
|
|||||||
customizedParams.setAppAuthToken(paramsDto.getAlipayAuthInfo().getAppAuthToken());
|
customizedParams.setAppAuthToken(paramsDto.getAlipayAuthInfo().getAppAuthToken());
|
||||||
|
|
||||||
AlipayTradeCreateResponseModel responseModel = api.create(model, customizedParams);
|
AlipayTradeCreateResponseModel responseModel = api.create(model, customizedParams);
|
||||||
return responseModel.getTradeNo();
|
log.info("支付宝 jsapi 支付结果: {}", responseModel);
|
||||||
|
return new HashMap<>();
|
||||||
} catch (ApiException e) {
|
} catch (ApiException e) {
|
||||||
String body = e.getResponseBody();
|
String body = e.getResponseBody();
|
||||||
log.error("支付宝 H5 api 支付异常: {}", body);
|
log.error("支付宝 H5 api 支付异常: {}", body);
|
||||||
@@ -53,6 +61,27 @@ public class AlipayIsvPayManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 条码支付
|
||||||
|
* @param configDto 配置
|
||||||
|
* @param paramsDto 参数
|
||||||
|
*/
|
||||||
|
public static Map<String, Object> barPay(AlipayConfigDto configDto, PayParamsDto paramsDto) {
|
||||||
|
try {
|
||||||
|
AlipayClient.setApiClient(configDto);
|
||||||
|
|
||||||
|
return new JSONObject();
|
||||||
|
// } catch (ApiException e) {
|
||||||
|
// String body = e.getResponseBody();
|
||||||
|
// log.error("支付宝 条码支付异常: {}", body);
|
||||||
|
// JSONObject object = JSONObject.parseObject(body);
|
||||||
|
// throw new CzgException(object.getString(PayCst.ALIPAY_ERROR_MSG_KEY));
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("支付宝 条码支付异常: {}", e.getMessage());
|
||||||
|
throw new CzgException(e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 金额转换
|
* 金额转换
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -1,9 +1,31 @@
|
|||||||
package com.czg.third.wechat;
|
package com.czg.third.wechat;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import cn.hutool.crypto.SecureUtil;
|
||||||
|
import cn.hutool.http.HttpUtil;
|
||||||
import com.alibaba.fastjson2.JSONObject;
|
import com.alibaba.fastjson2.JSONObject;
|
||||||
import com.czg.dto.req.PayParamsDto;
|
import com.czg.dto.req.PayParamsDto;
|
||||||
|
import com.czg.exception.CzgException;
|
||||||
import com.czg.third.wechat.dto.config.WechatPayConfigDto;
|
import com.czg.third.wechat.dto.config.WechatPayConfigDto;
|
||||||
|
import com.wechat.pay.java.core.Config;
|
||||||
|
import com.wechat.pay.java.core.cipher.Signer;
|
||||||
|
import com.wechat.pay.java.core.util.NonceUtil;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.dom4j.Document;
|
||||||
|
import org.dom4j.DocumentHelper;
|
||||||
|
import org.dom4j.Element;
|
||||||
|
import org.dom4j.io.OutputFormat;
|
||||||
|
import org.dom4j.io.SAXReader;
|
||||||
|
import org.dom4j.io.XMLWriter;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.time.Instant;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.TreeMap;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author yjjie
|
* @author yjjie
|
||||||
@@ -12,33 +34,211 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class WechatPayManager {
|
public class WechatPayManager {
|
||||||
|
|
||||||
|
private static final String PAY_SUCCESS = "SUCCESS";
|
||||||
|
private static final String BAR_PAY_URL = "https://api.mch.weixin.qq.com/pay/micropay";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* jsapi 小程序支付
|
* jsapi 小程序支付
|
||||||
* @param configDto 配置
|
*
|
||||||
* @param paramsDto 参数
|
* @param configDto 配置
|
||||||
|
* @param paramsDto 参数
|
||||||
*/
|
*/
|
||||||
public static String jsapiPay(WechatPayConfigDto configDto, PayParamsDto paramsDto) {
|
public static Map<String, Object> jsapiPay(WechatPayConfigDto configDto, PayParamsDto paramsDto) {
|
||||||
if (configDto == null) {
|
if (configDto == null) {
|
||||||
configDto = WechatPayConfigDto.getDefaultConfig();
|
configDto = WechatPayConfigDto.getDefaultConfig();
|
||||||
}
|
}
|
||||||
JSONObject reqData = new JSONObject();
|
|
||||||
reqData.put("sp_appid", paramsDto.getAppId());
|
|
||||||
reqData.put("sp_mchid", configDto.getMerchantId());
|
|
||||||
reqData.put("sub_mchid", paramsDto.getMerchantId());
|
|
||||||
reqData.put("description", paramsDto.getTitle());
|
|
||||||
reqData.put("out_trade_no", paramsDto.getOrderNo());
|
|
||||||
reqData.put("notify_url", paramsDto.getNotifyUrl());
|
|
||||||
|
|
||||||
JSONObject amount = new JSONObject();
|
try {
|
||||||
amount.put("total", paramsDto.getAmount());
|
JSONObject reqData = new JSONObject();
|
||||||
reqData.put("amount", amount);
|
reqData.put("sp_appid", paramsDto.getAppId());
|
||||||
|
reqData.put("sp_mchid", configDto.getMerchantId());
|
||||||
|
reqData.put("sub_mchid", paramsDto.getMerchantId());
|
||||||
|
reqData.put("description", paramsDto.getTitle());
|
||||||
|
reqData.put("out_trade_no", paramsDto.getOrderNo());
|
||||||
|
reqData.put("notify_url", paramsDto.getNotifyUrl());
|
||||||
|
reqData.put("attach", paramsDto.getExtData());
|
||||||
|
|
||||||
JSONObject payer = new JSONObject();
|
JSONObject amount = new JSONObject();
|
||||||
payer.put("sp_openid", paramsDto.getOpenId());
|
amount.put("total", paramsDto.getAmount());
|
||||||
reqData.put("payer", payer);
|
reqData.put("amount", amount);
|
||||||
|
|
||||||
String string = WechatReqUtils.postReq(configDto, "/v3/pay/partner/transactions/jsapi", reqData.toJSONString());
|
JSONObject payer = new JSONObject();
|
||||||
log.info("微信支付请求结果: orderNo = {}, res = {}", paramsDto.getOrderNo(), string);
|
payer.put("sp_openid", paramsDto.getOpenId());
|
||||||
return "";
|
reqData.put("payer", payer);
|
||||||
|
|
||||||
|
String string = WechatReqUtils.postReq(configDto, "/v3/pay/partner/transactions/jsapi", reqData.toJSONString());
|
||||||
|
log.info("微信支付请求结果: orderNo = {}, res = {}", paramsDto.getOrderNo(), string);
|
||||||
|
|
||||||
|
JSONObject object = JSONObject.parseObject(string);
|
||||||
|
|
||||||
|
long timestamp = Instant.now().getEpochSecond();
|
||||||
|
String nonceStr = NonceUtil.createNonce(32);
|
||||||
|
String packageVal = "prepay_id=" + object.getString("prepay_id");
|
||||||
|
String message =
|
||||||
|
paramsDto.getAppId() + "\n" + timestamp + "\n" + nonceStr + "\n" + packageVal + "\n";
|
||||||
|
|
||||||
|
Config config = WechatConfig.getRsaConfig(configDto);
|
||||||
|
Signer signer = config.createSigner();
|
||||||
|
String sign = signer.sign(message).getSign();
|
||||||
|
|
||||||
|
JSONObject res = new JSONObject();
|
||||||
|
res.put("appId", paramsDto.getAppId());
|
||||||
|
res.put("timeStamp", String.valueOf(timestamp));
|
||||||
|
res.put("nonceStr", nonceStr);
|
||||||
|
res.put("package", packageVal);
|
||||||
|
res.put("signType", "RSA");
|
||||||
|
res.put("paySign", sign);
|
||||||
|
log.info("微信支付签名结果: orderNo = {}, res = {}", paramsDto.getOrderNo(), res);
|
||||||
|
return res;
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("微信支付异常: orderNo = {}, e = {}", paramsDto.getOrderNo(), e.getMessage());
|
||||||
|
throw new CzgException("微信支付异常");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 条码支付
|
||||||
|
* 用户付款码规则:18位纯数字,前缀以10、11、12、13、14、15开头
|
||||||
|
* @param configDto 配置
|
||||||
|
* @param paramsDto 参数
|
||||||
|
*/
|
||||||
|
public static Map<String, Object> barPay(WechatPayConfigDto configDto, PayParamsDto paramsDto) {
|
||||||
|
try {
|
||||||
|
if (configDto == null) {
|
||||||
|
configDto = WechatPayConfigDto.getDefaultConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
String nonceStr = NonceUtil.createNonce(32);
|
||||||
|
|
||||||
|
Map<String, Object> payMap = new HashMap<>();
|
||||||
|
Document document = DocumentHelper.createDocument();
|
||||||
|
|
||||||
|
// 添加根元素
|
||||||
|
Element xml = document.addElement("xml");
|
||||||
|
|
||||||
|
addElementIfNotEmpty(xml, "appid", paramsDto.getAppId());
|
||||||
|
payMap.put("appid", paramsDto.getAppId());
|
||||||
|
addElementIfNotEmpty(xml, "mch_id", configDto.getMerchantId());
|
||||||
|
payMap.put("mch_id", configDto.getMerchantId());
|
||||||
|
addElementIfNotEmpty(xml, "sub_mch_id", paramsDto.getMerchantId());
|
||||||
|
payMap.put("sub_mch_id", paramsDto.getMerchantId());
|
||||||
|
addElementIfNotEmpty(xml, "nonce_str", nonceStr);
|
||||||
|
payMap.put("nonce_str", nonceStr);
|
||||||
|
addElementIfNotEmpty(xml, "body", paramsDto.getTitle());
|
||||||
|
payMap.put("body", paramsDto.getTitle());
|
||||||
|
addElementIfNotEmpty(xml, "out_trade_no", paramsDto.getOrderNo());
|
||||||
|
payMap.put("out_trade_no", paramsDto.getOrderNo());
|
||||||
|
addElementIfNotEmpty(xml, "total_fee", paramsDto.getAmount());
|
||||||
|
payMap.put("total_fee", paramsDto.getAmount());
|
||||||
|
addElementIfNotEmpty(xml, "spbill_create_ip", paramsDto.getClientIp());
|
||||||
|
payMap.put("spbill_create_ip", paramsDto.getClientIp());
|
||||||
|
addElementIfNotEmpty(xml, "auth_code", paramsDto.getBarCode());
|
||||||
|
payMap.put("auth_code", paramsDto.getBarCode());
|
||||||
|
|
||||||
|
if (StrUtil.isNotBlank(paramsDto.getExtData())) {
|
||||||
|
addElementIfNotEmpty(xml, "attach", paramsDto.getExtData());
|
||||||
|
payMap.put("attach", paramsDto.getExtData());
|
||||||
|
}
|
||||||
|
|
||||||
|
String sign = signBarPayParam(configDto, payMap);
|
||||||
|
addElementIfNotEmpty(xml, "sign", sign);
|
||||||
|
|
||||||
|
OutputFormat format = OutputFormat.createPrettyPrint();
|
||||||
|
XMLWriter writer = new XMLWriter(System.out, format);
|
||||||
|
writer.write(document);
|
||||||
|
|
||||||
|
String xmlStr = document.asXML();
|
||||||
|
log.info("微信条码支付参数:{}", xmlStr);
|
||||||
|
|
||||||
|
String post = HttpUtil.post(BAR_PAY_URL, xmlStr);
|
||||||
|
log.info("微信条码支付请求结果: orderNo = {}, res = {}", paramsDto.getOrderNo(), post);
|
||||||
|
|
||||||
|
JSONObject res = new JSONObject();
|
||||||
|
res.put("orderNo", paramsDto.getOrderNo());
|
||||||
|
|
||||||
|
SAXReader saxReader = new SAXReader();
|
||||||
|
Document read = saxReader.read(stringToInputStream(post));
|
||||||
|
Element rootElement = read.getRootElement();
|
||||||
|
String returnCode = rootElement.elementText("return_code");
|
||||||
|
|
||||||
|
String errCode = rootElement.elementText("err_code");
|
||||||
|
String errCodeDes = rootElement.elementText("err_code_des");
|
||||||
|
res.put("message", errCodeDes);
|
||||||
|
|
||||||
|
if (StrUtil.isNotBlank(errCode)) {
|
||||||
|
if ("SYSTEMERROR".equals(errCode) || "BANKERROR".equals(errCode) || "USERPAYING".equals(errCode)) {
|
||||||
|
// 等待结果
|
||||||
|
log.info("微信条码支付等待结果: orderNo = {}, err {}", paramsDto.getOrderNo(), errCodeDes);
|
||||||
|
res.put("status", "TRADE_AWAIT");
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
res.put("status", "TRADE_FAIL");
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!PAY_SUCCESS.equals(returnCode)) {
|
||||||
|
log.error("微信条码支付失败: orderNo = {}, msg = {}", paramsDto.getOrderNo(), rootElement.elementText("return_msg"));
|
||||||
|
res.put("status", "TRADE_FAIL");
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 支付成功
|
||||||
|
log.info("微信条码支付成功: orderNo = {}, msg = {}", paramsDto.getOrderNo(), rootElement.elementText("return_msg"));
|
||||||
|
res.put("status", "TRADE_SUCCESS");
|
||||||
|
return res;
|
||||||
|
} catch (CzgException e) {
|
||||||
|
throw e;
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("微信条码支付异常: orderNo = {}, e = {}", paramsDto.getOrderNo(), e.getMessage());
|
||||||
|
throw new CzgException("微信支付异常");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将String转换为InputStream
|
||||||
|
* @param str 待转换的字符串
|
||||||
|
* @return 转换后的InputStream
|
||||||
|
*/
|
||||||
|
private static InputStream stringToInputStream(String str) {
|
||||||
|
if (str == null || str.isEmpty()) {
|
||||||
|
return new ByteArrayInputStream(new byte[0]);
|
||||||
|
}
|
||||||
|
// 核心:先转字节数组,再封装为ByteArrayInputStream
|
||||||
|
byte[] byteArray = str.getBytes(StandardCharsets.UTF_8);
|
||||||
|
return new ByteArrayInputStream(byteArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static String signBarPayParam(WechatPayConfigDto configDto, Map<String, Object> params) {
|
||||||
|
TreeMap<String, Object> sortedParams = new TreeMap<>(params);
|
||||||
|
String sortParam = sortedParams.entrySet().stream()
|
||||||
|
// 将每个键值对转换为 "key=value" 格式的字符串
|
||||||
|
.map(entry -> entry.getKey() + "=" + (entry.getValue() == null ? "" : entry.getValue()))
|
||||||
|
// 使用 "&" 连接所有转换后的字符串
|
||||||
|
.collect(Collectors.joining("&"));
|
||||||
|
log.info("微信条码支付 加密前参数: {}", sortParam);
|
||||||
|
|
||||||
|
String signStr = sortParam + "&key=" + configDto.getApiV2Key();
|
||||||
|
return SecureUtil.md5(signStr).toUpperCase();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加元素,包括空值
|
||||||
|
*/
|
||||||
|
private static void addElementIfNotEmpty(Element parent, String name, Object value) {
|
||||||
|
Element element = parent.addElement(name);
|
||||||
|
element.addText(value == null ? "" : String.valueOf(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
barPay(null, new PayParamsDto()
|
||||||
|
.setMerchantId("1738216504")
|
||||||
|
.setOrderNo("sa101293120sss1")
|
||||||
|
.setTitle("1213")
|
||||||
|
.setBody("1213")
|
||||||
|
.setAmount(1L)
|
||||||
|
.setAppId("wxd88fffa983758a30")
|
||||||
|
.setClientIp("127.0.0.1")
|
||||||
|
.setBarCode("130013153082460022"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ public class WechatReqUtils {
|
|||||||
.header("Wechatpay-Serial", configDto.getPublicKeyId())
|
.header("Wechatpay-Serial", configDto.getPublicKeyId())
|
||||||
.charset(StandardCharsets.UTF_8)
|
.charset(StandardCharsets.UTF_8)
|
||||||
.contentType("application/json")
|
.contentType("application/json")
|
||||||
.body(body );
|
.body(body);
|
||||||
|
|
||||||
case "GET" -> HttpUtil.createGet(configDto.getDomain() + url)
|
case "GET" -> HttpUtil.createGet(configDto.getDomain() + url)
|
||||||
.header("Authorization", authorization)
|
.header("Authorization", authorization)
|
||||||
|
|||||||
@@ -21,6 +21,11 @@ public class WechatPayConfigDto {
|
|||||||
*/
|
*/
|
||||||
private String apiV3Key;
|
private String apiV3Key;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Api V2 密钥
|
||||||
|
*/
|
||||||
|
private String apiV2Key;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 商户证书序列号
|
* 商户证书序列号
|
||||||
*/
|
*/
|
||||||
@@ -51,6 +56,7 @@ public class WechatPayConfigDto {
|
|||||||
return new WechatPayConfigDto()
|
return new WechatPayConfigDto()
|
||||||
.setMerchantId("1643779408")
|
.setMerchantId("1643779408")
|
||||||
.setApiV3Key("a92baac5eb7a36ed8ec198113e769a03")
|
.setApiV3Key("a92baac5eb7a36ed8ec198113e769a03")
|
||||||
|
.setApiV2Key("3caf37225b6ea77a624ee03b7e3d03bb")
|
||||||
.setSerialNumber("4DE9BAC2EA584C3F274F694C9753CA814C4E9BF4")
|
.setSerialNumber("4DE9BAC2EA584C3F274F694C9753CA814C4E9BF4")
|
||||||
.setPublicKey("""
|
.setPublicKey("""
|
||||||
-----BEGIN PUBLIC KEY-----
|
-----BEGIN PUBLIC KEY-----
|
||||||
|
|||||||
Reference in New Issue
Block a user