-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathExactlyController.cs
More file actions
130 lines (118 loc) · 5.22 KB
/
ExactlyController.cs
File metadata and controls
130 lines (118 loc) · 5.22 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
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
using Lombiq.HelpfulLibraries.OrchardCore.DependencyInjection;
using Lombiq.HelpfulLibraries.OrchardCore.Validation;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Localization;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Logging;
using OrchardCore.Commerce.Abstractions.Models;
using OrchardCore.Commerce.MoneyDataType;
using OrchardCore.Commerce.Payment.Abstractions;
using OrchardCore.Commerce.Payment.Exactly.Drivers;
using OrchardCore.Commerce.Payment.Exactly.Models;
using OrchardCore.Commerce.Payment.Exactly.Services;
using OrchardCore.ContentManagement;
using OrchardCore.DisplayManagement.Notify;
using OrchardCore.Mvc.Core.Utilities;
using Refit;
using System;
using System.Linq;
using System.Threading.Tasks;
using static OrchardCore.Commerce.Abstractions.Constants.ContentTypes;
using AdminController = OrchardCore.Settings.Controllers.AdminController;
using FrontendException = Lombiq.HelpfulLibraries.AspNetCore.Exceptions.FrontendException;
namespace OrchardCore.Commerce.Payment.Exactly.Controllers;
public class ExactlyController : Controller
{
private readonly IContentManager _contentManager;
private readonly IExactlyService _exactlyService;
private readonly ILogger<ExactlyController> _logger;
private readonly INotifier _notifier;
private readonly IPaymentService _paymentService;
private readonly IHtmlLocalizer<ExactlyController> H;
private readonly IStringLocalizer<ExactlyController> S;
public ExactlyController(
IExactlyService exactlyService,
INotifier notifier,
IPaymentService paymentService,
IOrchardServices<ExactlyController> services)
{
_contentManager = services.ContentManager.Value;
_exactlyService = exactlyService;
_logger = services.Logger.Value;
_notifier = notifier;
_paymentService = paymentService;
H = services.HtmlLocalizer.Value;
S = services.StringLocalizer.Value;
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> CreateTransaction(string shoppingCartId) =>
await this.SafeJsonAsync(async () =>
{
var order = await _paymentService.CreatePendingOrderFromShoppingCartAsync(
shoppingCartId,
notifyOnError: false,
throwOnError: true);
return await _exactlyService.CreateTransactionAsync(order.GetOrCreate<OrderPart>());
});
public async Task<IActionResult> GetRedirectUrl(string transactionId) =>
await this.SafeJsonAsync<object>(async () => await GetActionRedirectRequestedAsync(transactionId));
public async Task<IActionResult> VerifyApi()
{
try
{
var testAmount = new Amount(1, Currency.Euro);
var order = await _contentManager.NewAsync(Order);
order.Alter<OrderPart>(part =>
{
part.OrderId.Text = Guid.NewGuid().ToString("D");
part.LineItems.Add(new OrderLineItem(
quantity: 1,
"TEST",
"TEST",
testAmount,
testAmount,
contentItemVersion: null));
});
order.DisplayText = S["Exactly API test order"];
await _contentManager.CreateAsync(order);
var result = await _exactlyService.CreateTransactionAsync(order.GetOrCreate<OrderPart>(), testAmount);
var action = await GetActionRedirectRequestedAsync(result.Id);
await _notifier.SuccessAsync(
H["The Exactly API access works correctly. You can test the redirection by clicking <a href=\"{0}\">here</a>", action.Url]);
}
catch (ApiException exception)
{
_logger.LogError(exception, "An API error was encountered.");
await _notifier.ErrorAsync(H["An API error was encountered: {0}", exception.Message]);
}
catch (FrontendException exception)
{
_logger.LogError(exception, "A front-end readable error was encountered.");
await _notifier.FrontEndErrorAsync(exception);
}
catch (Exception exception)
{
_logger.LogError(exception, "An unknown error was encountered.");
var error = exception.ToString();
var html = $"{H["An unknown error was encountered:"].Html()}<br>{error.Replace("\n", "<br>")}";
await _notifier.ErrorAsync(new LocalizedHtmlString(html, html));
}
return RedirectToAction(
nameof(AdminController.Index),
typeof(AdminController).ControllerName(),
new { area = "OrchardCore.Settings", groupId = ExactlySettingsDisplayDriver.EditorGroupId });
}
private async Task<ChargeAction.ChargeActionAttributes> GetActionRedirectRequestedAsync(string transactionId)
{
var result = await _exactlyService.GetTransactionDetailsAsync(
transactionId,
ChargeResponse.ChargeResponseStatus.ActionRequired,
HttpContext.RequestAborted);
return result
.Attributes
.Actions
.Select(action => action.Attributes)
.FirstOrDefault(action => action.Action == "redirect-required" && action.IsGet);
}
}