1、最近有个需求是对接工行新支付方式,废话不多话,直接贴代码
PC调取工行卡支付(可支持其他行支付) 我是直接把工行的sdk的稍微调整了下,一些无用参数就不用加了,直接对照我这个就行了 工行开放平台的sdk下载地址:
https://open.icbc.com.cn/icbc/apip/attach/icbc-api-sdk-cop_v2_20201109.zip@Controller public class CardbusinessEpaypcConsumptionV1Test{ // 1、网关公钥 protected static final String APIGW_PUBLIC_KEY = "";//需要和工行要账号 // 2、密钥对认证方式 //生产环境 protected static final String MY_PRIVATE_KEY = "xx"; //此私钥是和本地生产的,同时需要在工行取配置好 // 3、appid protected static final String APP_ID = "xxxx"; protected static final String MER_ID = "xxx"; protected static final String MER_PRTCL_NO = "xxx"; protected static final String SERVICE_URL = "https://gw.open.icbc.com.cn"; private Logger logger = Logger.getLogger(CardbusinessEpaypcConsumptionV1Test.class.getName()); @RequestMapping("/pcXinzhifu") public void pcXinzhifu(HttpServletResponse response) throws Exception { // 4、需传入appid,私钥,字符集 UiIcbcClient client = new UiIcbcClient(APP_ID, IcbcConstants.SIGN_TYPE_RSA2, MY_PRIVATE_KEY, IcbcConstants.CHARSET_UTF8); CardbusinessEpaypcConsumptionRequestV1 request = new CardbusinessEpaypcConsumptionRequestV1(); // 5、根据测试环境和生产环境替换相应ip和端口 request.setServiceUrl(SERVICE_URL+"/ui/cardbusiness/epaypc/consumption/V1"); //需要在工行申请权限,否则会报API权限不足 CardbusinessEpaypcConsumptionRequestV1Biz bizContent = new CardbusinessEpaypcConsumptionRequestV1Biz(); request.setBizContent(bizContent); // 6、请对照接口文档用bizContent.setxxx()方法对业务上送数据进行赋值 bizContent.setIcbc_flag("1"); bizContent.setIcbc_appid(APP_ID); //工行支付需要的参数 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); //测试环境的时间 bizContent.setOrder_date(sdf.format(new Date())); String order_no=UUID.randomUUID().toString().replaceAll("-", "").substring(0, 16); bizContent.setOut_trade_no(order_no); bizContent.setAmount("100"); bizContent.setInstallment_times("1"); bizContent.setCur_type("001"); bizContent.setMer_id(MER_ID); bizContent.setMer_prtcl_no(MER_PRTCL_NO); bizContent.setLanguage("zh_CN"); bizContent.setGoods_id("xx"); bizContent.setGoods_name("xx"); bizContent.setGoods_num("001"); bizContent.setMer_hint("xx"); bizContent.setExpire_time("xx"); bizContent.setMer_reference("xx"); bizContent.setMer_custom_ip("xx"); bizContent.setMer_var("test1"); bizContent.setMer_url("xx"); bizContent.setReturn_url("xx"); bizContent.setCredit_type("2"); bizContent.setAuto_refer_sec("60"); bizContent.setVerify_join_flag("0"); bizContent.setResult_type("0"); bizContent.setE_isMerFlag("1"); bizContent.setPay_expire("3600"); bizContent.setPage_linkage_flag("1"); PrintWriter out = response.getWriter(); try { String str = client.buildPostForm(request); logger.info(str); out.write("<html>"); out.write("<head>"); out.write("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=" + IcbcConstants.CHARSET_UTF8 + "\">"); out.write("</head>"); out.write("<body>"); out.write(str); out.write("</body>"); out.write("</html>"); } catch (IcbcApiException e) { e.printStackTrace(); } }
支付完成可以直接调用查询订单接口,也需要申请权限
//签名类型为RSA时,需传入appid,私钥和网关公钥,签名类型使用定值IcbcConstants.SIGN_TYPE_RSA,其他参数使用缺省值
DefaultIcbcClient client = new DefaultIcbcClient(APP_ID, IcbcConstants.SIGN_TYPE_RSA2, MY_PRIVATE_KEY, APIGW_PUBLIC_KEY);
CardbusinessAggregatepayB2cOnlineOrderqryRequestV1 requestV = new CardbusinessAggregatepayB2cOnlineOrderqryRequestV1();
//根据测试环境和生产环境替换相应ip和端口
requestV.setServiceUrl(SERVICE_URL + "/api/cardbusiness/aggregatepay/b2c/online/orderqry/V1");
CardbusinessAggregatepayB2cOnlineOrderqryRequestV1.CardbusinessAggregatepayB2cOnlineOrderqryRequestV1Biz bizContent = new CardbusinessAggregatepayB2cOnlineOrderqryRequestV1.CardbusinessAggregatepayB2cOnlineOrderqryRequestV1Biz();
requestV.setBizContent(bizContent);
//请对照接口文档用bizContent.setxxx()方法对业务上送数据进行赋值
bizContent.setMer_id(MER_ID);
bizContent.setOut_trade_no(orderNo);
bizContent.setOrder_id("");
bizContent.setDeal_flag("0");
bizContent.setIcbc_appid(APP_ID);
CardbusinessAggregatepayB2cOnlineOrderqryResponseV1 response;
try {
response = client.execute(requestV, System.currentTimeMillis() + "");//msgId消息通讯唯一编号,要求每次调用独立生成,APP级唯一
System.out.println("response:" + JSON.toJSONString(response));
if (response.getReturnCode() == 0) {
// 6、业务成功处理,请根据接口文档用response.getxxx()获取同步返回的业务数据
System.out.println("ReturnCode:" + response.getReturnCode());
System.out.println("response:" + JSON.toJSONString(response));
//如果订单状态为0
if (response.getPay_status().equals("0")) {
//外部订单号,商城的订单号
String out_trade_no = response.getOut_trade_no();
//工行订单号
String order_id = response.getOrder_id();
//支付的金额,以分为单位的
String total_amt = response.getTotal_amt();
//支付类型
String payType = response.getPay_type();
logger.info("外部订单号" + out_trade_no);
logger.info("工行订单号" + order_id);
logger.info("支付的金额" + total_amt);
}
} catch (IcbcApiException e) {
e.printStackTrace();
}
开发中需要的jar,可直接去工行开放平台的sdk demo中去下载所需的jar