越南支付

master
grimm 2024-08-09 15:36:18 +08:00
parent a5bb183320
commit dc19d70d89
4 changed files with 287 additions and 2 deletions

View File

@ -0,0 +1,116 @@
package com.jmfy.controller;
import com.google.gson.Gson;
import com.jmfy.paramBean.PaySdkEnum;
import com.jmfy.util.HttpUtils;
import com.jmfy.util.JsonUtil;
import com.jmfy.util.MD5Util;
import net.sf.json.JSON;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.xml.sax.InputSource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.StringReader;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@RestController
public class VietnamGamotaRechargeController {
private static final Logger LOGGER = LoggerFactory.getLogger(VietnamGamotaRechargeController.class);
private static final String apikey = "GMA202401-4B0C8B6C-C3B7290DEC0B";
private static final String secretkey = "XAhRvf9ycmv6BJGTUJci";
@RequestMapping(value = "/GamotaCallback")
public String GamotaCallback(HttpServletRequest request) throws Exception {
return process(request,secretkey,apikey);
}
public static String process(HttpServletRequest request,String appsecret,String md5key) throws Exception{
JSONObject result = new JSONObject();
result.put("error_code",1);
result.put("messsage","Transaction fail");
HashMap<String, String> parameterMap = JsonUtil.getInstence().getParameterMap2(request);
if (parameterMap.isEmpty()) {
LOGGER.error("request data is null");
result.put("messsage", "request data is null");
return result.toString();
}
Map<String, String> map = verifyOrder(parameterMap);
if (map == null || map.isEmpty()){
LOGGER.error("verify order is error");
result.put("messsage", "verify order is error");
return result.toString();
}
LOGGER.info("越南gamota支付验证结果:{}",map);
int errorCode = Integer.parseInt(map.get("error_code"));
if (errorCode != 0){
LOGGER.error("verify order is false");
result.put("messsage", "verify order is false");
return result.toString();
}
Gson gson = new Gson();
Map<String, String> data = gson.fromJson(map.get("data"), Map.class);
String inserted = insertOrder(parameterMap, data);
if ("SUCCESS".equals(inserted)) {
result.put("error_code",0);
result.put("message","success");
}
return result.toString();
}
public static Map<String, String> verifyOrder(Map<String,String> map) {
String url = "https://paygate.gamota.com/v1/services/check_transaction";
Map<String,String> parms = new HashMap<>();
parms.put("transaction_id",map.get("transaction_id"));
try {
Gson gson = new Gson();
String data = HttpUtils.doPost(url, parms);
LOGGER.info("越南gamota支付验证请求结果:{}",data);
return gson.fromJson(data, Map.class);
}catch (IOException e){
LOGGER.error("越南gamota支付验证请求报错",e);
return null;
}
}
/**
*
* @param params
* @param data
* id_id_ccId_
* @return
*/
public static String insertOrder(Map<String,String> params, Map<String,String> data) {
String roleId = params.get("role_id");
String itemId = params.get("item_id");
String type = data.get("type");
String ccId = "0";
String callbackInfo = roleId + "_" + itemId + "_" +ccId + "_" + type;
String orderNo = data.get("transaction_id");
String amount = String.valueOf(Float.parseFloat(data.get("amount")) * 100);
Date time = new Date(System.currentTimeMillis());
String s = PayLogic.initOrder(callbackInfo, orderNo, amount, time, callbackInfo, PaySdkEnum.GAMOTA);
if ("ORDER_IS_EXIST".equals(s)) {
return "SUCCESS";
}
return s;
}
public static void main(String[] args) {
String a = "{\"error_code\":0,\"message\":\"Charging successfully!\",\"data\":{\"transaction_id\":\"AP20102226662769G\",\"type\":\"GOOGLE\",\"amount\":\"1.99\",\"vendor\":\"\",\"currency\":\"USD\",\"target\":\"username:XuanXuXu|userid:2618078\",\"state\":\"4_637389746321354058_9_1_1603352647\",\"sandbox\":1,\"time\":\"22\\/10\\/2020 14:44:20 GMT+7\",\"product_id\":\"com.gunx.item1.199\"}}";
Gson gson = new Gson();
System.out.println(gson.fromJson(a, Map.class));
}
}

View File

@ -22,7 +22,8 @@ public enum PaySdkEnum {
YOUGU(12,"YOUGU"), YOUGU(12,"YOUGU"),
FENGTI(13,"FENGTI"), FENGTI(13,"FENGTI"),
U1GAME(14, "U1GAME"), U1GAME(14, "U1GAME"),
REBATES(15, "REBATES") REBATES(15, "REBATES"),
GAMOTA(16, "GAMOTA")
; ;
private int id; private int id;

View File

@ -0,0 +1,169 @@
package com.jmfy.util;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class HttpUtils {
public static String doPost(String url, Map<String,String> parms) throws IOException {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httpPost = new HttpPost(url);
List<NameValuePair> nvps =getFromMap(parms);
httpPost.setEntity(new UrlEncodedFormEntity(nvps));
CloseableHttpResponse response2 = httpclient.execute(httpPost);
try {
HttpEntity entity = response2.getEntity();
return EntityUtils.toString(entity);
} catch (IOException e) {
e.printStackTrace();
return null;
} finally {
response2.close();
}
}
public static List<NameValuePair> getFromMap(Map<String,String> parms){
List<NameValuePair> nvps = new ArrayList<NameValuePair>(parms.size());
for(Map.Entry<String,String> item : parms.entrySet()){
nvps.add(new BasicNameValuePair(item.getKey(), item.getValue()));
}
return nvps;
}
public static String httpGetRequest(String httpUrl){
HttpURLConnection connection = null;
InputStream is = null;
BufferedReader br = null;
String result = null;// 返回结果字符串
try {
// 创建远程url连接对象
URL url = new URL(httpUrl);
// 通过远程url连接对象打开一个连接强转成httpURLConnection类
connection = (HttpURLConnection) url.openConnection();
// 设置连接方式get
connection.setRequestMethod("GET");
// 设置连接主机服务器的超时时间15000毫秒
connection.setConnectTimeout(15000);
// 设置读取远程返回的数据时间60000毫秒
connection.setReadTimeout(60000);
// 发送请求
connection.connect();
// 通过connection连接获取输入流
if (connection.getResponseCode() == 200) {
is = connection.getInputStream();
// 封装输入流is并指定字符集
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
// 存放数据
StringBuffer sbf = new StringBuffer();
String temp = null;
while ((temp = br.readLine()) != null) {
sbf.append(temp);
sbf.append("\r\n");
}
result = sbf.toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭资源
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
connection.disconnect();// 关闭远程连接
}
return result;
}
/**
* URL POST
*
* @param url
* URL
* @param param
* name1=value1&name2=value2
* @return
*/
public static String sendPost(String url, JSONObject param) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(conn.getOutputStream());
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送 POST 请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输出流、输入流
finally {
try {
if (out != null) {
out.close();
}
if (in != null) {
in.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
return result;
}
}

View File

@ -58,7 +58,6 @@ public class JsonUtil {
String key = paramName; String key = paramName;
if (!StringUtils.checkIsEmpty(paramValue)) { if (!StringUtils.checkIsEmpty(paramValue)) {
key = paramName + "=" + paramValue; key = paramName + "=" + paramValue;
} }
map.put(key, paramValue); map.put(key, paramValue);
LOGGER.info("parameter : {} ", key); LOGGER.info("parameter : {} ", key);