-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPayPaymentProcessor.cs
428 lines (380 loc) · 17.3 KB
/
PayPaymentProcessor.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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using Nop.Core;
using Nop.Core.Domain.Directory;
using Nop.Core.Domain.Orders;
using Nop.Core.Domain.Shipping;
using Nop.Core.Infrastructure;
using Nop.Services.Common;
using Nop.Services.Configuration;
using Nop.Services.Directory;
using Nop.Services.Localization;
using Nop.Services.Logging;
using Nop.Services.Orders;
using Nop.Services.Payments;
using Nop.Services.Plugins;
using Nop.Services.Tax;
namespace NopFarsi.Payment.SizPay
{
/// <summary>
/// PayStandard payment processor
/// </summary>
public class PayPaymentProcessor : BasePlugin, IPaymentMethod
{
#region Fields
private readonly CurrencySettings _currencySettings;
private readonly ICheckoutAttributeParser _checkoutAttributeParser;
private readonly ICurrencyService _currencyService;
private readonly IGenericAttributeService _genericAttributeService;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly ILocalizationService _localizationService;
private readonly IOrderTotalCalculationService _orderTotalCalculationService;
private readonly ISettingService _settingService;
private readonly ITaxService _taxService;
private readonly IWebHelper _webHelper;
private readonly IPaymentService paymentService;
private readonly PayPaymentSettings _payPaymentSettings;
private readonly ILogger logger;
#endregion Fields
#region Ctor
public PayPaymentProcessor(CurrencySettings currencySettings,
ICheckoutAttributeParser checkoutAttributeParser,
ICurrencyService currencyService,
IGenericAttributeService genericAttributeService,
IHttpContextAccessor httpContextAccessor,
ILocalizationService localizationService,
IOrderTotalCalculationService orderTotalCalculationService,
ISettingService settingService,
ITaxService taxService,
IWebHelper webHelper,
PayPaymentSettings payPaymentSettings,
ILogger logger,
IPaymentService paymentService)
{
this._currencySettings = currencySettings;
this._checkoutAttributeParser = checkoutAttributeParser;
this._currencyService = currencyService;
this._genericAttributeService = genericAttributeService;
this._httpContextAccessor = httpContextAccessor;
this._localizationService = localizationService;
this._orderTotalCalculationService = orderTotalCalculationService;
this._settingService = settingService;
this._taxService = taxService;
this._webHelper = webHelper;
this._payPaymentSettings = payPaymentSettings;
this.logger = logger;
this.paymentService = paymentService;
}
#endregion Ctor
#region Utilities
/// <summary>
/// Gets Pay URL
/// </summary>
/// <returns></returns>
private string GetPayUrl()
{
return "https://sizpay.ir/payment/send";
}
/// <summary>
/// Gets IPN Pay URL
/// </summary>
/// <returns></returns>
private string GetIpnPayUrl()
{
return "https://sizpay.ir/payment/verify";
}
#endregion Utilities
#region Methods
/// <summary>
/// Process a payment
/// </summary>
/// <param name="processPaymentRequest">Payment info required for an order processing</param>
/// <returns>Process payment result</returns>
public ProcessPaymentResult ProcessPayment(ProcessPaymentRequest processPaymentRequest)
{
return 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>
public void PostProcessPayment(PostProcessPaymentRequest postProcessPaymentRequest)
{
//create common query parameters for the request
try
{
ServicePointManager.ServerCertificateValidationCallback =
delegate (object s, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) { return true; };
var kimial = new KimialPG.KimiaIPGRouteServiceSoapClient(KimialPG.KimiaIPGRouteServiceSoapClient.EndpointConfiguration.KimiaIPGRouteServiceSoap);
decimal OrderTotal = postProcessPaymentRequest.Order.OrderTotal;
if (!_payPaymentSettings.IsToman)
OrderTotal = postProcessPaymentRequest.Order.OrderTotal * 10;
string returnUrl = this._webHelper.GetStoreLocation() + "SizPay/Verify?OrderId=" + postProcessPaymentRequest.Order.Id.ToString();
DateTime d = DateTime.Now;
PersianCalendar pc = new PersianCalendar();
var result1 = kimial.GetTokenAsync(new KimialPG.GenerateToken()
{
UserName = _payPaymentSettings.UserName,
Password = _payPaymentSettings.Password,
MerchantID = _payPaymentSettings.MerchentId,
TerminalID = _payPaymentSettings.TerminalId,
Amount = Convert.ToInt32(OrderTotal),
DocDate = string.Format("{0}/{1}/{2}", pc.GetYear(d), pc.GetMonth(d), pc.GetDayOfMonth(d)),
OrderID = postProcessPaymentRequest.Order.Id.ToString(),
ReturnURL = returnUrl,
//ExtraInf=_payPaymentSettings.InvoiceNo,
InvoiceNo = postProcessPaymentRequest.Order.Id.ToString(),
AppExtraInf = new KimialPG.AppExtraInf(),
}).Result;//.Body.GetTokenResult;
var result2 = result1.Body;
var result = result2.GetTokenResult;
if (result.ResCod == 0)
{
postProcessPaymentRequest.Order.AuthorizationTransactionCode = result.Token;
EngineContext.Current.Resolve<IOrderService>().UpdateOrder(postProcessPaymentRequest.Order);
this._httpContextAccessor.HttpContext.Response.Redirect(
this._webHelper.GetStoreLocation() + "SizPay/Pay?MerchantID=" + _payPaymentSettings.MerchentId
+ "&TerminalID=" + _payPaymentSettings.TerminalId +
"&Token=" + result.Token);
}
else
{
this.logger.Error("Error Code : " + result.ResCod + " " + "Error Message : " + result.Message);
throw new NopException("Error Code : " + result.ResCod + " " + "Error Message : " + result.Message);
}
}
catch (Exception exp)
{
this.logger.Error("Error Code : " + exp.Message);
throw new NopException("Error Message" + exp.Message);
}
}
/// <summary>
/// Returns a value indicating whether payment method should be hidden during checkout
/// </summary>
/// <param name="cart">Shopping cart</param>
/// <returns>true - hide; false - display.</returns>
public bool HidePaymentMethod(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
return false;
}
/// <summary>
/// Gets additional handling fee
/// </summary>
/// <param name="cart">Shopping cart</param>
/// <returns>Additional handling fee</returns>
public decimal GetAdditionalHandlingFee(IList<ShoppingCartItem> cart)
{
return this.paymentService.CalculateAdditionalFee(cart, 0, false);
//return this.CalculateAdditionalFee(_orderTotalCalculationService, cart,
// _payPaymentSettings.AdditionalFee, _payPaymentSettings.AdditionalFeePercentage);
}
/// <summary>
/// Captures payment
/// </summary>
/// <param name="capturePaymentRequest">Capture payment request</param>
/// <returns>Capture payment result</returns>
public CapturePaymentResult Capture(CapturePaymentRequest capturePaymentRequest)
{
return new CapturePaymentResult { Errors = new[] { "Capture method not supported" } };
}
/// <summary>
/// Refunds a payment
/// </summary>
/// <param name="refundPaymentRequest">Request</param>
/// <returns>Result</returns>
public RefundPaymentResult Refund(RefundPaymentRequest refundPaymentRequest)
{
return new RefundPaymentResult { Errors = new[] { "Refund method not supported" } };
}
/// <summary>
/// Voids a payment
/// </summary>
/// <param name="voidPaymentRequest">Request</param>
/// <returns>Result</returns>
public VoidPaymentResult Void(VoidPaymentRequest voidPaymentRequest)
{
return 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>Process payment result</returns>
public ProcessPaymentResult ProcessRecurringPayment(ProcessPaymentRequest processPaymentRequest)
{
return new ProcessPaymentResult { Errors = new[] { "Recurring payment not supported" } };
}
/// <summary>
/// Cancels a recurring payment
/// </summary>
/// <param name="cancelPaymentRequest">Request</param>
/// <returns>Result</returns>
public CancelRecurringPaymentResult CancelRecurringPayment(CancelRecurringPaymentRequest cancelPaymentRequest)
{
return 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>Result</returns>
public bool CanRePostProcessPayment(Order order)
{
if (order == null)
throw new ArgumentNullException(nameof(order));
//let's ensure that at least 5 seconds passed after order is placed
//P.S. there's no any particular reason for that. we just do it
if ((DateTime.UtcNow - order.CreatedOnUtc).TotalSeconds < 5)
return false;
return true;
}
/// <summary>
/// Validate payment form
/// </summary>
/// <param name="form">The parsed form values</param>
/// <returns>List of validating errors</returns>
public IList<string> ValidatePaymentForm(IFormCollection form)
{
return new List<string>();
}
/// <summary>
/// Get payment information
/// </summary>
/// <param name="form">The parsed form values</param>
/// <returns>Payment info holder</returns>
public ProcessPaymentRequest GetPaymentInfo(IFormCollection form)
{
return new ProcessPaymentRequest();
}
/// <summary>
/// Gets a configuration page URL
/// </summary>
public override string GetConfigurationPageUrl()
{
if (this.PluginDescriptor.Author != "nopFarsi.ir")
throw new Exception();
return $"{_webHelper.GetStoreLocation()}Admin/SizPay/Configure";
}
/// <summary>
/// Gets a name of a view component for displaying plugin in public store ("payment info" checkout step)
/// </summary>
/// <returns>View component name</returns>
public string GetPublicViewComponentName()
{
return "PaymentPay";
}
/// <summary>
/// Install the plugin
/// </summary>
public override void Install()
{
base.Install();
this._localizationService.AddOrUpdatePluginLocaleResource("Plugins.Payments.Pay.Fields.UserName", "UserName");
this._localizationService.AddOrUpdatePluginLocaleResource("Plugins.Payments.Pay.Fields.Password", "Password");
this._localizationService.AddOrUpdatePluginLocaleResource("Plugins.Payments.Pay.Fields.MerchantID", "MerchantID");
this._localizationService.AddOrUpdatePluginLocaleResource("Plugins.Payments.Pay.Fields.TerminalID", "TerminalID");
this._localizationService.AddOrUpdatePluginLocaleResource("Plugins.Payments.Pay.Fields.IsToman", "IsToman");
this._localizationService.AddOrUpdatePluginLocaleResource("Plugins.Payments.SizPay.Fields.RedirectionTip", "به درگاه سیزپی منتقل می شوید.");
}
/// <summary>
/// Uninstall the plugin
/// </summary>
public override void Uninstall()
{
//settings
_settingService.DeleteSetting<PayPaymentSettings>();
//locales
_localizationService.DeletePluginLocaleResource("plugins.payments.pay.instructions");
_localizationService.DeletePluginLocaleResource("Plugins.Payments.Pay.Fields.Api");
_localizationService.DeletePluginLocaleResource("Plugins.Payments.Pay.Fields.Api.Hint");
_localizationService.DeletePluginLocaleResource("Plugins.Payments.Pay.Fields.Redirect");
_localizationService.DeletePluginLocaleResource("Plugins.Payments.Pay.Fields.Redirect.Hint");
_localizationService.DeletePluginLocaleResource("Plugins.Payments.Pay.Fields.AdditionalFee");
_localizationService.DeletePluginLocaleResource("Plugins.Payments.Pay.Fields.AdditionalFee.Hint");
_localizationService.DeletePluginLocaleResource("Plugins.Payments.Pay.Fields.AdditionalFeePercentage");
_localizationService.DeletePluginLocaleResource("Plugins.Payments.Pay.Fields.AdditionalFeePercentage.Hint");
_localizationService.DeletePluginLocaleResource("Plugins.Payments.Pay.Fields.RedirectionTip");
_localizationService.DeletePluginLocaleResource("Plugins.Payments.Pay.PaymentMethodDescription");
_localizationService.DeletePluginLocaleResource("Plugins.Payments.Pay.RoundingWarning");
base.Uninstall();
}
public void GetPublicViewComponent(out string viewComponentName)
{
viewComponentName = "PaymentPay";
}
#endregion Methods
#region Properties
/// <summary>
/// Gets a value indicating whether capture is supported
/// </summary>
public bool SupportCapture
{
get { return false; }
}
/// <summary>
/// Gets a value indicating whether partial refund is supported
/// </summary>
public bool SupportPartiallyRefund
{
get { return false; }
}
/// <summary>
/// Gets a value indicating whether refund is supported
/// </summary>
public bool SupportRefund
{
get { return false; }
}
/// <summary>
/// Gets a value indicating whether void is supported
/// </summary>
public bool SupportVoid
{
get { return false; }
}
/// <summary>
/// Gets a recurring payment type of payment method
/// </summary>
public RecurringPaymentType RecurringPaymentType
{
get { return RecurringPaymentType.NotSupported; }
}
/// <summary>
/// Gets a payment method type
/// </summary>
public PaymentMethodType PaymentMethodType
{
get { return PaymentMethodType.Redirection; }
}
/// <summary>
/// Gets a value indicating whether we should display a payment information page for this plugin
/// </summary>
public bool SkipPaymentInfo
{
get { return false; }
}
/// <summary>
/// Gets a payment method description that will be displayed on checkout pages in the public store
/// </summary>
public string PaymentMethodDescription
{
//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 Pay site to complete the payment"
get { return _localizationService.GetResource("Plugins.Payments.Pay.PaymentMethodDescription"); }
}
#endregion Properties
}
}