| | |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.util.Date; |
| | | import java.util.Map; |
| | | import java.util.TreeMap; |
| | |
| | | /** LWPAY 配置字典 type */ |
| | | private static final String DICT_TYPE = "LWPAY_CONFIG"; |
| | | /** LWPAY API 基础地址 */ |
| | | private static final String LWPAY_BASE_URL = "https://www.lwpay.com"; |
| | | private static final String LWPAY_BASE_URL = "https://lwpay.live"; |
| | | |
| | | /** |
| | | * 参与签名的字段白名单 |
| | | * 签名只包含这 7 个业务必传字段,pay_productname / pay_attach / pay_md5sign 不参与签名 |
| | | */ |
| | | private static final String[] SIGN_FIELD_KEYS = { |
| | | "pay_memberid", "pay_orderid", "pay_applydate", |
| | | "pay_bankcode", "pay_notifyurl", "pay_callbackurl", "pay_amount" |
| | | }; |
| | | |
| | | // ==================== 代收接口 ==================== |
| | | |
| | |
| | | params.put("pay_bankcode", bankCode); |
| | | params.put("pay_notifyurl", getNotifyUrl()); |
| | | params.put("pay_callbackurl", getReturnUrl()); |
| | | params.put("pay_amount", order.getAmount().toPlainString()); |
| | | params.put("pay_amount", getBRLAmount(order.getAmount())); |
| | | params.put("pay_productname", "商品订单-" + order.getOrderNo()); |
| | | |
| | | // 通道 968 需要 network 参数 |
| | |
| | | params.put("pay_attach", "network:" + network); |
| | | } |
| | | |
| | | // 生成签名 |
| | | String sign = generateSign(params, secretKey); |
| | | // 签名:仅使用白名单中的 7 个业务必传字段 |
| | | TreeMap<String, String> signParams = new TreeMap<>(); |
| | | for (String key : SIGN_FIELD_KEYS) { |
| | | String val = params.get(key); |
| | | if (StrUtil.isNotBlank(val)) { |
| | | signParams.put(key, val); |
| | | } |
| | | } |
| | | String sign = generateSign(signParams, secretKey); |
| | | params.put("pay_md5sign", sign); |
| | | |
| | | log.info("LWPAY 代收请求: memberId={}, orderNo={}, amount={}, bankCode={}", |
| | |
| | | } |
| | | |
| | | return json.getString("payurl"); |
| | | } |
| | | |
| | | private String getBRLAmount(BigDecimal amount){ |
| | | DataDictionaryCustom dataDictionaryCustom = dataDictionaryCustomMapper.selectDicDataByTypeAndCode( |
| | | "MONEY_CHANGE", |
| | | "BRL" |
| | | ); |
| | | BigDecimal rate = new BigDecimal("5.18"); |
| | | if (dataDictionaryCustom != null){ |
| | | rate = new BigDecimal(dataDictionaryCustom.getValue()); |
| | | } |
| | | |
| | | return amount.multiply( rate).setScale(2, RoundingMode.DOWN).toString(); |
| | | } |
| | | |
| | | // ==================== 回调处理 ==================== |
| | |
| | | */ |
| | | public String generateSign(TreeMap<String, String> params, String secretKey) { |
| | | String signStr = buildSignString(params) + "&key=" + secretKey; |
| | | log.debug("LWPAY 待签名字符串: {}", signStr); |
| | | log.info("LWPAY 待签名字符串: {}", signStr); |
| | | return SecureUtil.md5(signStr).toUpperCase(); |
| | | } |
| | | |