Skip to content
Open
Show file tree
Hide file tree
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 Nov 21, 2017
892c147
Merge pull request #9 from Varorbc/master
John0King Dec 7, 2017
8965a70
Merge pull request #10 from Varorbc/master
John0King Dec 22, 2017
22c5816
Merge pull request #11 from Varorbc/master
John0King Dec 26, 2017
e773f53
Merge pull request #13 from Varorbc/master
John0King Jan 17, 2018
60a5f9c
一些抽象接口的想法
John0King Jan 17, 2018
8f380f7
Merge pull request #14 from Varorbc/2.0.0-alpha
John0King Mar 7, 2018
eb5b163
a few code change
John0King Mar 7, 2018
e94d4ad
concept of IKeyValueProvider
John0King Mar 21, 2018
4f4999c
Merge pull request #15 from Varorbc/2.0.0-alpha
John0King Apr 18, 2018
44a7079
ServiceCollectionExtensions
Apr 18, 2018
3d86ed4
merge
John0King Apr 28, 2018
221ccdb
sync project
John0King Apr 28, 2018
9b52f88
abstractions
John0King Apr 29, 2018
4241fb6
添加 Asp.Net 的 Target
May 3, 2018
a4cbc73
[wip] notifyHub
May 3, 2018
e57f639
修复 DI 的问题,Singleton 的 IGatewayBuilder 配合 Scoped IGatewayProvider 会导…
John0King May 3, 2018
64fc676
修复 nuget 包
John0King May 3, 2018
a68a105
添加新测试
John0King May 4, 2018
b883acf
Merge remote-tracking branch 'varorbc/master' into 2.0.0-alpha
John0King May 4, 2018
458cee4
一点点更改
John0King May 4, 2018
d23dc9a
sync code
John0King Jun 3, 2018
36e27a8
一点点更新
John0King Jun 3, 2018
7b4991e
merge
John0King Jun 3, 2018
253b846
Merge remote-tracking branch 'varorbc/master' into 2.0.0-alpha
John0King Aug 24, 2018
467333a
some idea
John0King Aug 27, 2018
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
12 changes: 12 additions & 0 deletions PaySharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PaySharp.UnitTest", "test\P
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PaySharp.Demo", "sample\PaySharp.Demo\PaySharp.Demo.csproj", "{8A0208C0-7046-44A7-A67F-DA122AB280A3}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PaySharp.AspNetCore", "src\PaySharp.AspNetCore\PaySharp.AspNetCore.csproj", "{E649A330-1B5C-487B-99F7-208248161B8F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PaySharp.AspNet", "src\PaySharp.AspNet\PaySharp.AspNet.csproj", "{D20AC625-43A7-45A1-A111-81C3EDC7F6A5}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -51,6 +55,14 @@ Global
{8A0208C0-7046-44A7-A67F-DA122AB280A3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8A0208C0-7046-44A7-A67F-DA122AB280A3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8A0208C0-7046-44A7-A67F-DA122AB280A3}.Release|Any CPU.Build.0 = Release|Any CPU
{E649A330-1B5C-487B-99F7-208248161B8F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E649A330-1B5C-487B-99F7-208248161B8F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E649A330-1B5C-487B-99F7-208248161B8F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E649A330-1B5C-487B-99F7-208248161B8F}.Release|Any CPU.Build.0 = Release|Any CPU
{D20AC625-43A7-45A1-A111-81C3EDC7F6A5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D20AC625-43A7-45A1-A111-81C3EDC7F6A5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D20AC625-43A7-45A1-A111-81C3EDC7F6A5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D20AC625-43A7-45A1-A111-81C3EDC7F6A5}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
82 changes: 82 additions & 0 deletions src/PaySharp.AspNet/HttpContextKeyValueProvider.cs
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);
}
}
}
15 changes: 15 additions & 0 deletions src/PaySharp.AspNet/PaySharp.AspNet.csproj
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>
101 changes: 101 additions & 0 deletions src/PaySharp.AspNetCore/HttpContextKeyValueProvider.cs
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);
}
}
}
11 changes: 11 additions & 0 deletions src/PaySharp.AspNetCore/PaySharp.AspNetCore.csproj
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>
48 changes: 48 additions & 0 deletions src/PaySharp.AspNetCore/ServiceCollectionExtensions.cs
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();
});

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好像这里 上面用 Singletion 下面用 Scoped 不行呢, 好像会重复添加

@John0King John0King May 5, 2018

Copy link
Copy Markdown
Contributor Author

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> 能走多远吧

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

恩恩,先走走看吧,


return services;
}
}
}
61 changes: 61 additions & 0 deletions src/PaySharp.Core/Abstractions/HubOrder.cs
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
}
}
27 changes: 27 additions & 0 deletions src/PaySharp.Core/Abstractions/IGatewayBuilder.cs
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();
}
}
12 changes: 12 additions & 0 deletions src/PaySharp.Core/Abstractions/IGatewayProvider.cs
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;
}
}
Loading