package com.nova.sankuai.infra.utils.huifubao; import com.alibaba.fastjson.JSONObject; import com.fasterxml.jackson.databind.JsonNode; import com.nova.sankuai.infra.utils.huifubao.common.HttpUtil; import com.nova.sankuai.infra.utils.huifubao.common.MyKey; import com.nova.sankuai.infra.utils.huifubao.common.SignEncrypt; import org.bouncycastle.util.encoders.Base64; import java.security.KeyFactory; import java.security.PrivateKey; import java.security.PublicKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Random; import static com.nova.sankuai.infra.utils.huifubao.common.HttpUtil.parseJsonResponse; /** 快捷收银台接口,非快捷网关体系内 */ public class CashierApi { public static void main(String[] args) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date now = new Date(); String time = sdf.format(now); // 创建Random对象 Random random = new Random(); // 设置上限值 int upperBound = 1000000000; // 生成随机数 String randomNum = String.valueOf(random.nextInt(upperBound)); String method = "heepay.bank.apply.page.pay"; String version ="1.0"; String sign_type = "RSA1"; String pay_type = "63"; //请求商户ID String merch_id = "1664502"; String timestamp = time; String out_trade_time = time; String out_trade_no = randomNum; //异步返回的商户处理页面,URL参数是以http://或https://开头的完整URL地址(后台处理),提交的url地址必须外网能访问到,否则无法通知商户。值可以为空,但不可以为null String notify_url = "https://www.baidu.com/"; String return_url = "https://www.baidu.com/"; //商户平台用户唯一标识,自定义,一定要唯一 String merch_user_id = "WOWOWOWO12121216616161"; //商户平台商品名称 | 编码格式:utf-8 String goods_name = "米饭"; //支付金额 | 单位:元,支持小数点后两位 double pay_amt = 0.01; /* 业务参数注意 如果是大商户子商户模式,需要传子商户号,如果需要订单分润需要传is_profit_sharing 并且需要放在biz里面去组装 子商户号*/ //String sub_merch_id =""; //是否指定分账 | 枚举值 true:是 false:否 传 1 或 2 int is_profit_sharing = 1; //biz组装 JSONObject payer_info = new JSONObject(); payer_info.put("merch_user_id",merch_user_id); JSONObject goods_info = new JSONObject(); goods_info.put("goods_name",goods_name); JSONObject biz_content = new JSONObject(); biz_content.put("out_trade_no",out_trade_no); biz_content.put("out_trade_time",out_trade_time); biz_content.put("pay_type",pay_type); /* 业务参数注意 如果是大商户子商户模式,需要传子商户号,如果需要订单分润需要传is_profit_sharing 并且需要放在biz里面去组装 子商户号*/ //biz_content.put("sub_merch_id",sub_merch_id); biz_content.put("is_profit_sharing",is_profit_sharing); biz_content.put("pay_amt",pay_amt); biz_content.put("return_url",return_url); biz_content.put("notify_url",notify_url); biz_content.put("payer_info",payer_info); biz_content.put("goods_info",goods_info); System.out.println("加密串:--------->"+biz_content); //签名串组装 String OldSign = "biz_content=" + biz_content + "&merch_id=" + merch_id +"&method=" + method +"&sign_type=" + sign_type +"×tamp=" + timestamp +"&version=" + version; System.out.println("签名串:--------->"+OldSign); SignEncrypt signEncrypt = new SignEncrypt(); // 使用类名和常量名来访问常量 String publicKeystr = MyKey.publicKeystr; String privateKeystr = MyKey.privateKeystr; //密钥配置 KeyFactory keyFactory = KeyFactory.getInstance("RSA"); byte[] publicKeyPath = Base64.decode(publicKeystr.getBytes()); byte[] privateKeyPath = Base64.decode(privateKeystr.getBytes()); PublicKey heepayPublicKey = keyFactory.generatePublic(new X509EncodedKeySpec(publicKeyPath)); PrivateKey privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(privateKeyPath)); //加密 String content = signEncrypt.encrypt(String.valueOf(biz_content), heepayPublicKey); //加签 String sign = signEncrypt.sign(OldSign, privateKey); //最终请求数据 JSONObject param = new JSONObject(); param.put("method",method); param.put("version",version); param.put("merch_id",merch_id); param.put("timestamp",timestamp); param.put("biz_content",content); param.put("sign_type",sign_type); param.put("sign",sign); HttpUtil httpUtil = new HttpUtil(); String a = httpUtil.sendJsonHttpPost("https://Pay.Heepay.com/API/Cashier/ApplyPay.aspx", String.valueOf(param)); //解析返回参数 JsonNode jsonNode = parseJsonResponse(a); String msg = jsonNode.get("msg").asText(); if ( "调用成功".equals(msg) ){ String data = jsonNode.get("data").asText(); String DecryptedData = signEncrypt.decrypt(data,privateKey); JsonNode DecryptedData1 = parseJsonResponse(DecryptedData); String redirect_uri = DecryptedData1.get("redirect_uri").asText(); System.out.println("输出为收银台请求地址:--------->"+redirect_uri); }else { String sub_msg = jsonNode.get("sub_msg").asText(); System.out.println("输出失败sub_msg:--------->"+sub_msg); } } }