/
PaylikeProcessor.cs
271 lines (230 loc) · 11.2 KB
/
PaylikeProcessor.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
using Nop.Core;
using Nop.Core.Domain.Directory;
using Nop.Core.Plugins;
using Nop.Plugin.Payments.Paylike.Controllers;
using Nop.Services.Configuration;
using Nop.Services.Customers;
using Nop.Services.Directory;
using Nop.Services.Orders;
using Nop.Services.Payments;
using System;
using System.Collections.Generic;
using System.Linq;
using Nop.Core.Domain.Catalog;
using Nop.Core.Domain.Payments;
using Nop.Services.Localization;
using Paylike.NET.Interfaces;
using Paylike.NET;
using Paylike.NET.RequestModels.Transactions;
using System.Text;
using System.Web;
using Paylike.NET.ResponseModels.Apps;
using Paylike.NET.Entities;
using Paylike.NET.RequestModels.Merchants;
namespace Nop.Plugin.Payments.Paylike
{
public class PaylikeProcessor : BasePlugin, IPaymentMethod
{
private readonly PaylikePaymentSettings _paylikePaymentSettings;
private readonly ISettingService _settingService;
private readonly ICurrencyService _currencyService;
private readonly ICustomerService _customerService;
private readonly CurrencySettings _currencySettings;
private readonly IOrderService _orderService;
private readonly IWebHelper _webHelper;
private readonly IOrderTotalCalculationService _orderTotalCalculationService;
private readonly IPaylikeTransactionService _paylikeTransactionService;
private readonly HttpContextBase _httpContext;
public PaylikeProcessor(ISettingService settingService,
ICurrencyService currencyService, ICustomerService customerService,
CurrencySettings currencySettings, IWebHelper webHelper,
IOrderTotalCalculationService orderTotalCalculationService,
IOrderService orderService,
PaylikePaymentSettings paylikePaymentSettings,
HttpContextBase httpContext)
{
_paylikePaymentSettings = paylikePaymentSettings;
_settingService = settingService;
_currencyService = currencyService;
_customerService = customerService;
_currencySettings = currencySettings;
_webHelper = webHelper;
_orderTotalCalculationService = orderTotalCalculationService;
_orderService = orderService;
_httpContext = httpContext;
if (!paylikePaymentSettings.IsValid())
return;
_paylikeTransactionService = new PaylikeTransactionService(paylikePaymentSettings.AppKey);
}
public ProcessPaymentResult ProcessPayment(ProcessPaymentRequest processPaymentRequest)
{
var result = new ProcessPaymentResult();
var token = processPaymentRequest.CustomValues["paymenttoken"].ToString();
var primaryStoreCurrency = _currencyService.GetCurrencyById(_currencySettings.PrimaryStoreCurrencyId);
int amount = (int)(Decimal.Round(processPaymentRequest.OrderTotal, 2) * 100);
CreateTransactionRequest createTransactionRequest = new CreateTransactionRequest()
{
Amount = amount,
Currency = primaryStoreCurrency.CurrencyCode,
CardId = token,
MerchantId = _paylikePaymentSettings.MerchantId,
Descriptor = string.Empty,
Custom = new Dictionary<string, string>() { }
};
createTransactionRequest.Custom.Add("NopOrderGuid", processPaymentRequest.OrderGuid.ToString());
var createTransactionResponse = _paylikeTransactionService.CreateTransaction(createTransactionRequest);
if (createTransactionResponse.IsError)
{
result.AddError(createTransactionResponse.ErrorMessage);
result.AddError(createTransactionResponse.ErrorContent);
}
else
{
result.AuthorizationTransactionId = createTransactionResponse.Content.Id;
result.NewPaymentStatus = PaymentStatus.Authorized;
}
return result;
}
public void PostProcessPayment(PostProcessPaymentRequest postProcessPaymentRequest)
{
}
public bool HidePaymentMethod(IList<Core.Domain.Orders.ShoppingCartItem> cart)
{
return !_paylikePaymentSettings.IsValid();
}
public decimal GetAdditionalHandlingFee(IList<Core.Domain.Orders.ShoppingCartItem> cart)
{
var result = this.CalculateAdditionalFee(_orderTotalCalculationService, cart, 0, false);
return result;
}
public CapturePaymentResult Capture(CapturePaymentRequest capturePaymentRequest)
{
var result = new CapturePaymentResult();
var primaryStoreCurrency = _currencyService.GetCurrencyById(_currencySettings.PrimaryStoreCurrencyId);
var captureRequest = new CaptureTransactionRequest() {
Amount = (int)(Math.Round(capturePaymentRequest.Order.OrderTotal, 2) * 100),
Currency = primaryStoreCurrency.CurrencyCode,
Descriptor = _paylikePaymentSettings.CaptureDescriptor,
TransactionId = capturePaymentRequest.Order.AuthorizationTransactionId
};
var captureResponse = _paylikeTransactionService.CaptureTransaction(captureRequest);
if(captureResponse.IsError)
{
result.AddError(captureResponse.ErrorMessage);
result.AddError(captureResponse.ErrorContent);
}
else
{
result.CaptureTransactionId = captureResponse.Content.Id;
result.NewPaymentStatus = PaymentStatus.Paid;
}
return result;
}
public RefundPaymentResult Refund(RefundPaymentRequest refundPaymentRequest)
{
var result = new RefundPaymentResult();
var refundRequest = new RefundTransactionRequest()
{
Amount = (int)(Math.Round(refundPaymentRequest.AmountToRefund, 2) * 100),
Descriptor = _paylikePaymentSettings.RefundDescriptor,
TransactionId = refundPaymentRequest.Order.CaptureTransactionId
};
var refundResponse = _paylikeTransactionService.RefundTransaction(refundRequest);
if (refundResponse.IsError)
{
result.AddError(refundResponse.ErrorMessage);
result.AddError(refundResponse.ErrorContent);
}
else
{
if(refundPaymentRequest.IsPartialRefund)
result.NewPaymentStatus = PaymentStatus.PartiallyRefunded;
else
result.NewPaymentStatus = PaymentStatus.Refunded;
}
return result;
}
public VoidPaymentResult Void(VoidPaymentRequest voidPaymentRequest)
{
var result = new VoidPaymentResult();
var voidRequest = new VoidTransactionRequest()
{
Amount = (int)(Math.Round(voidPaymentRequest.Order.OrderTotal, 2) * 100),
TransactionId = voidPaymentRequest.Order.AuthorizationTransactionId
};
var voidResponse = _paylikeTransactionService.VoidTransaction(voidRequest);
if (voidResponse.IsError)
{
result.AddError(voidResponse.ErrorMessage);
result.AddError(voidResponse.ErrorContent);
}
else
{
result.NewPaymentStatus = PaymentStatus.Voided;
}
return result;
}
public ProcessPaymentResult ProcessRecurringPayment(ProcessPaymentRequest processPaymentRequest)
{
var result = new ProcessPaymentResult();
return result;
}
public CancelRecurringPaymentResult CancelRecurringPayment(CancelRecurringPaymentRequest cancelPaymentRequest)
{
var result = new CancelRecurringPaymentResult();
return result;
}
public bool CanRePostProcessPayment(Core.Domain.Orders.Order order)
{
return false;
}
public void GetConfigurationRoute(out string actionName, out string controllerName, out System.Web.Routing.RouteValueDictionary routeValues)
{
actionName = "Configure";
controllerName = "Paylike";
routeValues = new System.Web.Routing.RouteValueDictionary { { "Namespaces", "Nop.Plugin.Payments.Paylike.Controllers" }, { "area", null } };
}
public void GetPaymentInfoRoute(out string actionName, out string controllerName, out System.Web.Routing.RouteValueDictionary routeValues)
{
actionName = "PaymentInfo";
controllerName = "Paylike";
routeValues = new System.Web.Routing.RouteValueDictionary { { "Namespaces", "Nop.Plugin.Payments.Paylike.Controllers" }, { "area", null } };
}
public Type GetControllerType()
{
return typeof(PaylikeController);
}
public bool SupportCapture => true;
public bool SupportPartiallyRefund => true;
public bool SupportRefund => true;
public bool SupportVoid => true;
public RecurringPaymentType RecurringPaymentType => RecurringPaymentType.NotSupported;
public PaymentMethodType PaymentMethodType => PaymentMethodType.Redirection;
public bool SkipPaymentInfo => false;
public override void Install()
{
this.AddOrUpdatePluginLocaleResource("Plugins.Payments.Paylike.Fields.PublicKey", "Public key");
this.AddOrUpdatePluginLocaleResource("Plugins.Payments.Paylike.Fields.AppKey", "App key");
this.AddOrUpdatePluginLocaleResource("Plugins.Payments.Paylike.Fields.MerchantId", "Merchant Id");
this.AddOrUpdatePluginLocaleResource("Plugins.Payments.Paylike.Fields.CaptureDescriptor", "Capture Descriptor");
this.AddOrUpdatePluginLocaleResource("Plugins.Payments.Paylike.Fields.RefundDescriptor", "Refund Descriptor");
this.AddOrUpdatePluginLocaleResource("Plugins.Payments.Paylike.Fields.RedirectionTip", "You will be redirected to the Paylike website to finish your order.");
this.AddOrUpdatePluginLocaleResource("Plugins.Payments.Paylike.Errors.PaymentTokenRequired", "A payment token is required in order to continue the checkout process.");
base.Install();
}
public override void Uninstall()
{
//settings
_settingService.DeleteSetting<PaylikePaymentSettings>();
//locales
this.DeletePluginLocaleResource("Plugins.Payments.Paylike.Fields.PublicKey");
this.DeletePluginLocaleResource("Plugins.Payments.Paylike.Fields.AppKey");
this.DeletePluginLocaleResource("Plugins.Payments.Paylike.Fields.MerchantId");
this.DeletePluginLocaleResource("Plugins.Payments.Paylike.Fields.CaptureDescriptor");
this.DeletePluginLocaleResource("Plugins.Payments.Paylike.Fields.RefundDescriptor");
this.DeletePluginLocaleResource("Plugins.Payments.Paylike.Fields.RedirectionTip");
this.DeletePluginLocaleResource("Plugins.Payments.Paylike.Errors.PaymentTokenRequired");
base.Uninstall();
}
}
}