Repository navigation
Expand file tree
/
Copy pathAmazonPayController.cs
More file actions
201 lines (168 loc) · 6.35 KB
/
Copy pathAmazonPayController.cs
File metadata and controls
201 lines (168 loc) · 6.35 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
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
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using SmartStore.AmazonPay.Models;
using SmartStore.AmazonPay.Services;
using SmartStore.ComponentModel;
using SmartStore.Core.Domain.Customers;
using SmartStore.Services.Authentication.External;
using SmartStore.Services.Payments;
using SmartStore.Services.Tasks;
using SmartStore.Web.Framework;
using SmartStore.Web.Framework.Controllers;
using SmartStore.Web.Framework.Security;
using SmartStore.Web.Framework.Settings;
namespace SmartStore.AmazonPay.Controllers
{
public class AmazonPayController : PaymentControllerBase
{
private readonly IAmazonPayService _apiService;
private readonly Lazy<IScheduleTaskService> _scheduleTaskService;
private readonly Lazy<IOpenAuthenticationService> _openAuthenticationService;
private readonly Lazy<ExternalAuthenticationSettings> _externalAuthenticationSettings;
public AmazonPayController(
IAmazonPayService apiService,
Lazy<IScheduleTaskService> scheduleTaskService,
Lazy<IOpenAuthenticationService> openAuthenticationService,
Lazy<ExternalAuthenticationSettings> externalAuthenticationSettings)
{
_apiService = apiService;
_scheduleTaskService = scheduleTaskService;
_openAuthenticationService = openAuthenticationService;
_externalAuthenticationSettings = externalAuthenticationSettings;
}
[NonAction]
public override IList<string> ValidatePaymentForm(FormCollection form)
{
var warnings = new List<string>();
return warnings;
}
[NonAction]
public override ProcessPaymentRequest GetPaymentInfo(FormCollection form)
{
var paymentInfo = new ProcessPaymentRequest();
return paymentInfo;
}
[AdminAuthorize, LoadSetting]
public ActionResult Configure(AmazonPaySettings settings)
{
var model = new ConfigurationModel();
MiniMapper.Map(settings, model);
_apiService.SetupConfiguration(model);
return View(model);
}
[HttpPost, AdminAuthorize]
public ActionResult Configure(ConfigurationModel model, FormCollection form)
{
var storeDependingSettingHelper = new StoreDependingSettingHelper(ViewData);
var storeScope = this.GetActiveStoreScopeConfiguration(Services.StoreService, Services.WorkContext);
var settings = Services.Settings.LoadSetting<AmazonPaySettings>(storeScope);
if (!ModelState.IsValid)
return Configure(settings);
ModelState.Clear();
model.AccessKey = model.AccessKey.TrimSafe();
model.ClientId = model.ClientId.TrimSafe();
model.SecretKey = model.SecretKey.TrimSafe();
model.SellerId = model.SellerId.TrimSafe();
MiniMapper.Map(model, settings);
using (Services.Settings.BeginScope())
{
storeDependingSettingHelper.UpdateSettings(settings, form, storeScope, Services.Settings);
}
using (Services.Settings.BeginScope())
{
Services.Settings.SaveSetting(settings, x => x.DataFetching, 0, false);
Services.Settings.SaveSetting(settings, x => x.PollingMaxOrderCreationDays, 0, false);
}
var task = _scheduleTaskService.Value.GetTaskByType<DataPollingTask>();
if (task != null)
{
task.Enabled = settings.DataFetching == AmazonPayDataFetchingType.Polling;
_scheduleTaskService.Value.UpdateTask(task);
}
NotifySuccess(T("Plugins.Payments.AmazonPay.ConfigSaveNote"));
return RedirectToConfiguration(AmazonPayPlugin.SystemName);
}
[HttpPost, AdminAuthorize]
public ActionResult SaveAccessData(string accessData)
{
try
{
var storeScope = this.GetActiveStoreScopeConfiguration(Services.StoreService, Services.WorkContext);
_apiService.ShareKeys(accessData, storeScope);
NotifySuccess(T("Plugins.Payments.AmazonPay.SaveAccessDataSucceeded"));
}
catch (Exception exception)
{
NotifyError(exception.Message);
}
return RedirectToConfiguration(AmazonPayPlugin.SystemName);
}
[ValidateInput(false)]
public ActionResult ShareKey(string payload)
{
Response.AddHeader("Access-Control-Allow-Origin", "https://payments.amazon.com");
Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
Response.AddHeader("Access-Control-Allow-Headers", "Content-Type");
try
{
_apiService.ShareKeys(payload, 0);
}
catch (Exception exception)
{
Response.StatusCode = 400;
return Json(new { result = "error", message = exception.Message });
}
return Json(new { result = "success" });
}
[HttpPost]
[ValidateInput(false)]
public ActionResult IPNHandler()
{
_apiService.ProcessIpn(Request);
return Content("OK");
}
// Authentication
[ChildActionOnly]
public ActionResult AuthenticationPublicInfo()
{
var model = _apiService.CreateViewModel(AmazonPayRequestType.AuthenticationPublicInfo, TempData);
if (model != null)
{
return View(model);
}
return new EmptyResult();
}
public ActionResult AuthenticationButtonHandler()
{
var returnUrl = Session["AmazonAuthReturnUrl"] as string;
var processor = _openAuthenticationService.Value.LoadExternalAuthenticationMethodBySystemName(AmazonPayPlugin.SystemName, Services.StoreContext.CurrentStore.Id);
if (processor == null || !processor.IsMethodActive(_externalAuthenticationSettings.Value))
{
NotifyError(T("Plugins.Payments.AmazonPay.AuthenticationNotActive"));
return new RedirectResult(Url.LogOn(returnUrl));
}
var result = _apiService.Authorize(returnUrl);
switch (result.AuthenticationStatus)
{
case OpenAuthenticationStatus.Error:
result.Errors.Each(x => NotifyError(x));
return new RedirectResult(Url.LogOn(returnUrl));
case OpenAuthenticationStatus.AssociateOnLogon:
return new RedirectResult(Url.LogOn(returnUrl));
case OpenAuthenticationStatus.AutoRegisteredEmailValidation:
return RedirectToRoute("RegisterResult", new { resultId = (int)UserRegistrationType.EmailValidation, returnUrl });
case OpenAuthenticationStatus.AutoRegisteredAdminApproval:
return RedirectToRoute("RegisterResult", new { resultId = (int)UserRegistrationType.AdminApproval, returnUrl });
case OpenAuthenticationStatus.AutoRegisteredStandard:
return RedirectToRoute("RegisterResult", new { resultId = (int)UserRegistrationType.Standard, returnUrl });
default:
if (result.Result != null)
return result.Result;
if (HttpContext.Request.IsAuthenticated)
return RedirectToReferrer(returnUrl, "~/");
return new RedirectResult(Url.LogOn(returnUrl));
}
}
}
}