parent
63645f8a4f
commit
a25c65af85
@ -0,0 +1,109 @@ |
|||||||
|
package com.hfkj.service.pay.huift; |
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONObject; |
||||||
|
import com.hfkj.common.exception.ErrorCode; |
||||||
|
import com.hfkj.common.exception.ErrorHelp; |
||||||
|
import com.hfkj.common.exception.SysCode; |
||||||
|
import com.hfkj.common.utils.HttpsUtils; |
||||||
|
import com.hfkj.config.CommonSysConst; |
||||||
|
import com.hfkj.entity.BsOrder; |
||||||
|
import com.hfkj.model.order.OrderModel; |
||||||
|
import com.hfkj.service.order.BsOrderService; |
||||||
|
import com.hfkj.service.pay.huift.config.HuiftConfig; |
||||||
|
import com.hfkj.sysenum.order.OrderPayTypeEnum; |
||||||
|
import com.hfkj.sysenum.order.OrderStatusEnum; |
||||||
|
import org.springframework.stereotype.Component; |
||||||
|
|
||||||
|
import javax.annotation.Resource; |
||||||
|
import java.math.BigDecimal; |
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
/** |
||||||
|
* @className: TradeService |
||||||
|
* @author: HuRui |
||||||
|
* @date: 2024/4/7 |
||||||
|
**/ |
||||||
|
@Component |
||||||
|
public class TradeService { |
||||||
|
|
||||||
|
@Resource |
||||||
|
private BsOrderService orderService; |
||||||
|
|
||||||
|
/** |
||||||
|
* 交易-预下单 |
||||||
|
* @param order |
||||||
|
* @return |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
public JSONObject thirdPreOrder(OrderModel order) throws Exception { |
||||||
|
Map<String,Object> param = new HashMap<>(); |
||||||
|
param.put("shopCode", HuiftConfig.shopcode); |
||||||
|
param.put("outTradeNo", order.getOrderNo()); |
||||||
|
param.put("transAmt", order.getPayRealPrice().multiply(new BigDecimal("100")).intValue()); |
||||||
|
param.put("mobileNbr", order.getUserPhone()); // 用户标识
|
||||||
|
param.put("callbackUrl", CommonSysConst.getSysConfig().getDomainName() + "/order/payNotify/huiftPay"); |
||||||
|
// param.put("remark", ""); // 备注
|
||||||
|
// param.put("outAppId", "");
|
||||||
|
param.put("outRedirectPath", CommonSysConst.getSysConfig().getDomainName()+"/phg/#/pages/user/order_list/order_list?tbIndex=-1"); |
||||||
|
param.put("sign", HuiftConfig.md5Encode(HuiftConfig.paramSort(param, HuiftConfig.signSecret).getBytes()).toLowerCase()); |
||||||
|
JSONObject response = HttpsUtils.doPost(HuiftConfig.reqUrl + "/hfq/v1/thirdPay/thirdPreOrder", JSONObject.toJSONString(param)); |
||||||
|
if (response.getString("status").equals("ok")) { |
||||||
|
return response.getJSONObject("data"); |
||||||
|
} |
||||||
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR , response.getString("message")); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 查询交易结果 |
||||||
|
* @param preOrderNo |
||||||
|
* @return |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
public JSONObject thirdPayResult(String preOrderNo) throws Exception { |
||||||
|
Map<String,Object> param = new HashMap<>(); |
||||||
|
param.put("shopCode", HuiftConfig.shopcode); |
||||||
|
param.put("preOrderNo", preOrderNo); |
||||||
|
param.put("sign", HuiftConfig.md5Encode(HuiftConfig.paramSort(param, HuiftConfig.signSecret).getBytes()).toLowerCase()); |
||||||
|
JSONObject response = HttpsUtils.doPost(HuiftConfig.reqUrl + "/hfq/v1/thirdPay/thirdPayResult", JSONObject.toJSONString(param)); |
||||||
|
if (response.getString("status").equals("ok")) { |
||||||
|
return response.getJSONObject("data"); |
||||||
|
} |
||||||
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR , response.getString("message")); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* 交易退款 |
||||||
|
* @param refundOrderNo 退款订单号 |
||||||
|
* @param refundPrice 退款金额 |
||||||
|
* @param orderNo 订单号 |
||||||
|
* @return |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
public JSONObject thirdPayRefund(String refundOrderNo, BigDecimal refundPrice, String orderNo) throws Exception { |
||||||
|
// 查询订单
|
||||||
|
BsOrder order = orderService.getOrder(orderNo); |
||||||
|
if (order == null) { |
||||||
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的的订单"); |
||||||
|
} |
||||||
|
if (!order.getOrderStatus().equals(OrderStatusEnum.status2.getCode()) |
||||||
|
&& !order.getOrderStatus().equals(OrderStatusEnum.status3.getCode())) { |
||||||
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "订单当前状态无法退款"); |
||||||
|
} |
||||||
|
if (!order.getPayType().equals(OrderPayTypeEnum.type3.getCode())) { |
||||||
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "支付方式错误"); |
||||||
|
} |
||||||
|
Map<String,Object> param = new HashMap<>(); |
||||||
|
param.put("shopCode", HuiftConfig.shopcode); |
||||||
|
param.put("preOrderNo", order.getPayChannelOrderNo()); |
||||||
|
param.put("outRefundNo", refundOrderNo); |
||||||
|
param.put("refundAmt", refundPrice.multiply(new BigDecimal("100")).intValue()); |
||||||
|
param.put("sign", HuiftConfig.md5Encode(HuiftConfig.paramSort(param, HuiftConfig.signSecret).getBytes()).toLowerCase()); |
||||||
|
JSONObject response = HttpsUtils.doPost(HuiftConfig.reqUrl + "/hfq/v1/thirdPay/thirdPayRefund", JSONObject.toJSONString(param)); |
||||||
|
if (response.getString("status").equals("ok")) { |
||||||
|
return response.getJSONObject("data"); |
||||||
|
} |
||||||
|
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR , response.getString("message")); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,59 @@ |
|||||||
|
package com.hfkj.service.pay.huift.config; |
||||||
|
|
||||||
|
import com.hfkj.config.CommonSysConst; |
||||||
|
import org.apache.commons.lang3.StringUtils; |
||||||
|
|
||||||
|
import javax.xml.bind.annotation.adapters.HexBinaryAdapter; |
||||||
|
import java.security.MessageDigest; |
||||||
|
import java.util.Arrays; |
||||||
|
import java.util.Map; |
||||||
|
import java.util.Set; |
||||||
|
|
||||||
|
/** |
||||||
|
* @className: HuiftConfig |
||||||
|
* @author: HuRui |
||||||
|
* @date: 2024/4/7 |
||||||
|
**/ |
||||||
|
public class HuiftConfig { |
||||||
|
|
||||||
|
public final static String reqUrl = CommonSysConst.getSysConfig().getHuiftReqUrl(); |
||||||
|
public final static String shopcode = CommonSysConst.getSysConfig().getHuiftShopCode(); |
||||||
|
public final static String signSecret = CommonSysConst.getSysConfig().getHuiftSignSecret(); |
||||||
|
|
||||||
|
/** |
||||||
|
* 参数排序 |
||||||
|
* @param param |
||||||
|
* @return |
||||||
|
*/ |
||||||
|
public static String paramSort(final Map<String, Object> param, String key) { |
||||||
|
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 (StringUtils.isBlank(sb.toString())) { |
||||||
|
sb.append(k).append("=").append(param.get(k)); |
||||||
|
} else { |
||||||
|
sb.append("&").append(k).append("=").append(param.get(k)); |
||||||
|
} |
||||||
|
} |
||||||
|
sb.append(key); |
||||||
|
return sb.toString(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* MD5加密 |
||||||
|
* @param data |
||||||
|
* @return |
||||||
|
* @throws Exception |
||||||
|
*/ |
||||||
|
public static String md5Encode(byte[] data) throws Exception { |
||||||
|
// 初始化MessageDigest
|
||||||
|
MessageDigest md = MessageDigest.getInstance("MD5"); |
||||||
|
// 执行摘要信息
|
||||||
|
byte[] digest = md.digest(data); |
||||||
|
// 将摘要信息转换为32位的十六进制字符串
|
||||||
|
return new String(new HexBinaryAdapter().marshal(digest)); |
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue