181 lines
7.3 KiB
Java
181 lines
7.3 KiB
Java
package com.ruoyi.cai.trdpay;
|
|
|
|
|
|
import cn.hutool.crypto.digest.DigestUtil;
|
|
import com.alibaba.fastjson2.JSON;
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
import com.ruoyi.cai.domain.Account;
|
|
import com.ruoyi.cai.enums.SystemConfigEnum;
|
|
import com.ruoyi.cai.manager.SystemConfigManager;
|
|
import com.ruoyi.cai.pay.PayManager;
|
|
import com.ruoyi.cai.pay.PayOrderInfoDTO;
|
|
import com.ruoyi.cai.pay.PayTypeEnum;
|
|
import com.ruoyi.cai.service.AccountService;
|
|
import com.ruoyi.common.exception.ServiceException;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.http.client.BufferingClientHttpRequestFactory;
|
|
import org.springframework.http.client.SimpleClientHttpRequestFactory;
|
|
import org.springframework.http.converter.HttpMessageConverter;
|
|
import org.springframework.http.converter.StringHttpMessageConverter;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
|
import java.math.BigDecimal;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Component
|
|
@Slf4j
|
|
public class TrdPayManager {
|
|
|
|
private final String CREATE_ORDER_URL = "http://pay.jpay.one/api/pay/create_order";
|
|
private final String QUERY_ORDER_URL = "http://pay.jpay.one/api/pay/query_order";
|
|
|
|
private static final String NOTIFY_ALI_URL = "/api/pay/trd/notify";
|
|
|
|
@Autowired
|
|
private TrdPayProperties trdPayProperties;
|
|
|
|
public static RestTemplate restTemplate;
|
|
|
|
static {
|
|
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
|
|
requestFactory.setConnectTimeout(3000);
|
|
requestFactory.setReadTimeout(3000);
|
|
restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(requestFactory));
|
|
List<HttpMessageConverter<?>> messageConverters = restTemplate.getMessageConverters();
|
|
//添加转换器
|
|
for (HttpMessageConverter<?> messageConverter : messageConverters) {
|
|
if (messageConverter instanceof StringHttpMessageConverter) {
|
|
StringHttpMessageConverter converter = (StringHttpMessageConverter) messageConverter;
|
|
converter.setDefaultCharset(StandardCharsets.UTF_8);
|
|
}
|
|
}
|
|
}
|
|
|
|
public JSONObject createOrderWx(PayOrderInfoDTO payOrderInfoDTO){
|
|
return createOrder(payOrderInfoDTO,trdPayProperties.getWxProductId());
|
|
}
|
|
|
|
public JSONObject createOrderAli(PayOrderInfoDTO payOrderInfoDTO){
|
|
return createOrder(payOrderInfoDTO,trdPayProperties.getAliProductId());
|
|
}
|
|
|
|
@Autowired
|
|
private AccountService accountService;
|
|
@Autowired
|
|
private SystemConfigManager systemConfigManager;
|
|
|
|
public boolean useTrdPay(Long userId){
|
|
Account account = accountService.getByUserId(userId);
|
|
if(account == null){
|
|
return true;
|
|
}
|
|
BigDecimal payLimit = systemConfigManager.getSystemConfigOfBigDecimal(SystemConfigEnum.PAY_LIMIT);
|
|
if(payLimit == null || payLimit.compareTo(BigDecimal.ZERO) == 0){
|
|
return false;
|
|
}
|
|
if(account.getTotalTrdMoney().compareTo(payLimit) >= 0){
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
@Autowired
|
|
private PayManager payManager;
|
|
|
|
public JSONObject resetOrder(String orderNo){
|
|
JSONObject jsonObject = queryOrder(orderNo);
|
|
Integer status = jsonObject.getInteger("status");
|
|
if(status != null && status.equals(2)){
|
|
String mchOrderNo = jsonObject.getString("mchOrderNo");
|
|
String payOrderId = jsonObject.getString("payOrderId");
|
|
String productId = jsonObject.getString("productId");
|
|
Map<String,String> objectJson = new HashMap<>();
|
|
for (String key : jsonObject.keySet()) {
|
|
objectJson.put(key, jsonObject.getString(key));
|
|
}
|
|
payManager.callBack(mchOrderNo,payOrderId,objectJson,productId, PayTypeEnum.TRD);
|
|
}
|
|
return jsonObject;
|
|
}
|
|
public JSONObject queryOrder(String orderNo){
|
|
Map<String, String> params = new HashMap<>();
|
|
params.put("mchId", trdPayProperties.getMchId());
|
|
params.put("mchOrderNo", orderNo);
|
|
String para = createParams(params);
|
|
String url = QUERY_ORDER_URL + "?" + para;
|
|
String body = restTemplate.getForEntity(url, String.class).getBody();
|
|
JSONObject jsonObject = JSON.parseObject(body);
|
|
if(jsonObject == null){
|
|
log.error("第三方支付查询失败 返回数据为空");
|
|
throw new ServiceException("调用支付失败");
|
|
}
|
|
if(!"SUCCESS".equals(jsonObject.getString("retCode"))){
|
|
log.info("第三方支付查询失败 第三方支付查询失败 url={}, body={}",url, body);
|
|
throw new ServiceException("调用支付失败");
|
|
}
|
|
log.info("第三方支付成功 URL={}, body={}",url, body);
|
|
return jsonObject;
|
|
}
|
|
|
|
|
|
public JSONObject createOrder(PayOrderInfoDTO payOrderInfoDTO,String productId){
|
|
Map<String, String> params = new HashMap<>();
|
|
params.put("mchId", trdPayProperties.getMchId());
|
|
params.put("productId", productId);
|
|
params.put("mchOrderNo", payOrderInfoDTO.getOrderNo());
|
|
params.put("amount", payOrderInfoDTO.getPriceFenStr());
|
|
String notifyUrl = trdPayProperties.getNotifyUrlDomain() + NOTIFY_ALI_URL;
|
|
params.put("notifyUrl", notifyUrl);
|
|
String para = createParams(params);
|
|
String url = CREATE_ORDER_URL + "?" + para;
|
|
String body = restTemplate.getForEntity(url, String.class).getBody();
|
|
JSONObject jsonObject = JSON.parseObject(body);
|
|
if(jsonObject == null){
|
|
log.error("第三方支付失败 返回数据为空");
|
|
throw new ServiceException("调用支付失败");
|
|
}
|
|
if(!"SUCCESS".equals(jsonObject.getString("retCode"))){
|
|
log.info("第三方支付失败 统一支付失败失败 url={}, body={}",url, body);
|
|
throw new ServiceException("调用支付失败");
|
|
}
|
|
log.info("第三方支付成功 URL={}, body={}",url, body);
|
|
return jsonObject;
|
|
}
|
|
|
|
private String createParams(Map<String, String> params){
|
|
List<String> url = new ArrayList<>();
|
|
for (Map.Entry<String, String> entry : params.entrySet()) {
|
|
url.add(entry.getKey()+"="+entry.getValue());
|
|
}
|
|
url = url.stream().sorted().collect(Collectors.toList());
|
|
String stringSignTemp = StringUtils.join(url, "&") + "&key=" + trdPayProperties.getSignKey();
|
|
String sign = DigestUtil.md5Hex(stringSignTemp).toUpperCase();
|
|
url.add("sign="+sign);
|
|
return StringUtils.join(url, "&");
|
|
}
|
|
|
|
|
|
public static void main(String[] args) {
|
|
TrdPayProperties payProperties = new TrdPayProperties();
|
|
payProperties.setMchId("10418");
|
|
payProperties.setNotifyUrlDomain("https://api.ssss.com");
|
|
PayOrderInfoDTO payOrderInfo = new PayOrderInfoDTO();
|
|
payOrderInfo.setPrice(new BigDecimal("10"));
|
|
payOrderInfo.setOrderNo("DSKsdasklddshjkasdka");
|
|
TrdPayManager trdPayManager = new TrdPayManager();
|
|
// trdPayManager.(payProperties);
|
|
trdPayManager.createOrder(payOrderInfo,"8000");
|
|
|
|
|
|
}
|
|
|
|
}
|