Skip to content

Commit 9a50219

Browse files
committed
重构支付宝参数生成器
1 parent 462738b commit 9a50219

File tree

17 files changed

+146
-116
lines changed

17 files changed

+146
-116
lines changed

build/version.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<VersionMajor>1</VersionMajor>
44
<VersionMinor>0</VersionMinor>
55
<VersionPatch>11</VersionPatch>
6-
<VersionQuality>20180522-1</VersionQuality>
6+
<VersionQuality>20180523-1</VersionQuality>
77
<VersionPrefix>$(VersionMajor).$(VersionMinor).$(VersionPatch)</VersionPrefix>
88
<VersionSuffix>preview-$(VersionQuality)</VersionSuffix>
99
</PropertyGroup>

src/Util.Biz.Payments/Alipay/Configs/AlipayConfig.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,8 @@ public class AlipayConfig {
3636
/// <summary>
3737
/// 回调通知地址
3838
/// </summary>
39-
[Required( ErrorMessage = "回调通知地址[NotifyUrl]不能为空" )]
4039
public string NotifyUrl { get; set; }
4140

42-
/// <summary>
43-
/// 返回地址
44-
/// </summary>
45-
public string ReturnUrl { get; set; }
46-
4741
/// <summary>
4842
/// 验证
4943
/// </summary>

src/Util.Biz.Payments/Alipay/Parameters/AlipayContentBuilder.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ namespace Util.Biz.Payments.Alipay.Parameters {
99
/// </summary>
1010
public class AlipayContentBuilder : ParameterBuilder {
1111
/// <summary>
12-
/// 加载支付参数
12+
/// 初始化支付参数
1313
/// </summary>
1414
/// <param name="param">支付参数</param>
15-
public AlipayContentBuilder Load( PayParam param ) {
15+
public AlipayContentBuilder Init( PayParamBase param ) {
16+
if( param == null )
17+
return this;
1618
return OutTradeNo( param.OrderId )
1719
.Subject( param.Subject )
1820
.TotalAmount( param.Money )

src/Util.Biz.Payments/Alipay/Parameters/AlipayParameterBuilder.cs

Lines changed: 61 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.Collections.Generic;
22
using Util.Biz.Payments.Alipay.Configs;
3+
using Util.Biz.Payments.Core;
34
using Util.Helpers;
45
using Util.Parameters;
56
using Util.Signatures;
@@ -9,10 +10,6 @@ namespace Util.Biz.Payments.Alipay.Parameters {
910
/// 支付宝参数生成器
1011
/// </summary>
1112
public class AlipayParameterBuilder {
12-
/// <summary>
13-
/// 配置
14-
/// </summary>
15-
private readonly AlipayConfig _config;
1613
/// <summary>
1714
/// 参数生成器
1815
/// </summary>
@@ -22,23 +19,35 @@ public class AlipayParameterBuilder {
2219
/// 初始化支付宝参数生成器
2320
/// </summary>
2421
/// <param name="config">配置</param>
25-
public AlipayParameterBuilder( AlipayConfig config ) {
22+
/// <param name="param">参数</param>
23+
public AlipayParameterBuilder( AlipayConfig config, PayParamBase param = null ) {
2624
config.CheckNull( nameof( config ) );
27-
_config = config;
25+
Config = config;
2826
_builder = new UrlParameterBuilder();
29-
InitConfig();
27+
Content = new AlipayContentBuilder();
28+
Init( param );
3029
}
3130

3231
/// <summary>
3332
/// 配置
3433
/// </summary>
35-
public AlipayConfig Config => _config;
34+
public AlipayConfig Config { get; }
35+
36+
/// <summary>
37+
/// 内容
38+
/// </summary>
39+
public AlipayContentBuilder Content { get; }
3640

3741
/// <summary>
38-
/// 初始化配置
42+
/// 初始化
3943
/// </summary>
40-
private void InitConfig() {
41-
Format( "json" ).Charset( "utf-8" ).SignType( "RSA2" ).Timestamp().Version( "1.0" ).AppId( _config.AppId );
44+
private void Init( PayParamBase param ) {
45+
Format( "json" ).Charset( "utf-8" ).SignType( "RSA2" ).Timestamp().Version( "1.0" ).AppId( Config.AppId );
46+
if( param == null )
47+
return;
48+
param.Init();
49+
Content.Init( param );
50+
ReturnUrl( param.ReturnUrl ).NotifyUrl( param.NotifyUrl );
4251
}
4352

4453
/// <summary>
@@ -86,73 +95,85 @@ private AlipayParameterBuilder Version( string version ) {
8695
}
8796

8897
/// <summary>
89-
/// 设置应用Id
98+
/// 设置应用标识
9099
/// </summary>
91-
/// <param name="appId">应用Id</param>
100+
/// <param name="appId">应用标识</param>
92101
public AlipayParameterBuilder AppId( string appId ) {
93102
_builder.Add( AlipayConst.AppId, appId );
94103
return this;
95104
}
96105

97106
/// <summary>
98-
/// 设置请求方法
107+
/// 设置返回地址
99108
/// </summary>
100-
/// <param name="method">请求方法</param>
101-
public AlipayParameterBuilder Method( string method ) {
102-
_builder.Add( AlipayConst.Method, method );
109+
/// <param name="returnUrl">返回地址</param>
110+
public AlipayParameterBuilder ReturnUrl( string returnUrl ) {
111+
_builder.Add( AlipayConst.ReturnUrl, returnUrl );
103112
return this;
104113
}
105114

106115
/// <summary>
107-
/// 设置内容
116+
/// 设置回调通知地址
108117
/// </summary>
109-
/// <param name="content">内容</param>
110-
public AlipayParameterBuilder Content( string content ) {
111-
_builder.Add( AlipayConst.BizContent, content );
118+
/// <param name="notifyUrl">回调通知地址</param>
119+
public AlipayParameterBuilder NotifyUrl( string notifyUrl ) {
120+
_builder.Add( AlipayConst.NotifyUrl, GetNotifyUrl( notifyUrl ) );
112121
return this;
113122
}
114123

115124
/// <summary>
116-
/// 设置内容
125+
/// 获取回调通知地址
117126
/// </summary>
118-
/// <param name="builder">内容参数生成器</param>
119-
/// <param name="isConvertToSingleQuotes">是否将双引号转成单引号</param>
120-
public AlipayParameterBuilder Content( AlipayContentBuilder builder, bool isConvertToSingleQuotes = false ) {
121-
return Content( builder.ToJson( isConvertToSingleQuotes ) );
127+
private string GetNotifyUrl( string notifyUrl ) {
128+
if( notifyUrl.IsEmpty() )
129+
return Config.NotifyUrl;
130+
return notifyUrl;
131+
}
132+
133+
/// <summary>
134+
/// 设置请求方法
135+
/// </summary>
136+
/// <param name="method">请求方法</param>
137+
public AlipayParameterBuilder Method( string method ) {
138+
_builder.Add( AlipayConst.Method, method );
139+
return this;
140+
}
141+
142+
/// <summary>
143+
/// 获取值
144+
/// </summary>
145+
/// <param name="name">参数名</param>
146+
public string GetValue( string name ) {
147+
return _builder.GetValue( name );
122148
}
123149

124150
/// <summary>
125151
/// 获取参数字典
126152
/// </summary>
127-
public IDictionary<string, string> GetDictionary() {
128-
return GetSignBuilder().GetDictionary();
153+
/// <param name="isConvertToSingleQuotes">是否将双引号转成单引号</param>
154+
public IDictionary<string, string> GetDictionary( bool isConvertToSingleQuotes = false ) {
155+
return GetSignBuilder( isConvertToSingleQuotes ).GetDictionary();
129156
}
130157

131158
/// <summary>
132159
/// 获取签名的参数生成器
133160
/// </summary>
134-
private UrlParameterBuilder GetSignBuilder() {
161+
private UrlParameterBuilder GetSignBuilder( bool isConvertToSingleQuotes = false ) {
135162
var builder = new UrlParameterBuilder( _builder );
136-
builder.Add( AlipayConst.Sign, GetSign() );
163+
if( Content.IsEmpty == false )
164+
builder.Add( AlipayConst.BizContent, Content.ToJson( isConvertToSingleQuotes ) );
165+
builder.Add( AlipayConst.Sign, GetSign( builder ) );
137166
return builder;
138167
}
139168

140169
/// <summary>
141170
/// 获取签名
142171
/// </summary>
143-
private string GetSign() {
144-
var signManager = new SignManager( new SignKey( _config.AppPrivateKey ), _builder );
172+
private string GetSign( UrlParameterBuilder builder ) {
173+
var signManager = new SignManager( new SignKey( Config.AppPrivateKey ), builder );
145174
return signManager.Sign();
146175
}
147176

148-
/// <summary>
149-
/// 获取值
150-
/// </summary>
151-
/// <param name="name">参数名</param>
152-
public string GetValue( string name ) {
153-
return _builder.GetValue( name );
154-
}
155-
156177
/// <summary>
157178
/// 输出结果
158179
/// </summary>

src/Util.Biz.Payments/Alipay/Parameters/FormBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private void AddProperties( AlipayParameterBuilder builder ) {
9595
/// 添加input列表
9696
/// </summary>
9797
private void AddInputs( AlipayParameterBuilder builder ) {
98-
foreach( var item in builder.GetDictionary() )
98+
foreach( var item in builder.GetDictionary( true ) )
9999
AddInput( item.Key, item.Value );
100100
}
101101

@@ -123,7 +123,7 @@ private TagBuilder CreateSubmitButton() {
123123
public string Result() {
124124
using( var writer = new StringWriter() ) {
125125
_builder.WriteTo( writer, NullHtmlEncoder.Default );
126-
if ( IsAutoSubmit ) {
126+
if( IsAutoSubmit ) {
127127
var scriptBuilder = CreateScript();
128128
scriptBuilder.WriteTo( writer, NullHtmlEncoder.Default );
129129
}

src/Util.Biz.Payments/Alipay/Services/AlipayPagePayService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public AlipayPagePayService( IAlipayConfigProvider provider ) : base( provider )
2525
/// <param name="param">支付参数</param>
2626
public override async Task<PayResult> PayAsync( PayParam param ) {
2727
var config = await ConfigProvider.GetConfigAsync();
28-
Validate( param, config );
29-
var builder = new AlipayParameterBuilder( config );
30-
Config( param, builder, true );
28+
Validate( config, param );
29+
var builder = new AlipayParameterBuilder( config, param );
30+
Config( builder, param );
3131
var form = GetForm( builder );
3232
WriteLog( config, builder, form );
3333
return new PayResult { Result = form };

src/Util.Biz.Payments/Alipay/Services/AlipayServiceBase.cs

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@ protected AlipayServiceBase( IAlipayConfigProvider provider ) {
3636
/// <param name="param">支付参数</param>
3737
public virtual async Task<PayResult> PayAsync( PayParam param ) {
3838
var config = await ConfigProvider.GetConfigAsync();
39-
Validate( param, config );
40-
var builder = new AlipayParameterBuilder( config );
41-
Config( param, builder );
39+
Validate( config, param );
40+
var builder = new AlipayParameterBuilder( config, param );
41+
Config( builder, param );
4242
return await RequstResult( config, builder );
4343
}
4444

4545
/// <summary>
4646
/// 验证
4747
/// </summary>
48-
protected void Validate( PayParam param, AlipayConfig config ) {
48+
protected void Validate( AlipayConfig config, PayParam param ) {
4949
param.CheckNull( nameof( param ) );
5050
config.CheckNull( nameof( config ) );
5151
param.Validate();
@@ -61,34 +61,20 @@ protected virtual void ValidateParam( PayParam param ) {
6161
}
6262

6363
/// <summary>
64-
/// 参数配置
64+
/// 配置
6565
/// </summary>
66-
/// <param name="param">支付参数</param>
6766
/// <param name="builder">支付宝参数生成器</param>
68-
/// <param name="isConvertToSingleQuotes">是否将双引号转成单引号</param>
69-
protected void Config( PayParam param, AlipayParameterBuilder builder, bool isConvertToSingleQuotes = false ) {
70-
param.Init();
71-
var contentBuilder = CreateContentBuilder( param ).Scene( GetScene() );
72-
InitContentBuilder( contentBuilder, param );
73-
builder.Content( contentBuilder, isConvertToSingleQuotes ).Method( GetMethod() );
74-
}
75-
76-
/// <summary>
77-
/// 获取内容参数生成器
78-
/// </summary>
79-
protected AlipayContentBuilder CreateContentBuilder( PayParam param ) {
80-
var builder = new AlipayContentBuilder();
81-
builder.Load( param );
82-
return builder;
67+
/// <param name="param">支付参数</param>
68+
protected void Config( AlipayParameterBuilder builder, PayParam param ) {
69+
builder.Method( GetMethod() );
70+
builder.Content.Scene( GetScene() );
71+
InitContentBuilder( builder.Content, param );
8372
}
8473

8574
/// <summary>
86-
/// 初始化内容生成器
75+
/// 获取请求方法
8776
/// </summary>
88-
/// <param name="builder">内容参数生成器</param>
89-
/// <param name="param">支付参数</param>
90-
protected virtual void InitContentBuilder( AlipayContentBuilder builder, PayParam param ) {
91-
}
77+
protected abstract string GetMethod();
9278

9379
/// <summary>
9480
/// 获取场景
@@ -98,9 +84,12 @@ protected virtual string GetScene() {
9884
}
9985

10086
/// <summary>
101-
/// 获取请求方法
87+
/// 初始化内容生成器
10288
/// </summary>
103-
protected abstract string GetMethod();
89+
/// <param name="builder">内容参数生成器</param>
90+
/// <param name="param">支付参数</param>
91+
protected virtual void InitContentBuilder( AlipayContentBuilder builder, PayParam param ) {
92+
}
10493

10594
/// <summary>
10695
/// 请求结果
@@ -139,7 +128,6 @@ protected void WriteLog( AlipayConfig config, AlipayParameterBuilder builder, Al
139128
.Caption( "支付宝支付" )
140129
.Content( $"支付方式 : {GetPayWay().Description()}" )
141130
.Content( $"支付网关 : {config.GatewayUrl}" )
142-
.Content()
143131
.Content( "请求参数:" )
144132
.Content( builder.GetDictionary() )
145133
.Content()
@@ -157,17 +145,17 @@ protected void WriteLog( AlipayConfig config, AlipayParameterBuilder builder, Al
157145
/// <summary>
158146
/// 写日志
159147
/// </summary>
160-
protected void WriteLog( AlipayConfig config, AlipayParameterBuilder builder,string content ) {
148+
protected void WriteLog( AlipayConfig config, AlipayParameterBuilder builder, string content ) {
161149
var log = GetLog();
162150
if( log.IsTraceEnabled == false )
163151
return;
164152
log.Class( GetType().FullName )
165153
.Caption( "支付宝支付" )
166154
.Content( $"支付方式 : {GetPayWay().Description()}" )
167155
.Content( $"支付网关 : {config.GatewayUrl}" )
168-
.Content()
169156
.Content( "请求参数:" )
170157
.Content( builder.GetDictionary() )
158+
.Content()
171159
.Content( "原始请求:" )
172160
.Content( builder.ToString() )
173161
.Content()
@@ -208,9 +196,9 @@ protected virtual PayResult CreateResult( AlipayResult result ) {
208196
/// <param name="param">支付参数</param>
209197
public virtual async Task<string> Debug( PayParam param ) {
210198
var config = await ConfigProvider.GetConfigAsync();
211-
Validate( param, config );
212-
var builder = new AlipayParameterBuilder( config );
213-
Config( param, builder );
199+
Validate( config, param );
200+
var builder = new AlipayParameterBuilder( config, param );
201+
Config( builder, param );
214202
return builder.ToString();
215203
}
216204
}

src/Util.Biz.Payments/Alipay/Services/AlipayWapPayService.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public AlipayWapPayService( IAlipayConfigProvider provider ) : base( provider )
2525
/// <param name="param">支付参数</param>
2626
public override async Task<PayResult> PayAsync( PayParam param ) {
2727
var config = await ConfigProvider.GetConfigAsync();
28-
Validate( param, config );
29-
var builder = new AlipayParameterBuilder( config );
30-
Config( param, builder, true );
28+
Validate( config, param );
29+
var builder = new AlipayParameterBuilder( config, param );
30+
Config( builder, param );
3131
var form = GetForm( builder );
3232
WriteLog( config, builder, form );
3333
return new PayResult { Result = form };

src/Util.Biz.Payments/Core/PayParamBase.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ public class PayParamBase : IValidation {
2626
/// 支付订单付款超时时间,单位:分钟,默认为90分钟
2727
/// </summary>
2828
public int Timeout { get; set; } = 90;
29+
/// <summary>
30+
/// 返回地址
31+
/// </summary>
32+
public string ReturnUrl { get; set; }
33+
/// <summary>
34+
/// 回调通知地址
35+
/// </summary>
36+
public string NotifyUrl { get; set; }
2937

3038
/// <summary>
3139
/// 初始化

src/Util.Biz.Payments/Factories/PayFactory.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using Util.Biz.Payments.Alipay;
32
using Util.Biz.Payments.Alipay.Abstractions;
43
using Util.Biz.Payments.Alipay.Configs;
54
using Util.Biz.Payments.Alipay.Services;

0 commit comments

Comments
 (0)