mirror of
https://gitee.com/ccnetcore/Yi
synced 2026-04-02 07:06:37 +08:00
添加微信支付模块
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Yi.Framework.WeChatPay.Exceptions;
|
||||
|
||||
namespace Yi.Framework.WeChatPay.Core
|
||||
{
|
||||
public abstract class AbstractNotify
|
||||
{
|
||||
protected HttpContext _httpContext;
|
||||
|
||||
public AbstractNotify(IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
this._httpContext = httpContextAccessor.HttpContext;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 从请求对象中获取信息
|
||||
/// </summary>
|
||||
/// <param name="request"></param>
|
||||
/// <param name="encoding"></param>
|
||||
/// <returns></returns>
|
||||
private async Task<string> GetRawBodyStringAsync(HttpRequest request, Encoding encoding = null)
|
||||
{
|
||||
if (encoding is null)
|
||||
{
|
||||
encoding = Encoding.UTF8;
|
||||
}
|
||||
var stream = new MemoryStream();
|
||||
await request.Body.CopyToAsync(stream);
|
||||
stream.Seek(0, 0);
|
||||
using (var reader = new StreamReader(stream, encoding))
|
||||
{
|
||||
var result = await reader.ReadToEndAsync();
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 接收从微信支付后台发送过来的数据并验证签名
|
||||
/// </summary>
|
||||
/// <returns>微信支付后台返回的数据</returns>
|
||||
public PayData GetNotifyData()
|
||||
{
|
||||
//接收从微信后台POST过来的数据
|
||||
string content = GetRawBodyStringAsync(_httpContext.Request).Result;
|
||||
Console.WriteLine(this.GetType().ToString(), "Receive data from WeChat : " + content);
|
||||
|
||||
//转换数据格式并验证签名
|
||||
PayData data = new PayData();
|
||||
try
|
||||
{
|
||||
data.FromXml(content);
|
||||
}
|
||||
catch (PayException ex)
|
||||
{
|
||||
throw new Exception("验签失败", ex);
|
||||
}
|
||||
|
||||
Console.WriteLine(this.GetType().ToString(), "Check sign success");
|
||||
return data;
|
||||
}
|
||||
|
||||
//派生类需要重写这个方法,进行不同的回调处理
|
||||
public abstract PayData ProcessNotify();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Logging;
|
||||
|
||||
namespace Yi.Framework.WeChatPay.Core
|
||||
{
|
||||
public class ResultNotify : AbstractNotify
|
||||
{
|
||||
private readonly PayApi _PayApi;
|
||||
private readonly ILogger<ResultNotify> _logger;
|
||||
public ResultNotify(PayApi PayApi,ILogger<ResultNotify> logger, IHttpContextAccessor httpContextAccessor) :base(httpContextAccessor)
|
||||
{
|
||||
this._PayApi = PayApi;
|
||||
this._logger = logger;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 处理支付回调
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public override PayData ProcessNotify()
|
||||
{
|
||||
PayData notifyData = GetNotifyData();
|
||||
//解析数据
|
||||
string totalFee = notifyData.GetValue("total_fee").ToString(); //订单金额
|
||||
string outTradeNo = notifyData.GetValue("out_trade_no").ToString(); //订单编号
|
||||
string transactionId = notifyData.GetValue("transaction_id").ToString(); //商户订单号
|
||||
string bankType = notifyData.GetValue("bank_type").ToString(); //银行类型
|
||||
|
||||
_logger.LogInformation($"======支付回调参数:{totalFee}=={outTradeNo}=={transactionId}=={bankType}");
|
||||
if (totalFee.Equals("") || outTradeNo.Equals("") || transactionId.Equals("") || bankType.Equals(""))
|
||||
{
|
||||
PayData res = new();
|
||||
res.SetValue("return_code", "FAIL");
|
||||
res.SetValue("return_msg", "支付回调返回数据不正确");
|
||||
_logger.LogInformation("支付错误结果 : " + res.ToXml());
|
||||
return res;
|
||||
}
|
||||
|
||||
//检查支付结果中transaction_id是否存在--流水号
|
||||
if (!notifyData.IsSet("transaction_id"))
|
||||
{
|
||||
//若transaction_id不存在,则立即返回结果给微信支付后台
|
||||
PayData res = new();
|
||||
res.SetValue("return_code", "FAIL");
|
||||
res.SetValue("return_msg", "支付结果中微信订单号不存在");
|
||||
_logger.LogInformation("支付错误结果 : " + res.ToXml());
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
//查询订单,判断订单真实性
|
||||
if (!QueryOrder(transactionId))
|
||||
{
|
||||
//若订单查询失败,则立即返回结果给微信支付后台
|
||||
PayData res = new();
|
||||
res.SetValue("return_code", "FAIL");
|
||||
res.SetValue("return_msg", "订单查询失败");
|
||||
_logger.LogInformation("订单查询失败 : " + res.ToXml());
|
||||
|
||||
return res;
|
||||
}
|
||||
//查询订单成功
|
||||
else
|
||||
{
|
||||
// 打印结果 (仅打印)
|
||||
PayData res = new PayData();
|
||||
res.SetValue("return_code", "SUCCESS");
|
||||
res.SetValue("return_msg", "OK");
|
||||
res.SetValue("transaction_id", transactionId);
|
||||
_logger.LogInformation("订单查成功: " + res.ToXml());
|
||||
|
||||
// 设置返回响应结果
|
||||
notifyData.SetValue("return_code", "SUCCESS");
|
||||
notifyData.SetValue("return_msg", "OK");
|
||||
|
||||
return notifyData;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 根据流水号查询订单信息
|
||||
/// </summary>
|
||||
/// <param name="transaction_id"></param>
|
||||
/// <returns></returns>
|
||||
private bool QueryOrder(string transaction_id)
|
||||
{
|
||||
PayData req = new PayData();
|
||||
req.SetValue("transaction_id", transaction_id);
|
||||
PayData res = this._PayApi.OrderQuery(req, _httpContext);
|
||||
if (res.GetValue("return_code").ToString() == "SUCCESS" &&
|
||||
res.GetValue("result_code").ToString() == "SUCCESS")
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user