Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/PaySharp.Core/Gateways/BaseGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,30 @@ protected BaseGateway(IMerchant merchant)
/// </summary>
protected internal virtual void WriteSuccessFlag()
{
HttpUtil.Write("success");
HttpUtil.Write(GetSuccessFlag());
}

/// <summary>
/// 当接收到支付网关通知并验证有误时按照支付网关要求格式输出表示失败接收到网关通知的字符串
/// </summary>
protected internal virtual void WriteFailureFlag()
{
HttpUtil.Write("failure");
HttpUtil.Write(GetFailureFlag());
} /// <summary>
/// 当接收到支付网关通知并验证无误时按照支付网关要求格式输出表示成功接收到网关通知的字符串
/// </summary>
protected internal virtual string GetSuccessFlag()
{
return "success";
}

/// <summary>
/// 当接收到支付网关通知并验证有误时按照支付网关要求格式输出表示失败接收到网关通知的字符串
/// </summary>
protected internal virtual string GetFailureFlag()
{
return "failure";
}
/// <summary>
/// 执行请求
/// </summary>
Expand Down
191 changes: 147 additions & 44 deletions src/PaySharp.Core/Notify/Notify.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,61 +70,79 @@ public Notify(IGateways gateways)

private void OnUnknownGateway(UnknownGatewayEventArgs e) => UnknownGateway?.Invoke(this, e);

/// <summary>
/// 接收并验证网关的支付通知
/// </summary>
public async Task ReceivedAsync()


private async Task<NotifyEventArgs> GetNotifyEvent(BaseGateway gateway)
{
var gateway = await NotifyProcess.GetGatewayAsync(_gateways);
if (gateway is NullGateway)
{
OnUnknownGateway(new UnknownGatewayEventArgs(gateway));
return;
return new UnknownGatewayEventArgs(gateway);
}

if (!await gateway.ValidateNotifyAsync())
{
return new UnKnownNotifyEventArgs(gateway) { Message = "签名验证失败" };
}

if (HttpUtil.RequestType == "GET")
{
return new PaySucceedEventArgs(gateway);
}

if (gateway.IsPaySuccess)
{
return new PaySucceedEventArgs(gateway);
}

if (gateway.IsRefundSuccess)
{
return new RefundSucceedEventArgs(gateway);
}

if (gateway.IsCancelSuccess)
{
return new CancelSucceedEventArgs(gateway);
}

return new UnKnownNotifyEventArgs(gateway);


}
private async Task<SendEventResult> SendNotifyEventAsync(BaseGateway gateway)
{
var success = false;
try
{
if (!await gateway.ValidateNotifyAsync())
var eventArgs = await GetNotifyEvent(gateway);
switch (eventArgs)
{
OnUnknownNotify(new UnKnownNotifyEventArgs(gateway)
case UnknownGatewayEventArgs unknownGatewayEventArgs:
OnUnknownGateway(unknownGatewayEventArgs);
break;
case UnKnownNotifyEventArgs unKnownNotifyEventArgs:
OnUnknownNotify(unKnownNotifyEventArgs);
break;
case PaySucceedEventArgs args:
{
Message = "签名验证失败"
});
gateway.WriteFailureFlag();
return;
}
OnPaySucceed(args);
success = true;
break;
}

if (HttpUtil.RequestType == "GET")
{
OnPaySucceed(new PaySucceedEventArgs(gateway));
return;
}
case RefundSucceedEventArgs refundSucceedEventArgs:
{
OnRefundSucceed(refundSucceedEventArgs);
success = true;
break;
}

var result = false;
if (gateway.IsPaySuccess)
{
result = OnPaySucceed(new PaySucceedEventArgs(gateway));
}
else if (gateway.IsRefundSuccess)
{
result = OnRefundSucceed(new RefundSucceedEventArgs(gateway));
}
else if (gateway.IsCancelSuccess)
{
result = OnCancelSucceed(new CancelSucceedEventArgs(gateway));
}
else
{
result = OnUnknownNotify(new UnKnownNotifyEventArgs(gateway));
}
case CancelSucceedEventArgs cancelSucceedEventArgs:
{
OnCancelSucceed(cancelSucceedEventArgs);
success = true;
break;
}

if (result)
{
gateway.WriteSuccessFlag();
}
else
{
gateway.WriteFailureFlag();
}
}
catch (GatewayException ex)
Expand All @@ -133,8 +151,93 @@ public async Task ReceivedAsync()
{
Message = ex.Message
});
gateway.WriteFailureFlag();

}


return new SendEventResult(gateway, success);
}

public async Task<SendEventResult> ReceivedAsync(bool writeFlag)
{
var gateway = await NotifyProcess.GetGatewayAsync(_gateways);
var sendEventResult = await SendNotifyEventAsync(gateway);

if (writeFlag)
{
sendEventResult.WriteFlagXml();
}

return sendEventResult;
}

/// <summary>
/// 接收并验证网关的支付通知
/// </summary>
public Task ReceivedAsync()
{
return ReceivedAsync(true);

// var gateway = await NotifyProcess.GetGatewayAsync(_gateways);
// if (gateway is NullGateway)
// {
// OnUnknownGateway(new UnknownGatewayEventArgs(gateway));
// return;
// }
//
// try
// {
// if (!await gateway.ValidateNotifyAsync())
// {
// OnUnknownNotify(new UnKnownNotifyEventArgs(gateway)
// {
// Message = "签名验证失败"
// });
// gateway.WriteFailureFlag();
// return;
// }
//
// if (HttpUtil.RequestType == "GET")
// {
// OnPaySucceed(new PaySucceedEventArgs(gateway));
// return;
// }
//
// var result = false;
// if (gateway.IsPaySuccess)
// {
// result = OnPaySucceed(new PaySucceedEventArgs(gateway));
// }
// else if (gateway.IsRefundSuccess)
// {
// result = OnRefundSucceed(new RefundSucceedEventArgs(gateway));
// }
// else if (gateway.IsCancelSuccess)
// {
// result = OnCancelSucceed(new CancelSucceedEventArgs(gateway));
// }
// else
// {
// result = OnUnknownNotify(new UnKnownNotifyEventArgs(gateway));
// }
//
// if (result)
// {
// gateway.WriteSuccessFlag();
// }
// else
// {
// gateway.WriteFailureFlag();
// }
// }
// catch (GatewayException ex)
// {
// OnUnknownNotify(new UnKnownNotifyEventArgs(gateway)
// {
// Message = ex.Message
// });
// gateway.WriteFailureFlag();
// }
}

#endregion
Expand Down
39 changes: 39 additions & 0 deletions src/PaySharp.Core/Notify/SendEventResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
namespace PaySharp.Core
{
public class SendEventResult
{
private BaseGateway Gateway { get; }
public bool Success { get; }
public SendEventResult(BaseGateway gateway,bool success)
{
Gateway = gateway;
Success = success;
}

#region Overrides of Object
/// <summary>
/// 获取当前处理结果的XML
/// </summary>
/// <returns></returns>
public string GetFlagXml()
{
return Success ? Gateway.GetSuccessFlag() : Gateway.GetFailureFlag();
}

#endregion
/// <summary>
/// 立即响应处理结果的XML,并结束请求
/// </summary>
public void WriteFlagXml()
{
if (Success)
{
Gateway.WriteSuccessFlag();
}
else
{
Gateway.WriteFailureFlag();
}
}
}
}
14 changes: 10 additions & 4 deletions src/PaySharp.Qpay/QpayGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,24 @@ protected override async Task<bool> ValidateNotifyAsync()
return true;
}

protected override void WriteSuccessFlag()
/// <summary>
/// 当接收到支付网关通知并验证无误时按照支付网关要求格式输出表示成功接收到网关通知的字符串
/// </summary>
protected override string GetSuccessFlag()
{
GatewayData.Clear();
GatewayData.Add("return_code", "SUCCESS");
HttpUtil.Write(GatewayData.ToXml());
return GatewayData.ToXml();
}

protected override void WriteFailureFlag()
/// <summary>
/// 当接收到支付网关通知并验证有误时按照支付网关要求格式输出表示失败接收到网关通知的字符串
/// </summary>
protected override string GetFailureFlag()
{
GatewayData.Clear();
GatewayData.Add("return_code", "FAIL");
HttpUtil.Write(GatewayData.ToXml());
return GatewayData.ToXml();
}

public override TResponse Execute<TModel, TResponse>(Request<TModel, TResponse> request)
Expand Down
14 changes: 10 additions & 4 deletions src/PaySharp.Wechatpay/WechatpayGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,25 @@ protected override async Task<bool> ValidateNotifyAsync()
return true;
}

protected override void WriteSuccessFlag()
/// <summary>
/// 当接收到支付网关通知并验证无误时按照支付网关要求格式输出表示成功接收到网关通知的字符串
/// </summary>
protected override string GetSuccessFlag()
{
GatewayData.Clear();
GatewayData.Add("return_code", "SUCCESS");
GatewayData.Add("return_msg", "OK");
HttpUtil.Write(GatewayData.ToXml());
return GatewayData.ToXml();
}

protected override void WriteFailureFlag()
/// <summary>
/// 当接收到支付网关通知并验证有误时按照支付网关要求格式输出表示失败接收到网关通知的字符串
/// </summary>
protected override string GetFailureFlag()
{
GatewayData.Clear();
GatewayData.Add("return_code", "FAIL");
HttpUtil.Write(GatewayData.ToXml());
return GatewayData.ToXml();
}

public override TResponse Execute<TModel, TResponse>(Request<TModel, TResponse> request)
Expand Down