-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPaymentsController.cs
More file actions
64 lines (52 loc) · 2.81 KB
/
Copy pathPaymentsController.cs
File metadata and controls
64 lines (52 loc) · 2.81 KB
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
namespace Comanda.Orchestrator.WebApi.Controllers;
#pragma warning disable S6960
[ApiController]
[Route("api/v1/payments")]
public sealed class PaymentsController(IDispatcher dispatcher, IEventDispatcher eventDispatcher) : ControllerBase
{
[HttpPost("online")]
[Idempotent] // https://developer.mozilla.org/en-US/docs/Glossary/Idempotent
[Authorize(Roles = Permissions.MakePayment)]
public async Task<IActionResult> CreateOnlineChargeAsync(
[FromBody] CheckoutSessionCreationScheme request, [FromHeader(Name = Headers.Credential)] string credential, CancellationToken cancellation)
{
var charge = new OnlineChargeScheme(request, credential);
var result = await dispatcher.DispatchAsync(charge, cancellation);
return result switch
{
{ IsSuccess: true } when result.Data is not null =>
StatusCode(StatusCodes.Status200OK, result.Data),
/* returning 502 Bad Gateway because an unexpected or invalid response was received from the external provider */
/* https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/502 */
{ IsFailure: true } when result.Error == AbacatePayErrors.OperationFailed =>
StatusCode(StatusCodes.Status502BadGateway, result.Error),
{ IsFailure: true } when result.Error == CommonErrors.OperationFailed =>
StatusCode(StatusCodes.Status500InternalServerError, result.Error),
{ IsFailure: true } when result.Error == CommonErrors.RateLimitExceeded =>
StatusCode(StatusCodes.Status429TooManyRequests, result.Error)
};
}
[HttpPost("offline")]
[Idempotent] // https://developer.mozilla.org/en-US/docs/Glossary/Idempotent
[Authorize(Roles = Permissions.MakePayment)]
public async Task<IActionResult> CreateOfflinePaymentAsync([FromBody] OfflinePaymentScheme request, CancellationToken cancellation)
{
var result = await dispatcher.DispatchAsync(request, cancellation);
return result switch
{
{ IsSuccess: true } when result.Data is not null =>
StatusCode(StatusCodes.Status200OK, result.Data),
{ IsFailure: true } when result.Error == CommonErrors.OperationFailed =>
StatusCode(StatusCodes.Status500InternalServerError, result.Error),
{ IsFailure: true } when result.Error == CommonErrors.RateLimitExceeded =>
StatusCode(StatusCodes.Status429TooManyRequests, result.Error)
};
}
[HttpPost("webhook")]
public async Task<IActionResult> OnWebhookNotificationAsync(
[FromBody] BillingPaidNotificationScheme request, CancellationToken cancellation)
{
await eventDispatcher.DispatchAsync(request, cancellation);
return StatusCode(StatusCodes.Status200OK);
}
}