-
Notifications
You must be signed in to change notification settings - Fork 263
[WIP] Idea: One Handler rule both Asp.Net and Asp.Net-Core
#29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
John0King
wants to merge
26
commits into
Varorbc:master
Choose a base branch
from
John0King:2.0.0-alpha
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 16 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
88573d5
Merge pull request #8 from Varorbc/master
John0King 892c147
Merge pull request #9 from Varorbc/master
John0King 8965a70
Merge pull request #10 from Varorbc/master
John0King 22c5816
Merge pull request #11 from Varorbc/master
John0King e773f53
Merge pull request #13 from Varorbc/master
John0King 60a5f9c
一些抽象接口的想法
John0King 8f380f7
Merge pull request #14 from Varorbc/2.0.0-alpha
John0King eb5b163
a few code change
John0King e94d4ad
concept of IKeyValueProvider
John0King 4f4999c
Merge pull request #15 from Varorbc/2.0.0-alpha
John0King 44a7079
ServiceCollectionExtensions
3d86ed4
merge
John0King 221ccdb
sync project
John0King 9b52f88
abstractions
John0King 4241fb6
添加 Asp.Net 的 Target
a4cbc73
[wip] notifyHub
e57f639
修复 DI 的问题,Singleton 的 IGatewayBuilder 配合 Scoped IGatewayProvider 会导…
John0King 64fc676
修复 nuget 包
John0King a68a105
添加新测试
John0King b883acf
Merge remote-tracking branch 'varorbc/master' into 2.0.0-alpha
John0King 458cee4
一点点更改
John0King d23dc9a
sync code
John0King 36e27a8
一点点更新
John0King 7b4991e
merge
John0King 253b846
Merge remote-tracking branch 'varorbc/master' into 2.0.0-alpha
John0King 467333a
some idea
John0King File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| using PaySharp.Abstractions; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
| using System.Web; | ||
|
|
||
| namespace PaySharp.AspNetCore | ||
| { | ||
| /// <summary> | ||
| /// Asp.Net 的键值提供器 | ||
| /// </summary> | ||
| public class HttpContextKeyValueProvider : IKeyValueProvider | ||
| { | ||
| public HttpContextBase HttpContext { get; } | ||
|
|
||
| public IEnumerable<string> SupportedParts => _supportedParts; | ||
|
|
||
| private IEnumerable<string> _supportedParts = new[] { "QueryString", "Form" }; | ||
|
|
||
| public HttpContextKeyValueProvider(HttpContextBase httpContext) | ||
| { | ||
| HttpContext = HttpContext; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public byte[] Get() | ||
| { | ||
| using (var ms = new MemoryStream()) | ||
| { | ||
| HttpContext.Request.GetBufferedInputStream().CopyTo(ms); | ||
| return ms.ToArray(); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public IEnumerable<string> GetKeys(string part = null) | ||
| { | ||
| var request = HttpContext.Request; | ||
| var result = new List<string>(); | ||
| if (NullEq(part, "QueryString")) | ||
| { | ||
| result.AddRange(request.QueryString.AllKeys); | ||
|
|
||
| } | ||
| if (NullEq(part, "Form")) | ||
| { | ||
| result.AddRange(request.Form.AllKeys); | ||
| } | ||
|
|
||
| return result.Distinct(); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public string GetValue(string key, string part = null) | ||
| { | ||
| var request = HttpContext.Request; | ||
| if (NullEq(part, "QueryString")) | ||
| { | ||
| return request.QueryString[key]; | ||
| } | ||
|
|
||
| if (NullEq(part, "Form")) | ||
| { | ||
| return request.Form[key]; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
|
|
||
| private bool NullEq(string val, string compare) | ||
| { | ||
| return string.IsNullOrEmpty(val) || string.Equals(val, compare, StringComparison.OrdinalIgnoreCase); | ||
| } | ||
| private bool Eq(string str1, string str2) | ||
| { | ||
| return string.Equals(str1, str2, StringComparison.OrdinalIgnoreCase); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net45</TargetFramework> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\PaySharp.Core\PaySharp.Core.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Reference Include="System.Web" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| using PaySharp.Abstractions; | ||
| using Microsoft.AspNetCore.Http; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using System.Linq; | ||
|
|
||
| namespace PaySharp.AspNetCore | ||
| { | ||
| /// <summary> | ||
| /// Asp.Net-Core 的键值提供器 | ||
| /// </summary> | ||
| public class HttpContextKeyValueProvider : IKeyValueProvider | ||
| { | ||
| public HttpContext HttpContext { get; } | ||
|
|
||
| public IEnumerable<string> SupportedParts => _supportedParts; | ||
|
|
||
| private IEnumerable<string> _supportedParts = new[] { "QueryString", "Form" }; | ||
|
|
||
| public HttpContextKeyValueProvider(IHttpContextAccessor httpContextAccessor) | ||
| { | ||
| HttpContext = httpContextAccessor.HttpContext; | ||
| } | ||
|
|
||
| public HttpContextKeyValueProvider(HttpContext httpContext) | ||
| { | ||
| HttpContext = httpContext; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public byte[] Get() | ||
| { | ||
| using(var ms = new MemoryStream()) | ||
| { | ||
| HttpContext.Request.Body.CopyTo(ms); | ||
| return ms.ToArray(); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public IEnumerable<string> GetKeys(string part = null) | ||
| { | ||
| var request = HttpContext.Request; | ||
| var result = new List<string>(); | ||
| if(NullEq(part,"QueryString")) | ||
| { | ||
| result.AddRange(request.Query.Keys); | ||
|
|
||
|
|
||
| } | ||
| if(NullEq(part, "Form")) | ||
| { | ||
| if (request.HasFormContentType && Eq("POST", request.Method)) | ||
| { | ||
| result.AddRange(request.Form.Keys); | ||
| } | ||
| } | ||
|
|
||
| return result.Distinct(); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public string GetValue(string key, string part = null) | ||
| { | ||
| var request = HttpContext.Request; | ||
| if (NullEq(part, "QueryString")) | ||
| { | ||
| if (request.Query.ContainsKey(key)) | ||
| { | ||
| return request.Query[key]; | ||
| } | ||
| } | ||
|
|
||
| if (NullEq(part, "Form")) | ||
| { | ||
| if (request.HasFormContentType && Eq("POST", request.Method)) | ||
| { | ||
| if (request.Form.ContainsKey(key)) | ||
| { | ||
| return request.Form[key]; | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
|
|
||
| private bool NullEq(string val,string compare) | ||
| { | ||
| return string.IsNullOrEmpty(val) || string.Equals(val, compare, StringComparison.OrdinalIgnoreCase); | ||
| } | ||
| private bool Eq(string str1, string str2) | ||
| { | ||
| return string.Equals(str1, str2, StringComparison.OrdinalIgnoreCase); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>netstandard2.0</TargetFramework> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\PaySharp.Core\PaySharp.Core.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| using PaySharp.Internal; | ||
| using Microsoft.Extensions.DependencyInjection.Extensions; | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
| using PaySharp.Abstractions; | ||
| using System.ComponentModel; | ||
| using PaySharp.AspNetCore; | ||
|
|
||
| namespace Microsoft.Extensions.DependencyInjection | ||
| { | ||
| /// <summary> | ||
| /// PaySharp 对 <see cref="IServiceCollection"/> 的扩展方法 | ||
| /// </summary> | ||
| public static class ServiceCollectionExtensions | ||
| { | ||
| [EditorBrowsable(EditorBrowsableState.Never)]//先不要让其他人访问 | ||
| public static IServiceCollection AddPaySharp(this IServiceCollection services) | ||
| { | ||
| services = services ?? throw new ArgumentNullException(nameof(services)); | ||
| AddPaySharp(services, null); | ||
| return services; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 添加PaySharp相关服务 | ||
| /// </summary> | ||
| /// <param name="services"></param> | ||
| /// <param name="buildAction"></param> | ||
| /// <returns></returns> | ||
| public static IServiceCollection AddPaySharp(this IServiceCollection services,Action<IGatewayBuilder> buildAction) | ||
| { | ||
| services.TryAddSingleton<IGatewayBuilder, GatewayBuilder>(); | ||
| services.TryAddScoped<IKeyValueProvider, HttpContextKeyValueProvider>(); | ||
| services.TryAddScoped(svcs => | ||
| { | ||
| var builder = svcs.GetRequiredService<IGatewayBuilder>(); | ||
| if (builder != null) | ||
| { | ||
| buildAction(builder); | ||
| } | ||
| return builder.Build(); | ||
| }); | ||
|
|
||
| return services; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using System.Text; | ||
| using System.Threading.Tasks; | ||
|
|
||
| namespace PaySharp.Abstractions | ||
| { | ||
| /// <summary> | ||
| /// 通用的处理数据 | ||
| /// </summary> | ||
| public class HubOrder | ||
| { | ||
| /// <summary> | ||
| /// 支付机构订单Id | ||
| /// </summary> | ||
| public virtual string GatewayOrderId { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// 业务逻辑的订单Id | ||
| /// </summary> | ||
| public virtual string BusinessOrderId { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// 支付机构此次处理的时间 | ||
| /// </summary> | ||
| public virtual DateTimeOffset ProcessTime { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// 支付处理器给的类型 | ||
| /// </summary> | ||
| /// <remarks>可以利用此值决定 <see cref="RawData"/>的类型</remarks> | ||
| public virtual string GatewayType { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// 原本的数据 | ||
| /// </summary> | ||
| public virtual object RawData { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// 状态, 当为 null 时,为未知 | ||
| /// </summary> | ||
| public virtual HubHandlerDataStatus? Status { get; set; } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// 支付数据状态 | ||
| /// </summary> | ||
| public enum HubHandlerDataStatus | ||
| { | ||
| /// <summary> | ||
| /// 支付成功 | ||
| /// </summary> | ||
| Success, | ||
|
|
||
| /// <summary> | ||
| /// 支付失败 | ||
| /// </summary> | ||
| Fail | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
|
|
||
| namespace PaySharp.Abstractions | ||
| { | ||
| /// <summary> | ||
| /// 网关提供器的构造器 | ||
| /// </summary> | ||
| public interface IGatewayBuilder | ||
| { | ||
| /// <summary> | ||
| /// 添加一个命名的网关 | ||
| /// </summary> | ||
| /// <typeparam name="T">网关的类型</typeparam> | ||
| /// <param name="name">网关的名字</param> | ||
| /// <param name="gateway">网关</param> | ||
| /// <returns></returns> | ||
| bool TryAdd<T>(string name,T gateway) where T:class; | ||
|
|
||
| /// <summary> | ||
| /// 返回一个 <see cref="IGatewayBuilder"/> 的实例 | ||
| /// </summary> | ||
| /// <returns></returns> | ||
| IGatewayProvider Build(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Text; | ||
|
|
||
| namespace PaySharp.Abstractions | ||
| { | ||
| public interface IGatewayProvider | ||
| { | ||
| T GetGateway<T>() where T : class; | ||
| T GetGateway<T>(string gatewayName) where T : class; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
好像这里 上面用 Singletion 下面用 Scoped 不行呢, 好像会重复添加
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add 的时候使用
Lazy<T>, 好像可以, 不过我发现,我有点偏离了我的目标, 我打算 发起支付的 部分叫 Client , 处理回调的部分叫 NotifyHandler , 这样用Layz<T>也不是不行,感觉不如IServiceProvider给力 , 先试试Lazy<T>能走多远吧There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
恩恩,先走走看吧,