嗨森逛服务
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
hai-server/hai-service/src/main/java/com/hai/config/UnionPayConfig.java

135 lines
5.2 KiB

package com.hai.config;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hai.common.pay.util.sdk.WXPayConstants;
import com.hai.common.utils.HttpsUtils;
import com.hai.common.utils.MD5Util;
import org.apache.commons.lang3.StringUtils;
import java.math.BigDecimal;
import java.util.*;
/**
* 银联支付
* @author hurui
*/
public class UnionPayConfig {
/**
* 【注意】请勿随意调用,会更新签名秘钥。会导致接口签名错误
* 更新并获取签名密钥
* 文档地址: https://doc.eycard.cn/web/#/42?page_id=542
* @return 响应信息
* @throws Exception
*/
private static JSONObject getSign() throws Exception {
Map<String,Object> paramMap = new HashMap<>();
paramMap.put("channelid", CommonSysConst.getSysConfig().getUnionPayChannelid());
paramMap.put("opt", "getSign");
return HttpsUtils.doPostForm(CommonSysConst.getSysConfig().getUnionPayUrl(), generateRequestParam(paramMap), new HashMap<>());
}
/**
* APP控件支付
* 文档地址: https://doc.eycard.cn/web/#/42?page_id=1516
* @param orderNo 订单号
* @param payPrice 支付金额 【单位:分】
* @param orderDesc 订单描述 或 商品描述;例如:购买中石化加油券300元。
* @return 响应信息
* @throws Exception
*/
public static JSONObject upPreOrder(String orderNo, BigDecimal payPrice, String orderDesc) throws Exception {
Map<String,Object> paramMap = new HashMap<>();
paramMap.put("channelid", CommonSysConst.getSysConfig().getUnionPayChannelid());
paramMap.put("merid", CommonSysConst.getSysConfig().getUnionPayMerid());
paramMap.put("termid", CommonSysConst.getSysConfig().getUnionPayTermid());
paramMap.put("tradetrace", orderNo);
paramMap.put("opt", "upPreOrder");
paramMap.put("tradetype", "APP");
paramMap.put("tradeamt", payPrice.multiply(new BigDecimal("100")).intValue());
paramMap.put("body", orderDesc);
paramMap.put("notifyurl", CommonSysConst.getSysConfig().getUnionPayNotifyUrl());
return HttpsUtils.doPostForm(CommonSysConst.getSysConfig().getUnionPayUrl(), generateRequestParam(paramMap), new HashMap<>());
}
/**
* 交易查询
* @param paySerialNo 第三方支付流水
* @return
* @throws Exception
*/
public static JSONObject tradeQuery(String paySerialNo) throws Exception {
Map<String,Object> paramMap = new HashMap<>();
paramMap.put("channelid", CommonSysConst.getSysConfig().getUnionPayChannelid());
paramMap.put("merid", CommonSysConst.getSysConfig().getUnionPayMerid());
paramMap.put("termid", CommonSysConst.getSysConfig().getUnionPayTermid());
paramMap.put("tradetrace", paySerialNo);
paramMap.put("opt", "tradeQuery");
paramMap.put("tradetype", "samecardQuery");
return HttpsUtils.doPostForm(CommonSysConst.getSysConfig().getUnionPayUrl(), generateRequestParam(paramMap), new HashMap<>());
}
/**
* 交易退货
* @param orderNo 订单号
* @param paySerialNo 第三方订单号
* @param refundPrice 退款金额
* @return
* @throws Exception
*/
public static JSONObject zwrefund(String orderNo,String paySerialNo,Long refundPrice) throws Exception {
Map<String,Object> paramMap = new HashMap<>();
paramMap.put("channelid", CommonSysConst.getSysConfig().getUnionPayChannelid());
paramMap.put("merid", CommonSysConst.getSysConfig().getUnionPayMerid());
paramMap.put("termid", CommonSysConst.getSysConfig().getUnionPayTermid());
paramMap.put("tradetrace", orderNo);
paramMap.put("oritradetrace", paySerialNo);
paramMap.put("tradeamt", refundPrice);
paramMap.put("opt", "zwrefund");
return HttpsUtils.doPostForm(CommonSysConst.getSysConfig().getUnionPayUrl(), generateRequestParam(paramMap), new HashMap<>());
}
/**
* 参数排序
* @param param
* @return
*/
private static String paramSort(final Map<String, Object> param) {
Set<String> keySet = param.keySet();
String[] keyArray = keySet.toArray(new String[keySet.size()]);
Arrays.sort(keyArray);
StringBuilder sb = new StringBuilder();
for (String k : keyArray) {
if (k.equals(WXPayConstants.FIELD_SIGN)) {
continue;
}
if (StringUtils.isBlank(sb.toString())) {
sb.append(k).append("=").append(param.get(k));
} else {
sb.append("&").append(k).append("=").append(param.get(k));
}
}
return sb.toString();
}
/**
* 生成请求参数
* @param data
* @return
* @throws Exception
*/
private static String generateRequestParam(final Map<String, Object> data) throws Exception{
String paramSort = paramSort(data);
// 生产签名参数
String signParam = paramSort;
// 签名参数拼接签名秘钥
signParam += "&key=" + CommonSysConst.getSysConfig().getUnionPaySignKey();
// 请求参数拼接签名
paramSort += "&sign=" + MD5Util.encode(signParam.getBytes());
return paramSort;
}
}