maven引入
配置类
package com.hyt.mall.pay.web.config;
import com.wechat.pay.java.core.cipher.RSASigner;
import com.wechat.pay.java.core.cipher.SignatureResult;
import com.wechat.pay.java.core.cipher.Signer;
import com.wechat.pay.java.core.util.IOUtil;
import com.wechat.pay.java.core.util.PemUtil;
import com.hyt.mall.pay.web.domain.SignInfo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import java.io.IOException;
import java.security.PrivateKey;
/**
-
@author zww
-
@Description:
-
@date 2022/7/4 15:24
*/
@Configuration
@PropertySource("classpath:wxpay.properties")
@ConfigurationProperties(prefix = "wxpay")
public class WxpayConfig {public static String appId;
public static String mchId;//商户号
public static String mchSerialNo;//商户证书序列号 例如-证书序列号
public static String privateKeyPath;//商户API私钥路径
public static String apiV3Key;//微信支付 APIv3 密钥
public static String domain;
public static String notifyDomain;
public static String refundNotify;
public static String wechatPayCertificatePath;//微信支付平台证书路径
// public static Config getConfig() {
// return new RSAAutoCertificateConfig.Builder()
// .merchantId(mchId)
// .privateKeyFromPath(privateKeyPath)
// .merchantSerialNumber(mchSerialNo)
// .apiV3Key(apiV3Key)
// .build();
//
// }
public static String generateSign(String prepayId, String nonceStr, String timeStamp){String privateKeyStr = null;try {privateKeyStr = IOUtil.loadStringFromPath(privateKeyPath);} catch (IOException e) {e.printStackTrace();}PrivateKey key = PemUtil.loadPrivateKeyFromString(privateKeyStr);SignInfo info = new SignInfo();info.setAppId(appId);info.setTimeStamp(timeStamp);info.setNonceStr(nonceStr);info.setPrepayId(prepayId);String str = info.toString();Signer rsaSigner = new RSASigner(mchSerialNo, key);SignatureResult signatureResult = rsaSigner.sign(str);return signatureResult.getSign();
}public String getAppId() {return appId;
}public void setAppId(String appId) {WxpayConfig.appId = appId;
}public String getMchId() {return mchId;
}public void setMchId(String mchId) {WxpayConfig.mchId = mchId;
}public String getMchSerialNo() {return mchSerialNo;
}public void setMchSerialNo(String mchSerialNo) {WxpayConfig.mchSerialNo = mchSerialNo;
}public String getPrivateKeyPath() {return privateKeyPath;
}public void setPrivateKeyPath(String privateKeyPath) {WxpayConfig.privateKeyPath = privateKeyPath;
}public String getApiV3Key() {return apiV3Key;
}public void setApiV3Key(String apiV3Key) {WxpayConfig.apiV3Key = apiV3Key;
}public String getDomain() {return domain;
}public void setDomain(String domain) {WxpayConfig.domain = domain;
}public String getNotifyDomain() {return notifyDomain;
}public void setNotifyDomain(String notifyDomain) {WxpayConfig.notifyDomain = notifyDomain;
}public String getRefundNotify() {return refundNotify;
}public void setRefundNotify(String refundNotify) {WxpayConfig.refundNotify = refundNotify;
}
}
小程序支付实现类
package com.hyt.mall.pay.web.service.impl.wechat;
import com.alibaba.fastjson.JSON;
import com.hyt.mall.pay.web.config.WxpayConfig;
import com.hyt.mall.pay.web.req.;
import com.hyt.mall.pay.web.resp.ErrorResp;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.exception.ServiceException;
import com.wechat.pay.java.core.util.NonceUtil;
import com.wechat.pay.java.service.payments.jsapi.JsapiService;
import com.wechat.pay.java.service.payments.jsapi.model.;
import com.wechat.pay.java.service.payments.model.Transaction;
import com.wechat.pay.java.service.refund.model.Refund;
import com.wechat.pay.java.service.transferbatch.TransferBatchService;
import com.wechat.pay.java.service.transferbatch.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
-
@author zww
-
@Description:
-
@date 2023/3/3 14:05
*/
@Service
public class WechatMiniServiceImpl {private static final Logger LOG = LoggerFactory.getLogger(WechatMiniServiceImpl.class);
@Resource
private WechatRefundServiceImpl refundService;private JsapiService service;
public TransferBatchService transferBatchService;
public String pay(OrderPayReq param, RSAAutoCertificateConfig config) {
LOG.info("支付参数:{}", param.toString());
service = new JsapiService.Builder().config(config).build();
PrepayRequest request = new PrepayRequest();
Amount amount = new Amount();
BigDecimal payAmount = param.getAmount().multiply(BigDecimal.valueOf(100));
amount.setTotal(payAmount.intValue());
request.setAmount(amount);
request.setAppid(WxpayConfig.appId);
request.setMchid(WxpayConfig.mchId);
request.setDescription(param.getGoodsTitle());
request.setNotifyUrl(WxpayConfig.notifyDomain);
request.setOutTradeNo(param.getOrderNo());
Payer payer = new Payer();
payer.setOpenid(param.getOpenId());
request.setPayer(payer);
try {
PrepayResponse response = service.prepay(request);
LOG.info("返回参数:{}", response.toString());
MiniPayReq payReq = new MiniPayReq();
payReq.setAppId(WxpayConfig.appId);
String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
payReq.setTimeStamp(timestamp);
payReq.setPackageStr("prepay_id=" + response.getPrepayId());
payReq.setNonceStr(NonceUtil.createNonce(32));
payReq.setSignType("RSA");
payReq.setSign(WxpayConfig.generateSign(payReq.getPackageStr(), payReq.getNonceStr(), payReq.getTimeStamp()));
return JSON.toJSONString(payReq);
} catch (ServiceException e) {
LOG.error("code:{},message:{}", e.getErrorCode(), e.getErrorMessage());
LOG.error("reponseBody:{}", e.getResponseBody());
ErrorResp errorResp = new ErrorResp();
errorResp.setCode(e.getErrorCode());
errorResp.setMsg(e.getErrorMessage());
errorResp.setStatus("ERROR");
return JSON.toJSONString(errorResp);
}
}public String query(OrderQueryReq param, RSAAutoCertificateConfig config) {
// QueryOrderByIdRequest queryRequest = new QueryOrderByIdRequest();
// queryRequest.setMchid(WxpayConfig.mchId);
// queryRequest.setTransactionId(param.get("transaction_id").toString());
QueryOrderByOutTradeNoRequest request = new QueryOrderByOutTradeNoRequest();
request.setMchid(WxpayConfig.mchId);
request.setOutTradeNo(param.getOrderNo());
try {
service = new JsapiService.Builder().config(config).build();
Transaction result = service.queryOrderByOutTradeNo(request);//service.queryOrderById(queryRequest);
return JSON.toJSONString(result);
} catch (ServiceException e) {
// API返回失败, 例如ORDER_NOT_EXISTS
LOG.error("code:{},message:{}", e.getErrorCode(), e.getErrorMessage());
LOG.error("reponseBody:{}", e.getResponseBody());
ErrorResp errorResp = new ErrorResp();
errorResp.setCode(e.getErrorCode());
errorResp.setMsg(e.getErrorMessage());
errorResp.setStatus("ERROR");
return JSON.toJSONString(errorResp);
}
}public String refund(OrderRefundReq param, RSAAutoCertificateConfig config) {
LOG.info("退款参数:{}", param.toString());
try {
Refund refund = refundService.create(param, config);
return JSON.toJSONString(refund);
} catch (ServiceException e) {
LOG.error("code:{},message:{}", e.getErrorCode(), e.getErrorMessage());
LOG.error("reponseBody:{}", e.getResponseBody());
ErrorResp errorResp = new ErrorResp();
errorResp.setCode(e.getErrorCode());
errorResp.setMsg(e.getErrorMessage());
errorResp.setStatus("ERROR");
return JSON.toJSONString(errorResp);
}
}public String refundQuery(Map<String, Object> param, RSAAutoCertificateConfig config) {
try {
Refund refund = refundService.queryByOutRefundNo(param, config);
return JSON.toJSONString(refund);
} catch (ServiceException e) {
LOG.error("code:{},message:{}", e.getErrorCode(), e.getErrorMessage());
LOG.error("reponseBody:{}", e.getResponseBody());
ErrorResp errorResp = new ErrorResp();
errorResp.setCode(e.getErrorCode());
errorResp.setMsg(e.getErrorMessage());
errorResp.setStatus("ERROR");
return JSON.toJSONString(errorResp);
}
}public String close(OrderQueryReq param, RSAAutoCertificateConfig config) {
LOG.info("关闭参数:{}", param.toString());
service = new JsapiService.Builder().config(config).build();
CloseOrderRequest closeRequest = new CloseOrderRequest();
closeRequest.setMchid(WxpayConfig.mchId);
closeRequest.setOutTradeNo(param.getOrderNo());
service.closeOrder(closeRequest);
return null;
}public String notify(HttpServletRequest request, HttpServletResponse response) {
return null;
}/**
- 发起商家转账
- ACCEPTED:已受理。批次已受理成功,若发起批量转账的30分钟后,转账批次单仍处于该状态,可能原因是商户账户余额不足等。商户可查询账户资金流水,
- 若该笔转账批次单的扣款已经发生,则表示批次已经进入转账中,请再次查单确认
- PROCESSING:转账中。已开始处理批次内的转账明细单
- FINISHED:已完成。批次内的所有转账明细单都已处理完成
- CLOSED:已关闭。可查询具体的批次关闭原因确认
- @return String
*/
public String initiateBatchTransfer(TransferDetailReq transferDetailReq, RSAAutoCertificateConfig config) {
transferBatchService = new TransferBatchService.Builder().config(config).build();
InitiateBatchTransferRequest request = new InitiateBatchTransferRequest();
BigDecimal amount = transferDetailReq.getTransferAmount().multiply(BigDecimal.valueOf(100));
request.setAppid(WxpayConfig.appId);
request.setOutBatchNo(transferDetailReq.getOutDetailNo());
request.setBatchName(transferDetailReq.getTransferRemark());
request.setBatchRemark(transferDetailReq.getTransferRemark());
request.setTotalAmount(amount.longValue());
request.setTotalNum(1);
TransferDetailInput transferDetailInput = new TransferDetailInput();
transferDetailInput.setOutDetailNo(transferDetailReq.getOutDetailNo() + "Z" + transferDetailReq.getId());
transferDetailInput.setTransferAmount(amount.longValue());
transferDetailInput.setTransferRemark(transferDetailReq.getTransferRemark());
transferDetailInput.setOpenid(transferDetailReq.getOpenId());
Listlist = new ArrayList<>();
list.add(transferDetailInput);
request.setTransferDetailList(list);
try {
InitiateBatchTransferResponse response = transferBatchService.initiateBatchTransfer(request);
String result = JSON.toJSONString(response);
LOG.info("转账结果:{}", result);
return result;
} catch (ServiceException e) {
LOG.error("code:{},message:{},statusCode:{}", e.getErrorCode(), e.getErrorMessage(), e.getHttpStatusCode());
LOG.error("reponseBody:{}", e.getResponseBody());
ErrorResp errorResp = new ErrorResp();
errorResp.setCode(e.getErrorCode());
errorResp.setMsg(e.getErrorMessage());
errorResp.setStatus("ERROR");
return JSON.toJSONString(errorResp);
}
}
public String initiateBatchTransferList(List
transferDetailList, RSAAutoCertificateConfig config) {
transferBatchService = new TransferBatchService.Builder().config(config).build();
InitiateBatchTransferRequest request = new InitiateBatchTransferRequest();
BigDecimal amountCount = transferDetailList.stream().map(TransferDetailReq::getTransferAmount).reduce(BigDecimal.ZERO, BigDecimal::add);
request.setAppid(WxpayConfig.appId);
request.setOutBatchNo(transferDetailList.get(0).getOutDetailNo());
request.setBatchName(transferDetailList.get(0).getTransferRemark());
request.setBatchRemark(transferDetailList.get(0).getTransferRemark());
request.setTotalAmount(amountCount.multiply(BigDecimal.valueOf(100)).longValue());
int count = transferDetailList.size();
request.setTotalNum(count);
Listlist = new ArrayList<>();
for (TransferDetailReq transferDetail : transferDetailList) {
TransferDetailInput transferDetailInput = new TransferDetailInput();
BigDecimal amount = transferDetail.getTransferAmount().multiply(BigDecimal.valueOf(100));
transferDetailInput.setOutDetailNo(transferDetail.getOutDetailNo() + "Z" + transferDetail.getId());
transferDetailInput.setTransferAmount(amount.longValue());
transferDetailInput.setTransferRemark(transferDetail.getTransferRemark());
transferDetailInput.setOpenid(transferDetail.getOpenId());
list.add(transferDetailInput);
}
request.setTransferDetailList(list);
try {
InitiateBatchTransferResponse response = transferBatchService.initiateBatchTransfer(request);
String result = JSON.toJSONString(response);
LOG.info("转账结果:{}", result);
return result;
} catch (ServiceException e) {
LOG.error("code:{},message:{},statusCode:{}", e.getErrorCode(), e.getErrorMessage(), e.getHttpStatusCode());
LOG.error("reponseBody:{}", e.getResponseBody());
ErrorResp errorResp = new ErrorResp();
errorResp.setCode(e.getErrorCode());
errorResp.setMsg(e.getErrorMessage());
errorResp.setStatus("ERROR");
return JSON.toJSONString(errorResp);
}
}/**
- 通过微信批次单号查询批次单
*/
public TransferBatchEntity getTransferBatchByNo() {
GetTransferBatchByNoRequest request = new GetTransferBatchByNoRequest();
return transferBatchService.getTransferBatchByNo(request);
}
/**
-
通过商家批次单号查询批次单
*/
public TransferBatchEntity getTransferBatchByOutNo() {GetTransferBatchByOutNoRequest request = new GetTransferBatchByOutNoRequest();
return transferBatchService.getTransferBatchByOutNo(request);
}
/**
-
通过微信明细单号查询明细单
*/
public TransferDetailEntity getTransferDetailByNo() {GetTransferDetailByNoRequest request = new GetTransferDetailByNoRequest();
return transferBatchService.getTransferDetailByNo(request);
}
/**
-
通过商家明细单号查询明细单
*/
public TransferDetailEntity getTransferDetailByOutNo() {GetTransferDetailByOutNoRequest request = new GetTransferDetailByOutNoRequest();
return transferBatchService.getTransferDetailByOutNo(request);
}
}
app支付实现类
package com.hyt.mall.pay.web.service.impl.wechat;
import com.alibaba.fastjson.JSON;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.core.exception.ServiceException;
import com.wechat.pay.java.core.util.NonceUtil;
import com.wechat.pay.java.service.payments.app.AppService;
import com.wechat.pay.java.service.payments.app.model.*;
import com.wechat.pay.java.service.payments.model.Transaction;
import com.wechat.pay.java.service.refund.model.Refund;
import com.hyt.mall.pay.web.config.WxpayConfig;
import com.hyt.mall.pay.web.req.OrderQueryReq;
import com.hyt.mall.pay.web.req.OrderRefundReq;
import com.hyt.mall.pay.web.req.OrderPayReq;
import com.hyt.mall.pay.web.req.AppPayReq;
import com.hyt.mall.pay.web.resp.ErrorResp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Map;
/**
-
@author zww
-
@Description:
-
@date 2023/3/10 11:14
*/
@Service
public class WechatAppServiceImpl {private static final Logger LOG = LoggerFactory.getLogger(WechatAppServiceImpl.class);
@Resource
private WechatRefundServiceImpl refundService;private AppService service;
public String pay(OrderPayReq param, RSAAutoCertificateConfig config) {
LOG.info("支付参数:{}", param.toString());
service = new AppService.Builder().config(config).build();
PrepayRequest request = new PrepayRequest();
Amount amount = new Amount();
BigDecimal payAmount = param.getAmount().multiply(BigDecimal.valueOf(100));
LOG.info("支付金额(分):{}",payAmount.intValue());
amount.setTotal(payAmount.intValue());
request.setAmount(amount);
request.setAppid(WxpayConfig.appId);
request.setMchid(WxpayConfig.mchId);
request.setDescription(param.getGoodsTitle());
request.setNotifyUrl(WxpayConfig.notifyDomain);
request.setOutTradeNo(param.getOrderNo());
//try {
PrepayResponse response = service.prepay(request);
LOG.info("返回参数:{}",response.toString());
AppPayReq payReq = new AppPayReq();
payReq.setAppId(WxpayConfig.appId);
payReq.setPartnerId(WxpayConfig.mchId);
payReq.setPrepayId(response.getPrepayId());
String timestamp = String.valueOf(System.currentTimeMillis()/1000);
payReq.setTimestamp(timestamp);
payReq.setPackageStr("Sign=WXPay");
payReq.setNonceStr(NonceUtil.createNonce(32));
payReq.setSign(WxpayConfig.generateSign(response.getPrepayId(), payReq.getNonceStr(), payReq.getTimestamp()));
return JSON.toJSONString(payReq);
// } catch (ServiceException e) {
// LOG.error("code:{},message:{}", e.getErrorCode(), e.getErrorMessage());
// LOG.error("reponseBody:{}", e.getResponseBody());
// ErrorResp errorResp = new ErrorResp();
// errorResp.setCode(e.getErrorCode());
// errorResp.setMsg(e.getErrorMessage());
// errorResp.setStatus("ERROR");
// return JSON.toJSONString(errorResp);
// }
}public String query(OrderQueryReq param, RSAAutoCertificateConfig config) {
//QueryOrderByIdRequest request = new QueryOrderByIdRequest();
QueryOrderByOutTradeNoRequest request = new QueryOrderByOutTradeNoRequest();
request.setMchid(WxpayConfig.mchId);
request.setOutTradeNo(param.getOrderNo());
//request.setTransactionId(param.get("transaction_id").toString());
try {
service = new AppService.Builder().config(config).build();
Transaction result = service.queryOrderByOutTradeNo(request);//service.queryOrderById(request);
return JSON.toJSONString(result);
} catch (ServiceException e) {
// API返回失败, 例如ORDER_NOT_EXISTS
LOG.error("code:{},message:{}", e.getErrorCode(), e.getErrorMessage());
LOG.error("reponseBody:{}", e.getResponseBody());
ErrorResp errorResp = new ErrorResp();
errorResp.setCode(e.getErrorCode());
errorResp.setMsg(e.getErrorMessage());
errorResp.setStatus("ERROR");
return JSON.toJSONString(errorResp);
}
}public String refund(OrderRefundReq param, RSAAutoCertificateConfig config) {
LOG.info("退款参数:{}", param.toString());
try {
Refund refund = refundService.create(param, config);
return JSON.toJSONString(refund);
} catch (ServiceException e) {
LOG.error("code:{},message:{}", e.getErrorCode(), e.getErrorMessage());
LOG.error("reponseBody:{}", e.getResponseBody());
ErrorResp errorResp = new ErrorResp();
errorResp.setCode(e.getErrorCode());
errorResp.setMsg(e.getErrorMessage());
errorResp.setStatus("ERROR");
return JSON.toJSONString(errorResp);
}
}public String refundQuery(Map<String, Object> param, RSAAutoCertificateConfig config) {
try {
Refund refund = refundService.queryByOutRefundNo(param,config);
return JSON.toJSONString(refund);
} catch (ServiceException e) {
LOG.error("code:{},message:{}", e.getErrorCode(), e.getErrorMessage());
LOG.error("reponseBody:{}", e.getResponseBody());
ErrorResp errorResp = new ErrorResp();
errorResp.setCode(e.getErrorCode());
errorResp.setMsg(e.getErrorMessage());
errorResp.setStatus("ERROR");
return JSON.toJSONString(errorResp);
}
}public String close(OrderQueryReq param, RSAAutoCertificateConfig config) {
LOG.info("关闭参数:{}", param.toString());
service = new AppService.Builder().config(config).build();
CloseOrderRequest closeRequest = new CloseOrderRequest();
closeRequest.setMchid(WxpayConfig.mchId);
closeRequest.setOutTradeNo(param.getOrderNo());
service.closeOrder(closeRequest);
return null;
}public String notify(HttpServletRequest request, HttpServletResponse response) {
return null;
}
}
调用类
package com.hyt.mall.pay.web.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hyt.mall.pay.web.service.impl.alipay.AlipayServiceImpl;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.hyt.mall.pay.web.config.WxpayConfig;
import com.hyt.mall.pay.web.req.OrderQueryReq;
import com.hyt.mall.pay.web.req.OrderRefundReq;
import com.hyt.mall.pay.web.req.OrderPayReq;
import com.hyt.mall.pay.web.req.TransferDetailReq;
import com.hyt.mall.pay.web.service.IPayService;
import com.hyt.mall.pay.web.service.IUrlService;
import com.hyt.mall.pay.web.service.impl.wechat.WechatAppServiceImpl;
import com.hyt.mall.pay.web.service.impl.wechat.WechatMiniServiceImpl;
import com.hyt.mall.pay.web.util.AesUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
-
@author zww
-
@Description:
-
@date 2023/3/3 14:08
*/
@Service
public class PayServiceImpl implements IPayService {private static final Logger LOG = LoggerFactory.getLogger(PayServiceImpl.class);
@Resource
private WechatMiniServiceImpl wechatMiniService;
@Resource
private WechatAppServiceImpl wechatAppService;
@Resource
private AlipayServiceImpl alipayService;
@Resource
private IUrlService urlService;private RSAAutoCertificateConfig config;
@Autowired
public void setConfig(){// 检查文件是否存在 防止本地调试时,项目启动异常File file = new File(WxpayConfig.privateKeyPath);if (!file.exists()) {LOG.error("\033[31m警告:未找到微信支付私钥文件,微信支付功能将被禁用\033[0m");return;}config = new RSAAutoCertificateConfig.Builder().merchantId(WxpayConfig.mchId).privateKeyFromPath(WxpayConfig.privateKeyPath).merchantSerialNumber(WxpayConfig.mchSerialNo).apiV3Key(WxpayConfig.apiV3Key).build();
}
@Override
public String pay(OrderPayReq param){
switch (param.getPayType()) {
case 1:
return wechatMiniService.pay(param,config);
case 2:
return wechatAppService.pay(param,config);
case 3:
return alipayService.pay(param);
default:
return null;
}
}@Override
public String query(OrderQueryReq param) {
switch (param.getPayType()) {
case 1:
return wechatMiniService.query(param,config);
case 2:
return wechatAppService.query(param,config);
case 3:
return alipayService.query(param);
default:
return null;
}
}@Override
public String refund(OrderRefundReq param) {
switch (param.getPayType()) {
case 1:
return wechatMiniService.refund(param,config);
case 2:
return wechatAppService.refund(param,config);
case 3:
return alipayService.refund(param);
default:
return null;
}
}@Override
public String refundQuery(Map<String, Object> param) {
switch (param.get("payType").toString()) {
case "1":
return wechatMiniService.refundQuery(param, config);
case "2":
return wechatAppService.refundQuery(param, config);
case "3":
return alipayService.refundQuery(param);
default:
return null;
}
}@Override
public String close(OrderQueryReq param) {
switch (param.getPayType()) {
case 1:
return wechatMiniService.close(param, config);
case 2:
return wechatAppService.close(param, config);
case 3:
return alipayService.close(param);
default:
return null;
}
}@Override
public String notify(HttpServletRequest request, HttpServletResponse response) {
return null;
}@Override
public Map<String,String> wxNotify(JSONObject jsonObject) {
//应答体
Map<String,String> map = new HashMap<>();
try {
String key = WxpayConfig.apiV3Key;
LOG.info("支付回调结果:{}", JSON.toJSONString(jsonObject));
String paymentId = jsonObject.getString("id");
JSONObject resourceObj = JSONObject.parseObject(JSON.toJSONString(jsonObject.get("resource")),JSONObject.class);
String associated_data = resourceObj.getString("associated_data");
String ciphertext = resourceObj.getString("ciphertext");
String nonce = resourceObj.getString("nonce");String decryptData = new AesUtil(key.getBytes(StandardCharsets.UTF_8)).decryptToString(associated_data.getBytes(StandardCharsets.UTF_8),nonce.getBytes(StandardCharsets.UTF_8), ciphertext);//验签成功JSONObject obj = JSONObject.parseObject(decryptData, JSONObject.class);if (obj.get("trade_state").toString().equals("SUCCESS")) {String orderNo = obj.get("out_trade_no").toString();//String tradeType = obj.get("trade_type").toString();int result = urlService.updateOrderStatus(orderNo, 1, obj.get("transaction_id").toString());if (result > 0) {map.put("code","SUCCESS");map.put("message","成功");} else {LOG.info("单号:{}, 异常:{}", obj.get("out_trade_no").toString(), "修改订单状态失败!");map.put("code","FAIL");map.put("message","失败");}} else {LOG.info("单号:{}, 交易状态:{},异常:{}", obj.get("out_trade_no").toString(), obj.get("trade_state").toString(), "微信支付失败!");map.put("code","FAIL");map.put("message","失败");}return map;} catch (Exception e){LOG.info("params{}, 异常:", jsonObject.toJSONString(), e);map.put("code","FAIL");map.put("message","失败");return map;}
}
@Override
public Map<String,String> wxRefundNotify(JSONObject jsonObject) {
//应答体
Map<String,String> map = new HashMap<>();
try {
String key = WxpayConfig.apiV3Key;
JSONObject resourceObj = JSONObject.parseObject(JSON.toJSONString(jsonObject.get("resource")),JSONObject.class);
String associated_data = resourceObj.getString("associated_data");
String ciphertext = resourceObj.getString("ciphertext");
String nonce = resourceObj.getString("nonce");String decryptData = new AesUtil(key.getBytes(StandardCharsets.UTF_8)).decryptToString(associated_data.getBytes(StandardCharsets.UTF_8),nonce.getBytes(StandardCharsets.UTF_8), ciphertext);//验签成功JSONObject obj = JSONObject.parseObject(decryptData, JSONObject.class);switch (obj.get("refund_status").toString()) {case "SUCCESS":JSONObject amountObj = JSONObject.parseObject(JSON.toJSONString(obj.get("amount")), JSONObject.class);Integer refundAmount = (Integer) amountObj.get("payer_refund");BigDecimal amount = BigDecimal.valueOf(refundAmount.floatValue()/100);int result = urlService.updateOrderRefundStatus(obj.get("out_trade_no").toString(), amount.setScale(2, BigDecimal.ROUND_HALF_UP));LOG.info("result:{}", result);if (result > 0) {map.put("code","SUCCESS");map.put("message","成功");} else {LOG.info("单号:{}, 异常:{}", obj.get("out_trade_no").toString(), "退款修改订单状态失败!");map.put("code", "FAIL");map.put("message", "退款关闭!");}return map;case "CLOSED":LOG.info("单号:{}, 异常:{}", obj.get("out_trade_no").toString(), "退款关闭!");map.put("code", "FAIL");map.put("message", "退款关闭!");break;case "ABNORMAL":LOG.info("单号:{}, 异常:{}", obj.get("out_trade_no").toString(), "退款异常,用户的卡作废或者冻结了!");map.put("code", "FAIL");map.put("message", "失败!退款异常,用户的卡作废或者冻结了!");break;default:map.put("code", "FAIL");map.put("message", "失败");break;}return map;}catch (Exception e){LOG.info("params{}, 异常:", jsonObject.toJSONString(), e);map.put("code","FAIL");map.put("message","失败");return map;}
}
@Override
public String initiateBatchTransfer(TransferDetailReq param) {
if (param.getTransferAmount().compareTo(BigDecimal.ZERO) <= 0) {
LOG.info("initiateBatchTransfer:{}", param.toString());
return null;
}
// String result = wechatMiniService.initiateBatchTransfer(param, config);
// JSONObject json = JSONObject.parseObject(result, JSONObject.class);
return wechatMiniService.initiateBatchTransfer(param, config);
}@Override
public String initiateBatchTransferList(Listlist) {
if (ObjectUtils.isEmpty(list) || list.size() < 1) {
return null;
}
return wechatMiniService.initiateBatchTransferList(list, config);
}
}