Solution/ 解决方案

解决方案分类

如何通过Hessian方式调用专业风控接口

发布时间:2020-03-20 点击数:1306

1、业务工程引入

     需引入外部jar包,包名:hessian-4.0.37.jarfastjson-1.2.31.jar

 

2、然后实现,代码示例(Java)如下:

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.caucho.hessian.client.HessianProxyFactory;

/**
* 先导入到工程,需要jar包:fastjson-1.2.31.jar , hessian-4.0.37.jar
*
* @author admin
*
*/

public class HessianClientPAY_EVENT {


/**
* 通知方式(status=1 or status=-1)
*/
public static HashMap<String, Object> getAsyncParamsMap(HashMap<String, Object> map, String status) {

map.put("finish_time", getDateTime());
map.put("status", status);
return map;
}

/**
* 请求方式(status=0)
*/
public static HashMap<String, Object> getSyncParamsMap() {

HashMap<String, Object> map = new HashMap<String, Object>();
// 封装事件类型以及事件类型中的字段值
map.put("EVENT_TYPE", "PAY_EVENT");// REQUIRED
map.put("client_ip", "212.129.10.111");

map.put("client_mobile", "13305662206");
map.put("device_id", "70010000001");
map.put("occur_time", getDateTime());// 必填
map.put("payee_userid", "70010000001");

map.put("session_id", "70010000001");
map.put("status","0");// 必填
map.put("user_id", "70010000001");


return map;
}

private static String getDateTime() {
Date date = new Date();
return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(date);
}

// --内部类,实际使用可以提取到外面成为公共接口类--------------------------------------------
public interface ReceiveOrderHessianService {

public String syncEventReceive(HashMap<String, Object> map);
}

// --HessianClient内部类,实际使用可以提取到外面成为公共类------------------------------------
public static class HessianClient {

private static ReceiveOrderHessianService service;

public static String execRequest(String url, HashMap<String, Object> map) {
String result = "";
try {
HessianProxyFactory myClient = new HessianProxyFactory();
myClient.setReadTimeout(10000);
service = (ReceiveOrderHessianService) myClient.create(ReceiveOrderHessianService.class, url);
result = service.syncEventReceive(map);
} catch (Exception e) {
System.out.print("HessionClientTest err=" + e.toString());
}
return result;
}
}

// =====================================Main===============================================
public static void main(String[] args) {


String url = "http://192.168.5.7:8099/ReceiveOrderHessianService";// 需根据实际情况调整
int num = 100;//测试循环次数
for(int i = 0; i< num;i++){

HashMap<String, Object> paramsMap = getSyncParamsMap();
String result = HessianClient.execRequest(url, paramsMap);

// json 解释
JSONObject jsonObject = JSON.parseObject(result);

System.out.println("success(调用接口是否成功,true成功,false失败): " + jsonObject.get("success"));
System.out.println("reasonCode(调用失败原因码,参见原因码表): " + jsonObject.get("reasonCode"));
System.out.println("reasonMsg(调用失败原因描述): " + jsonObject.get("reasonMsg"));
System.out.println("orderNo(订单号); " + jsonObject.get("orderNo"));
System.out.println("riskResult(风险评定指令码,有三个,放行(ACCEPT)、待审核(REVIEW)、拒绝(REJECT)): " + jsonObject.get("riskResult"));
System.out.println("riskScore(事件总体评分): " + jsonObject.get("riskScore"));
System.out.println("costTime(处理花费时间(毫秒)): " + jsonObject.get("costTime"));
System.out.println("fireRules(触发的规则列表): " + jsonObject.get("fireRules"));
// fireRules: 触发的规则列表
if (jsonObject.get("fireRules") != null && !"".equals(jsonObject.get("fireRules"))) {

JSONArray fireRules = (JSONArray) jsonObject.get("fireRules");
if (fireRules != null && fireRules.size() > 0) {
System.out.println("---------" + "fireRules: 触发的规则列表" + "-----------------");
for (Object object : fireRules) {
JSONObject ruleJson = JSON.parseObject(object.toString());
System.out.println("code(规则编码): " + ruleJson.get("code"));
System.out.println("name(规则名称): " + ruleJson.get("name"));
System.out.println("isPolicy(预警决策(0,决策;1 预警)): " + ruleJson.get("isPolicy"));
System.out.println("ruleResult(风控结果,单个规则的风控结果。10000(放行),30000(待审核),99999(拒绝)): " + ruleJson.get("ruleResult"));
System.out.println("ruleScore(风控评分,单个规则的风控评分): " + ruleJson.get("ruleScore"));
}
}
}

// 客户根据需要判断
if ("ACCEPT".equals(jsonObject.get("riskResult"))) {

paramsMap = getAsyncParamsMap(paramsMap,"1");
String sucResult = HessianClient.execRequest(url, paramsMap);
System.out.println(sucResult);
} else {
paramsMap = getAsyncParamsMap(paramsMap,"-1");
String failResult = HessianClient.execRequest(url, paramsMap);
System.out.println(failResult);
}
}

}
}

3、对接需要注意如下:

1)  接口参数EVENT_TYPEstatusoccur_time必填;

2)  status1(成功)或-1(失败)时,此字段finish_time必填;

3)  occur_timefinish_time日期格式“yyyy-MM-dd HH:mm:ss.SSS”;

4)  请求与通知不同点:不同在通知时status1-1,必须有finish_time;