generated from root/miduo_server
222 lines
8.8 KiB
Java
222 lines
8.8 KiB
Java
package com.jmfy.controller;
|
|
|
|
import com.google.gson.Gson;
|
|
import com.jmfy.dto.CPayOrder;
|
|
import com.jmfy.dto.CUserDao;
|
|
import com.jmfy.dto.CUserInfo;
|
|
import com.jmfy.paramBean.CHRechargeBean;
|
|
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.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
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.*;
|
|
|
|
/**
|
|
* @author lvxinran
|
|
* @date 2021/6/8
|
|
* @discribe
|
|
*/
|
|
@RestController
|
|
public class CHGnRechargeController {
|
|
@Resource
|
|
private CUserDao cuserDao;
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(CHGnRechargeController.class);
|
|
private static Gson gson = new Gson();
|
|
|
|
|
|
@RequestMapping(value = "/Web/CHGnCallback")
|
|
public ResponseEntity CHGnCallback(HttpServletRequest request) throws Exception {
|
|
ResponseEntity<String> tResponseEntity = new ResponseEntity<String>(HttpStatus.NON_AUTHORITATIVE_INFORMATION);
|
|
return process(request,"2d74a35892ca1f4f8070bd6afe796cd9");
|
|
}
|
|
|
|
@RequestMapping(value = "/Web/CHGnIosCallback")
|
|
public ResponseEntity CHGnIosCallback(HttpServletRequest request) throws Exception {
|
|
return process(request,"2d74a35892ca1f4f8070bd6afe796cd9");
|
|
}
|
|
|
|
@RequestMapping(value = "/Web/CHGnTestCallback")
|
|
public ResponseEntity CHGnTestCallback(HttpServletRequest request) throws Exception {
|
|
return process(request,"2d74a35892ca1f4f8070bd6afe796cd9");
|
|
}
|
|
|
|
protected ResponseEntity process(HttpServletRequest request,String appsecret) throws Exception{
|
|
Map<String, String> parameterMap = JsonUtil.getInstence().getParameterMap2(request);
|
|
ResponseEntity<String> responseEntity = new ResponseEntity<>(HttpStatus.NON_AUTHORITATIVE_INFORMATION);
|
|
if (parameterMap.isEmpty()) {
|
|
LOGGER.info("data is null");
|
|
return responseEntity;
|
|
}
|
|
|
|
CHRechargeBean data = getData(parameterMap);
|
|
|
|
if (data == null) {
|
|
LOGGER.info("data is null");
|
|
return responseEntity;
|
|
}
|
|
try {
|
|
String consumerid = data.getUserid();
|
|
String sign = data.getSign();
|
|
|
|
String sig = getMySign(data, appsecret);
|
|
|
|
if (!sig.toUpperCase().equals(sign)) {
|
|
responseEntity = new ResponseEntity<>(HttpStatus.OK);
|
|
LOGGER.info("callback==>roleUid={},sin derify fail,mine sign={} ", consumerid, sig);
|
|
return responseEntity;
|
|
}
|
|
|
|
String response = insertOrder(data);
|
|
if(response.equals("SUCCESS")){
|
|
LOGGER.info("successful xuexinpeng");
|
|
Map<String,Object> map = new HashMap<String, Object>();
|
|
map.put("code",200);
|
|
map.put("msg","成功");
|
|
map.put("data","");
|
|
//return new ResponseEntity<>(HttpStatus.OK);
|
|
return new ResponseEntity<Map<String, Object>>(map, HttpStatus.OK);
|
|
}else{
|
|
return responseEntity;
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
LOGGER.info("callback==>err " + e.toString());
|
|
return responseEntity;
|
|
}
|
|
}
|
|
|
|
|
|
private CHRechargeBean getData(Map<String, String> parameterMap) {
|
|
CHRechargeBean rechargeRequestBean = gson.fromJson( new Gson().toJson(parameterMap), CHRechargeBean.class);
|
|
return rechargeRequestBean;
|
|
}
|
|
|
|
|
|
private String insertOrder(CHRechargeBean data) {
|
|
try {
|
|
//String user_id = data.getUserid();
|
|
String user_id = data.getExtra();
|
|
|
|
//String consumerName = data.getExtra();
|
|
|
|
String orderNo = data.getOrderno_cp();
|
|
int amount = data.getPay_amt();
|
|
String uid = user_id.split("_")[0];
|
|
String goodid = user_id.split("_")[1];
|
|
CUserInfo cUserInfo = cuserDao.findUserInfo(Integer.valueOf(uid));
|
|
if (cUserInfo == null) {
|
|
LOGGER.info("该用户无此角色" + uid);
|
|
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(uid);
|
|
cPayOrder.setAmount(String.valueOf(amount));
|
|
cPayOrder.setGoodsId(goodid);
|
|
// cPayOrder.setRecharge_type();
|
|
|
|
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(),goodid, data.getUserid(), orderNo, 0, 0);
|
|
|
|
|
|
//result = rPCClient.getClient().deliveryRecharge(cUserInfo.getId(), consumerName, data.getConsumerid(), orderNo, 0, 0);
|
|
} 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={} ", uid);
|
|
return "FAIL";
|
|
}
|
|
if (result != null && result.getResultCode() == 1) {
|
|
LOGGER.info(" callback==>RPC result : ResultCode : " + result.getResultCode() + "; ResultMsg : " + result.getResultMsg());
|
|
cuserDao.addCpayOrder(cPayOrder);
|
|
} else {
|
|
System.out.println("测试代码");
|
|
return "FAIL";
|
|
}
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
LOGGER.info("callback==>err " + e.toString());
|
|
return "FAIL";
|
|
}
|
|
return "SUCCESS";
|
|
}
|
|
|
|
private static String getMySign(CHRechargeBean 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);
|
|
}
|
|
});
|
|
|
|
Class<? extends CHRechargeBean> 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(rechargeRequestBean).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) + appsecret;
|
|
System.out.println(plain);
|
|
return MD5Util.encrypByMd5(plain);
|
|
}
|
|
}
|