356 lines
16 KiB
C#
356 lines
16 KiB
C#
using CsharpHttpHelper;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace Api.Framework.Tools
|
|
{
|
|
/// <summary>
|
|
/// 商户支付类
|
|
/// </summary>
|
|
public class WechatPay
|
|
{
|
|
public WechatPay()
|
|
{
|
|
|
|
}
|
|
private string wechat_pay_url = "http://wxpay.api.52cmg.cn";
|
|
private const int wechat_appid = 85599;
|
|
private const string wechat_appkey = "<RSAKeyValue><Modulus>vssNGL91TxssqPHsOek+febRdhhPDf5BwIAO2S/jr0kGsFJIrG1IbG7Y73kM14h+71q1KepvUSG6ngSvZzHxQsBNSUFWd0/kOqjTJp5az5CssPEZOE+5Y9JyOQQ6P6QijdhSxtGSrJt3RLKAeQGbEnc5lU5WB8g1zgIODrLzRQ0=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";
|
|
|
|
/// <summary>
|
|
/// 请求服务器
|
|
/// </summary>
|
|
/// <param name="method"></param>
|
|
/// <param name="param"></param>
|
|
/// <returns></returns>
|
|
private WebResult SendServer(string method, object param = null)
|
|
{
|
|
AuthEndpoint end = new AuthEndpoint()
|
|
{
|
|
Appid = wechat_appid,
|
|
Enckey = wechat_appkey,
|
|
Host = wechat_pay_url + "/api/pay.asmx/send_data",
|
|
Method = method
|
|
};
|
|
if (param != null)
|
|
{
|
|
var _param = param.GetType().GetProperties();
|
|
foreach (var item in _param)
|
|
{
|
|
end.Param[item.Name] = item.GetValue(param);
|
|
}
|
|
}
|
|
HttpHelper http = new HttpHelper();
|
|
return http.SendData(end);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 商户数据上传
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="gzh_appid"></param>
|
|
/// <param name="gzh_appsecret"></param>
|
|
/// <param name="sh_appid"></param>
|
|
/// <param name="sh_appsecret"></param>
|
|
/// <param name="sh_cerdata"></param>
|
|
/// <param name="footer"></param>
|
|
/// <returns></returns>
|
|
public string Upload(string url, string gzh_appid, string gzh_appsecret, string sh_appid, string sh_appsecret, byte[] sh_cerdata, string footer)
|
|
{
|
|
if (string.IsNullOrEmpty(url) || string.IsNullOrEmpty(gzh_appid) || string.IsNullOrEmpty(sh_appid)) throw new Exception("商户填写的数据信息不完整!");
|
|
var hex_data = HttpExtend.ByteToHex(sh_cerdata);
|
|
var result = SendServer("upload_shop", new { url = url, gzh_appid = gzh_appid, gzh_appsecret = gzh_appsecret, sh_appid = sh_appid, sh_appsecret = sh_appsecret, sh_cerdata = hex_data, footer = footer });
|
|
return result.message.ToString();
|
|
}
|
|
const string rsa_key = @"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCgPPwyc5JAbPRjkulZcat/JAYm
|
|
/eiPRhtyod1CmqLteE1v90HCPoLwejkrmUToRsaJKKOgObKdh/t3Od/41RBc3rJw
|
|
S4uX8VXqsq/VcLWh/GiieCNYqpb/7Chp7tfW7Sp+eyA/reT9LHRg27hLYfkhmu9f
|
|
eFGAS17iICAyv3jpUwIDAQAB";
|
|
|
|
/// <summary>
|
|
/// 商户创建
|
|
/// </summary>
|
|
/// <param name="url">商户地址</param>
|
|
/// <param name="remark">备注</param>
|
|
/// <param name="describe">描述</param>
|
|
/// <param name="money">金额</param>
|
|
/// <returns></returns>
|
|
[Obsolete("改方法已过时")]
|
|
public string Create(string url, object remark, string describe, double money)
|
|
{
|
|
var post = string.Empty;// "amount=[金额]&describe=[描述]&remark=[备注]".Replace("[金额]", money.ToString()).Replace("[描述]", HttpHelper.URLEncode(describe)).Replace("[备注]", HttpHelper.URLEncode(HttpHelper.ObjectToJson(remark)));
|
|
|
|
var http = new HttpHelper();
|
|
url = url.Replace("[金额]", money.ToString()).Replace("[描述]", HttpHelper.URLEncode(describe, Encoding.UTF8)).Replace("[备注]", HttpHelper.URLEncode(HttpHelper.ObjectToJson(remark), Encoding.UTF8));
|
|
//if (url.StartsWith("https://pay.api.52cmg.cn"))
|
|
//{
|
|
// RSACryption rsa = new RSACryption();
|
|
// var request_data = rsa.RsaEncrypt(HttpHelper.ObjectToJson(new
|
|
// {
|
|
// money = money,
|
|
// describe = describe,
|
|
// remark = remark
|
|
// }),rsa_key);
|
|
//}
|
|
|
|
var item = http.GetItem(url);
|
|
item.Encoding = Encoding.UTF8;
|
|
var data = http.GetHtml(item);
|
|
var result = data.Html;
|
|
var json = HttpExtend.JsonToDictionary(result);
|
|
if (json != null && json.ContainsKey("code"))
|
|
{
|
|
var code = int.Parse(json["code"].ToString());
|
|
if (code == 1)
|
|
{
|
|
var ispaid = false;
|
|
var msg = string.Empty;
|
|
if (json.ContainsKey("message"))
|
|
msg = json["message"].ToString();
|
|
else if (json.ContainsKey("msg"))
|
|
msg = json["msg"].ToString();
|
|
if (json.ContainsKey("ispaid"))
|
|
ispaid = json["ispaid"].ToString().ToUpper() == "TRUE";
|
|
return msg;
|
|
}
|
|
//else if (code == 2)
|
|
//{
|
|
// return "已转账";
|
|
//}
|
|
else EventClient.OnEvent(this, $"生成兑换链接失败:{json["msg"].ToString()}");
|
|
}
|
|
else EventClient.OnEvent(this, $"生成兑换链接失败:{result}");
|
|
|
|
return string.Empty;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 商户创建
|
|
/// </summary>
|
|
/// <param name="url">商户地址</param>
|
|
/// <param name="remark">备注</param>
|
|
/// <param name="describe">描述</param>
|
|
/// <param name="money">金额</param>
|
|
/// <param name="ispaid">是否直接到账</param>
|
|
/// <returns></returns>
|
|
public string Create(string url, object remark, string describe, double money, out bool ispaid)
|
|
{
|
|
var html = string.Empty;
|
|
try
|
|
{
|
|
var post = string.Empty;// "amount=[金额]&describe=[描述]&remark=[备注]".Replace("[金额]", money.ToString()).Replace("[描述]", HttpHelper.URLEncode(describe)).Replace("[备注]", HttpHelper.URLEncode(HttpHelper.ObjectToJson(remark)));
|
|
|
|
var http = new HttpHelper();
|
|
url = url.Replace("[金额]", money.ToString("0.00")).Replace("[描述]", HttpHelper.URLEncode(describe, Encoding.UTF8)).Replace("[备注]", HttpHelper.URLEncode(HttpHelper.ObjectToJson(remark), Encoding.UTF8));
|
|
//if (url.StartsWith("https://pay.api.52cmg.cn"))
|
|
//{
|
|
// RSACryption rsa = new RSACryption();
|
|
// var request_data = rsa.RsaEncrypt(HttpHelper.ObjectToJson(new
|
|
// {
|
|
// money = money,
|
|
// describe = describe,
|
|
// remark = remark
|
|
// }),rsa_key);
|
|
//}
|
|
|
|
ispaid = false;
|
|
var item = http.GetItem(url);
|
|
item.Timeout = 1000 * 60 * 10;
|
|
item.ReadWriteTimeout = 20000;
|
|
item.Encoding = Encoding.UTF8;
|
|
var data = http.GetHtml(item);
|
|
html = data.Html;
|
|
var json = HttpExtend.JsonToDictionary(html);
|
|
if (json != null && json.ContainsKey("code"))
|
|
{
|
|
var code = int.Parse(json["code"].ToString());
|
|
if (code == 1)
|
|
{
|
|
var msg = string.Empty;
|
|
if (json.ContainsKey("message"))
|
|
msg = json["message"]?.ToString();
|
|
else if (json.ContainsKey("msg"))
|
|
msg = json["msg"]?.ToString();
|
|
|
|
if (json.ContainsKey("ispaid"))
|
|
ispaid = json["ispaid"]?.ToString().ToUpper() == "TRUE";
|
|
return msg;
|
|
}
|
|
else
|
|
{
|
|
var msg = string.Empty;
|
|
if (json.ContainsKey("msg"))
|
|
msg = json["msg"]?.ToString();
|
|
else if (json.ContainsKey("message"))
|
|
msg = json["message"]?.ToString();
|
|
|
|
EventClient.OnEvent(this, $"生成商户兑换链接失败:{msg}");
|
|
}
|
|
}
|
|
else
|
|
EventClient.OnEvent(this, $"生成商户兑换链接失败:{html}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
EventClient.OnEvent(this, $"生成兑换链接失败:{html} - {ex.Message}");
|
|
}
|
|
ispaid = false;
|
|
return string.Empty;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 商户创建
|
|
/// </summary>
|
|
/// <param name="url">商户地址</param>
|
|
/// <param name="remark">备注</param>
|
|
/// <param name="describe">描述</param>
|
|
/// <param name="money">金额</param>
|
|
/// <param name="ispaid">是否直接到账</param>
|
|
/// <param name="appinfo">小程序信息,微信可以用来发送小程序;不支持小程序返回null</param>
|
|
/// <returns></returns>
|
|
public string Create(string url, object remark, string describe, double money, out bool ispaid, out object appinfo)
|
|
{
|
|
var html = string.Empty;
|
|
appinfo = null;
|
|
try
|
|
{
|
|
var post = string.Empty;// "amount=[金额]&describe=[描述]&remark=[备注]".Replace("[金额]", money.ToString()).Replace("[描述]", HttpHelper.URLEncode(describe)).Replace("[备注]", HttpHelper.URLEncode(HttpHelper.ObjectToJson(remark)));
|
|
|
|
var http = new HttpHelper();
|
|
url = url.Replace("[金额]", money.ToString("0.00")).Replace("[描述]", HttpHelper.URLEncode(describe, Encoding.UTF8)).Replace("[备注]", HttpHelper.URLEncode(HttpHelper.ObjectToJson(remark), Encoding.UTF8));
|
|
//if (url.StartsWith("https://pay.api.52cmg.cn"))
|
|
//{
|
|
// RSACryption rsa = new RSACryption();
|
|
// var request_data = rsa.RsaEncrypt(HttpHelper.ObjectToJson(new
|
|
// {
|
|
// money = money,
|
|
// describe = describe,
|
|
// remark = remark
|
|
// }),rsa_key);
|
|
//}
|
|
|
|
ispaid = false;
|
|
var item = http.GetItem(url);
|
|
item.Timeout = 30000;
|
|
item.ReadWriteTimeout = 20000;
|
|
item.Encoding = Encoding.UTF8;
|
|
var data = http.GetHtml(item);
|
|
html = data.Html;
|
|
|
|
//{"code":1,"msg":"https://ygh8i.kuaizhan.com/52/52/p825791697c346b?orderid=e8255ae1e8d928555d61de464d73af75\u0026appid=wx6e0df44505ec10f5","appinfo":null,"aliUrl":"https://ulink.alipay.com/?scheme=alipays%3A%2F%2Fplatformapi%2Fstartapp%3FsaId%3D10000007%26clientVersion%3D3.7.0.0718%26qrcode%3Dhttps%3A%2F%2Fpay.api.52cmg.cn%2Falipay%3Forderid%3De8255ae1e8d928555d61de464d73af75"}
|
|
|
|
var json = HttpExtend.JsonToDictionary(html);
|
|
if (json != null && json.ContainsKey("code"))
|
|
{
|
|
var code = int.Parse(json["code"].ToString());
|
|
if (code == 1)
|
|
{
|
|
var msg = string.Empty;
|
|
if (json.ContainsKey("message"))
|
|
msg = json["message"]?.ToString();
|
|
else if (json.ContainsKey("msg"))
|
|
msg = json["msg"]?.ToString();
|
|
|
|
if (json.ContainsKey("ispaid"))
|
|
ispaid = json["ispaid"]?.ToString().ToUpper() == "TRUE";
|
|
|
|
if (json.ContainsKey("appinfo"))
|
|
appinfo = json["appinfo"];
|
|
|
|
return msg;
|
|
}
|
|
else
|
|
{
|
|
var msg = string.Empty;
|
|
if (json.ContainsKey("msg"))
|
|
msg = json["msg"]?.ToString();
|
|
else if (json.ContainsKey("message"))
|
|
msg = json["message"]?.ToString();
|
|
|
|
EventClient.OnEvent(this, $"生成商户兑换链接失败:{msg}");
|
|
}
|
|
}
|
|
else
|
|
EventClient.OnEvent(this, $"生成商户兑换链接失败:{html}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
EventClient.OnEvent(this, $"生成兑换链接失败:{html} - {ex.Message}");
|
|
}
|
|
ispaid = false;
|
|
return string.Empty;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="url"></param>
|
|
/// <param name="remark"></param>
|
|
/// <param name="describe"></param>
|
|
/// <param name="money"></param>
|
|
/// <returns></returns>
|
|
public string Create2(string url, object remark, string describe, double money)
|
|
{
|
|
var html = string.Empty;
|
|
try
|
|
{
|
|
var http = new HttpHelper();
|
|
url = url.Replace("[金额]", ((int)(money * 100)).ToString()).Replace("[描述]", HttpHelper.URLEncode(describe, Encoding.UTF8)).Replace("[备注]", HttpHelper.URLEncode(HttpHelper.ObjectToJson(remark), Encoding.UTF8));
|
|
|
|
var item = http.GetItem(url);
|
|
item.Timeout = 30000;
|
|
item.ReadWriteTimeout = 20000;
|
|
item.Encoding = Encoding.UTF8;
|
|
var data = http.GetHtml(item);
|
|
html = data.Html;
|
|
var jObj = JObject.Parse(html);
|
|
if (jObj != null && jObj["code"] != null)
|
|
{
|
|
if ((int)jObj["code"] == 1)
|
|
{
|
|
var msg = jObj["data"]["withdraw_url"]?.ToString();
|
|
return msg;
|
|
}
|
|
else
|
|
EventClient.OnEvent(this, $"生成商户兑换链接失败1:{html}");
|
|
}
|
|
else
|
|
EventClient.OnEvent(this, $"生成商户兑换链接失败2:{html}");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
EventClient.OnEvent(this, $"生成兑换链接失败3:{html} - {ex.Message}");
|
|
}
|
|
return string.Empty;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 商户创建
|
|
/// </summary>
|
|
/// <param name="gzh_appid">公众号appid</param>
|
|
/// <param name="sh_appid">商户appid</param>
|
|
/// <param name="gzh_appkey">公众号appkey</param>
|
|
/// <param name="sh_appkey">商户appkey</param>
|
|
/// <param name="amount">金额</param>
|
|
/// <param name="remark">备注</param>
|
|
/// <param name="describe">描述</param>
|
|
/// <returns></returns>
|
|
public string Create(string gzh_appid, string sh_appid, string gzh_appkey, string sh_appkey, double amount, string remark, string describe)
|
|
{
|
|
if (string.IsNullOrEmpty(gzh_appid) || string.IsNullOrEmpty(sh_appid)) throw new Exception("商户填写的数据信息不完整!");
|
|
var password = HttpExtend.GetMD5String(gzh_appkey + sh_appkey);
|
|
var result = SendServer("create_order", new { gzh_appid = gzh_appid, sh_appid = sh_appid, amount = amount.ToString("0.00"), remark = remark, describe = describe, password = password });
|
|
return result.message.ToString();
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|