-
Notifications
You must be signed in to change notification settings - Fork 82
/
WeChatPayController.cs
206 lines (180 loc) · 7.7 KB
/
WeChatPayController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
using System;
using System.IO;
using System.Text.Json;
using System.Threading.Tasks;
using EasyAbp.Abp.WeChat.Common;
using EasyAbp.Abp.WeChat.Pay.RequestHandling;
using EasyAbp.Abp.WeChat.Pay.RequestHandling.Dtos;
using JetBrains.Annotations;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp;
using Volo.Abp.AspNetCore.Mvc;
namespace EasyAbp.Abp.WeChat.Pay.Controller
{
[RemoteService(Name = AbpWeChatRemoteServiceConsts.RemoteServiceName)]
[Area(AbpWeChatRemoteServiceConsts.ModuleName)]
[ControllerName("WeChatPay")]
[Route("/wechat-pay")]
public class WeChatPayController : AbpControllerBase
{
private readonly IWeChatPayEventRequestHandlingService _eventRequestHandlingService;
private readonly IWeChatPayClientRequestHandlingService _clientRequestHandlingService;
public WeChatPayController(
IWeChatPayEventRequestHandlingService eventRequestHandlingService,
IWeChatPayClientRequestHandlingService clientRequestHandlingService)
{
_eventRequestHandlingService = eventRequestHandlingService;
_clientRequestHandlingService = clientRequestHandlingService;
}
/// <summary>
/// 微信支付模块提供的支付成功通知接口,开发人员需要实现 <see cref="IWeChatPayEventHandler"/> 处理器来处理回调请求。
/// </summary>
[HttpPost]
[Route("notify")]
public virtual async Task<IActionResult> NotifyAsync([CanBeNull] [FromQuery] string tenantId,
[CanBeNull] [FromQuery] string mchId)
{
using var changeTenant = CurrentTenant.Change(tenantId.IsNullOrWhiteSpace() ? null : Guid.Parse(tenantId!));
var result = await _eventRequestHandlingService.PaidNotifyAsync(
BuildNotifyInputDto(await GetPostDataAsync(), mchId));
return !result.Success ? NotifyFailure(result.FailureReason) : NotifySuccess();
}
/// <summary>
/// 本方法是为了避免多 Route 导致 ABP ApiDescription 报 Warning。
/// 见 <see cref="NotifyAsync"/>
/// </summary>
[HttpPost]
[Route("notify/tenant-id/{tenantId}")]
public virtual Task<IActionResult> Notify2Async([CanBeNull] string tenantId, [CanBeNull] string mchId)
{
return NotifyAsync(tenantId, mchId);
}
/// <summary>
/// 本方法是为了避免多 Route 导致 ABP ApiDescription 报 Warning。
/// 见 <see cref="NotifyAsync"/>
/// </summary>
[HttpPost]
[Route("notify/mch-id/{mchId}")]
public virtual Task<IActionResult> Notify3Async([CanBeNull] string tenantId, [CanBeNull] string mchId)
{
return NotifyAsync(tenantId, mchId);
}
/// <summary>
/// 本方法是为了避免多 Route 导致 ABP ApiDescription 报 Warning。
/// 见 <see cref="NotifyAsync"/>
/// </summary>
[HttpPost]
[Route("notify/tenant-id/{tenantId}/mch-id/{mchId}")]
public virtual Task<IActionResult> Notify4Async([CanBeNull] string tenantId, [CanBeNull] string mchId)
{
return NotifyAsync(tenantId, mchId);
}
/// <summary>
/// 微信支付模块提供的退款回调接口,开发人员需要实现 <see cref="IWeChatPayEventHandler"/> 处理器来处理回调请求。
/// </summary>
[HttpPost]
[Route("refund-notify")]
public virtual async Task<IActionResult> RefundNotifyAsync([CanBeNull] string tenantId, [CanBeNull] string mchId)
{
using var changeTenant = CurrentTenant.Change(tenantId.IsNullOrWhiteSpace() ? null : Guid.Parse(tenantId!));
var result = await _eventRequestHandlingService.RefundNotifyAsync(BuildNotifyInputDto(await GetPostDataAsync(), mchId));
return !result.Success ? NotifyFailure(result.FailureReason) : NotifySuccess();
}
/// <summary>
/// 本方法是为了避免多 Route 导致 ABP ApiDescription 报 Warning。
/// 见 <see cref="RefundNotifyAsync"/>
/// </summary>
[HttpPost]
[Route("refund-notify/tenant-id/{tenantId}")]
public virtual Task<IActionResult> RefundNotify2Async([CanBeNull] string tenantId, [CanBeNull] string mchId)
{
return RefundNotifyAsync(tenantId, mchId);
}
/// <summary>
/// 本方法是为了避免多 Route 导致 ABP ApiDescription 报 Warning。
/// 见 <see cref="RefundNotifyAsync"/>
/// </summary>
[HttpPost]
[Route("refund-notify/mch-id/{mchId}")]
public virtual Task<IActionResult> RefundNotify3Async([CanBeNull] string tenantId, [CanBeNull] string mchId)
{
return RefundNotifyAsync(tenantId, mchId);
}
/// <summary>
/// 本方法是为了避免多 Route 导致 ABP ApiDescription 报 Warning。
/// 见 <see cref="RefundNotifyAsync"/>
/// </summary>
[HttpPost]
[Route("refund-notify/tenant-id/{tenantId}/mch-id/{mchId}")]
public virtual Task<IActionResult> RefundNotify4Async([CanBeNull] string tenantId, [CanBeNull] string mchId)
{
return RefundNotifyAsync(tenantId, mchId);
}
/// <summary>
/// 根据统一下单接口返回的预支付 Id 生成支付签名。
/// </summary>
/// <param name="appId">AppId</param>
/// <param name="prepayId">预支付 Id。</param>
/// <param name="mchId">商户 Id</param>
[HttpGet]
[Route("js-sdk-config-parameters")]
public virtual async Task<IActionResult> GetJsSdkWeChatPayParametersAsync(string mchId,
[FromQuery] string appId, string prepayId)
{
var result = await _clientRequestHandlingService.GetJsSdkWeChatPayParametersAsync(
new GetJsSdkWeChatPayParametersInput
{
MchId = mchId,
AppId = appId,
PrepayId = prepayId
});
return new JsonResult(new
{
nonceStr = result.NonceStr,
timeStamp = result.TimeStamp,
package = result.Package,
signType = result.SignType,
paySign = result.PaySign
});
}
#region > Utilities methods <
private IActionResult NotifySuccess()
{
return Ok(new WeChatPayNotificationOutput
{
Code = "SUCCESS"
});
}
private IActionResult NotifyFailure(string message)
{
return BadRequest(new WeChatPayNotificationOutput
{
Code = "FAIL",
Message = message
});
}
private NotifyInputDto BuildNotifyInputDto(string requestBody, string mchId)
{
return new NotifyInputDto
{
MchId = mchId,
RequestBodyString = requestBody,
RequestBody = JsonSerializer.Deserialize<WeChatPayNotificationInput>(requestBody),
HttpHeader = new NotifyHttpHeaderModel(Request.Headers["Wechatpay-Serial"],
Request.Headers["Wechatpay-TimeStamp"],
Request.Headers["Wechatpay-Nonce"],
Request.Headers["Wechatpay-Signature"])
};
}
protected virtual async Task<string> GetPostDataAsync()
{
Request.EnableBuffering();
using var streamReader = new StreamReader(Request.Body);
var postData = await streamReader.ReadToEndAsync();
Request.Body.Position = 0;
return postData;
}
#endregion
}
}