-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPaybyrdPaymentProcessor.cs
466 lines (399 loc) · 17.6 KB
/
PaybyrdPaymentProcessor.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.Routing;
using Nop.Core;
using Nop.Core.Domain.Orders;
using Nop.Plugin.Payments.Paybyrd.Components;
using Nop.Plugin.Payments.Paybyrd.Localization;
using Nop.Services.Configuration;
using Nop.Services.Localization;
using Nop.Services.Orders;
using Nop.Services.Payments;
using Nop.Services.Plugins;
using Nop.Plugin.Payments.Paybyrd.Models;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Nop.Core.Domain.Payments;
using System.Net.Http.Headers;
using System.Net.Http;
using System.Linq;
using System.Text;
using Nop.Services.Messages;
namespace Nop.Plugin.Payments.Paybyrd;
/// <summary>
/// Paybyrd payment processor
/// </summary>
public class PaybyrdPaymentProcessor : BasePlugin, IPaymentMethod
{
#region Fields
protected readonly PaybyrdPaymentSettings _paybyrdPaymentSettings;
protected readonly ILocalizationService _localizationService;
protected readonly INotificationService _notificationService;
protected readonly IOrderTotalCalculationService _orderTotalCalculationService;
protected readonly ISettingService _settingService;
protected readonly IStoreContext _storeContext;
protected readonly IShoppingCartService _shoppingCartService;
protected readonly IWebHelper _webHelper;
protected readonly IOrderService _orderService;
protected readonly IUrlHelperFactory _urlHelperFactory;
protected readonly IActionContextAccessor _actionContextAccessor;
protected readonly IHttpContextAccessor _httpContextAccessor;
#endregion
#region Ctor
public PaybyrdPaymentProcessor(PaybyrdPaymentSettings paybyrdPaymentSettings,
ILocalizationService localizationService,
INotificationService notificationService,
IOrderTotalCalculationService orderTotalCalculationService,
ISettingService settingService,
IStoreContext storeContext,
IShoppingCartService shoppingCartService,
IWebHelper webHelper,
IOrderService orderService,
IUrlHelperFactory urlHelperFactory,
IActionContextAccessor actionContextAccessor,
IHttpContextAccessor httpContextAccessor)
{
_paybyrdPaymentSettings = paybyrdPaymentSettings;
_localizationService = localizationService;
_notificationService = notificationService;
_orderTotalCalculationService = orderTotalCalculationService;
_settingService = settingService;
_storeContext = storeContext;
_shoppingCartService = shoppingCartService;
_webHelper = webHelper;
_orderService = orderService;
_urlHelperFactory = urlHelperFactory;
_actionContextAccessor = actionContextAccessor;
_httpContextAccessor = httpContextAccessor;
}
#endregion
#region Methods
/// <summary>
/// Process a payment
/// </summary>
/// <param name="processPaymentRequest">Payment info required for an order processing</param>
/// <returns>
/// A task that represents the asynchronous operation
/// The task result contains the process payment result
/// </returns>
public Task<ProcessPaymentResult> ProcessPaymentAsync(ProcessPaymentRequest processPaymentRequest)
{
return Task.FromResult(new ProcessPaymentResult());
}
/// <summary>
/// Post process payment (used by payment gateways that require redirecting to a third-party URL)
/// </summary>
/// <param name="postProcessPaymentRequest">Payment info required for an order processing</param>
/// <returns>A task that represents the asynchronous operation</returns>
public Task PostProcessPaymentAsync(PostProcessPaymentRequest postProcessPaymentRequest)
{
var urlHelper = _urlHelperFactory.GetUrlHelper(_actionContextAccessor.ActionContext);
var redirectUrl = urlHelper.Action("HandleOrderPostProcessing", "PaymentPaybyrd", new { area = "Admin", orderId = postProcessPaymentRequest.Order.Id }, _webHelper.GetCurrentRequestProtocol());
// Redirects the user to hosted form handler
_httpContextAccessor.HttpContext.Response.Redirect(redirectUrl);
return Task.CompletedTask;
}
/// <summary>
/// Returns a value indicating whether payment method should be hidden during checkout
/// </summary>
/// <param name="cart">Shopping cart</param>
/// <returns>
/// A task that represents the asynchronous operation
/// The task result contains true - hide; false - display.
/// </returns>
public async Task<bool> HidePaymentMethodAsync(IList<ShoppingCartItem> cart)
{
// You can put any logic here
// for example, hide this payment method if all products in the cart are downloadable
// or hide this payment method if current customer is from certain country
if (!await _shoppingCartService.ShoppingCartRequiresShippingAsync(cart))
return true;
return false;
}
/// <summary>
/// Gets additional handling fee
/// </summary>
/// <param name="cart">Shopping cart</param>
/// <returns>
/// A task that represents the asynchronous operation
/// The task result contains the additional handling fee
/// </returns>
public async Task<decimal> GetAdditionalHandlingFeeAsync(IList<ShoppingCartItem> cart)
{
return await _orderTotalCalculationService.CalculatePaymentAdditionalFeeAsync(cart, 0, false);
}
/// <summary>
/// Captures payment
/// </summary>
/// <param name="capturePaymentRequest">Capture payment request</param>
/// <returns>
/// A task that represents the asynchronous operation
/// The task result contains the capture payment result
/// </returns>
public Task<CapturePaymentResult> CaptureAsync(CapturePaymentRequest capturePaymentRequest)
{
return Task.FromResult(new CapturePaymentResult { Errors = new[] { "Capture method not supported" } });
}
/// <summary>
/// Refunds a payment
/// </summary>
/// <param name="refundPaymentRequest">Request</param>
/// <returns>
/// A task that represents the asynchronous operation
/// The task result contains the result
/// </returns>
public async Task<RefundPaymentResult> RefundAsync(RefundPaymentRequest refundPaymentRequest)
{
var result = new RefundPaymentResult();
try
{
var client = new HttpClient();
var orderId = refundPaymentRequest.Order.OrderGuid;
using (client)
{
var storeScope = await _storeContext.GetActiveStoreScopeConfigurationAsync();
var settings = await _settingService.LoadSettingAsync<PaybyrdPaymentSettings>(storeScope);
var testModeEnabled = settings.EnableTestMode;
var apiKey = testModeEnabled ? settings.TestApiKey : settings.LiveApiKey;
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("X-API-Key", apiKey);
var response = await client.GetAsync($"{PaybyrdPaymentDefaults.PaybyrdAPIBasePath}/orders/{orderId}");
if (!response.IsSuccessStatusCode)
{
var errorMessage = await response.Content.ReadAsStringAsync();
var orderStatusErrorMessage = await _localizationService.GetResourceAsync("Plugins.Payments.Paybyrd.OrderStatusError");
_notificationService.ErrorNotification($"{orderStatusErrorMessage} {errorMessage}");
return result;
}
var responseString = await response.Content.ReadAsStringAsync();
var responseObject = JsonConvert.DeserializeObject<dynamic>(responseString);
var transactions = ((JArray)responseObject.transactions).ToObject<TransactionModel[]>();
Guid refundTransactionId;
var firstSuccessTransaction = transactions.ToList().Find(t => t.Status == "Success");
// Check if a successful transaction was found
if (firstSuccessTransaction != null)
{
refundTransactionId = firstSuccessTransaction.TransactionId;
}
else
{
var refundErrorMessage = await _localizationService.GetResourceAsync("Plugins.Payments.Paybyrd.OrderRefundError");
_notificationService.ErrorNotification(refundErrorMessage);
return result;
}
// Set up the refund request payload
var refundPayload = new
{
isoAmount = (int)(refundPaymentRequest.AmountToRefund * 100)
};
var jsonPayload = JsonConvert.SerializeObject(refundPayload);
var content = new StringContent(jsonPayload, Encoding.UTF8, "application/json");
// POST the Refund Request
var refundResponse = await client.PostAsync($"{PaybyrdPaymentDefaults.PaybyrdAPIBasePath}/refund/{refundTransactionId}", content);
// Check if the refund was successful
if (refundResponse.IsSuccessStatusCode)
{
var order = await _orderService.GetOrderByIdAsync(refundPaymentRequest.Order.Id);
if (order == null)
{
var orderNotFoundMessage = await _localizationService.GetResourceAsync("Plugins.Payments.Paybyrd.OrderNotFound");
_notificationService.ErrorNotification(orderNotFoundMessage);
return result;
}
if (refundPaymentRequest.IsPartialRefund)
{
order.PaymentStatus = PaymentStatus.PartiallyRefunded;
result.NewPaymentStatus = PaymentStatus.PartiallyRefunded;
}
else
{
order.PaymentStatus = PaymentStatus.Refunded;
result.NewPaymentStatus = PaymentStatus.Refunded;
}
// Update order payment status
await _orderService.UpdateOrderAsync(order);
var orderRefundedMessage = await _localizationService.GetResourceAsync("Plugins.Payments.Paybyrd.OrderRefunded");
_notificationService.SuccessNotification(orderRefundedMessage);
return result;
}
else
{
result.Errors.Add($"Refund request failed with status code: {refundResponse.StatusCode}");
return result;
}
}
}
catch (Exception ex)
{
// Log exception and set errors
result.Errors.Add($"An error occurred while processing the refund: {ex.Message}");
}
return result;
}
/// <summary>
/// Voids a payment
/// </summary>
/// <param name="voidPaymentRequest">Request</param>
/// <returns>
/// A task that represents the asynchronous operation
/// The task result contains the result
/// </returns>
public Task<VoidPaymentResult> VoidAsync(VoidPaymentRequest voidPaymentRequest)
{
return Task.FromResult(new VoidPaymentResult { Errors = new[] { "Void method not supported" } });
}
/// <summary>
/// Process recurring payment
/// </summary>
/// <param name="processPaymentRequest">Payment info required for an order processing</param>
/// <returns>
/// A task that represents the asynchronous operation
/// The task result contains the process payment result
/// </returns>
public Task<ProcessPaymentResult> ProcessRecurringPaymentAsync(ProcessPaymentRequest processPaymentRequest)
{
return Task.FromResult(new ProcessPaymentResult { Errors = new[] { "Recurring payment not supported" } });
}
/// <summary>
/// Cancels a recurring payment
/// </summary>
/// <param name="cancelPaymentRequest">Request</param>
/// <returns>
/// A task that represents the asynchronous operation
/// The task result contains the result
/// </returns>
public Task<CancelRecurringPaymentResult> CancelRecurringPaymentAsync(CancelRecurringPaymentRequest cancelPaymentRequest)
{
return Task.FromResult(new CancelRecurringPaymentResult { Errors = new[] { "Recurring payment not supported" } });
}
/// <summary>
/// Gets a value indicating whether customers can complete a payment after order is placed but not completed (for redirection payment methods)
/// </summary>
/// <param name="order">Order</param>
/// <returns>
/// A task that represents the asynchronous operation
/// The task result contains the result
/// </returns>
public Task<bool> CanRePostProcessPaymentAsync(Order order)
{
ArgumentNullException.ThrowIfNull(order);
// It's a redirection payment method, so we always return true
return Task.FromResult(true);
}
/// <summary>
/// Validate payment form
/// </summary>
/// <param name="form">The parsed form values</param>
/// <returns>
/// A task that represents the asynchronous operation
/// The task result contains the list of validating errors
/// </returns>
public Task<IList<string>> ValidatePaymentFormAsync(IFormCollection form)
{
return Task.FromResult<IList<string>>(new List<string>());
}
/// <summary>
/// Get payment information
/// </summary>
/// <param name="form">The parsed form values</param>
/// <returns>
/// A task that represents the asynchronous operation
/// The task result contains the payment info holder
/// </returns>
public Task<ProcessPaymentRequest> GetPaymentInfoAsync(IFormCollection form)
{
return Task.FromResult(new ProcessPaymentRequest());
}
/// <summary>
/// Gets a configuration page URL
/// </summary>
public override string GetConfigurationPageUrl()
{
return $"{_webHelper.GetStoreLocation()}Admin/PaymentPaybyrd/Configure";
}
/// <summary>
/// Gets a type of a view component for displaying plugin in public store ("payment info" checkout step)
/// </summary>
/// <returns>View component type</returns>
public Type GetPublicViewComponent()
{
return typeof(PaybyrdViewComponent);
}
/// <summary>
/// Install the plugin
/// </summary>
/// <returns>A task that represents the asynchronous operation</returns>
public override async Task InstallAsync()
{
// Set initial settings
var settings = new PaybyrdPaymentSettings
{
HFBackgroundColor = "#dbdbdb",
HFPaymentBackgroundColor = "#ffffff",
HFPrimaryColor = "#dbdbdb",
HFTextColor = "#2c2c2c",
PostPaymentOrderStatus = PostPaymentOrderStatus.Processing
};
await _settingService.SaveSettingAsync(settings);
// Load localization resources
await _localizationService.AddOrUpdateLocaleResourceAsync(LocalizationResources.GetLocaleResources());
await base.InstallAsync();
}
/// <summary>
/// Uninstall the plugin
/// </summary>
/// <returns>A task that represents the asynchronous operation</returns>
public override async Task UninstallAsync()
{
// Delete all settings before uninstalling the plugin
await _settingService.DeleteSettingAsync<PaybyrdPaymentSettings>();
// Delete all localization resources before uninstalling the plugin
await _localizationService.DeleteLocaleResourcesAsync("Plugins.Payment.Paybyrd");
await base.UninstallAsync();
}
/// <summary>
/// Gets a payment method description that will be displayed on checkout pages in the public store
/// </summary>
/// <remarks>
/// return description of this payment method to be display on "payment method" checkout step. good practice is to make it localizable
/// for example, for a redirection payment method, description may be like this: "You will be redirected to PayPal site to complete the payment"
/// </remarks>
/// <returns>A task that represents the asynchronous operation</returns>
public async Task<string> GetPaymentMethodDescriptionAsync()
{
return await _localizationService.GetResourceAsync("Plugins.Payments.Paybyrd.PaymentMethodDescription");
}
#endregion
#region Properties
/// <summary>
/// Gets a value indicating whether capture is supported
/// </summary>
public bool SupportCapture => false;
/// <summary>
/// Gets a value indicating whether partial refund is supported
/// </summary>
public bool SupportPartiallyRefund => true;
/// <summary>
/// Gets a value indicating whether refund is supported
/// </summary>
public bool SupportRefund => true;
/// <summary>
/// Gets a value indicating whether void is supported
/// </summary>
public bool SupportVoid => false;
/// <summary>
/// Gets a recurring payment type of payment method
/// </summary>
public RecurringPaymentType RecurringPaymentType => RecurringPaymentType.NotSupported;
/// <summary>
/// Gets a payment method type
/// </summary>
public PaymentMethodType PaymentMethodType => PaymentMethodType.Redirection;
/// <summary>
/// Gets a value indicating whether we should display a payment information page for this plugin
/// </summary>
public bool SkipPaymentInfo => false;
#endregion
}