更新api和部分方法
This commit is contained in:
@@ -34,7 +34,7 @@ namespace COMCMS.Web.Controllers
|
||||
ViewBag.siteName = cfg.SiteName;
|
||||
|
||||
ViewBag.c = CookiesHelper.GetCookie("abc");
|
||||
ViewBag.d = Utils.SIGNSALT;
|
||||
ViewBag.d = Utils.GetServerUrl();
|
||||
|
||||
return View();
|
||||
}
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using COMCMS.Common;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace COMCMS.Web.Controllers.api
|
||||
{
|
||||
[Produces("application/json")]
|
||||
public class APIBaseController : Controller
|
||||
{
|
||||
#region 通用信息
|
||||
public ReJSON reJson = new ReJSON();
|
||||
#endregion
|
||||
|
||||
#region 验证签名
|
||||
public bool CheckSignature(SortedDictionary<string, string> pars, string signature)
|
||||
{
|
||||
if (!MySign.CheckSign(pars, signature))
|
||||
{
|
||||
reJson.code = 40004;
|
||||
reJson.message = "signature 错误!";
|
||||
return false;
|
||||
}
|
||||
//判断是否超时
|
||||
string timeStamp = pars["timeStamp"];
|
||||
//判断时间有效性
|
||||
DateTime postTime = Utils.StampToDateTime(timeStamp);
|
||||
if (postTime < DateTime.Now.AddSeconds(-120))//30秒有效期
|
||||
{
|
||||
reJson.code = 40004;
|
||||
reJson.message = "数据请求超时!";
|
||||
reJson.isReload = 1;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region 自动验证QueryString 的签名
|
||||
/// <summary>
|
||||
/// 自动验证QueryString 的签名
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public bool AutoCheckQueryStringSignature()
|
||||
{
|
||||
var queryStrings = Request.Query;
|
||||
List<string> keys = new List<string>();
|
||||
//string[] keys = HttpContext.Current.Request.QueryString.AllKeys;
|
||||
if (queryStrings == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
foreach (var item in queryStrings.Keys)
|
||||
{
|
||||
keys.Add(item);
|
||||
}
|
||||
SortedDictionary<string, string> pars = new SortedDictionary<string, string>();
|
||||
string signature = "";
|
||||
foreach (var k in keys)
|
||||
{
|
||||
if (k != "signature")
|
||||
{
|
||||
string v = Request.Query[k];
|
||||
pars.Add(k, v);
|
||||
}
|
||||
else
|
||||
{
|
||||
signature = Request.Query[k];
|
||||
}
|
||||
}
|
||||
//没有签名返回错误
|
||||
if (string.IsNullOrEmpty(signature))
|
||||
return false;
|
||||
return CheckSignature(pars, signature);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
||||
#region 通用返回信息
|
||||
public class ReJSON
|
||||
{
|
||||
/// <summary>
|
||||
/// 返回代码 0 为正确
|
||||
/// </summary>
|
||||
public int code { get; set; } = 40000;
|
||||
|
||||
/// <summary>
|
||||
/// 提示语
|
||||
/// </summary>
|
||||
public string message { get; set; }
|
||||
/// <summary>
|
||||
/// 是否重新加载
|
||||
/// </summary>
|
||||
public int isReload { get; set; } = 0;
|
||||
|
||||
/// <summary>
|
||||
/// 数据详情 object
|
||||
/// </summary>
|
||||
public object detail { get; set; }
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
@@ -11,8 +11,8 @@ using XCode;
|
||||
|
||||
namespace COMCMS.Web.Controllers.api
|
||||
{
|
||||
[Route("api/[controller]")]
|
||||
public class IndexController : Controller
|
||||
[Route("api/[controller]/[action]")]
|
||||
public class IndexController : APIBaseController
|
||||
{
|
||||
// GET: api/<controller>
|
||||
[HttpGet]
|
||||
@@ -22,11 +22,30 @@ namespace COMCMS.Web.Controllers.api
|
||||
return cfg;
|
||||
}
|
||||
|
||||
// GET api/<controller>/5
|
||||
[HttpGet("{id}")]
|
||||
public string Get(int id)
|
||||
#region 测试验签
|
||||
/// <summary>
|
||||
/// 测试验签
|
||||
/// </summary>
|
||||
/// <param name="id">id</param>
|
||||
/// <param name="random"></param>
|
||||
/// <param name="timeStamp"></param>
|
||||
/// <param name="signature"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public object CheckSign(string id, string random = "", string timeStamp = "", string signature = "")
|
||||
{
|
||||
return "value";
|
||||
if (!AutoCheckQueryStringSignature())
|
||||
{
|
||||
reJson.message = "验签失败!";
|
||||
return reJson;
|
||||
}
|
||||
else
|
||||
{
|
||||
reJson.code = 0;
|
||||
reJson.message = "验签成功!";
|
||||
return reJson;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using COMCMS.Common;
|
||||
using COMCMS.Core;
|
||||
using XCode;
|
||||
using NewLife.Log;
|
||||
using Senparc.Weixin.MP;
|
||||
using Senparc.Weixin.MP.TenPayLibV3;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace COMCMS.Web.Controllers.api
|
||||
{
|
||||
/// <summary>
|
||||
/// 支付API
|
||||
/// </summary>
|
||||
[Produces("application/json")]
|
||||
[Route("api/Payment")]
|
||||
public class PaymentController : APIBaseController
|
||||
{
|
||||
#region 微信小程序订单支付
|
||||
[HttpGet]
|
||||
public object DoPayOrder(string ordernum, string random = "", string timeStamp = "", string signature = "")
|
||||
{
|
||||
if (!AutoCheckQueryStringSignature())
|
||||
{
|
||||
return reJson;
|
||||
}
|
||||
|
||||
//获取订单
|
||||
Order entity = Order.Find(Order._.OrderNum == ordernum);
|
||||
if (entity == null)
|
||||
{
|
||||
reJson.code = 40000;
|
||||
reJson.message = "系统找不到本订单!";
|
||||
return reJson;
|
||||
}
|
||||
//判断订单状态
|
||||
if (entity.OrderStatus == Utils.OrdersState[3])
|
||||
{
|
||||
reJson.code = 40000;
|
||||
reJson.message = "已完成订单不允许支付!";
|
||||
return reJson;
|
||||
}
|
||||
if (entity.PaymentStatus != Utils.PaymentState[0])
|
||||
{
|
||||
reJson.code = 40000;
|
||||
reJson.message = "当前订单支付状态不允许支付!";
|
||||
return reJson;
|
||||
}
|
||||
//获取用户并判断是否是已经注册用户
|
||||
Member my = Member.FindById(entity.UId);
|
||||
if (my == null || string.IsNullOrEmpty(my.WeixinAppOpenId))
|
||||
{
|
||||
reJson.code = 40000;
|
||||
reJson.message = "用户状态错误,无法使用本功能!";
|
||||
return reJson;
|
||||
}
|
||||
//开始生成支付订单
|
||||
OnlinePayOrder model = new OnlinePayOrder();
|
||||
model.OrderId = entity.Id;
|
||||
model.OrderNum = entity.OrderNum;
|
||||
model.PayId = 1;
|
||||
model.PaymentNotes = "微信支付";
|
||||
model.PaymentStatus = Utils.PaymentState[0];
|
||||
model.PayOrderNum = Utils.GetOrderNum();//在线支付订单的订单号
|
||||
model.PayType = "微信支付";
|
||||
model.TotalPrice = entity.TotalPay;
|
||||
model.TotalQty = entity.TotalQty;
|
||||
model.UId = entity.UId;
|
||||
model.IP = Utils.GetIP();
|
||||
model.IsOK = 0;
|
||||
model.AddTime = DateTime.Now;
|
||||
model.Insert();
|
||||
|
||||
//写入日志
|
||||
OrderLog log = new OrderLog();
|
||||
log.AddTime = DateTime.Now;
|
||||
log.OrderId = entity.Id;
|
||||
log.OrderNum = entity.OrderNum;
|
||||
log.UId = entity.UId;
|
||||
log.Actions = "用户使用微信支付;支付订单号:" + model.PayOrderNum;
|
||||
log.Insert();
|
||||
|
||||
Core.Config cfg = Core.Config.GetSystemConfig();
|
||||
string appId = cfg.WXAppId;// ConfigurationManager.AppSettings["WeixinAppId"];
|
||||
string appSecrect = cfg.WXAppSecret;// ConfigurationManager.AppSettings["WeixinAppSecrect"];
|
||||
string wxmchId = cfg.MCHId;// ConfigurationManager.AppSettings["WeixinMCHId"];
|
||||
string wxmchKey = cfg.MCHKey;// ConfigurationManager.AppSettings["WeixinMCHKey"];
|
||||
|
||||
|
||||
|
||||
TenPayV3Info TenPayV3Info = new TenPayV3Info(appId, appSecrect, wxmchId, wxmchKey, Utils.GetServerUrl() + "/wxpayment/notify");
|
||||
TenPayV3Info.TenPayV3Notify = Utils.GetServerUrl() + "/wxpayment/notify";
|
||||
XTrace.WriteLine("微信支付异步通知地址:" + TenPayV3Info.TenPayV3Notify);
|
||||
//创建支付应答对象
|
||||
RequestHandler packageReqHandler = new RequestHandler(null);
|
||||
var sp_billno = DateTime.Now.ToString("HHmmss") + TenPayV3Util.BuildRandomStr(26);//最多32位
|
||||
var nonceStr = TenPayV3Util.GetNoncestr();
|
||||
string rtimeStamp = Utils.GetTimeStamp();
|
||||
|
||||
//创建请求统一订单接口参数
|
||||
var xmlDataInfo = new TenPayV3UnifiedorderRequestData(TenPayV3Info.AppId, TenPayV3Info.MchId, entity.Title, model.PayOrderNum, (int)(entity.TotalPay * 100), Utils.GetIP(), TenPayV3Info.TenPayV3Notify, TenPayV3Type.JSAPI, my.WeixinAppOpenId, TenPayV3Info.Key, nonceStr);
|
||||
|
||||
//返回给微信的请求
|
||||
RequestHandler res = new RequestHandler(null);
|
||||
try
|
||||
{
|
||||
//调用统一订单接口
|
||||
var result = TenPayV3.Unifiedorder(xmlDataInfo);
|
||||
XTrace.WriteLine("微信支付统一下单返回:" + JsonConvert.SerializeObject(result));
|
||||
|
||||
if (result.return_code == "FAIL")
|
||||
{
|
||||
reJson.code = 40005;
|
||||
reJson.message = result.return_msg;
|
||||
return reJson;
|
||||
}
|
||||
string nativeReqSign = res.CreateMd5Sign("key", TenPayV3Info.Key);
|
||||
//https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_7&index=3
|
||||
//paySign = MD5(appId=wxd678efh567hg6787&nonceStr=5K8264ILTKCH16CQ2502SI8ZNMTM67VS&package=prepay_id=wx2017033010242291fcfe0db70013231072&signType=MD5&timeStamp=1490840662&key=qazwsxedcrfvtgbyhnujmikolp111111)
|
||||
string paySign = Utils.MD5($"appId={TenPayV3Info.AppId}&nonceStr={nonceStr}&package=prepay_id={result.prepay_id}&signType=MD5&timeStamp={rtimeStamp}&key={TenPayV3Info.Key}").ToUpper();
|
||||
|
||||
string package = $"prepay_id={result.prepay_id}";
|
||||
|
||||
dynamic detail = new { timeStamp = rtimeStamp, nonceStr = nonceStr, package = package, signType = "MD5", paySign = paySign };
|
||||
|
||||
reJson.code = 0;
|
||||
reJson.message = "下单成功!";
|
||||
reJson.detail = detail;
|
||||
return reJson;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
res.SetParameter("return_code", "FAIL");
|
||||
res.SetParameter("return_msg", "统一下单失败");
|
||||
XTrace.WriteLine($"统一下单失败:{ex.Message}");
|
||||
|
||||
reJson.code = 40005;
|
||||
reJson.message = "统一下单失败,请联系管理员!";
|
||||
return reJson;
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user