| | |
| | | package cc.mrbird.febs.pay.service.impl; |
| | | |
| | | import cc.mrbird.febs.common.entity.FebsResponse; |
| | | import cc.mrbird.febs.pay.model.UnipayDto; |
| | | import cc.mrbird.febs.pay.model.*; |
| | | import cc.mrbird.febs.pay.service.UnipayService; |
| | | import cc.mrbird.febs.pay.util.HttpRequester; |
| | | import cc.mrbird.febs.pay.util.HttpRespons; |
| | | import cc.mrbird.febs.pay.util.*; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import cn.hutool.json.JSONUtil; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import lombok.extern.slf4j.Slf4j; |
| | |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.io.IOException; |
| | | import java.lang.reflect.Field; |
| | | import java.math.BigDecimal; |
| | | import java.math.RoundingMode; |
| | | import java.text.DecimalFormat; |
| | | import java.util.*; |
| | | |
| | | @Slf4j |
| | | @Service |
| | | public class UnipayServiceImpl implements UnipayService { |
| | | |
| | | //汇聚支付平台公钥 |
| | | public static final String platformPublicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwSAEXsiC0IYh" + |
| | | "a6a94imKq8VfOkk7WjDRAQWMBRnoKOZeEUeMrHYiblcrqeMYXGpV13288iUOkuyKwkPXkYXyIQK8emvJIbQOhtB5bS" + |
| | | "lAbodsPgncM9Ney1GFiz+7ogBxyt58mP8AA9UHtMw7u78zZoQ1+dUWwUUowVXml3Q0cVQIDAQAB"; |
| | | //本地生成的公钥 |
| | | public static final String paySecretKey = |
| | | "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJSsoLHAy4P+H76l4i6IMZ/JKKTVphvrJBSB777N8h" + |
| | | "gzN38M+66IqazRQLNjjyn2UZxm4eSfuJ4OBxCTGLJgDOw8B9l9lVo+0bzhNxsy5ae20EOlQa3h8xfCjCCU" + |
| | | "Xb8VP8yknG43Wk1WtwLX5VYWmJFTwA1I0dWQR4RlbHH2q2aVAgMBAAECgYBZNyoK4JV/tFwaTHLo12Nn7g" + |
| | | "9MssRGFpmFEN/sEKuJKBrSS9kvx+SBxuPbgg/j72LTxs0hI1NmzSYiJRL53zeBFM0wM2/D2zW0pZfogUPN" + |
| | | "7Mb3FkFOoE0CxFwn7pjjBkvAsZQJS2fZFY6cf/WYhVI+XCMzjiQWJSQUXKEfWzUflQJBAMpLzinyAsBHKe" + |
| | | "6lmAkSh/yaQX76cnxNZGmfc/gj62qM5YDzpt3+Za5Y4NSq5kYu36NAgorf2ipJPRwpgS13fK8CQQC8JKVP" + |
| | | "2YgZ39fa+xVJu3yTT48k9EnYIEfsv/Bc7AVdpaZKDeEBuGpBb0JQzuIg3Zd6yNQCzH3oBqp8l6l/fgn7Ak" + |
| | | "EAtFluvHB4yWjoVk0lRPlTaP0w5P5ssKrimVPBtPh4+a4RMayHGKSjjBLKpm6SCwHg+Q8bEqpNOqO+qmvK" + |
| | | "MXm0GwJAZd4Bk8ZYJopIOUyRLibRQIFnI78Q7HAuAUW7QtSX4yh5bMcu+Nt8zIkNAuvBC8Ju7hAmmo1V7n" + |
| | | "cNgAAtydXYWQJALLOFCjCkRgeRVL8YE8bVi4U16b8ltAN1DlbWEzui6VFy2vIga3IryesNVAOOdornyAwf" + |
| | | "1huqB2lYfuQwtrIBKg=="; |
| | | public static final String notifyUrl = "http://47.111.90.145:8800/api/unipay/unipayCallBack"; |
| | | public static final String agreementPayNotifyUrl = "http://47.111.90.145:8800/api/unipay/agreementPayCallBack"; |
| | | public static final String p1MerchantNo = "888118000001971";/** 商户编号 */ |
| | | public static final String aesKey = "1234567891234567";/** 商户编号 */ |
| | | |
| | | @Override |
| | | @Transactional |
| | |
| | | String key = "2e95f6a3e11e47fa8a4386d6aefe1735";/** md5密钥商户后台-商户中心-商户设置-密钥管理获取 必填!*/ |
| | | Map<String, String> map = new HashMap<String, String>(); |
| | | map.put("p0_Version", "1.0");/** 版本号 */ |
| | | map.put("p1_MerchantNo", "888118000001971");/** 商户编号 */ |
| | | map.put("p1_MerchantNo", p1MerchantNo);/** 商户编号 */ |
| | | map.put("p2_OrderNo", unipayDto.getOrderNo()); /**商户订单号*/ |
| | | map.put("p3_Amount", unipayDto.getAmount().toString());/**订单金额*/ |
| | | map.put("p4_Cur", "1"); /**交易币种 */ |
| | |
| | | |
| | | } |
| | | |
| | | |
| | | |
| | | @Override |
| | | @Transactional |
| | | public String agreementPay(AgreementPayDto agreementPayDto) { |
| | | |
| | | String secretKey =paySecretKey; |
| | | String key = aesKey;//敏感信息加密key |
| | | |
| | | Map<String, Object> map = new HashMap<>(); |
| | | RequestParam requestParam = new RequestParam(); |
| | | requestParam.setMch_no(p1MerchantNo);// 商户编号 |
| | | requestParam.setMethod("fastPay.agreement.pay");// 固定方法名 |
| | | requestParam.setVersion("1.0");// 版本号 |
| | | requestParam.setRand_str("12345678901234567890123456789012");// 随机字符串 |
| | | requestParam.setSign_type("2");// 签名类型 |
| | | try { |
| | | requestParam.setSec_key(RSAUtil.encryptByPublicKey(platformPublicKey,aesKey));//加密密钥 |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | map.put("mch_order_no",agreementPayDto.getOrderNo()); // 商户订单号 |
| | | map.put("order_amount", agreementPayDto.getOrderAmount()); // 订单金额;金额保留两位小数 |
| | | map.put("mch_req_time", agreementPayDto.getOrderTime()); // 订单时间 |
| | | map.put("order_desc", agreementPayDto.getOrderDesc()); // 订单时间 |
| | | map.put("callback_url", agreementPayNotifyUrl); // 异步通知地址 |
| | | map.put("bank_card_no", AESUtil.Aes256Encode(agreementPayDto.getBankNo(),aesKey)); // 签约银行卡号 |
| | | |
| | | requestParam.setData(JsonUtil.toString(map)); |
| | | |
| | | String signStr = null; |
| | | try { |
| | | signStr = getSortedString(requestParam); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | System.out.println("待签名字符串:"+signStr);// 待签名字符串 |
| | | |
| | | // 签名 |
| | | if (SignTypeEnum.RSA.getValue().equals(requestParam.getSign_type())) { |
| | | try { |
| | | signStr =(RSAUtil.sign(signStr, secretKey, false)); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } else { |
| | | signStr = ""; |
| | | System.out.println("未预期的签名类型:" + requestParam.getSign_type()); |
| | | } |
| | | requestParam.setSign(signStr); |
| | | |
| | | // Map转json字符串 |
| | | String reqBodyJson = JSON.toJSONString(requestParam); |
| | | System.out.println(reqBodyJson); |
| | | String httpResponseJson = null; |
| | | try { |
| | | httpResponseJson = HttpClientUtil.sendHttpPost("https://api.joinpay.com/fastpay", reqBodyJson); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | System.out.println(httpResponseJson); |
| | | |
| | | if(StrUtil.isNotBlank(httpResponseJson)){ |
| | | return httpResponseJson; |
| | | }else{ |
| | | return "fail"; |
| | | } |
| | | } |
| | | |
| | | public static void main(String[] args) { |
| | | // BigDecimal value = new BigDecimal("0.10858").setScale(2,BigDecimal.ROUND_HALF_UP); |
| | | BigDecimal value = new BigDecimal("1231.1").setScale(2, BigDecimal.ROUND_DOWN); |
| | | DecimalFormat decimalFormat = new DecimalFormat("0.00#"); |
| | | String strVal = decimalFormat.format(value); |
| | | System.out.println(strVal); |
| | | } |
| | | |
| | | @Override |
| | | public String getAgreeMentPaySms(AgreeMentPaySmsDto agreeMentPaySmsDto) { |
| | | String secretKey = paySecretKey; |
| | | |
| | | Map<String, Object> map = new HashMap<>(); |
| | | RequestParam requestParam = new RequestParam(); |
| | | requestParam.setMch_no(p1MerchantNo);// 商户编号 |
| | | requestParam.setMethod("fastPay.agreement.signSms");// 固定方法名 |
| | | requestParam.setVersion("1.0");// 版本号 |
| | | requestParam.setRand_str("12345678901234567890123456789012");// 随机字符串 |
| | | requestParam.setSign_type("2");// 签名类型 |
| | | try { |
| | | requestParam.setSec_key(RSAUtil.encryptByPublicKey(platformPublicKey,aesKey));//加密密钥 |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | map.put("mch_order_no", "NO_"+agreeMentPaySmsDto.getOrderNo()); // 商户订单号 |
| | | map.put("order_amount", agreeMentPaySmsDto.getOrderAmount()); // 订单金额 |
| | | map.put("mch_req_time", agreeMentPaySmsDto.getCreatedTime()); // 下单时间 |
| | | map.put("payer_name",AESUtil.Aes256Encode(agreeMentPaySmsDto.getName(),aesKey)); // 姓名 |
| | | map.put("id_type", agreeMentPaySmsDto.getIdType()); // 证件类型 |
| | | map.put("id_no", AESUtil.Aes256Encode(agreeMentPaySmsDto.getIdCardNum(),aesKey)); // 证件号码 |
| | | map.put("bank_card_no", AESUtil.Aes256Encode(agreeMentPaySmsDto.getBankNo(),aesKey)); // 银行卡号 |
| | | map.put("mobile_no", AESUtil.Aes256Encode(agreeMentPaySmsDto.getPhone(), aesKey)); // 手机号 |
| | | |
| | | requestParam.setData(JsonUtil.toString(map)); |
| | | |
| | | String signStr = null; |
| | | try { |
| | | signStr = getSortedString(requestParam); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | System.out.println("待签名字符串:"+signStr);// 待签名字符串 |
| | | |
| | | // 签名 |
| | | if (SignTypeEnum.RSA.getValue().equals(requestParam.getSign_type())) { |
| | | try { |
| | | signStr =(RSAUtil.sign(signStr, secretKey, false)); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } else { |
| | | signStr = ""; |
| | | System.out.println("未预期的签名类型:" + requestParam.getSign_type()); |
| | | } |
| | | requestParam.setSign(signStr); |
| | | |
| | | |
| | | // Map转json字符串 |
| | | String reqBodyJson = JSON.toJSONString(requestParam); |
| | | System.out.println(reqBodyJson); |
| | | String httpResponseJson = null; |
| | | try { |
| | | httpResponseJson = HttpClientUtil.sendHttpPost("https://api.joinpay.com/fastpay", reqBodyJson); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | System.out.println(httpResponseJson); |
| | | if(StrUtil.isNotBlank(httpResponseJson)){ |
| | | cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(httpResponseJson); |
| | | System.out.println(jsonObject); |
| | | String biz_code = jsonObject.get("biz_code").toString(); |
| | | if("JS000000" == biz_code){ |
| | | return httpResponseJson; |
| | | } |
| | | return "fail"; |
| | | }else{ |
| | | return "fail"; |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public String agreementSign(AgreementSignDto agreementSignDto) { |
| | | String secretKey = paySecretKey; |
| | | |
| | | Map<String, Object> map = new HashMap<>(); |
| | | RequestParam requestParam = new RequestParam(); |
| | | requestParam.setMch_no(p1MerchantNo);// 商户编号 |
| | | requestParam.setMethod("fastPay.agreement.smsSign");// 固定方法名 |
| | | requestParam.setVersion("1.0");// 版本号 |
| | | requestParam.setRand_str("12345678901234567890123456789012");// 随机字符串 |
| | | requestParam.setSign_type("2");// 签名类型 |
| | | try { |
| | | requestParam.setSec_key(RSAUtil.encryptByPublicKey(platformPublicKey,aesKey)); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | |
| | | map.put("mch_order_no", agreementSignDto.getOrderNo()); // 商户订单号 |
| | | map.put("sms_code", agreementSignDto.getSmsCode()); // 签约短信验证码 |
| | | |
| | | requestParam.setData(JsonUtil.toString(map)); |
| | | |
| | | String signStr = null; |
| | | try { |
| | | signStr = getSortedString(requestParam); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | System.out.println(signStr);// 待签名字符串 |
| | | |
| | | // 签名 |
| | | if (SignTypeEnum.RSA.getValue().equals(requestParam.getSign_type())) { |
| | | try { |
| | | signStr =(RSAUtil.sign(signStr, secretKey, false)); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } else { |
| | | signStr = ""; |
| | | System.out.println("未预期的签名类型:" + requestParam.getSign_type()); |
| | | } |
| | | requestParam.setSign(signStr); |
| | | |
| | | |
| | | // Map转json字符串 |
| | | String reqBodyJson = JSON.toJSONString(requestParam); |
| | | System.out.println(reqBodyJson); |
| | | String httpResponseJson = null; |
| | | try { |
| | | httpResponseJson = HttpClientUtil.sendHttpPost("https://api.joinpay.com/fastpay", reqBodyJson); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | System.out.println(httpResponseJson); |
| | | |
| | | if(StrUtil.isNotBlank(httpResponseJson)){ |
| | | return httpResponseJson; |
| | | }else{ |
| | | return "fail"; |
| | | } |
| | | } |
| | | |
| | | protected static String getSortedString(Object obj) throws Exception { |
| | | Field[] fields = obj.getClass().getDeclaredFields(); |
| | | |
| | | Map<String, String> map = new HashMap<String, String>(); |
| | | for (int i = 0; i < fields.length; i++) { |
| | | Field filed = fields[i]; |
| | | String name = filed.getName(); |
| | | if (SignUtil.NOT_SIGN_PARAM.contains(name)) {// 不参与签名或验签的参数直接跳过 |
| | | continue; |
| | | } |
| | | filed.setAccessible(true); |
| | | map.put(name, String.valueOf(filed.get(obj))); |
| | | } |
| | | StringBuffer content = new StringBuffer(); |
| | | List<String> keys = new ArrayList(map.keySet()); |
| | | Collections.sort(keys); // 排序map |
| | | for (int i = 0; i < keys.size(); i++) { |
| | | String key = keys.get(i); |
| | | String value = map.get(key); |
| | | |
| | | if (i != 0) { |
| | | content.append(SignUtil.SIGN_SEPARATOR); |
| | | } |
| | | content.append(key).append(SignUtil.SIGN_EQUAL).append(value); |
| | | } |
| | | return content.toString(); |
| | | } |
| | | |
| | | } |