package com.chongxuan.web.pay.processor;
import com.chongxuan.web.config.cache.PayCacheUtil;
import com.chongxuan.web.config.cache.SysConfigCacheUtil;
import com.chongxuan.web.entity.MemberPayJour;
import com.chongxuan.web.entity.PayChannelNew;
import com.chongxuan.web.entity.PayPlatformConfig;
import com.chongxuan.web.entity.PayPlatformNew;
import com.chongxuan.web.mapper.MemberPayJourMapper;
import com.chongxuan.web.mapper.PayPlatformNewMapper;
import com.chongxuan.web.service.IPayService;
import com.chongxuan.web.util.DateFormatUtils;
import com.chongxuan.web.util.JsonUtil;
import com.chongxuan.web.vo.ReqPayRechargeNew;
import com.google.common.collect.Sets;
import com.google.common.io.CharStreams;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.math.BigDecimal;
import java.util.*;
@Log4j2
public class TogetherPayProcessor {
@Autowired
protected MemberPayJourMapper payJourMapper;
@Autowired
protected PayPlatformNewMapper payPlatformNewMapper;
@Autowired
protected RestTemplate restTemplate;
@Autowired
protected IPayService payService;
@Autowired
protected static SysConfigCacheUtil sysConfigCacheUtil;
@Autowired
protected PayCacheUtil payCacheUtil;
public static String orderPay(PayChannelNew payChannelNew, PayPlatformNew payPlatformNew, PayPlatformConfig payPlatformConfig, ReqPayRechargeNew reqPayRecharge) {
SortedMap<String, String> params = new TreeMap<>();
//下单商户id参数
params.put(payPlatformConfig.getPayMerid(), payPlatformNew.getMerId());
//支付通道参数
params.put(payPlatformConfig.getPayCode(), payChannelNew.getPayMethod());
//下单订单号参数
params.put(payPlatformConfig.getPayOrderno(), reqPayRecharge.getOrderNo());
//下单金额参数
String[] paymoney = payPlatformConfig.getPayMoney().split(",");
if ("0".equals(paymoney[1])) {
params.put(paymoney[0], reqPayRecharge.getMoney().setScale(2,
BigDecimal.ROUND_HALF_UP).toString());
} else if ("1".equals(paymoney[1])) {
params.put(paymoney[0], reqPayRecharge.getMoney().multiply(BigDecimal.valueOf(100)).setScale(0,
BigDecimal.ROUND_HALF_UP).toString());
}
//回调地址参数
if (payPlatformConfig.getPayCallbackurl() != null) {
params.put(payPlatformConfig.getPayCallbackurl(), sysConfigCacheUtil.getConf("payCallbackUrl") + payPlatformConfig.getPayCode());
}
//下单成功跳转地址参数
if (payPlatformConfig.getPayReturnurl() != null) {
params.put(payPlatformConfig.getPayReturnurl(), sysConfigCacheUtil.getConf("payReturnUrl"));
}
//下单IP参数
if (payPlatformConfig.getPayIp() != null) {
params.put(payPlatformConfig.getPayIp(), reqPayRecharge.getRealIp());
}
//下单应用id参数
if (payPlatformConfig.getPayAppid() != null) {
params.put(payPlatformConfig.getPayAppid(), payPlatformNew.getOrgId());
}
//下单时间戳参数,以逗号分隔,后面0为秒,1为毫秒
if (payPlatformConfig.getPayTimesecond() != null) {
String[] paytimescond = payPlatformConfig.getPayTimesecond().split(",");
if ("0".equals(paytimescond[1])) {
params.put(paytimescond[0], System.currentTimeMillis() + "");
} else if ("1".equals(paytimescond[1])) {
params.put(paytimescond[0], System.currentTimeMillis() / 1000 + "");
}
}
//下单时间格式参数,以逗号分隔,后面 0为yyyy-mm-dd HH:mm:ss, 1为yyyyMMddHHmmss
if (payPlatformConfig.getPayTime() != null) {
String[] paytime = payPlatformConfig.getPayTime().split(",");
if ("0".equals(paytime[1])) {
params.put(paytime[0], DateFormatUtils.formate(new Date(), "yyyy-MM-dd HH:mm:ss"));
} else if ("1".equals(paytime[1])) {
params.put(paytime[0], DateFormatUtils.formate(new Date(), "yyyyMMddHHmmss"));
}
}
//下单UUID任意值参数
if (payPlatformConfig.getPayUuidname() != null) {
params.put(payPlatformConfig.getPayUuidname(), UUID.randomUUID() + "");
}
//额外参数模块
if (payPlatformConfig.getPayParam1() != null) {
params.put(payPlatformConfig.getPayParam1(), payPlatformConfig.getPayParam1());
}
if (payPlatformConfig.getPayParam2() != null) {
params.put(payPlatformConfig.getPayParam2(), payPlatformConfig.getPayParam2());
}
if (payPlatformConfig.getPayParam3() != null) {
params.put(payPlatformConfig.getPayParam3(), payPlatformConfig.getPayParam3());
}
if (payPlatformConfig.getPayParam4() != null) {
String[] param4 = payPlatformConfig.getPayParam4().split(",");
params.put(param4[0], param4[1]);
}
if (payPlatformConfig.getPayParam5() != null) {
String[] param5 = payPlatformConfig.getPayParam5().split(",");
params.put(param5[0], param5[1]);
}
//下单加密排序方式 0为ASCII码排序
String tmpStr = null;
if (payPlatformConfig.getPaySort() == 0) {
tmpStr = assemblyUrl(params);
}
//下单拼接密钥方式 0为&key= 1为key= 2为直接拼接
switch (payPlatformConfig.getPaySplice()) {
case 0:
tmpStr = tmpStr + "&key=" + payPlatformNew.getSignMd5();
break;
case 1:
tmpStr = tmpStr + "key=" + payPlatformNew.getSignMd5();
break;
case 2:
tmpStr = tmpStr + payPlatformNew.getSignMd5();
break;
}
//下单MD5加密转大小写,0转大写,1转小写
String sign = null;
if (payPlatformConfig.getPayCase() == 0) {
sign = DigestUtils.md5Hex(tmpStr).toUpperCase();
} else if (payPlatformConfig.getPayCase() == 1) {
sign = DigestUtils.md5Hex(tmpStr).toLowerCase();
}
// 下单签名参数
params.put(payPlatformConfig.getPaySign(), sign);
//下单请求方式 0为FORM_URLENCODED 1为FROM_DATA 2为JSON 3为get
Map<String, Object> resultMap = null;
if (payPlatformConfig.getPayHttpmethod() == 0 || payPlatformConfig.getPayHttpmethod() == 1) {
HttpEntity<MultiValueMap<String, String>> httpEntity = null;
switch (payPlatformConfig.getPayHttpmethod()) {
case 0:
httpEntity = MFORM_URLENCODE(params);
break;
case 1:
httpEntity = MFORM_DATA(params);
break;
}
//下单返回值类型 0为String 1为Map, 2为html页面
if (payPlatformConfig.getPayReturntype() == 2) {
return FromHTML(httpEntity, payPlatformNew.getPlatPayUrl(), reqPayRecharge);
}
switch (payPlatformConfig.getPayReturntype()) {
case 0:
resultMap = FromString(payPlatformNew.getName(), httpEntity, payPlatformNew.getPlatPayUrl(), reqPayRecharge);
break;
case 1:
resultMap = FromMap(payPlatformNew.getName(), httpEntity, payPlatformNew.getPlatPayUrl(), reqPayRecharge);
break;
}
} else if (payPlatformConfig.getPayHttpmethod() == 2) {
HttpEntity<Map<String, String>> httpEntityMap = null;
httpEntityMap = MJSON(params);
//下单返回值类型 0为String 1为Map, 2为html页面
if (payPlatformConfig.getPayReturntype() == 2) {
return JsonHTML(httpEntityMap, payPlatformNew.getPlatPayUrl(), reqPayRecharge);
}
switch (payPlatformConfig.getPayReturntype()) {
case 0:
resultMap = JsonString(payPlatformNew.getName(), httpEntityMap, payPlatformNew.getPlatPayUrl(), reqPayRecharge);
break;
case 1:
resultMap = JsonMap(payPlatformNew.getName(), httpEntityMap, payPlatformNew.getPlatPayUrl(), reqPayRecharge);
break;
}
} else if (payPlatformConfig.getPayHttpmethod() == 3) {
if (payPlatformConfig.getPayReturntype() == 2) {
return getForObjectHTML(params, payPlatformNew.getPlatPayUrl(), payPlatformNew.getName());
}
switch (payPlatformConfig.getPayReturntype()) {
case 0:
resultMap = getForObjectString(params, payPlatformNew.getPlatPayUrl(), payPlatformNew.getName());
break;
case 1:
resultMap = getForObjectMap(params, payPlatformNew.getPlatPayUrl(), payPlatformNew.getName());
break;
}
}
if (!CollectionUtils.isEmpty(resultMap)) {
//下单返回状态判断参数
if (payPlatformConfig.getPayStatus() != null) {
String[] paystatus = payPlatformConfig.getPayStatus().split(",");
String status = resultMap.getOrDefault(paystatus[0], "").toString();
if (status.equals(paystatus[1])) {
log.warn(payPlatformNew.getName() + "支付下单成功,订单号:{}", reqPayRecharge.getOrderNo());
} else {
reqPayRecharge.setFailReason(resultMap.getOrDefault(payPlatformConfig.getPayFailreason(), "").toString());
log.warn(payPlatformNew.getName() + "支付下单失败,订单号:{}", reqPayRecharge.getOrderNo());
return null;
}
}
/** 下单返回嵌套map的参数名称,如没有为空 */
if (payPlatformConfig.getPayMapname() != null) {
String paymapname = payPlatformConfig.getPayMapname();
Map<String, String> dataMap = (Map<String, String>) resultMap.getOrDefault(paymapname, "");
//返回链接参数
return dataMap.getOrDefault(payPlatformConfig.getPayUrl(), "");
}
return resultMap.getOrDefault(payPlatformConfig.getPayUrl(), "").toString();
}
log.warn(payPlatformNew.getName() + "支付下单失败,订单号:{}", reqPayRecharge.getOrderNo());
return null;
}
public boolean queryPay(MemberPayJour payJour, PayPlatformNew payPlatformNew, PayPlatformConfig payPlatformConfig, PayChannelNew payChannelNew) {
SortedMap<String, String> params = new TreeMap<>();
//查询商户id参数
params.put(payPlatformConfig.getQueryMerid(), payPlatformNew.getMerId());
//查询订单号参数I
params.put(payPlatformConfig.getQueryOrderno(), payJour.getOrderNo());
//查询时间戳参数,以逗号分隔,后面0为秒,1为毫秒
if (payPlatformConfig.getQueryTimesecond() != null) {
String[] querytimesecond = payPlatformConfig.getQueryTimesecond().split(",");
if ("0".equals(querytimesecond[1])) {
params.put(querytimesecond[0], System.currentTimeMillis() + "");
} else if ("1".equals(querytimesecond[1])) {
params.put(querytimesecond[0], System.currentTimeMillis() / 1000 + "");
}
}
//查询时间格式参数,以逗号分隔,后面 0为yyyy-mm-dd HH:mm:ss, 1为yyyyMMddHHmmss
if (payPlatformConfig.getQueryTime() != null) {
String[] querytime = payPlatformConfig.getQueryTime().split(",");
if ("0".equals(querytime[1])) {
params.put(querytime[0], DateFormatUtils.formate(new Date(), "yyyy-MM-dd HH:mm:ss"));
} else if ("1".equals(querytime[1])) {
params.put(querytime[0], DateFormatUtils.formate(new Date(), "yyyyMMddHHmmss"));
}
}
//查询UUID任意值参数
if (payPlatformConfig.getQueryUuidname() != null) {
params.put(payPlatformConfig.getQueryUuidname(), UUID.randomUUID() + "");
}
//查询应用ID参数
if (payPlatformConfig.getQueryAppid() != null) {
params.put(payPlatformConfig.getQueryAppid(), payPlatformNew.getOrgId());
}
//额外参数模块,以逗号分隔
if (payPlatformConfig.getQueryParam1() != null) {
String[] queryparam1 = payPlatformConfig.getQueryParam1().split(",");
params.put(queryparam1[0], queryparam1[1]);
}
//查询加密排序方式 0为ASCII码排序
String tmpStr = null;
if (payPlatformConfig.getQuerySort() == 0) {
tmpStr = this.assemblyUrl(params);
}
//查询拼接密钥方式 0为&key= 1为key= 2为直接拼接
switch (payPlatformConfig.getQuerySplice()) {
case 0:
tmpStr = tmpStr + "&key=" + payPlatformNew.getSignMd5();
break;
case 1:
tmpStr = tmpStr + "key=" + payPlatformNew.getSignMd5();
break;
case 2:
tmpStr = tmpStr + payPlatformNew.getSignMd5();
break;
}
//查询MD5加密转大小写,0转大写,1转小写
String sign = null;
if (payPlatformConfig.getQueryCase() == 0) {
sign = DigestUtils.md5Hex(tmpStr).toUpperCase();
} else if (payPlatformConfig.getQueryCase() == 1) {
sign = DigestUtils.md5Hex(tmpStr).toLowerCase();
}
//查询签名参数
params.put(payPlatformConfig.getQuerySign(), sign);
//查询请求方式 0为FORM_URLENCODED 1为FROM_DATA 2为JSON 3为get
Map<String, Object> resultMap = null;
if (payPlatformConfig.getQueryHttpmethod() == 0 || payPlatformConfig.getQueryHttpmethod() == 1) {
HttpEntity<MultiValueMap<String, String>> httpEntity = null;
switch (payPlatformConfig.getQueryHttpmethod()) {
case 0:
httpEntity = this.MFORM_URLENCODE(params);
break;
case 1:
httpEntity = this.MFORM_DATA(params);
break;
}
//查询返回值类型 0为String 1为Map
switch (payPlatformConfig.getQueryReturntype()) {
case 0:
resultMap = QueryFromString(payPlatformNew.getName(), httpEntity, payPlatformNew.getPlatQueryUrl());
break;
case 1:
resultMap = QueryFromMap(payPlatformNew.getName(), httpEntity, payPlatformNew.getPlatQueryUrl());
break;
}
} else if (payPlatformConfig.getQueryHttpmethod() == 2) {
HttpEntity<Map<String, String>> httpEntityMap = null;
httpEntityMap = this.MJSON(params);
//查询返回值类型 0为String 1为Map
switch (payPlatformConfig.getQueryReturntype()) {
case 0:
resultMap = QueryJsonString(payPlatformNew.getName(), httpEntityMap, payPlatformNew.getPlatQueryUrl());
break;
case 1:
resultMap = QueryJsonMap(payPlatformNew.getName(), httpEntityMap, payPlatformNew.getPlatQueryUrl());
break;
}
} else if (payPlatformConfig.getQueryHttpmethod() == 3) {
switch (payPlatformConfig.getQueryReturntype()) {
case 0:
resultMap = QuerygetForObjectString(params, payPlatformNew.getPlatQueryUrl(), payPlatformNew.getName());
break;
case 1:
resultMap = QuerygetForObjectMap(params, payPlatformNew.getPlatQueryUrl(), payPlatformNew.getName());
break;
}
}
if (!CollectionUtils.isEmpty(resultMap)) {
//查询返回状态判断参数
if (payPlatformConfig.getQueryStatus() != null) {
String[] paystatus = payPlatformConfig.getQueryStatus().split(",");
String status = resultMap.getOrDefault(paystatus[0], "").toString();
if (status.equals(paystatus[1])) {
log.warn(payPlatformNew.getName() + "支付查询成功,订单号:{}", payJour.getOrderNo());
} else {
log.warn(payPlatformNew.getName() + "支付查询失败,订单号:{}", payJour.getOrderNo());
return false;
}
}
// 查询返回金额参数名称,以逗号分隔,后面0为元,1为分
String[] querymoney = payPlatformConfig.getQueryMoney().split(",");
//查询返回平台订单号参数名称
String querysanorderno = payPlatformConfig.getQuerySanorderno();
// 查询返回嵌套map的参数名称,如没有为空
if (payPlatformConfig.getQueryMapname() != null) {
String paymapname = payPlatformConfig.getQueryMapname();
Map<String, String> dataMap = (Map<String, String>) resultMap.getOrDefault(paymapname, "");
//返回金额,平台订单号参数
if ("0".equals(querymoney[1])) {
payJour.setMoney(new BigDecimal(dataMap.getOrDefault(querymoney[0], "")).multiply(BigDecimal.valueOf(100)).setScale(0,
BigDecimal.ROUND_HALF_UP));
} else {
payJour.setMoney(new BigDecimal(dataMap.getOrDefault(querymoney[0], "")));
}
payJour.setTradeSn(dataMap.getOrDefault(querysanorderno, ""));
return true;
}
if ("0".equals(querymoney[1])) {
payJour.setMoney(new BigDecimal(resultMap.getOrDefault(querymoney[0], "").toString()).multiply(BigDecimal.valueOf(100)).setScale(0,
BigDecimal.ROUND_HALF_UP));
} else {
payJour.setMoney(new BigDecimal(resultMap.getOrDefault(querymoney[0], "").toString()));
}
payJour.setTradeSn(resultMap.getOrDefault(querysanorderno, "").toString());
return true;
}
log.warn(payPlatformNew.getName() + "支付查询失败,订单号:{}", payJour.getOrderNo());
return false;
}
public String callbackPay(Map<String, Object> requestMap, PayPlatformConfig payPlatformConfig, String realIp) {
//商户订单ID参数
String callbackorderno = requestMap.getOrDefault(payPlatformConfig.getCallbackOrderno(), "").toString();
if(payPlatformConfig.getCallbackParam1() != null){
requestMap.remove(payPlatformConfig.getCallbackParam1());
}
if(payPlatformConfig.getCallbackParam2() != null){
requestMap.remove(payPlatformConfig.getCallbackParam2());
}
MemberPayJour payJour = payJourMapper.findByOrderNo(callbackorderno);
if (Objects.nonNull(payJour) && Strings.isNotBlank(payJour.getStatus())) {
if ("1".equals(payJour.getStatus())) {
log.warn("订单已成功,无需继续回调 - orderNo:{}", callbackorderno);
return payPlatformConfig.getCallbackSuccess();
}
}
PayPlatformNew payPlatformNew = payCacheUtil.getPayPlatform(payJour.getPlatformId());
if (this.verifyIP(requestMap, realIp, payPlatformNew)) {
return "fail";
}
PayChannelNew payChannelNew = payCacheUtil.getPayChannel(payJour.getChannelId());
if (this.diffPayTime12Hour(payJour.getPayTime(), callbackorderno)) {
return "fail";
}
if (!payChannelNew.getIsCanCallback()) {
log.warn("平台已拒绝三方支付通道回调 - 三方支付平台:{};三方支付编码:{};orderNo:{}", payPlatformNew.getName(),
payChannelNew.getName(), callbackorderno);
return "fail";
}
//查询加密排序方式 0为ASCII码排序
String tmpStr = null;
if (payPlatformConfig.getCallbackSort() == 0) {
SortedMap<String, String> params = new TreeMap(requestMap);
tmpStr = this.assemblyUrl(params);
}
//查询拼接密钥方式 0为&key= 1为key= 2为直接拼接
switch (payPlatformConfig.getCallbackSplice()) {
case 0:
tmpStr = tmpStr + "&key=" + payPlatformNew.getSignMd5();
break;
case 1:
tmpStr = tmpStr + "key=" + payPlatformNew.getSignMd5();
break;
case 2:
tmpStr = tmpStr + payPlatformNew.getSignMd5();
break;
}
String signMd5 = DigestUtils.md5Hex(tmpStr);
//三方平台订单ID参数
String callbacksanorderno = requestMap.getOrDefault(payPlatformConfig.getCallbackSanorderno(), "").toString();
//回调状态参数名称
String[] callbackstatus = payPlatformConfig.getCallbackStatus().split(",");
String status = requestMap.getOrDefault(callbackstatus[0], "").toString();
//回调金额参数名称
String[] callbackmoney = payPlatformConfig.getCallbackMoney().split(",");
String money = requestMap.getOrDefault(callbackmoney[0], "").toString();
//回调签名参数名称
String callbacksign = requestMap.getOrDefault(payPlatformConfig.getCallbackSign(), "").toString();
log.warn("支付回调:{}" , callbacksign + "_" + signMd5);
if (callbacksign.equalsIgnoreCase(signMd5)) {
if (status.equals(callbackstatus[1])) {
if (this.queryPay(payJour, payPlatformNew, payPlatformConfig, payChannelNew)) {
if("0".equals(callbackmoney[1])) {
payJour.setSubMoney(new BigDecimal(money));
} else {
payJour.setSubMoney(new BigDecimal(money).multiply(BigDecimal.valueOf(100)).setScale(0,
BigDecimal.ROUND_HALF_UP));
}
payJour.setTradeSn(callbacksanorderno);
return payService.updatePayJourStatus(payJour, new String[]{payPlatformConfig.getCallbackSuccess(), "fail"}, payChannelNew.getName());
}
}
}
return "fail";
}
protected boolean diffPayTime12Hour( String payTime, String merOrderNo ) {
Date payTimeDate = DateFormatUtils.parse( payTime );
// 计算当前时间与下单时间相差的小时数
int hourDiff = ( int ) ( ( System.currentTimeMillis() - payTimeDate.getTime() ) / ( 1000 * 60 * 60 ) );
// 超过48小时拒绝回调,可人工补单
if ( hourDiff >= 48 ) {
log.warn( "超过48小时拒绝回调 - orderNo:{};payTime:{}", merOrderNo, payTime );
return true;
}
return false;
}
protected boolean verifyIP(Map<String, ?> requestMap, String realIp, PayPlatformNew payPlatformNew ) {
if ( org.springframework.util.StringUtils.hasText( payPlatformNew.getPlatWhiteIpList() ) ) {
Set<String> whiteIpSet = Sets.newHashSet( payPlatformNew.getPlatWhiteIpList().split( "," ) );
if ( !whiteIpSet.contains( realIp ) && !"0:0:0:0:0:0:0:1".equals( realIp ) ) {
log.warn( "请求ip非白名单:{};支付平台:{};request:{}", realIp, payPlatformNew.getName(),
JsonUtil.object2Json( requestMap ) );
return true;
}
}
return false;
}
//ASCII码排序
protected static String assemblyUrl(Map<String, ?> bodyMap) {
//字典顺序a----z
StringBuilder sb = new StringBuilder();
bodyMap.forEach((k, v) -> sb.append(k).append("=").append(v).append("&"));
return sb.substring(0, sb.length() - 1);
}
//请求方式get,返回值类型Map.class
public static Map<String, Object> getForObjectMap(SortedMap<String, String> params, String platPayUrl, String name) {
MultiValueMap<String, String> requestMap = new LinkedMultiValueMap<>();
requestMap.setAll(params);
UriComponents uriComponents = UriComponentsBuilder.fromUriString(platPayUrl).queryParams(requestMap).build();
Map<String, Object> resultMap = null;
try {
RestTemplate restTemplate = new RestTemplate();
resultMap = restTemplate.getForObject(uriComponents.toUriString(), Map.class);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
log.warn(name + "支付下单结果:{}", JsonUtil.object2Json(resultMap));
if (!CollectionUtils.isEmpty(resultMap)) {
return resultMap;
}
log.warn(name + "支付下单失败");
return null;
}
//请求方式get,返回值类型String.class
public static Map<String, Object> getForObjectString(SortedMap<String, String> params, String platPayUrl, String name) {
MultiValueMap<String, String> requestMap = new LinkedMultiValueMap<>();
requestMap.setAll(params);
UriComponents uriComponents = UriComponentsBuilder.fromUriString(platPayUrl).queryParams(requestMap).build();
String res = null;
try {
RestTemplate restTemplate = new RestTemplate();
res = restTemplate.getForObject(uriComponents.toUriString(), String.class);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
log.warn(name + "支付下单结果:{}", res);
if (StringUtils.isNotBlank(res)) {
Map<String, Object> resultMap = JsonUtil.json2Map(res);
return resultMap;
}
log.warn(name + "支付下单失败");
return null;
}
//请求方式get,返回值类型HTML
public static String getForObjectHTML(SortedMap<String, String> params, String platPayUrl, String name) {
MultiValueMap<String, String> requestMap = new LinkedMultiValueMap<>();
requestMap.setAll(params);
UriComponents uriComponents = UriComponentsBuilder.fromUriString(platPayUrl).queryParams(requestMap).build();
String res = null;
try {
RestTemplate restTemplate = new RestTemplate();
res = restTemplate.getForObject(uriComponents.toUriString(), String.class);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
return res;
}
//请求方式FORM_URLENCODED
public static HttpEntity<MultiValueMap<String, String>> MFORM_URLENCODE(SortedMap<String, String> params) {
MultiValueMap<String, String> requestMap = new LinkedMultiValueMap<>();
requestMap.setAll(params);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(requestMap, httpHeaders);
return httpEntity;
}
//请求方式FORM_DATA
public static HttpEntity<MultiValueMap<String, String>> MFORM_DATA(SortedMap<String, String> params) {
MultiValueMap<String, String> requestMap = new LinkedMultiValueMap<>();
requestMap.setAll(params);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
HttpEntity<MultiValueMap<String, String>> httpEntity = new HttpEntity<>(requestMap, httpHeaders);
return httpEntity;
}
//请求方式Json
public static HttpEntity<Map<String, String>> MJSON(SortedMap<String, String> params) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Map<String, String>> httpEntity = new HttpEntity(params, httpHeaders);
return httpEntity;
}
//请求方式From,返回值类型Map.class
public static Map<String, Object> FromMap(String name, HttpEntity<MultiValueMap<String, String>> httpEntity, String platPayUrl, ReqPayRechargeNew reqPayRecharge) {
Map<String, Object> resultMap = null;
try {
RestTemplate restTemplate = new RestTemplate();
resultMap = restTemplate.execute( platPayUrl, HttpMethod.POST,
restTemplate.httpEntityCallback( httpEntity ), response -> {
InputStream bodyStream = response.getBody();
String text;
try ( Reader reader = new InputStreamReader( bodyStream ) ) {
text = CharStreams.toString( reader );
}
return JsonUtil.json2Map( text );
} );
} catch (Exception e) {
reqPayRecharge.setFailReason(e.getMessage());
}
log.warn(name + "支付下单结果:{}", JsonUtil.object2Map(resultMap));
if (!CollectionUtils.isEmpty(resultMap)) {
return resultMap;
}
log.warn(name + "支付下单失败");
return null;
}
//请求方式From,返回值类型String.class
public static Map<String, Object> FromString(String name, HttpEntity<MultiValueMap<String, String>> httpEntity, String platPayUrl, ReqPayRechargeNew reqPayRecharge) {
String res = null;
try {
RestTemplate restTemplate = new RestTemplate();
res = restTemplate.postForObject(platPayUrl, httpEntity, String.class);
} catch (Exception e) {
reqPayRecharge.setFailReason(e.getMessage());
}
log.warn(name + "支付下单结果:{}", res);
if (StringUtils.isNotBlank(res)) {
Map<String, Object> resultMap = JsonUtil.json2Map(res);
return resultMap;
}
log.warn(name + "支付下单失败");
return null;
}
//请求方式From,返回值类型HTML
public static String FromHTML(HttpEntity<MultiValueMap<String, String>> httpEntity, String platPayUrl, ReqPayRechargeNew reqPayRecharge) {
String res = null;
try {
RestTemplate restTemplate = new RestTemplate();
res = restTemplate.postForObject(platPayUrl, httpEntity, String.class);
} catch (Exception e) {
reqPayRecharge.setFailReason(e.getMessage());
}
return res;
}
//请求方式Json,返回值类型Map.class
public static Map<String, Object> JsonMap(String name, HttpEntity<Map<String, String>> httpEntity, String platPayUrl, ReqPayRechargeNew reqPayRecharge) {
Map<String, Object> resultMap = null;
try {
RestTemplate restTemplate = new RestTemplate();
resultMap = restTemplate.execute( platPayUrl, HttpMethod.POST,
restTemplate.httpEntityCallback( httpEntity ), response -> {
InputStream bodyStream = response.getBody();
String text;
try ( Reader reader = new InputStreamReader( bodyStream ) ) {
text = CharStreams.toString( reader );
}
return JsonUtil.json2Map( text );
} );
} catch (Exception e) {
reqPayRecharge.setFailReason(e.getMessage());
}
log.warn(name + "支付下单结果:{}", JsonUtil.object2Map(resultMap));
if (!CollectionUtils.isEmpty(resultMap)) {
return resultMap;
}
log.warn(name + "支付下单失败");
return null;
}
//请求方式Json,返回值类型String.class
public static Map<String, Object> JsonString(String name, HttpEntity<Map<String, String>> httpEntity, String platPayUrl, ReqPayRechargeNew reqPayRecharge) {
String res = null;
try {
RestTemplate restTemplate = new RestTemplate();
res = restTemplate.postForObject(platPayUrl, httpEntity, String.class);
} catch (Exception e) {
reqPayRecharge.setFailReason(e.getMessage());
}
log.warn(name + "支付下单结果:{}", res);
if (StringUtils.isNotBlank(res)) {
Map<String, Object> resultMap = JsonUtil.json2Map(res);
return resultMap;
}
log.warn(name + "支付下单失败");
return null;
}
//请求方式Json,返回值类型HTML
public static String JsonHTML(HttpEntity<Map<String, String>> httpEntity, String platPayUrl, ReqPayRechargeNew reqPayRecharge) {
String res = null;
try {
RestTemplate restTemplate = new RestTemplate();
res = restTemplate.postForObject(platPayUrl, httpEntity, String.class);
} catch (Exception e) {
reqPayRecharge.setFailReason(e.getMessage());
}
return res;
}
//----------------------查询分隔线------------------------
//请求方式get,返回值类型Map.class
public Map<String, Object> QuerygetForObjectMap(SortedMap<String, String> params, String platQueryUrl, String name) {
MultiValueMap<String, String> requestMap = new LinkedMultiValueMap<>();
requestMap.setAll(params);
UriComponents uriComponents = UriComponentsBuilder.fromUriString(platQueryUrl).queryParams(requestMap).build();
Map<String, Object> resultMap = null;
try {
RestTemplate restTemplate = new RestTemplate();
resultMap = restTemplate.getForObject(uriComponents.toUriString(), Map.class);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
log.warn(name + "支付查询结果:{}", JsonUtil.object2Json(resultMap));
if (!CollectionUtils.isEmpty(resultMap)) {
return resultMap;
}
log.warn(name + "支付查询失败");
return null;
}
//请求方式get,返回值类型String.class
public Map<String, Object> QuerygetForObjectString(SortedMap<String, String> params, String platQueryUrl, String name) {
MultiValueMap<String, String> requestMap = new LinkedMultiValueMap<>();
requestMap.setAll(params);
UriComponents uriComponents = UriComponentsBuilder.fromUriString(platQueryUrl).queryParams(requestMap).build();
String res = null;
try {
res = restTemplate.getForObject(uriComponents.toUriString(), String.class);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
log.warn(name + "支付查询结果:{}", res);
if (StringUtils.isNotBlank(res)) {
Map<String, Object> resultMap = JsonUtil.json2Map(res);
return resultMap;
}
log.warn(name + "支付查询失败");
return null;
}
//请求方式From,返回值类型Map.class
public Map<String, Object> QueryFromMap(String name, HttpEntity<MultiValueMap<String, String>> httpEntity, String platQueryUrl) {
Map<String, Object> resultMap = null;
try {
resultMap = restTemplate.execute( platQueryUrl, HttpMethod.POST,
restTemplate.httpEntityCallback( httpEntity ), response -> {
InputStream bodyStream = response.getBody();
String text;
try ( Reader reader = new InputStreamReader( bodyStream ) ) {
text = CharStreams.toString( reader );
}
return JsonUtil.json2Map( text );
} );
} catch (Exception e) {
log.error(e.getMessage(), e);
}
log.warn(name + "支付查询结果:{}", JsonUtil.object2Map(resultMap));
if (!CollectionUtils.isEmpty(resultMap)) {
return resultMap;
}
log.warn(name + "支付查询失败");
return null;
}
//请求方式From,返回值类型String.class
public Map<String, Object> QueryFromString(String name, HttpEntity<MultiValueMap<String, String>> httpEntity, String platQueryUrl) {
String res = null;
try {
res = restTemplate.postForObject(platQueryUrl, httpEntity, String.class);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
log.warn(name + "支付查询结果:{}", res);
if (StringUtils.isNotBlank(res)) {
Map<String, Object> resultMap = JsonUtil.json2Map(res);
return resultMap;
}
log.warn(name + "支付查询失败");
return null;
}
//请求方式Json,返回值类型Map.class
public Map<String, Object> QueryJsonMap(String name, HttpEntity<Map<String, String>> httpEntity, String platQueryUrl) {
Map<String, Object> resultMap = null;
try {
resultMap = restTemplate.execute( platQueryUrl, HttpMethod.POST,
restTemplate.httpEntityCallback( httpEntity ), response -> {
InputStream bodyStream = response.getBody();
String text;
try ( Reader reader = new InputStreamReader( bodyStream ) ) {
text = CharStreams.toString( reader );
}
return JsonUtil.json2Map( text );
} );
} catch (Exception e) {
log.error(e.getMessage(), e);
}
log.warn(name + "支付查询结果:{}", JsonUtil.object2Map(resultMap));
if (!CollectionUtils.isEmpty(resultMap)) {
return resultMap;
}
log.warn(name + "支付查询失败");
return null;
}
//请求方式Json,返回值类型String.class
public Map<String, Object> QueryJsonString(String name, HttpEntity<Map<String, String>> httpEntity, String platQueryUrl) {
String res = null;
try {
res = restTemplate.postForObject(platQueryUrl, httpEntity, String.class);
} catch (Exception e) {
log.error(e.getMessage(), e);
}
log.warn(name + "支付查询结果:{}", res);
if (StringUtils.isNotBlank(res)) {
Map<String, Object> resultMap = JsonUtil.json2Map(res);
return resultMap;
}
log.warn(name + "支付查询失败");
return null;
}
}
智能对接支付
最新推荐文章于 2025-10-04 03:19:20 发布
该代码实现了一个支付平台接口处理类,包括下单、查询和回调处理功能。根据配置参数,对请求参数进行签名加密,并通过HTTP请求调用支付平台API。同时,类中包含了对回调数据的验证和订单状态更新操作。
3302

被折叠的 条评论
为什么被折叠?



