miduo_pay/src/main/java/com/jmfy/controller/HaoGameRechargeController.java

188 lines
7.5 KiB
Java

package com.jmfy.controller;
import com.jmfy.dto.CPayOrder;
import com.jmfy.dto.CUserDao;
import com.jmfy.dto.CUserInfo;
import com.jmfy.paramBean.HaoGameParamBean;
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.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* 好游戏
*/
@RestController
public class HaoGameRechargeController {
@Resource
private CUserDao cuserDao;
private static final String PAYKEY = "77cb6d6666424ade9783f1830d8fc243";
private static final Logger LOGGER = LoggerFactory.getLogger(HaoGameRechargeController.class);
@RequestMapping(value = "/haoGameCallback")
public String haoGameCallback(HttpServletRequest request, @RequestBody String body) throws Exception {
LOGGER.info("haoGameCallback body.length={} body={}", body.length(), body);
InputStream is = null;
try {
is = request.getInputStream();
StringBuilder sb = new StringBuilder();
byte[] b = new byte[4096];
for (int n; (n = is.read(b)) != -1; ) {
sb.append(new String(b, 0, n));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// try {
// BufferedReader reader = request.getReader();
// StringBuilder sb = new StringBuilder();
// String str;
// while ((str = reader.readLine()) != null) {
// sb.append(str);
// LOGGER.info("haoGameCallback getOuttradeno={} ",str);
// }
// String body = sb.toString();
// if (body.isEmpty()) {
// LOGGER.info("haoGameCallback body.isEmpty=");
// }
// JsonUtil.getInstence().getGson().fromJson(buffer)
HaoGameParamBean haoGameParamBean = JsonUtil.serializeToObject(body);
LOGGER.info("haoGameCallback getOuttradeno={} getOpenid={} getSign={} ={}", haoGameParamBean.getOuttradeno(), haoGameParamBean.getOpenid(), haoGameParamBean.getSign(), haoGameParamBean.getExtra());
// } catch (Exception e) {
// e.printStackTrace();
// }
SortedMap<String, String> parameterMap = new TreeMap<>();
if (parameterMap.isEmpty()) {
LOGGER.info("data is null");
return "failure";
}
String outtradeno = parameterMap.get("outtradeno");
String orderId = parameterMap.get("tradeno");
String status = parameterMap.get("status");
if (status == null || status.isEmpty() || "fail".equals(status)) {
LOGGER.info("status is null {} ", status);
return "failure";
}
String timeend = parameterMap.get("timeend");
String amount = parameterMap.get("amount");
String openid = parameterMap.get("openid");
String extra = parameterMap.get("extra");
String sign = parameterMap.get("sign");
if (extra == null || extra.isEmpty()) {
LOGGER.info("extra is null");
return "failure";
}
StringBuilder sbKey = new StringBuilder();
Set<Map.Entry<String, String>> entries = parameterMap.entrySet();
Iterator<Map.Entry<String, String>> iterator = entries.iterator();
while (iterator.hasNext()) {
Map.Entry<String, String> next = iterator.next();
String key = next.getKey();
Object value = next.getValue();
if (value == null || "".equals(value)) {
continue;
}
sbKey.append(key).append("=").append(value).append("&");
}
sbKey.append("secret=").append(PAYKEY);
String mySign = MD5Util.encrypByMd5(sbKey.toString()).toUpperCase();
if (!mySign.equals(sign)) {
LOGGER.info("callback==>roleUid={},sin derify fail, my sign={} sign={}", openid, mySign, sign);
return "failure";
}
JSONObject data = new JSONObject(extra);
String serverID = data.getString("districtid");// 区服id
String productID = data.getString("productId");
String ccId = data.getString("ccId");
String platform = data.getString("platform");
CUserInfo cUserInfo = cuserDao.findUserByOpenIdAndUidInfo(openid, serverID);
if (cUserInfo == null) {
LOGGER.info("该用户无此角色 openid={} serverID={}", openid, serverID);
return "failure";
}
CPayOrder cPayOrder = cuserDao.getCpayOrderByOrderId(orderId);
String response = "success";
if (cPayOrder != null) {
LOGGER.info("callback==>creditId={},uId={} orderId is exit!!!", cUserInfo.getId(), cUserInfo.getOpenId());
return response;
}
DateFormat dateTimeformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = dateTimeformat.format(new Date());
cPayOrder = new CPayOrder();
cPayOrder.setOrderId(orderId);
cPayOrder.setServerId(cUserInfo.getServerid());
cPayOrder.setDelivery_time(date);
cPayOrder.setAmount(amount);
cPayOrder.setGoodsId(productID);
cPayOrder.setCc_id(ccId);
cPayOrder.setPaySdk("haoyouxi");
cPayOrder.setPlatform(platform);
cPayOrder.setUserId(Integer.toString(cUserInfo.getId()));
Result result = null;
String userAddress = cuserDao.findUserAddress(cUserInfo.getId());
if (userAddress == null || userAddress.isEmpty()) {
LOGGER.info("callback==>userAddress={} openId={} uid={}", userAddress, openid, cUserInfo.getId());
return "failure";
}
String[] split = userAddress.split(":");
String ip = split[0];
String port = split[1];
LOGGER.info("RPC address uid={}--> IP : {} PORT : {}", cUserInfo.getId(), ip, port);
ClientAdapterPo<RPCRequestIFace.Client> rPCClient = null;
String serviceKey = JsonUtil.getServiceKey(ServiceKey.RPCCore, ip, port);
try {
rPCClient = ClientAdapterPo.getClientAdapterPo(serviceKey);
result = rPCClient.getClient().deliveryRecharge(cUserInfo.getId(), productID, openid, orderId, Long.parseLong(timeend), Integer.parseInt(amount));
} catch (Exception e) {
LOGGER.info("callback=>", e);
return "failure";
} finally {
if (rPCClient != null) {
rPCClient.returnObject(serviceKey);
} else {
LOGGER.info("callback=> rPCClient is null ");
}
}
if (result != null && result.getResultCode() == 1) {
LOGGER.info(" callback==>RPC result : ResultCode : " + result.getResultCode() + "; ResultMsg : " + result.getResultMsg());
cuserDao.addCpayOrder(cPayOrder);
return response;
}
return "failure";
}
}