241 lines
8.1 KiB
C#
241 lines
8.1 KiB
C#
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace WX.CRM.Common.WxPay
|
|
{
|
|
public class CheckPayApi
|
|
{
|
|
public CheckPayApi(string username, string secret, string identityKey)
|
|
{
|
|
UserName = username;
|
|
Secret = secret;
|
|
IdentityKey = identityKey;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 微信支付状态
|
|
/// </summary>
|
|
/// <param name="param"></param>
|
|
/// <returns></returns>
|
|
public ApiResult<PayStatus> WechatPayStatus(Dictionary<string, object> param)
|
|
{
|
|
try
|
|
{
|
|
string url = $"{apiPath}wechat/queryTradeStatusByChannel";
|
|
//默认参数
|
|
|
|
//拼接验证头
|
|
Dictionary<string, string> header = new Dictionary<string, string>();
|
|
var timestamps = GetTimeStamp();
|
|
var sign = GetSign(param, timestamps);
|
|
var authorization = $"{UserName}:{sign}";
|
|
|
|
header.Add("authorization", authorization);
|
|
header.Add("timestamps", timestamps);
|
|
|
|
//请求
|
|
var req = Post(url, param, header);
|
|
LogHelper.Info("wechat:" + req);
|
|
//返回
|
|
var reqInfo = JsonConvert.DeserializeObject<ApiResult<PayStatus>>(req);
|
|
if (reqInfo == null)
|
|
{
|
|
return new ApiResult<PayStatus> { code = "501", message = "数据为空" };
|
|
}
|
|
return reqInfo;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ApiResult<PayStatus> { code = "501", message = ex.Message };
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 支付宝支付状态
|
|
/// </summary>
|
|
/// <param name="param"></param>
|
|
/// <returns></returns>
|
|
public ApiResult<PayStatus> AliPayStatus(Dictionary<string, object> param)
|
|
{
|
|
try
|
|
{
|
|
string url = $"{apiPath}alipay/queryTradeStatusByChannel";
|
|
|
|
//拼接验证头
|
|
Dictionary<string, string> header = new Dictionary<string, string>();
|
|
var timestamps = GetTimeStamp();
|
|
var sign = GetSign(param, timestamps);
|
|
var authorization = $"{UserName}:{sign}";
|
|
|
|
header.Add("authorization", authorization);
|
|
header.Add("timestamps", timestamps);
|
|
|
|
//请求
|
|
var req = Post(url, param, header);
|
|
LogHelper.Info("alipay:" + req);
|
|
//返回
|
|
var reqInfo = JsonConvert.DeserializeObject<ApiResult<PayStatus>>(req);
|
|
if (reqInfo == null)
|
|
{
|
|
return new ApiResult<PayStatus> { code = "501", message = "数据为空" };
|
|
}
|
|
return reqInfo;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ApiResult<PayStatus> { code = "501", message = ex.Message };
|
|
}
|
|
}
|
|
|
|
|
|
#region 内部参数
|
|
// https://uppay.soft.dn8188.com/payServer/ 正式
|
|
// https://uppay.soft.dn8188.com/payServerTest/ 测试
|
|
// "https://uppay.soft.dn8188.com/unifiedpaytest/"; 本地
|
|
private string apiPath = Utility.GetSettingByKey("DGpayApi");
|
|
private string UserName { get; set; }
|
|
private string Secret { get; set; }
|
|
//DG_SOFTWARE
|
|
private string IdentityKey { get; set; }
|
|
#endregion
|
|
|
|
#region 内部方法
|
|
/// <summary>
|
|
/// 内置请求
|
|
/// </summary>
|
|
/// <param name=""></param>
|
|
/// <returns></returns>
|
|
private string Post(string url, Dictionary<string, object> param, Dictionary<string, string> header)
|
|
{
|
|
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
|
|
//请求设置
|
|
request.Method = "POST";
|
|
request.ContentType = "application/json;charset=UTF-8";
|
|
request.Timeout = 3000;//设置超时时间
|
|
//拼接请求头
|
|
foreach (var item in header)
|
|
{
|
|
request.Headers.Add(item.Key, item.Value);
|
|
}
|
|
//拼接参数
|
|
if (param.Any())
|
|
{
|
|
var pStr = JsonConvert.SerializeObject(param);
|
|
byte[] data = Encoding.UTF8.GetBytes(pStr);
|
|
using (Stream stream = request.GetRequestStream())
|
|
{
|
|
stream.Write(data, 0, data.Length);
|
|
}
|
|
}
|
|
//提交请求
|
|
HttpWebResponse response;
|
|
try
|
|
{
|
|
response = (HttpWebResponse)request.GetResponse();
|
|
|
|
}
|
|
catch (WebException ex)
|
|
{
|
|
response = (HttpWebResponse)ex.Response;
|
|
}
|
|
//解析请求结果
|
|
using (Stream myResponseStream = response.GetResponseStream())
|
|
{
|
|
StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
|
|
string retString = myStreamReader.ReadToEnd();
|
|
myStreamReader.Close();
|
|
//记录请求情况
|
|
LogHelper.Info($"url:{url},param:{JsonConvert.SerializeObject(param)},header:{JsonConvert.SerializeObject(header)},req:{retString}");
|
|
return retString;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 计算签名
|
|
/// </summary>
|
|
/// <param name="param"></param>
|
|
/// <param name="timestamps"></param>
|
|
/// <returns></returns>
|
|
private string GetSign(Dictionary<string, object> param, string timestamps)
|
|
{
|
|
|
|
//一次排序
|
|
var newP = param.OrderBy(m => m.Key).ToDictionary(m => m.Key, n => n.Value);
|
|
var pJosn = JsonConvert.SerializeObject(newP);
|
|
|
|
//二次排序
|
|
var enStrList = new string[] { UserName, pJosn, Secret, timestamps };
|
|
Array.Sort(enStrList, string.CompareOrdinal);
|
|
|
|
//拼接
|
|
var enStr = string.Join("", enStrList);
|
|
//md5 加密
|
|
return _md5(enStr);
|
|
}
|
|
/// <summary>
|
|
/// 计算 md5
|
|
/// </summary>
|
|
/// <param name="enCode"></param>
|
|
/// <returns></returns>
|
|
private string _md5(string enCode)
|
|
{
|
|
string res = "";
|
|
byte[] data = Encoding.GetEncoding("utf-8").GetBytes(enCode);
|
|
MD5 md5 = new MD5CryptoServiceProvider();
|
|
byte[] bytes = md5.ComputeHash(data);
|
|
for (int i = 0; i < bytes.Length; i++)
|
|
{
|
|
res += bytes[i].ToString("x2");
|
|
}
|
|
return res;
|
|
}
|
|
/// <summary>
|
|
/// 获取时间戳
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private string GetTimeStamp()
|
|
{
|
|
TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 8, 0, 0, 0);
|
|
return Convert.ToInt64(ts.TotalSeconds).ToString();
|
|
}
|
|
#endregion
|
|
}
|
|
/// <summary>
|
|
/// 返回格式
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
public class ApiResult<T> where T : new()
|
|
{
|
|
public string code { get; set; }
|
|
public T data { get; set; }
|
|
public string message { get; set; }
|
|
}
|
|
public class PayStatus
|
|
{
|
|
/// <summary>
|
|
/// 三方订单号(微信/支付宝)
|
|
/// </summary>
|
|
public string tradeNo { get; set; }
|
|
/// <summary>
|
|
/// 商户订单号
|
|
/// </summary>
|
|
public string outTradeNo { get; set; }
|
|
/// <summary>
|
|
/// 金额
|
|
/// </summary>
|
|
public int totalAmount { get; set; }
|
|
/// <summary>
|
|
/// 状态 0 未支付 1 已支付
|
|
/// </summary>
|
|
public int tradeStatus { get; set; }
|
|
/// <summary>
|
|
/// 付款时间
|
|
/// </summary>
|
|
public DateTime? payTime { get; set; }
|
|
}
|
|
}
|