quick支付

master
mengchengzhen 2021-06-28 10:17:27 +08:00
parent ee2dc9aac8
commit a45f2e71f5
2 changed files with 263 additions and 0 deletions

View File

@ -87,6 +87,12 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> <artifactId>spring-boot-starter-web</artifactId>
</dependency> </dependency>
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>

View File

@ -0,0 +1,257 @@
package com.jmfy.controller;
import com.jmfy.dto.CPayOrder;
import com.jmfy.dto.CUserDao;
import com.jmfy.dto.CUserInfo;
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.springframework.web.bind.annotation.RestController;
import org.xml.sax.InputSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.DocumentException;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.StringReader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@RestController
public class QuickRechargeController {
@Resource
private CUserDao cuserDao;
private static final Logger LOGGER = LoggerFactory.getLogger(QuickRechargeController.class);
private static final String callbackkey = "95443521616386247430552526885495";
private static final String md5key = "zr2i1hqfp54iijlvk8yjhopxrdq23nb3";
@RequestMapping(value = "/QUICKCallback")
public String QuickGnCallback(HttpServletRequest request) throws Exception {
return process(request,callbackkey);
}
private String process(HttpServletRequest request,String appsecret) throws Exception{
HashMap<String, String> parameterMap = JsonUtil.getInstence().getParameterMap2(request);
if (parameterMap.isEmpty()) {
LOGGER.info("data is null");
return "FAILED";
}
String ntdata0 = parameterMap.get("nt_data");
String ntdata = decode(ntdata0,callbackkey);
Map<String,String> map = getData(ntdata);
LOGGER.info("MapInfo: is_test:{},channel:{},channel_uid:{},game_order:{},order_no:{},pay_time:{}," +
"amount:{},atatus:{}",map.get("is_test"),map.get("channel"),map.get("channel_uid"),
map.get("game_order"),map.get("order_no"),map.get("pay_time"));
if (map.size() == 0) {
LOGGER.info("data is null");
return "FAILED";
}
String response = "SUCCESS";
try {
String consumerid = map.get("channel_uid");
String sign = parameterMap.get("sign");
String md5Sign = parameterMap.get("md5Sign");
if(!sign(ntdata0,sign,md5Sign)){
LOGGER.info("callback==>roleUid={},sin derify fail ", consumerid);
return "FAILED";
}
if(map.get("status").equals("1")){
LOGGER.info("callback==>status:FAILED ");
return "FAILED";
}
response = insertOrder(map);
} catch (Exception e) {
e.printStackTrace();
LOGGER.info("callback==>err " + e.toString());
response = "FAILED";
}
return response;
}
private String insertOrder(Map<String,String> map) {
try {
// 角色id_物品id_cc值
String callbackInfo = map.get("extras_params");
String[] callback = callbackInfo.split("_");
String uid,goodid,cc_id;
if (callback.length >= 3){
uid = callback[0];
goodid = callback[1];
cc_id = callback[2];
}else {
uid = callback[0];
goodid = callback[1];
cc_id = "";
}
String orderNo = map.get("order_no");
String amount = map.get("amount");
CUserInfo cUserInfo = cuserDao.findUserInfo(Integer.valueOf(uid));
if (cUserInfo == null) {
LOGGER.info("该用户无此角色" + uid);
return "FAILED";
}
CPayOrder cPayOrder = cuserDao.getCpayOrderByOrderId(orderNo);
if (cPayOrder != null) {
LOGGER.info("callback==>creditId={},uId={} orderId is exit!!!", cUserInfo.getId(), cUserInfo.getOpenId());
return "FAILED";
} else {
DateFormat dateTimeformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = dateTimeformat.format(new Date(System.currentTimeMillis()));
cPayOrder = new CPayOrder();
cPayOrder.setOrderId(orderNo);
cPayOrder.setServerId(cUserInfo.getServerid());
cPayOrder.setDelivery_time(date);
cPayOrder.setUserId(uid);
cPayOrder.setAmount(amount);
cPayOrder.setGoodsId(goodid);
cPayOrder.setCc_id(cc_id);
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, uid, orderNo, 0, Integer.parseInt(amount));
} 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 "FAILED";
}
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 "FAILED";
}
}
} catch (Exception e) {
e.printStackTrace();
LOGGER.info("callback==>err " + e.toString());
return "FAILED";
}
return "SUCCESS";
}
public boolean sign(String ntdata0,String sign0,String md5Sign){
String paramStr = ntdata0+sign0+md5key;
String sign = MD5Util.encrypByMd5(paramStr);
LOGGER.info("加密前字符串:{}",paramStr);
LOGGER.info("加密后得:{}",sign);
if(sign.equals(md5Sign)){
return true;
}else{
LOGGER.info("sign{} md5Sign{}",sign,md5Sign);
return false;
}
}
public String encode(String src,String key) {
byte[] data = src.getBytes();
byte[] keys = key.getBytes();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < data.length; i++) {
int n = (0xff & data[i]) + (0xff & keys[i % keys.length]);
sb.append("@" + n);
}
return sb.toString();
}
public String decode(String src,String key) {
if(src == null || src.length() == 0){
return src;
}
Pattern pattern = Pattern.compile("@([^@]*)");
Matcher m = pattern.matcher(src);
List<Integer> list = new ArrayList();
while (m.find()) {
try {
String group = m.group(1);
if(group.equals(""))
continue;
list.add(Integer.valueOf(group));
} catch (Exception e) {
e.printStackTrace();
return src;
}
}
if (list.size() > 0) {
byte[] data = new byte[list.size()];
byte[] keys = key.getBytes();
for (int i = 0; i < data.length; i++) {
data[i] = (byte) (list.get(i) - (0xff & keys[i % keys.length]));
}
return new String(data);
} else {
return src;
}
}
public static Map<String,String> getData(String xml){
//1.创建Reader对象
SAXReader reader = new SAXReader();
StringReader sr = new StringReader(xml);
InputSource is = new InputSource(sr);
Map<String,String> map = new HashMap<>();
//2.加载xml
try {
Document document = reader.read(is);
//3.获取根节点
Element rootElement = document.getRootElement();
Iterator iterator = rootElement.elementIterator();
while (iterator.hasNext()){
Element stu = (Element) iterator.next();
LOGGER.info("======遍历子节点======");
Iterator iterator1 = stu.elementIterator();
while (iterator1.hasNext()){
Element stuChild = (Element) iterator1.next();
map.put(stuChild.getName(),stuChild.getStringValue());
LOGGER.info("节点名:"+stuChild.getName()+"---节点值:"+stuChild.getStringValue());
}
}
}catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return map;
}
}