generated from root/miduo_server
233 lines
9.3 KiB
Java
233 lines
9.3 KiB
Java
package com.jmfy.controller;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.jmfy.dto.CPOrder;
|
|
import com.jmfy.dto.CPayOrder;
|
|
import com.jmfy.dto.CUserDao;
|
|
import com.jmfy.dto.CUserInfo;
|
|
import com.jmfy.paramBean.MHTRechargeRequestBean;
|
|
import com.jmfy.thrift.idl.RPCRequestIFace;
|
|
import com.jmfy.thrift.idl.Result;
|
|
import com.jmfy.thrift.pool.ClientAdapterPo;
|
|
import com.jmfy.thrift.pool.ServiceKey;
|
|
import com.jmfy.util.JsonUtil;
|
|
import com.jmfy.util.MD5Util;
|
|
import org.apache.commons.lang.StringUtils;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
import javax.annotation.Resource;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
import java.lang.reflect.Field;
|
|
import java.text.DateFormat;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.*;
|
|
|
|
/**
|
|
* consumerId 游戏用户唯一标识
|
|
* consumerName Cp自定义信息
|
|
* orderNo 平台订单编号
|
|
* orderDetail 订单详情
|
|
* mhtOrderAmt 充值金额(分)
|
|
* time 平台支付时间
|
|
* sign 签名
|
|
*/
|
|
@RestController
|
|
public class MHTHwRechargeController {
|
|
@Resource
|
|
private CUserDao cuserDao;
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(MHTHwRechargeController.class);
|
|
private static Gson gson = new Gson();
|
|
|
|
@RequestMapping(value = "/MHTHwcallback")
|
|
public String MHTcallback(HttpServletRequest request) throws Exception {
|
|
return process(request,"gp_online","6d415f7ae72649b5b9a248677b080034");
|
|
}
|
|
|
|
@RequestMapping(value = "/MHTHwIosCallback")
|
|
public String MHTHwIosCallback(HttpServletRequest request) throws Exception {
|
|
return process(request,"ios_online","5a367d9f2b60b7cf6c894b4e0acaa437");
|
|
}
|
|
|
|
@RequestMapping(value = "/MHTHwTestCallback")
|
|
public String MHTHwIestCallback(HttpServletRequest request) throws Exception {
|
|
return process(request,"gp_test","02738d4f8aad8f4860c3916be35dc89b");
|
|
}
|
|
|
|
@RequestMapping(value = "/MHTHwIosTestCallback")
|
|
public String MHTHwIosTestCallback(HttpServletRequest request) throws Exception {
|
|
return process(request,"ios_test","5f620985bd0be3d5e5fe66a289aaadc1");
|
|
}
|
|
|
|
|
|
private String process(HttpServletRequest request,String rechargeType,String appsecret) throws Exception{
|
|
HashMap<String, String> parameterMap = JsonUtil.getInstence().getParameterMap2(request);
|
|
if (parameterMap.isEmpty()) {
|
|
LOGGER.info("data is null");
|
|
return "FAIL";
|
|
}
|
|
|
|
MHTRechargeRequestBean.DataBean rechargeRequestBean = getData(parameterMap);
|
|
|
|
MHTRechargeRequestBean.DataBean data = rechargeRequestBean;
|
|
if (data == null) {
|
|
LOGGER.info("data is null");
|
|
return "FAIL";
|
|
}
|
|
|
|
String response = "SUCCESS";
|
|
try {
|
|
String consumerid = data.getConsumerId();
|
|
String sign = data.getSign();
|
|
|
|
String sig = getMySign(data, appsecret);
|
|
|
|
if (!sig.equals(sign)) {
|
|
LOGGER.info("callback==>roleUid={},sin derify fail,mine sign={} ", consumerid, sig);
|
|
return "sign fail";
|
|
}
|
|
|
|
response = insertOrder(data,rechargeType);
|
|
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
LOGGER.info("callback==>err " + e.toString());
|
|
response = "FAIL";
|
|
}
|
|
|
|
return response;
|
|
}
|
|
|
|
|
|
private MHTRechargeRequestBean.DataBean getData(HashMap<String, String> parameterMap) {
|
|
MHTRechargeRequestBean.DataBean rechargeRequestBean = gson.fromJson( new Gson().toJson(parameterMap), MHTRechargeRequestBean.DataBean.class);
|
|
return rechargeRequestBean;
|
|
}
|
|
|
|
|
|
private String insertOrder(MHTRechargeRequestBean.DataBean data,String rechargeType) {
|
|
try {
|
|
String consumerid = data.getConsumerId();
|
|
String consumerName = data.getConsumerName();
|
|
String cpOrderId = "0";
|
|
String serverId = "0";
|
|
String orderNo = data.getOrderNo();
|
|
String[] split1 = consumerid.split("_");
|
|
if(split1.length==4){
|
|
consumerid = split1[0];
|
|
consumerName = split1[1];
|
|
cpOrderId = split1[2];
|
|
serverId = split1[3];
|
|
LOGGER.info("callback==>roleUid={},consumerName={}",consumerid,consumerName);
|
|
}else{
|
|
LOGGER.info("参数结构不对{}",consumerid);
|
|
return "FAIL";
|
|
}
|
|
|
|
CUserInfo cUserInfo = cuserDao.findUserInfo(Integer.valueOf(consumerid));
|
|
if (cUserInfo == null) {
|
|
LOGGER.info("该用户无此角色{}" , consumerid);
|
|
return "FAIL";
|
|
}
|
|
CPOrder cpOrder = cuserDao.findCpOrderInfo(serverId,cpOrderId);
|
|
if (cpOrder == null) {
|
|
LOGGER.info("{}区服无此cp订单发起,或已完成此订单==>{}",serverId, cpOrderId);
|
|
return "FAIL";
|
|
}
|
|
|
|
CPayOrder cPayOrder = cuserDao.getCpayOrderByOrderId(orderNo);
|
|
|
|
if (cPayOrder != null) {
|
|
LOGGER.info("callback==>creditId={},uId={} orderId is exit!!!", cUserInfo.getId(), cUserInfo.getOpenId());
|
|
return "FAIL";
|
|
} else {
|
|
DateFormat dateTimeformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
String date = dateTimeformat.format(new Date());
|
|
cPayOrder = new CPayOrder();
|
|
cPayOrder.setOrderId(orderNo);
|
|
cPayOrder.setServerId(cUserInfo.getServerid());
|
|
cPayOrder.setDelivery_time(date);
|
|
cPayOrder.setUserId(consumerid);
|
|
cPayOrder.setAmount(String.valueOf( (int)(Double.valueOf(data.getMhtOrderAmt())*100)));
|
|
cPayOrder.setGoodsId(consumerName);
|
|
cPayOrder.setRecharge_type(rechargeType);
|
|
Result result = null;
|
|
String userAddress = cuserDao.findUserAddress(cUserInfo.getId());
|
|
if (userAddress != null) {
|
|
LOGGER.info("callback==>userAddress={} ", userAddress);
|
|
String[] split = userAddress.split(":");
|
|
String ip = split[0];
|
|
String port = split[1];
|
|
LOGGER.info("RPC address --> IP : " + ip + "; PORT : " + port);
|
|
ClientAdapterPo<RPCRequestIFace.Client> rPCClient = null;
|
|
String serviceKey = JsonUtil.getServiceKey(ServiceKey.RPCCore, ip, port);
|
|
LOGGER.info("serviceKey : " + serviceKey);
|
|
try {
|
|
rPCClient = ClientAdapterPo.getClientAdapterPo(serviceKey);
|
|
result = rPCClient.getClient().deliveryRecharge(cUserInfo.getId(), consumerName, consumerid, orderNo+"_"+cpOrderId, 0, (int)(Double.valueOf(data.getMhtOrderAmt())*10000));
|
|
} catch (Exception e) {
|
|
LOGGER.info("callback=>", e);
|
|
} finally {
|
|
if (rPCClient != null) {
|
|
rPCClient.returnObject(serviceKey);
|
|
} else {
|
|
LOGGER.info("callback=> rPCClient is null ");
|
|
}
|
|
}
|
|
} else {
|
|
LOGGER.info("not get the user url,the openID={} ", consumerid);
|
|
return "FAIL";
|
|
}
|
|
if (result != null && result.getResultCode() == 1) {
|
|
LOGGER.info(" callback==>RPC result : ResultCode : " + result.getResultCode() + "; ResultMsg : " + result.getResultMsg());
|
|
cuserDao.addCpayOrder(cPayOrder);
|
|
} else {
|
|
return "FAIL";
|
|
}
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
LOGGER.info("callback==>err " + e.toString());
|
|
return "FAIL";
|
|
}
|
|
return "SUCCESS";
|
|
}
|
|
|
|
private static String getMySign(MHTRechargeRequestBean.DataBean rechargeRequestBean,String appsecret) throws IllegalAccessException {
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
TreeMap<String, String> tempMap = new TreeMap<>(new Comparator<String>() {
|
|
@Override
|
|
public int compare(String o1, String o2) {
|
|
return o1.compareTo(o2);
|
|
}
|
|
});
|
|
|
|
MHTRechargeRequestBean.DataBean data = rechargeRequestBean;
|
|
Class<? extends MHTRechargeRequestBean.DataBean> aClass = rechargeRequestBean.getClass();
|
|
Field[] fields = aClass.getDeclaredFields();
|
|
for (Field field : fields) {
|
|
field.setAccessible(true);
|
|
if ("sign".equals(field.getName())) {
|
|
continue;
|
|
}
|
|
tempMap.put(field.getName(), field.get(data).toString());
|
|
}
|
|
for (Map.Entry<String, String> item : tempMap.entrySet()) {
|
|
String value = item.getValue();
|
|
if (StringUtils.isEmpty(value)) {
|
|
continue;
|
|
}
|
|
String key = item.getKey();
|
|
stringBuilder.append(key).append("=").append(value).append("&");
|
|
}
|
|
String plain = stringBuilder.substring(0, stringBuilder.length() - 1) +MD5Util.encrypByMd5(appsecret);
|
|
System.out.println(plain);
|
|
return MD5Util.encrypByMd5(plain);
|
|
}
|
|
|
|
|
|
}
|