package com.tourguide.common.huifu.biz; import com.alibaba.fastjson.JSONObject; import com.huifu.bspay.sdk.opps.core.net.BasePayRequest; import com.tourguide.common.huifu.properties.HuiFuProperties; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.springframework.util.StringUtils; import java.math.BigDecimal; import java.math.RoundingMode; import java.util.HashMap; import java.util.Map; /** 斗拱主动取现和出金交易查询。 */ @Component public class DouGongWithdrawBiz { private static final Logger log = LoggerFactory.getLogger(DouGongWithdrawBiz.class); private final HuiFuProperties huiFuProperties; public DouGongWithdrawBiz(HuiFuProperties huiFuProperties) { this.huiFuProperties = huiFuProperties; } public JSONObject encashment(BigDecimal cashAmt, String huifuId, String tokenNo, String requestSequence, String requestDate) { try { DouGongRequestSupport.initBasePay(huiFuProperties); Map params = new HashMap<>(); params.put("req_date", requestDate); params.put("req_seq_id", requestSequence); params.put("cash_amt", cashAmt.setScale(2, RoundingMode.HALF_UP).toString()); params.put("huifu_id", huifuId); params.put("into_acct_date_type", "D1"); params.put("token_no", tokenNo); params.put("enchashment_channel", "00"); params.put("notify_url", callbackBaseUrl() + "/base/dougong/encashment"); log.info("调用斗拱取现接口,reqSeqId={},huifuId={},cashAmt={}", requestSequence, huifuId, cashAmt); return json(BasePayRequest.requestBasePay( "v2/trade/settlement/encashment", params, null, false)); } catch (Exception exception) { log.error("调用斗拱取现接口异常,reqSeqId={},原因={}", requestSequence, exception.getMessage(), exception); return error(exception.getMessage()); } } public JSONObject query(String huifuId, String requestDate, String requestSequence) { return query(huifuId, requestDate, requestSequence, null); } /** 取现查询同时支持原请求流水号和汇付全局流水号,便于补偿对账。 */ public JSONObject query(String huifuId, String requestDate, String requestSequence, String hfSequence) { try { DouGongRequestSupport.initBasePay(huiFuProperties); Map params = new HashMap<>(); params.put("huifu_id", huifuId); params.put("org_req_date", requestDate); params.put("org_req_seq_id", requestSequence); if (StringUtils.hasText(hfSequence)) { params.put("org_hf_seq_id", hfSequence.trim()); } return json(BasePayRequest.requestBasePay( "v2/trade/settlement/query", params, null, false)); } catch (Exception exception) { log.warn("查询斗拱取现状态异常,reqSeqId={},原因={}", requestSequence, exception.getMessage()); return error(exception.getMessage()); } } private JSONObject json(Map response) { return response == null ? null : JSONObject.parseObject(JSONObject.toJSONString(response)); } private JSONObject error(String message) { JSONObject result = new JSONObject(); result.put("code", "9999"); result.put("msg", message == null ? "斗拱服务调用异常" : message); result.put("success", false); return result; } private String callbackBaseUrl() { String value = huiFuProperties.getCallbackPath().trim(); while (value.endsWith("/")) value = value.substring(0, value.length() - 1); return value; } }