忽略dll文件git

This commit is contained in:
“wanyongkang”
2023-07-29 10:19:42 +08:00
parent 7f97317bcc
commit b562aba2b1
3868 changed files with 63608 additions and 385427 deletions

View File

@@ -1,195 +1,195 @@
using Hncore.Infrastructure.Common;
using Hncore.Infrastructure.Extension;
using Hncore.Infrastructure.Serializer;
using Hncore.Infrastructure.WebApi;
using Hncore.Pass.PaymentCenter.Domain;
using Hncore.Pass.PaymentCenter.Pay.WxPay;
using Hncore.Pass.PaymentCenter.Request.WechatJsPay;
using Hncore.Pass.PaymentCenter.Service;
using Hncore.Pass.PaymentCenter.WxPay.WechatJsPay;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System.Threading.Tasks;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Alipay.AopSdk.Core.Domain;
using Alipay.AopSdk.Core.Request;
using Alipay.AopSdk.Core;
using System.Collections.Generic;
using Alipay.AopSdk.Core.Util;
using System;
using Microsoft.AspNetCore.Http;
namespace Hncore.Pass.PaymentCenter.Controllers
{
/// <summary>
/// 微信小程序、公众号支付
/// </summary>
[ApiVersion("1.0")]
[Route("api/paymentcenter/v{version:apiVersion}/AliPay/[action]")]
public class AliPayController : PaymentControllerBase
{
private PaymentRecordService m_PaymentRecordService;
private WxPayClient m_WxPayClient;
DefaultAopClient _aopClient;
public AliPayController(PaymentRecordService paymentRecordService
, WxPayClient _WxPayClient, TenantService _TenantService, IConfiguration _Configuration):base(_TenantService, _Configuration)
{
m_PaymentRecordService = paymentRecordService;
m_WxPayClient = _WxPayClient;
}
[HttpPost, InternalApiAuth]
public async Task<ApiResult> CreateOrder([FromBody] CreateOrderRequest request)
{
LogHelper.Trace($"微信支付,收到支付请求", request.ToJson(true));
request.AppId = "wx18683d61ce7da361";
//增加支付记录
var entity = request.MapTo<PaymentRecord>();
entity.Openid = request.UserOpenId;
PaymentRecord paymentRecord = await m_PaymentRecordService.CreatePaymentRecord(entity);
paymentRecord.PaymentChannel = PaymentChannel.WxPay;
//todo
var mchInfo = await GetMchInfoAsync(paymentRecord.TenantId
, paymentRecord.StoreId
, paymentRecord.PaymentChannel);
//发起支付
var APP_ID = "";
var APP_PRIVATE_KEY = "";
var CHARSET = "";
var ALIPAY_PUBLIC_KEY = "";
// 组装业务参数model
AlipayTradePagePayModel model = new AlipayTradePagePayModel
{
Body = request.Body,
Subject = request.Body,
TotalAmount =request.TotalFee.ToString(),
OutTradeNo = request.OrderId,
ProductCode = "FAST_INSTANT_TRADE_PAY"//QUICK_WAP_PAY
};
AlipayTradePagePayRequest aliRequest = new AlipayTradePagePayRequest();
// 设置同步回调地址
aliRequest.SetReturnUrl($"http://{Request.Host}/Pay/Callback");
// 设置异步通知接收地址
aliRequest.SetNotifyUrl("");
// 将业务model载入到request
aliRequest.SetBizModel(model);
var _aopClient = new DefaultAopClient("https://openapi.alipay.com/gateway.do", APP_ID, APP_PRIVATE_KEY);
var response = _aopClient.PageExecute(aliRequest);
return Success();
}
using Hncore.Infrastructure.Common;
using Hncore.Infrastructure.Extension;
using Hncore.Infrastructure.Serializer;
using Hncore.Infrastructure.WebApi;
using Hncore.Pass.PaymentCenter.Domain;
using Hncore.Pass.PaymentCenter.Pay.WxPay;
using Hncore.Pass.PaymentCenter.Request.WechatJsPay;
using Hncore.Pass.PaymentCenter.Service;
using Hncore.Pass.PaymentCenter.WxPay.WechatJsPay;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System.Threading.Tasks;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Http;
using Aliyun.Acs.Core.Profile;
using Alipay.AopSdk.Core.Domain;
using Alipay.AopSdk.Core.Request;
using Alipay.AopSdk.Core;
using System.Collections.Generic;
using Alipay.AopSdk.Core.Util;
using System;
using Microsoft.AspNetCore.Http;
namespace Hncore.Pass.PaymentCenter.Controllers
{
/// <summary>
/// 微信小程序、公众号支付
/// </summary>
[ApiVersion("1.0")]
[Route("api/paymentcenter/v{version:apiVersion}/AliPay/[action]")]
public class AliPayController : PaymentControllerBase
{
private PaymentRecordService m_PaymentRecordService;
private WxPayClient m_WxPayClient;
DefaultAopClient _aopClient;
public AliPayController(PaymentRecordService paymentRecordService
, WxPayClient _WxPayClient, TenantService _TenantService, IConfiguration _Configuration):base(_TenantService, _Configuration)
{
m_PaymentRecordService = paymentRecordService;
m_WxPayClient = _WxPayClient;
}
[HttpPost, InternalApiAuth]
public async Task<ApiResult> CreateOrder([FromBody] CreateOrderRequest request)
{
LogHelper.Trace($"微信支付,收到支付请求", request.ToJson(true));
request.AppId = "wx18683d61ce7da361";
//增加支付记录
var entity = request.MapTo<PaymentRecord>();
entity.Openid = request.UserOpenId;
PaymentRecord paymentRecord = await m_PaymentRecordService.CreatePaymentRecord(entity);
paymentRecord.PaymentChannel = PaymentChannel.WxPay;
//todo
var mchInfo = await GetMchInfoAsync(paymentRecord.TenantId
, paymentRecord.StoreId
, paymentRecord.PaymentChannel);
//发起支付
var APP_ID = "";
var APP_PRIVATE_KEY = "";
var CHARSET = "";
var ALIPAY_PUBLIC_KEY = "";
// 组装业务参数model
AlipayTradePagePayModel model = new AlipayTradePagePayModel
{
Body = request.Body,
Subject = request.Body,
TotalAmount =request.TotalFee.ToString(),
OutTradeNo = request.OrderId,
ProductCode = "FAST_INSTANT_TRADE_PAY"//QUICK_WAP_PAY
};
AlipayTradePagePayRequest aliRequest = new AlipayTradePagePayRequest();
// 设置同步回调地址
aliRequest.SetReturnUrl($"http://{Request.Host}/Pay/Callback");
// 设置异步通知接收地址
aliRequest.SetNotifyUrl("");
// 将业务model载入到request
aliRequest.SetBizModel(model);
var _aopClient = new DefaultAopClient("https://openapi.alipay.com/gateway.do", APP_ID, APP_PRIVATE_KEY);
var response = _aopClient.PageExecute(aliRequest);
return Success();
}
#region
/// <summary>
/// 支付异步回调通知 需配置域名 因为是支付宝主动post请求这个action 所以要通过域名访问或者公网ip
/// </summary>
public async void Notify()
{
public async void Notify()
{
/* 实际验证过程建议商户添加以下校验。
1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号
2、判断total_amount是否确实为该订单的实际金额即商户订单创建时的金额
3、校验通知中的seller_id或者seller_email) 是否为out_trade_no这笔单据的对应的操作方有的时候一个商户可能有多个seller_id/seller_email
4、验证app_id是否为该商户本身。
*/
Dictionary<string, string> sArray = GetRequestPost();
if (sArray.Count != 0)
{
// bool flag = AlipaySignature.RSACheckV1(sArray, Options.AlipayPublicKey, Options.CharSet, Options.SignType, false);
bool flag = AlipaySignature.RSACheckV1(sArray, "", "", "", false);
if (flag)
{
//交易状态
//判断该笔订单是否在商户网站中已经做过处理
//如果没有做过处理根据订单号out_trade_no在商户网站的订单系统中查到该笔订单的详细并执行商户的业务程序
//请务必判断请求时的total_amount与通知时获取的total_fee为一致的
//如果有做过处理,不执行商户的业务程序
//注意:
//退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知
Console.WriteLine(Request.Form["trade_status"]);
await Response.WriteAsync("success");
}
else
{
await Response.WriteAsync("fail");
}
}
}
*/
Dictionary<string, string> sArray = GetRequestPost();
if (sArray.Count != 0)
{
// bool flag = AlipaySignature.RSACheckV1(sArray, Options.AlipayPublicKey, Options.CharSet, Options.SignType, false);
bool flag = AlipaySignature.RSACheckV1(sArray, "", "", "", false);
if (flag)
{
//交易状态
//判断该笔订单是否在商户网站中已经做过处理
//如果没有做过处理根据订单号out_trade_no在商户网站的订单系统中查到该笔订单的详细并执行商户的业务程序
//请务必判断请求时的total_amount与通知时获取的total_fee为一致的
//如果有做过处理,不执行商户的业务程序
//注意:
//退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知
Console.WriteLine(Request.Form["trade_status"]);
await Response.WriteAsync("success");
}
else
{
await Response.WriteAsync("fail");
}
}
}
#endregion
#region
/// <summary>
/// 支付同步回调
/// </summary>
[HttpGet]
public IActionResult Callback()
{
[HttpGet]
public IActionResult Callback()
{
/* 实际验证过程建议商户添加以下校验。
1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号
2、判断total_amount是否确实为该订单的实际金额即商户订单创建时的金额
3、校验通知中的seller_id或者seller_email) 是否为out_trade_no这笔单据的对应的操作方有的时候一个商户可能有多个seller_id/seller_email
4、验证app_id是否为该商户本身。
*/
Dictionary<string, string> sArray = GetRequestGet();
if (sArray.Count != 0)
{
bool flag = AlipaySignature.RSACheckV1(sArray, "", "", "", false);
if (flag)
{
Console.WriteLine($"同步验证通过,订单号:{sArray["out_trade_no"]}");
// ViewData["PayResult"] = "同步验证通过";
}
else
{
Console.WriteLine($"同步验证失败,订单号:{sArray["out_trade_no"]}");
// ViewData["PayResult"] = "同步验证失败";
}
}
return Content("");
}
#endregion
private Dictionary<string, string> GetRequestGet()
{
Dictionary<string, string> sArray = new Dictionary<string, string>();
ICollection<string> requestItem = Request.Query.Keys;
foreach (var item in requestItem)
{
sArray.Add(item, Request.Query[item]);
}
return sArray;
}
private Dictionary<string, string> GetRequestPost()
{
Dictionary<string, string> sArray = new Dictionary<string, string>();
ICollection<string> requestItem = Request.Form.Keys;
foreach (var item in requestItem)
{
sArray.Add(item, Request.Form[item]);
}
return sArray;
*/
Dictionary<string, string> sArray = GetRequestGet();
if (sArray.Count != 0)
{
bool flag = AlipaySignature.RSACheckV1(sArray, "", "", "", false);
if (flag)
{
Console.WriteLine($"同步验证通过,订单号:{sArray["out_trade_no"]}");
// ViewData["PayResult"] = "同步验证通过";
}
else
{
Console.WriteLine($"同步验证失败,订单号:{sArray["out_trade_no"]}");
// ViewData["PayResult"] = "同步验证失败";
}
}
return Content("");
}
}
#endregion
private Dictionary<string, string> GetRequestGet()
{
Dictionary<string, string> sArray = new Dictionary<string, string>();
ICollection<string> requestItem = Request.Query.Keys;
foreach (var item in requestItem)
{
sArray.Add(item, Request.Query[item]);
}
return sArray;
}
private Dictionary<string, string> GetRequestPost()
{
Dictionary<string, string> sArray = new Dictionary<string, string>();
ICollection<string> requestItem = Request.Form.Keys;
foreach (var item in requestItem)
{
sArray.Add(item, Request.Form[item]);
}
return sArray;
}
}
}

View File

@@ -1,272 +1,272 @@
using Hncore.Infrastructure.Common;
using Hncore.Infrastructure.Extension;
using Hncore.Infrastructure.Serializer;
using Hncore.Infrastructure.WebApi;
using Hncore.Pass.PaymentCenter.Domain;
using Hncore.Pass.PaymentCenter.Pay.WxPay;
using Hncore.Pass.PaymentCenter.Response;
using Hncore.Pass.PaymentCenter.Response.PaymentRecord;
using Hncore.Pass.PaymentCenter.Service;
using Hncore.Pass.PaymentCenter.WeiFuTong;
using Hncore.Pass.PaymentCenter.WeiFuTong.Notify;
using Hncore.Pass.PaymentCenter.WxPay.WechatJsPay;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace Hncore.Pass.PaymentCenter.Controllers
{
[ApiVersion("1.0")]
[Route("api/paymentcenter/v{version:apiVersion}/Payment/[action]")]
public class PaymentController : PaymentControllerBase
{
private PaymentRecordService m_PaymentRecordService;
private WeiFuTongService m_WeiFuTongService;
private InternalNotifySerivce m_InternalNotifySerivce;
private WxPayClient m_WxPayClient { get; set; }
public PaymentController(
PaymentRecordService _PaymentRecordService
, InternalNotifySerivce internalNotifySerivce
, TenantService _TenantService
, WxPayClient _WxPayClient
, WeiFuTongService weiFuTongService
, IConfiguration _Configuration) : base(_TenantService, _Configuration)
{
// m_WeiFuTongService = weiFuTongService;
m_PaymentRecordService = _PaymentRecordService;
m_InternalNotifySerivce = internalNotifySerivce;
m_WxPayClient = _WxPayClient;
}
private async Task<QueryOrderResponse> QueryOrderByPaymentRecordAsync(PaymentRecord paymentRecord)
{
QueryOrderResponse result = null;
//威富通的订单
if (paymentRecord.PaymentChannel == PaymentChannel.WeiFuTong
|| paymentRecord.PaymentChannel == PaymentChannel.QuanFuTong
&& paymentRecord.FromPos==0)
{
var mchInfo = await GetMchInfoAsync(paymentRecord.TenantId
, paymentRecord.StoreId
, paymentRecord.PaymentChannel);
result = await m_WeiFuTongService.QueryOrderAsync(paymentRecord, mchInfo);
await UofCommitAsync();
}
return result;
}
/// <summary>
/// 通用订单查询
/// </summary>
/// <param name="orderNo"></param>
/// <returns></returns>
[HttpGet]
public async Task<ApiResult> QueryOrder(string orderNo = "", int? recordId = 0)
{
PaymentRecord paymentRecord = null;
if (recordId == null || recordId == 0)
{
paymentRecord = await m_PaymentRecordService.Query(false)
.OrderByDescending(t => t.Id)
.FirstOrDefaultAsync(t => t.OrderId == orderNo);
}
else if (recordId > 0)
{
paymentRecord = await m_PaymentRecordService.GetById(recordId.ToInt());
}
CheckHelper.NotNull(paymentRecord, "支付记录不存在");
var result = await QueryOrderByPaymentRecordAsync(paymentRecord);
if (result == null)
{
return Error("未知的支付渠道");
}
return Success(result);
}
/// <summary>
/// 根据orderid查询一条支付记录
/// </summary>
/// <param name="orderId"></param>
/// <returns></returns>
[HttpGet]
public async Task<ApiResult> GetDetail([FromQuery] string orderId)
{
return Success(await QueryPaymentRecordByOrderIdResponse.Query(
m_PaymentRecordService.Query(false)
, orderId)
);
}
/// <summary>
/// 威富通、全付通 接收通知
/// </summary>
/// <returns></returns>
[HttpPost]
public async Task<string> HWCNotify()
{
try
{
string xml = "";
if (Request.Body.CanSeek)
{
Request.Body.Position = 0;
}
using (System.IO.StreamReader reader = new System.IO.StreamReader(Request.Body))
{
xml = reader.ReadToEnd();
}
LogHelper.Trace("收到威富通支付通知,原始数据:", $"{xml}");
var notifyEntity = XML.XmlDeserialize<NotifyResponse>(xml);
// 给支付平台的订单号为支付记录id
int paymentRecordId = notifyEntity.OutTradeNo.ToInt();
var paymentRecord = await m_PaymentRecordService.GetById(paymentRecordId);
var mchInfo = await GetMchInfoAsync(paymentRecord.TenantId, paymentRecord.StoreId,
paymentRecord.PaymentChannel);
Util.CheckSignFromXml(xml, mchInfo);
if (notifyEntity.PayResult == 0) //支付成功
{
paymentRecord.SetPaySuccessed(notifyEntity.TransactionId, notifyEntity.GetPaySuccessTime());
paymentRecord.SetPaymentTypeFromTradeType(notifyEntity.TradeType);
}
else
{
paymentRecord.SetPayFailed();
}
await m_PaymentRecordService.Update(paymentRecord);
var status = notifyEntity.PayResult == 0 ? "成功" : "失败";
LogHelper.Info("威富通回调信息:",
$"支付记录Id{paymentRecord.Id}\n订单号{paymentRecord.OrderId}\n交易号{paymentRecord.TransactionId}\n支付状态{status}\n发起时间{paymentRecord.RequestTime.Format("yyyy-MM-dd HH:mm:ss")}\n付款成功时间{notifyEntity.GetPaySuccessTime().Format("yyyy-MM-dd HH:mm:ss")}\n收到通知时间{DateTime.Now.Format("yyyy-MM-dd HH:mm:ss")}\n总用时{(DateTime.Now - paymentRecord.RequestTime).TotalSeconds}秒");
if (notifyEntity.PayResult == 0)
{
//发起内部通知
await m_InternalNotifySerivce.Notify(paymentRecord);
}
}
catch (Exception e)
{
LogHelper.Error("威富通通知处理失败", e);
return "fail";
}
return "success";
}
/// <summary>
/// 微信支付回调 接收通知
/// </summary>
/// <returns></returns>
[HttpPost, AllowAnonymous]
public async Task<string> Notify()
{
try
{
string xml = "";
LogHelper.Trace("微信支付回调开始","Notify");
if (Request.Body.CanSeek)
{
Request.Body.Position = 0;
}
using (System.IO.StreamReader reader = new System.IO.StreamReader(Request.Body))
{
xml = reader.ReadToEnd();
}
LogHelper.Trace("微信支付回调,原始数据:", $"{xml}");
WxPayChecker payData = new WxPayChecker();
payData.FromXmlNoCheckSign(xml);
if (!payData.IsSet("out_trade_no"))
{
return FailXml();
}
// 给支付平台的订单号为支付记录id
string orderId = payData["out_trade_no"];
string TransactionId = payData["transaction_id"];
var paymentRecord = await m_PaymentRecordService.GetOneByOrderId(orderId);
if (paymentRecord.TransactionId.NotHas())
{
paymentRecord.TransactionId = TransactionId;
await m_PaymentRecordService.Update(paymentRecord);
}
var mchInfo = await GetMchInfoAsync(paymentRecord.TenantId, paymentRecord.StoreId,
paymentRecord.PaymentChannel);
payData.MchKey = mchInfo.Key;
var queryRet = await m_WxPayClient.OrderQuery(new WxJsPayOrderQueryRequest()
{
AppId = paymentRecord.Appid,
MchId = mchInfo.MchId,
NonceStr = payData.GenerateNonceStr(),
TransactionId = TransactionId
}, mchInfo);
if (!queryRet) return FailXml();
if (!payData.CheckSign()) return FailXml();
paymentRecord.PaymentStatus = PaymentStatus.OkPay;
paymentRecord.UpdateTime = DateTime.Now;
paymentRecord.PaymentCompletionTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
await m_PaymentRecordService.Update(paymentRecord);
//发起内部通知
await m_InternalNotifySerivce.Notify(paymentRecord);
}
catch (Exception e)
{
LogHelper.Error("微信支付通知处理失败", e);
return FailXml();
}
return SuccessXml();
}
public string FailXml()
{
return "<xml><return_code>FAIL</return_code> </xml>";
}
private string SuccessXml()
{
return "<xml><return_code>SUCCESS</return_code> </xml>";
}
}
using Hncore.Infrastructure.Common;
using Hncore.Infrastructure.Extension;
using Hncore.Infrastructure.Serializer;
using Hncore.Infrastructure.WebApi;
using Hncore.Pass.PaymentCenter.Domain;
using Hncore.Pass.PaymentCenter.Pay.WxPay;
using Hncore.Pass.PaymentCenter.Response;
using Hncore.Pass.PaymentCenter.Response.PaymentRecord;
using Hncore.Pass.PaymentCenter.Service;
using Hncore.Pass.PaymentCenter.WeiFuTong;
using Hncore.Pass.PaymentCenter.WeiFuTong.Notify;
using Hncore.Pass.PaymentCenter.WxPay.WechatJsPay;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System;
using System.Linq;
using System.Threading.Tasks;
namespace Hncore.Pass.PaymentCenter.Controllers
{
[ApiVersion("1.0")]
[Route("api/paymentcenter/v{version:apiVersion}/Payment/[action]")]
public class PaymentController : PaymentControllerBase
{
private PaymentRecordService m_PaymentRecordService;
private WeiFuTongService m_WeiFuTongService;
private InternalNotifySerivce m_InternalNotifySerivce;
private WxPayClient m_WxPayClient { get; set; }
public PaymentController(
PaymentRecordService _PaymentRecordService
, InternalNotifySerivce internalNotifySerivce
, TenantService _TenantService
, WxPayClient _WxPayClient
, WeiFuTongService weiFuTongService
, IConfiguration _Configuration) : base(_TenantService, _Configuration)
{
// m_WeiFuTongService = weiFuTongService;
m_PaymentRecordService = _PaymentRecordService;
m_InternalNotifySerivce = internalNotifySerivce;
m_WxPayClient = _WxPayClient;
}
private async Task<QueryOrderResponse> QueryOrderByPaymentRecordAsync(PaymentRecord paymentRecord)
{
QueryOrderResponse result = null;
//威富通的订单
if (paymentRecord.PaymentChannel == PaymentChannel.WeiFuTong
|| paymentRecord.PaymentChannel == PaymentChannel.QuanFuTong
&& paymentRecord.FromPos==0)
{
var mchInfo = await GetMchInfoAsync(paymentRecord.TenantId
, paymentRecord.StoreId
, paymentRecord.PaymentChannel);
result = await m_WeiFuTongService.QueryOrderAsync(paymentRecord, mchInfo);
await UofCommitAsync();
}
return result;
}
/// <summary>
/// 通用订单查询
/// </summary>
/// <param name="orderNo"></param>
/// <returns></returns>
[HttpGet]
public async Task<ApiResult> QueryOrder(string orderNo = "", int? recordId = 0)
{
PaymentRecord paymentRecord = null;
if (recordId == null || recordId == 0)
{
paymentRecord = await m_PaymentRecordService.Query(false)
.OrderByDescending(t => t.Id)
.FirstOrDefaultAsync(t => t.OrderId == orderNo);
}
else if (recordId > 0)
{
paymentRecord = await m_PaymentRecordService.GetById(recordId.ToInt());
}
CheckHelper.NotNull(paymentRecord, "支付记录不存在");
var result = await QueryOrderByPaymentRecordAsync(paymentRecord);
if (result == null)
{
return Error("未知的支付渠道");
}
return Success(result);
}
/// <summary>
/// 根据orderid查询一条支付记录
/// </summary>
/// <param name="orderId"></param>
/// <returns></returns>
[HttpGet]
public async Task<ApiResult> GetDetail([FromQuery] string orderId)
{
return Success(await QueryPaymentRecordByOrderIdResponse.Query(
m_PaymentRecordService.Query(false)
, orderId)
);
}
/// <summary>
/// 威富通、全付通 接收通知
/// </summary>
/// <returns></returns>
[HttpPost]
public async Task<string> HWCNotify()
{
try
{
string xml = "";
if (Request.Body.CanSeek)
{
Request.Body.Position = 0;
}
using (System.IO.StreamReader reader = new System.IO.StreamReader(Request.Body))
{
xml = reader.ReadToEnd();
}
LogHelper.Trace("收到威富通支付通知,原始数据:", $"{xml}");
var notifyEntity = XML.XmlDeserialize<NotifyResponse>(xml);
// 给支付平台的订单号为支付记录id
int paymentRecordId = notifyEntity.OutTradeNo.ToInt();
var paymentRecord = await m_PaymentRecordService.GetById(paymentRecordId);
var mchInfo = await GetMchInfoAsync(paymentRecord.TenantId, paymentRecord.StoreId,
paymentRecord.PaymentChannel);
Util.CheckSignFromXml(xml, mchInfo);
if (notifyEntity.PayResult == 0) //支付成功
{
paymentRecord.SetPaySuccessed(notifyEntity.TransactionId, notifyEntity.GetPaySuccessTime());
paymentRecord.SetPaymentTypeFromTradeType(notifyEntity.TradeType);
}
else
{
paymentRecord.SetPayFailed();
}
await m_PaymentRecordService.Update(paymentRecord);
var status = notifyEntity.PayResult == 0 ? "成功" : "失败";
LogHelper.Info("威富通回调信息:",
$"支付记录Id{paymentRecord.Id}\n订单号{paymentRecord.OrderId}\n交易号{paymentRecord.TransactionId}\n支付状态{status}\n发起时间{paymentRecord.RequestTime.Format("yyyy-MM-dd HH:mm:ss")}\n付款成功时间{notifyEntity.GetPaySuccessTime().Format("yyyy-MM-dd HH:mm:ss")}\n收到通知时间{DateTime.Now.Format("yyyy-MM-dd HH:mm:ss")}\n总用时{(DateTime.Now - paymentRecord.RequestTime).TotalSeconds}秒");
if (notifyEntity.PayResult == 0)
{
//发起内部通知
await m_InternalNotifySerivce.Notify(paymentRecord);
}
}
catch (Exception e)
{
LogHelper.Error("威富通通知处理失败", e);
return "fail";
}
return "success";
}
/// <summary>
/// 微信支付回调 接收通知
/// </summary>
/// <returns></returns>
[HttpPost, AllowAnonymous]
public async Task<string> Notify()
{
try
{
string xml = "";
LogHelper.Trace("微信支付回调开始","Notify");
if (Request.Body.CanSeek)
{
Request.Body.Position = 0;
}
using (System.IO.StreamReader reader = new System.IO.StreamReader(Request.Body))
{
xml = reader.ReadToEnd();
}
LogHelper.Trace("微信支付回调,原始数据:", $"{xml}");
WxPayChecker payData = new WxPayChecker();
payData.FromXmlNoCheckSign(xml);
if (!payData.IsSet("out_trade_no"))
{
return FailXml();
}
// 给支付平台的订单号为支付记录id
string orderId = payData["out_trade_no"];
string TransactionId = payData["transaction_id"];
var paymentRecord = await m_PaymentRecordService.GetOneByOrderId(orderId);
if (paymentRecord.TransactionId.NotHas())
{
paymentRecord.TransactionId = TransactionId;
await m_PaymentRecordService.Update(paymentRecord);
}
var mchInfo = await GetMchInfoAsync(paymentRecord.TenantId, paymentRecord.StoreId,
paymentRecord.PaymentChannel);
payData.MchKey = mchInfo.Key;
var queryRet = await m_WxPayClient.OrderQuery(new WxJsPayOrderQueryRequest()
{
AppId = paymentRecord.Appid,
MchId = mchInfo.MchId,
NonceStr = payData.GenerateNonceStr(),
TransactionId = TransactionId
}, mchInfo);
if (!queryRet) return FailXml();
if (!payData.CheckSign()) return FailXml();
paymentRecord.PaymentStatus = PaymentStatus.OkPay;
paymentRecord.UpdateTime = DateTime.Now;
paymentRecord.PaymentCompletionTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
await m_PaymentRecordService.Update(paymentRecord);
//发起内部通知
await m_InternalNotifySerivce.Notify(paymentRecord);
}
catch (Exception e)
{
LogHelper.Error("微信支付通知处理失败", e);
return FailXml();
}
return SuccessXml();
}
public string FailXml()
{
return "<xml><return_code>FAIL</return_code> </xml>";
}
private string SuccessXml()
{
return "<xml><return_code>SUCCESS</return_code> </xml>";
}
}
}

View File

@@ -1,87 +1,87 @@
using Hncore.Infrastructure.Data;
using Hncore.Infrastructure.DDD;
using Hncore.Infrastructure.Extension;
using Hncore.Infrastructure.WebApi;
using Hncore.Pass.PaymentCenter.Domain;
using Hncore.Pass.PaymentCenter.Model;
using Hncore.Pass.PaymentCenter.Service;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.Linq;
using System.Threading.Tasks;
namespace Hncore.Pass.PaymentCenter.Controllers
{
public class PaymentControllerBase : HncoreControllerBase
{
TenantService m_TenantService;
IConfiguration m_Configuration;
public PaymentControllerBase(TenantService _TenantService, IConfiguration _Configuration)
{
m_TenantService = _TenantService;
m_Configuration = _Configuration;
}
[NonAction]
protected async Task<MchInfo> GetMchInfoAsync(int tenantId, int storeId,
PaymentChannel paymentChannel)
{
if (paymentChannel == PaymentChannel.WxPay)
{
MchInfo mchInfo;
mchInfo = await RedisHelper.GetAsync<MchInfo>(GetWxPayCacheKey(tenantId, storeId));
if (mchInfo == null)
{
var config = await m_TenantService.GetById(tenantId);
if (config == null
|| !config.MchId.Has()
|| !config.MchKey.Has())
{
BusinessException.Throw("支付信息未设置");
}
mchInfo = new MchInfo()
{
GroupNo = config?.MchId,
Key = config?.MchKey,
MchId = config?.MchId
};
await RedisHelper.SetAsync(GetWxPayCacheKey(tenantId, storeId), mchInfo);
return mchInfo;
}
return mchInfo;
}
BusinessException.Throw("未知的支付渠道");
return null;
}
[NonAction]
protected string GetWxPayCacheKey(int ownerId, int projectCode)
{
return $"PaymentCenter:WxPayMchInfo:{ownerId}:{projectCode}";
}
/// <summary>
/// 微信支付 接收通知url
/// </summary>
/// <returns></returns>
protected string GetNotifyUrl()
{
return Request.HttpContext.RequestServices.GetService<IConfiguration>()["NotifyUrl"];
}
}
using Hncore.Infrastructure.Data;
using Hncore.Infrastructure.DDD;
using Hncore.Infrastructure.Extension;
using Hncore.Infrastructure.WebApi;
using Hncore.Pass.PaymentCenter.Domain;
using Hncore.Pass.PaymentCenter.Model;
using Hncore.Pass.PaymentCenter.Service;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System.Linq;
using System.Threading.Tasks;
namespace Hncore.Pass.PaymentCenter.Controllers
{
public class PaymentControllerBase : HncoreControllerBase
{
TenantService m_TenantService;
IConfiguration m_Configuration;
public PaymentControllerBase(TenantService _TenantService, IConfiguration _Configuration)
{
m_TenantService = _TenantService;
m_Configuration = _Configuration;
}
[NonAction]
protected async Task<MchInfo> GetMchInfoAsync(int tenantId, int storeId,
PaymentChannel paymentChannel)
{
if (paymentChannel == PaymentChannel.WxPay)
{
MchInfo mchInfo;
mchInfo = await RedisHelper.GetAsync<MchInfo>(GetWxPayCacheKey(tenantId, storeId));
if (mchInfo == null)
{
var config = await m_TenantService.GetById(tenantId);
if (config == null
|| !config.MchId.Has()
|| !config.MchKey.Has())
{
BusinessException.Throw("支付信息未设置");
}
mchInfo = new MchInfo()
{
GroupNo = config?.MchId,
Key = config?.MchKey,
MchId = config?.MchId
};
await RedisHelper.SetAsync(GetWxPayCacheKey(tenantId, storeId), mchInfo);
return mchInfo;
}
return mchInfo;
}
BusinessException.Throw("未知的支付渠道");
return null;
}
[NonAction]
protected string GetWxPayCacheKey(int ownerId, int projectCode)
{
return $"PaymentCenter:WxPayMchInfo:{ownerId}:{projectCode}";
}
/// <summary>
/// 微信支付 接收通知url
/// </summary>
/// <returns></returns>
protected string GetNotifyUrl()
{
return Request.HttpContext.RequestServices.GetService<IConfiguration>()["NotifyUrl"];
}
}
}

View File

@@ -1,85 +1,85 @@
using Hncore.Infrastructure.Common;
using Hncore.Infrastructure.Extension;
using Hncore.Infrastructure.Serializer;
using Hncore.Infrastructure.WebApi;
using Hncore.Pass.PaymentCenter.Domain;
using Hncore.Pass.PaymentCenter.Pay.WxPay;
using Hncore.Pass.PaymentCenter.Request.WechatJsPay;
using Hncore.Pass.PaymentCenter.Service;
using Hncore.Pass.PaymentCenter.WxPay.WechatJsPay;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System.Threading.Tasks;
namespace Hncore.Pass.PaymentCenter.Controllers
{
/// <summary>
/// 微信小程序、公众号支付
/// </summary>
[ApiVersion("1.0")]
[Route("api/paymentcenter/v{version:apiVersion}/WechatJsPay/[action]")]
public class WechatJsPayController : PaymentControllerBase
{
private PaymentRecordService m_PaymentRecordService;
private WxPayClient m_WxPayClient;
public WechatJsPayController(PaymentRecordService paymentRecordService
, WxPayClient _WxPayClient, TenantService _TenantService, IConfiguration _Configuration):base(_TenantService, _Configuration)
{
m_PaymentRecordService = paymentRecordService;
m_WxPayClient = _WxPayClient;
}
[HttpPost, InternalApiAuth]
public async Task<ApiResult> CreateOrder([FromBody] CreateOrderRequest request)
{
LogHelper.Trace($"微信支付,收到支付请求", request.ToJson(true));
request.AppId = "wx18683d61ce7da361";
//增加支付记录
var entity = request.MapTo<PaymentRecord>();
entity.Openid = request.UserOpenId;
PaymentRecord paymentRecord = await m_PaymentRecordService.CreatePaymentRecord(entity);
paymentRecord.PaymentChannel = PaymentChannel.WxPay;
//todo
var mchInfo = await GetMchInfoAsync(paymentRecord.TenantId
, paymentRecord.StoreId
, paymentRecord.PaymentChannel);
//发起支付
//todo
//WxJsPayCreateOrderRequest payRequest = new WxJsPayCreateOrderRequest()
//{
// AppId = request.AppId,
// Body = request.Body,
// MchId = mchInfo.MchId,
// NotifyUrl = GetNotifyUrl(),
// OutTradeNo = request.OrderId,
// StoreId = request.StoreId,
// TenantId = request.TenantId,
// TotalFee = request.TotalFee,
// UserOpenId = request.UserOpenId
//};
//var createOrderRes = await m_WxPayClient.JsPayCreateOrderAsync(payRequest, mchInfo);
var payRequest = new WxScanPayCreateOrderRequest()
{
AppId = request.AppId,
Body = request.Body,
MchId = mchInfo.MchId,
NotifyUrl = GetNotifyUrl(),
OutTradeNo = request.OrderId,
StoreId = request.StoreId,
TenantId = request.TenantId,
TotalFee = request.TotalFee,
ProductId ="1"
};
var createOrderRes = await m_WxPayClient.ScanPayCreateOrderAsync(payRequest, mchInfo);
LogHelper.Trace("PayCenter.CreateOrder", createOrderRes);
return Success(createOrderRes);
}
}
using Hncore.Infrastructure.Common;
using Hncore.Infrastructure.Extension;
using Hncore.Infrastructure.Serializer;
using Hncore.Infrastructure.WebApi;
using Hncore.Pass.PaymentCenter.Domain;
using Hncore.Pass.PaymentCenter.Pay.WxPay;
using Hncore.Pass.PaymentCenter.Request.WechatJsPay;
using Hncore.Pass.PaymentCenter.Service;
using Hncore.Pass.PaymentCenter.WxPay.WechatJsPay;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System.Threading.Tasks;
namespace Hncore.Pass.PaymentCenter.Controllers
{
/// <summary>
/// 微信小程序、公众号支付
/// </summary>
[ApiVersion("1.0")]
[Route("api/paymentcenter/v{version:apiVersion}/WechatJsPay/[action]")]
public class WechatJsPayController : PaymentControllerBase
{
private PaymentRecordService m_PaymentRecordService;
private WxPayClient m_WxPayClient;
public WechatJsPayController(PaymentRecordService paymentRecordService
, WxPayClient _WxPayClient, TenantService _TenantService, IConfiguration _Configuration):base(_TenantService, _Configuration)
{
m_PaymentRecordService = paymentRecordService;
m_WxPayClient = _WxPayClient;
}
[HttpPost, InternalApiAuth]
public async Task<ApiResult> CreateOrder([FromBody] CreateOrderRequest request)
{
LogHelper.Trace($"微信支付,收到支付请求", request.ToJson(true));
request.AppId = "wx18683d61ce7da361";
//增加支付记录
var entity = request.MapTo<PaymentRecord>();
entity.Openid = request.UserOpenId;
PaymentRecord paymentRecord = await m_PaymentRecordService.CreatePaymentRecord(entity);
paymentRecord.PaymentChannel = PaymentChannel.WxPay;
//todo
var mchInfo = await GetMchInfoAsync(paymentRecord.TenantId
, paymentRecord.StoreId
, paymentRecord.PaymentChannel);
//发起支付
//todo
//WxJsPayCreateOrderRequest payRequest = new WxJsPayCreateOrderRequest()
//{
// AppId = request.AppId,
// Body = request.Body,
// MchId = mchInfo.MchId,
// NotifyUrl = GetNotifyUrl(),
// OutTradeNo = request.OrderId,
// StoreId = request.StoreId,
// TenantId = request.TenantId,
// TotalFee = request.TotalFee,
// UserOpenId = request.UserOpenId
//};
//var createOrderRes = await m_WxPayClient.JsPayCreateOrderAsync(payRequest, mchInfo);
var payRequest = new WxScanPayCreateOrderRequest()
{
AppId = request.AppId,
Body = request.Body,
MchId = mchInfo.MchId,
NotifyUrl = GetNotifyUrl(),
OutTradeNo = request.OrderId,
StoreId = request.StoreId,
TenantId = request.TenantId,
TotalFee = request.TotalFee,
ProductId ="1"
};
var createOrderRes = await m_WxPayClient.ScanPayCreateOrderAsync(payRequest, mchInfo);
LogHelper.Trace("PayCenter.CreateOrder", createOrderRes);
return Success(createOrderRes);
}
}
}