首先需要提供的crt文件以及p8文件,放在项目对应的包下。
1.获取token,这个稍微简单点访问路径获取参数的方法:
//根据地址以及参数获取到返回回来的数据
public static String postData(String urlStr, String data, String contentType) {
BufferedReader reader = null;
try {
URL url = new URL(urlStr);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setConnectTimeout(CONNECT_TIMEOUT);
conn.setReadTimeout(CONNECT_TIMEOUT);
if (contentType != null){
conn.setRequestProperty("content-type", contentType);
}
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream(), DEFAULT_ENCODING);
if (data == null){
data = "";
}
writer.write(data);
writer.flush();
writer.close();
reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), DEFAULT_ENCODING));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line);
sb.append("\r\n");
}
return sb.toString();
} catch (IOException e) {
} finally {
try {
if (reader != null){
reader.close();
}
} catch (IOException e) {
}
}
return null;
}
获取对应的token
public static String getAccessToken(){
JSONObject jo = new JSONObject();
jo.put("client_id", clientId);//商户client id
jo.put("client_secret", clientSecret);//商户client secret
jo.put("grant_type", "client_credentials");
return UMFUtil.postData(umfUrl+"oauth/authorize", jo.toString(), "application/json");
}
这样返回回来的就是联动优势对应的token了。
2.发送请求到联动优势
首先创建请求方法:
/**
* 方法功能说明: 请求地址,获得数据
* @param urlStr 请求地址
* @param param 请求数据
* @return
* @return String
*/
public static String httpPostData(String urlStr, String param,String sign) {
String token = UMFConfig.getAccessToken();//获取token
JSONObject jo = JSONObject.fromObject(token);
try {
Map<String,String> postMap = new HashMap<String,String>();
postMap.put("Content-Type", "application/json");
postMap.put("url", urlStr);
postMap.put("type", "POST");
postMap.put("json", param);
token = "Bearer" + jo.get("access_token");
postMap.put("Authorization", token);
postMap.put("Signature", sign);
String reBody = RestUtil.httpRestString(postMap);
return reBody;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
然后构造属于自己的数据:
/**
* 方法功能说明: 充值调用的方法
* @param payNumber 交易流水号
* @param payAmount 金额
* @param currency 币种
* @param url 当前域名
* @param returnUrl 回调地址
* @param crtPath crt路径
* @param p8Path p8路径
*/
public String buildUmf(String payNumber, String payAmount, String currency, String url, String returnUrl, String crtPath, String p8Path) {
RSAUtils rsaUtils = new RSAUtils(crtPath,p8Path , "", "UTF-8");
//组装payment支付
Payment payment = new Payment();
Payer payer = new Payer();
payer.setPaymentMethod(PaymentMethod.NOT_APPLICABLE);
payer.setInterfaceType(InterfaceType.SERVER_TO_WEB);
payer.setBusinessType(BusinessType.B2C);
PayerInfo payerInfo = new PayerInfo();
payer.setPayerInfo(payerInfo);
payment.setPayer(payer);
RiskInfo riskInfo = new RiskInfo();
riskInfo.setB0002("02");
payment.setRiskInfo(riskInfo);
com.umf.api.payments.Order order = new com.umf.api.payments.Order();
order.setMerReferenceId(payNumber);
order.setMerDate(DateUtils.formatDate(new Date(), "yyyyMMdd"));
Amount amount = new Amount();
amount.setTotal(payAmount);
amount.setCurrency(currency);
order.setAmount(amount);
order.setOrderSummary("pay");
SubOrder subOrder = new SubOrder();
subOrder.setMerSubReferenceId(payNumber);
subOrder.setTransCode(DateUtils.formatDate(new Date(), "MMddssSS"));
subOrder.setAmount(amount);
subOrder.setTransCode(UMFConfig.transCode);
List<SubOrder> subOrders = new ArrayList<SubOrder>();
subOrders.add(subOrder);
order.setSubOrders(subOrders);
payment.setOrder(order);
payment.setNotifyUrl(returnUrl);
payment.setRetUrl(url+Global.getFrontPath()+".html");
String s = new Gson().toJson(payment);
String data = UMFUtil.httpPostData(UMFConfig.umfUrl+"payments/payment", s, rsaUtils.createSign(s, 1, "rest"));
JSONObject jo = JSONObject.fromObject(data);
JSONObject ja1 = JSONObject.fromObject(jo.get("meta"));
if("0000".equals(ja1.get("ret_code"))){//返回成功时
JSONArray ja = JSONArray.fromObject(jo.get("links"));
List<Link> list = (List<Link>) JSONArray.toCollection(ja, Link.class);
for (Link link : list) {
if("upay".equals(link.getRef())){//获取页面代码
return link.getHref();
}
}
}
return null;
}
最后返回回来的就是联动优势的相应数据如下:/payments/payment请求数据:
{
"payer": {
"payment_method": "NOT_APPLICABLE",
"payer_info": {
},
"interface_type": "SERVER_TO_WEB",
"external_customer_id": "UMP1499826888843602",
"business_type": "B2C"
},
"order": {
"mer_reference_id": "0328133806172101",
"mer_date": "20180328",
"amount": {
"total": "0.02",
"currency": "CNY"
},
"order_summary": "CrossBorderOrderSummary",
"expire_time": "360",
"user_ip": "10.10.70.134",
"sub_orders": [{
"mer_sub_reference_id": "0328133806173102",
"trans_code": "03223010",
"amount": {
"total": "0.01",
"currency": "CNY"
},
"invoice_id": "123456"
},
{
"trans_code": "01121990",
"amount": {
"total": "0.01",
"currency": "CNY"
},
"is_customs": "True",
"invoice_id": "123456",
"items": [{
"mer_item_id": "0328133806173103",
"type": "FOOD",
"name": "banana",
"quantity": "2",
"description": "banana",
"amount": {
"total": "0.01",
"currency": "CNY"
}
}]
}
],
"sub_mer_id": "umfsubmer001"
},
"notify_url": ".",
"ret_url": "http://192.168.2.101:2213/notify0000V4.jsp",
"risk_info": {
"trans_type": "02"
}
}
响应数据: {
"meta": {
"ret_msg": "交易成功",
"ret_code": "0000"
},
"payment": {
"risk_info": {
"trans_type": "02"
},
"id": "PAY_GM4DAMZSHAYTGMZYGYYTSNZRGMZDAMJYGAZTEODA",
"state": "WAIT_BUYER_PAY",
"notify_url": "http://192.168.2.101:2216/spay_rest/payments/test/mer",
"payer": {
"external_customer_id": "UMP1499826888843602",
"business_type": "B2C",
"interface_type": "SERVER_TO_WEB",
"payment_method": "NOT_APPLICABLE",
"payer_info": {
}
},
"ret_url": "http://192.168.2.101:2213/notify0000V4.jsp",
"is_from_US": "N",
"order": {
"user_ip": "192.168.2.101",
"amount": {
"total": "0.02",
"currency": "CNY"
},
"sub_orders": [{
"mer_sub_reference_id": "0328133806173102",
"trans_code": "03223010",
"amount": {
"total": "0.01",
"currency": "CNY"
},
"invoice_id": "123456"
},
{
"mer_sub_reference_id": "0328133806358107",
"trans_code": "01121990",
"amount": {
"total": "0.01",
"currency": "CNY"
},
"invoice_id": "123456",
"is_customs": "True",
"items": [{
"amount": {
"total": "0.01",
"currency": "CNY"
},
"quantity": "2",
"mer_item_id": "0328133806173103",
"name": "banana",
"description": "banana",
"type": "FOOD"
}]
}
],
"sub_mer_id": "umfsubmer001",
"mer_date": "20180328",
"expire_time": "2018-03-28T19:38:06+0800",
"mer_reference_id": "0328133806172101",
"order_summary": "CrossBorderOrderSummary"
}
},
"links": [{
"ref": "self",
"method": "GET",
"href": "http://fx.soopay.net/cberest/v1/payments/payment/PAY_GM4DAMZSHAYTXXXZYGYTTTMZDAMJYGAZTEODA"
},
{
"ref": "refund",
"method": "POST",
"href": "http://fx.soopay.net/cberest/v1/payments/payment/PAY_GM4DAMZSHAYTGMTTXXSQDFRZRGMZDAMJYGAZTEODA/refund"
},
{
"ref": "asynchronous_refund",
"method": "POST",
"href": "http://fx.soopay.net/cberest/v1/payments/payment/PAY_GM4DAMZSJHNDSERYYTSNZRGMZDAMJYGAZTEODA/asynchronous_refund"
},
{
"ref": "apply_to_customs",
"method": "POST",
"href": "http://fx.soopay.net/cberest/v1/payments/payment/PAY_GM4DAMZMINDNSERZYGYYTSNZRGMZDAMJYGAZTEODA/apply_to_customs"
},
{
"ref": "upay",
"method": "GET",
"href": "https://fx.soopay.net/fx_upay/cbPluginPay.do?cbAmount=0.02&retUrl=https%3A%2F%2Ffx.soopay.net%2Ffx_upay%2FcbPluginPayReturn.do&busiId=06&orderId=0328133806172101&isShowTransUse=N&merName=%E8%B7%A8%E5%A2%83%E4%B8%BB%E6%89%AB%E6%94%AF%E4%BB%98%E6%B5%8B%E8%AF%95&isShowFrame=Y&isCollectUserInf=Y&interfaceType=01&trace=3803281338929813&exchangeRate=1¤cy=CNY&cbOrderType=&oriAmount=2&amount=2&goodsInf=CrossBorderOrderSummary&idenCheckFlag=N&rpid=PSP133806ca40bfc&instId=20000001&isShowCustomServiceInf=Y&expireTime=2018-03-28+19%3A38%3A06.484¬ifyUrl=http%3A%2F%2Ffx.soopay.net%2Fcberest%2Fv1%2Fpayments%2Fpayment%2Fnotify%2FN&merId=8139&busiPayUrl=http%3A%2F%2F192.168.2.101%3A8080%2Ffx_paybusi%2FA01002&orderDate=20180328&payDate=20180328&sign=e6e01ff02e0925ddw5ca201a964c3b115abe2b70"
}
]
}
返回回来后自动跳转到相应的地址做相应操作就好了。联动优势API上有部分解释可以对照理解。http://fx.soopay.net/rest_api/index_zh_cn.html#2-1
以上就是对联动优势的开发,还请大牛指教。