You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

397 lines
14 KiB

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Http;
using Sog;
using LitJson;
using ProtoCSStruct;
namespace HttpProxyWorld
{
public class MidasApi
{
private const string urlBalance = "/v3/r/mpay/get_balance_m";
private const string urlPay = "/v3/r/mpay/pay_m";
private const string urlCancelPay = "/v3/r/mpay/cancel_pay_m";
private const string urlPresent = "/v3/r/mpay/present_m";
static private string MakeQueryString(Dictionary<string, string> param)
{
if (param.Count == 0)
{
return "";
}
string query_string = "";
foreach (string key in param.Keys)
{
query_string = query_string + SnsSigCheck.UrlEncode(key, Encoding.UTF8) + "=" + SnsSigCheck.UrlEncode(param[key], Encoding.UTF8) + "&";
}
query_string = query_string.Substring(0, query_string.Length - 1);
return query_string;
}
static private string MakeCookieString(Dictionary<string, string> cookie)
{
if (cookie.Count == 0)
{
return null;
}
string[] arr_cookies = new string[cookie.Count];
int i = 0;
foreach (string key in cookie.Keys)
{
arr_cookies[i] = key + "=" + cookie[key];
i++;
}
return string.Join("; ", arr_cookies);
}
private static void MakeMidasCommParam(string url_script, ref AccountInfo account, ref QQPayInfo qqpayinfo, int zoneId
, Dictionary<string, string> param, Dictionary<string, string> cookie)
{
string sessionid = "";
string sessiontype = "";
string openkey = "";
if (account.AccountType == (int)AccountType.AccountType_QQ)
{
openkey = qqpayinfo.PayToken.GetString();
sessionid = "openid";
sessiontype = "kp_actoken";
}
else if (account.AccountType == (int)AccountType.AccountType_WX)
{
openkey = account.AccountToken.GetString();
sessionid = "hy_gameid";
sessiontype = "wc_actoken";
}
param.Add("appid", HttpProxyWorldServerUtils.GetServerConfig().appid);
param.Add("openid", account.AccountID.GetString());
param.Add("openkey", openkey);
param.Add("pf", qqpayinfo.Pf.GetString());
param.Add("pfkey", qqpayinfo.Pf.GetString());
param.Add("zoneid", zoneId.ToString());
param.Add("ts", HttpProxyWorldServerUtils.GetTimeSecond().ToString());
cookie.Add("session_id", sessionid);
cookie.Add("session_type", sessiontype);
cookie.Add("org_loc", SnsSigCheck.UrlEncode(url_script, Encoding.UTF8));
}
public static int QueryBalance(ref AccountInfo account, ref QQPayInfo qqpayinfo, int zoneId, out int iBalance)
{
iBalance = 0;
Dictionary<string, string> param = new Dictionary<string, string>();
Dictionary<string, string> cookie = new Dictionary<string, string>();
string url_script = urlPresent;
MakeMidasCommParam(url_script, ref account, ref qqpayinfo, zoneId, param, cookie);
// 生成签名
string secret = HttpProxyWorldServerUtils.GetServerConfig().appkey + "&";
string sig = SnsSigCheck.MakeSig("GET", url_script, param, secret);
param.Add("sig", sig);
string url = HttpProxyWorldServerUtils.GetServerConfig().midasHttpAddress + url_script;
string query_string = MakeQueryString(param);
string cookie_string = MakeCookieString(cookie);
Dictionary<string, string> httpHeaders = new Dictionary<string, string>();
httpHeaders.Add("Cookie", cookie_string);
url += "?" + query_string;
bool exception;
string strRet;
//最多尝试2次
using (HttpClient httpClient = new HttpClient())
{
for (int i = 0; i < 2; i++)
{
strRet = HttpUtils.HttpGet(httpClient, url, out exception, httpHeaders);
if (exception || strRet == null)
{
TraceLog.Error("MidasApi.QueryBalance, HttpGet return null query failed");
continue;
}
LitJson.JsonData jsonData = JsonMapper.ToObject(strRet);
LitJson.JsonData ret = jsonData["ret"];
LitJson.JsonData balance = jsonData["balance"];
if (ret == null)
{
TraceLog.Error("MidasApi.QueryBalance, ret {0}, no data", strRet);
continue;
}
if(ret.ToString() != "0")
{
return -1;
}
iBalance = Convert.ToInt32(balance.ToString());
return 0;
}
}
return -1;
}
public static int CostDiamond(ref SSPayDiamondHolderCostReq req, out int iBalance)
{
iBalance = 0;
Dictionary<string, string> param = new Dictionary<string, string>();
Dictionary<string, string> cookie = new Dictionary<string, string>();
string url_script = urlPay;
MakeMidasCommParam(url_script, ref req.Account, ref req.Qqpayinfo, req.RealmId, param, cookie);
param.Add("amt", req.Cost.ToString());
param.Add("billno", req.OrderId.GetString());
// 生成签名
string secret = HttpProxyWorldServerUtils.GetServerConfig().appkey + "&";
string sig = SnsSigCheck.MakeSig("GET", url_script, param, secret);
param.Add("sig", sig);
string url = HttpProxyWorldServerUtils.GetServerConfig().midasHttpAddress + url_script;
string query_string = MakeQueryString(param);
string cookie_string = MakeCookieString(cookie);
Dictionary<string, string> httpHeaders = new Dictionary<string, string>();
httpHeaders.Add("Cookie", cookie_string);
url += "?" + query_string;
bool exception;
string strRet;
//最多尝试3次
using (HttpClient httpClient = new HttpClient())
{
for (int i = 0; i < 3; i++)
{
strRet = HttpUtils.HttpGet(httpClient, url, out exception, httpHeaders);
if (exception || strRet == null)
{
TraceLog.Error("MidasApi.CostDiamond, HttpGet return null query failed");
continue;
}
LitJson.JsonData jsonData = JsonMapper.ToObject(strRet);
LitJson.JsonData ret = jsonData["ret"];
LitJson.JsonData balance = jsonData["balance"];
if (ret == null)
{
TraceLog.Error("MidasApi.CostDiamond, ret {0}, no data", strRet);
continue;
}
int iRet = Convert.ToInt32(ret.ToString());
if (iRet == 3000111)
{
TraceLog.Error("MidasApi.CostDiamond, ret {0}, no data", strRet);
continue;
}
//1002215这个表示成功(上次成功了)
if (iRet == 1002215)
{
TraceLog.Debug("MidasApi.CostDiamond, 1002215 ret {0}, no data", strRet);
}
else if (iRet == 0)
{
//成功
}
else
{
TraceLog.Error("MidasApi.CostDiamond, other ret {0}, no data", strRet);
return -1;
}
iBalance = Convert.ToInt32(balance.ToString());
return 0;
}
}
return -1;
}
public static int CancelCostDiamond(ref SSPayDiamondHolderCostCancelReq req)
{
Dictionary<string, string> param = new Dictionary<string, string>();
Dictionary<string, string> cookie = new Dictionary<string, string>();
string url_script = urlCancelPay;
MakeMidasCommParam(url_script, ref req.Account, ref req.Qqpayinfo, req.RealmId, param, cookie);
param.Add("billno", req.OrderId.GetString());
// 生成签名
string secret = HttpProxyWorldServerUtils.GetServerConfig().appkey + "&";
string sig = SnsSigCheck.MakeSig("GET", url_script, param, secret);
param.Add("sig", sig);
string url = HttpProxyWorldServerUtils.GetServerConfig().midasHttpAddress + url_script;
string query_string = MakeQueryString(param);
string cookie_string = MakeCookieString(cookie);
Dictionary<string, string> httpHeaders = new Dictionary<string, string>();
httpHeaders.Add("Cookie", cookie_string);
url += "?" + query_string;
bool exception;
string strRet;
//最多尝试3次
using (HttpClient httpClient = new HttpClient())
{
for (int i = 0; i < 3; i++)
{
strRet = HttpUtils.HttpGet(httpClient, url, out exception, httpHeaders);
if (exception || strRet == null)
{
TraceLog.Error("MidasApi.CancelCostDiamond, HttpGet return null query failed");
continue;
}
LitJson.JsonData jsonData = JsonMapper.ToObject(strRet);
LitJson.JsonData ret = jsonData["ret"];
if (ret == null)
{
TraceLog.Error("MidasApi.CancelCostDiamond, ret {0}, no data", strRet);
continue;
}
int iRet = Convert.ToInt32(ret.ToString());
if (iRet == 3000111)
{
TraceLog.Error("MidasApi.CancelCostDiamond, ret {0}, no data", strRet);
continue;
}
//1002215这个表示成功(上次成功了)
if (iRet == 1002215)
{
TraceLog.Debug("MidasApi.CancelCostDiamond, 1002215 ret {0}, no data", strRet);
}
else if (iRet == 0)
{
//成功
}
else
{
TraceLog.Error("MidasApi.CancelCostDiamond, other ret {0}, no data", strRet);
return -1;
}
return 0;
}
}
return -1;
}
public static int PresentDiamond(ref SSPayDiamondHolderAddReq req, out int iBalance)
{
iBalance = 0;
Dictionary<string, string> param = new Dictionary<string, string>();
Dictionary<string, string> cookie = new Dictionary<string, string>();
string url_script = urlPresent;
MakeMidasCommParam(url_script, ref req.Account, ref req.Qqpayinfo, req.RealmId, param, cookie);
param.Add("presenttimes", req.Add.ToString());
param.Add("billno", req.OrderId.GetString());
// 生成签名
string secret = HttpProxyWorldServerUtils.GetServerConfig().appkey + "&";
string sig = SnsSigCheck.MakeSig("GET", url_script, param, secret);
param.Add("sig", sig);
string url = HttpProxyWorldServerUtils.GetServerConfig().midasHttpAddress + url_script;
string query_string = MakeQueryString(param);
string cookie_string = MakeCookieString(cookie);
Dictionary<string, string> httpHeaders = new Dictionary<string, string>();
httpHeaders.Add("Cookie", cookie_string);
url += "?" + query_string;
bool exception;
string strRet;
//最多尝试3次
using (HttpClient httpClient = new HttpClient())
{
for (int i = 0; i < 3; i++)
{
strRet = HttpUtils.HttpGet(httpClient, url, out exception, httpHeaders);
if (exception || strRet == null)
{
TraceLog.Error("MidasApi.PresentDiamond, HttpGet return null query failed");
continue;
}
LitJson.JsonData jsonData = JsonMapper.ToObject(strRet);
LitJson.JsonData ret = jsonData["ret"];
LitJson.JsonData balance = jsonData["balance"];
if (ret == null)
{
TraceLog.Error("MidasApi.PresentDiamond, ret {0}, no data", strRet);
continue;
}
int iRet = Convert.ToInt32(ret.ToString());
if (iRet == 3000111)
{
TraceLog.Error("MidasApi.PresentDiamond, ret {0}, no data", strRet);
continue;
}
//1002215这个表示成功(上次成功了)
if (iRet == 1002215)
{
TraceLog.Debug("MidasApi.PresentDiamond, 1002215 ret {0}, no data", strRet);
}
else if (iRet == 0)
{
//成功
}
else
{
TraceLog.Error("MidasApi.AuthToken, other ret {0}, no data", strRet);
return -1;
}
iBalance = Convert.ToInt32(balance.ToString());
return 0;
}
}
return -1;
}
}
}