-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZarinPalPaymentProcessor.cs
334 lines (282 loc) · 14.9 KB
/
ZarinPalPaymentProcessor.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Routing;
using System.Web.UI.WebControls;
using Nop.Core;
using Nop.Core.Domain.Customers;
using Nop.Core.Domain.Orders;
using Nop.Core.Domain.Payments;
using Nop.Core.Plugins;
using Nop.Plugin.Payments.ZarinPal.Controllers;
using Nop.Plugin.Payments.ZarinPal.Models;
using Nop.Services.Common;
using Nop.Services.Configuration;
using Nop.Services.Directory;
using Nop.Services.Localization;
using Nop.Services.Orders;
using Nop.Services.Payments;
using Nop.Services.Stores;
namespace Nop.Plugin.Payments.ZarinPal
{
public class ZarinPalPaymentProcessor : BasePlugin, IPaymentMethod
{
private readonly ISettingService _settingService;
private readonly ILocalizationService _localizationService;
private readonly HttpContextBase _httpContext;
private readonly IWebHelper _webHelper;
private readonly ICurrencyService _currencyService;
private readonly IWorkContext _workContext;
private readonly ILanguageService _languageService;
private readonly IOrderService _orderService;
private readonly IStoreService _storeService;
public ZarinPalPaymentProcessor(ISettingService settingService,
ILocalizationService localizationService,
HttpContextBase httpContext,
IWebHelper webHelper,
ICurrencyService currencyService,
IWorkContext workContext1,
ILanguageService languageService,
IOrderService orderService,
IStoreService storeService)
{
_settingService = settingService;
_localizationService = localizationService;
_httpContext = httpContext;
_webHelper = webHelper;
_currencyService = currencyService;
_workContext = workContext1;
_languageService = languageService;
_orderService = orderService;
_storeService = storeService;
}
public ProcessPaymentResult ProcessPayment(ProcessPaymentRequest processPaymentRequest)
{
var result = new ProcessPaymentResult();
string Authority;
System.Net.ServicePointManager.Expect100Continue = false;
var zp = new ZarinPalWebService.PaymentGatewayImplementationService();
var storeScope = this.GetActiveStoreScopeConfiguration(_storeService, _workContext);
var payPalStandardPaymentSettings = _settingService.LoadSetting<ZarinPalPaymentSettings>(storeScope);
var email = processPaymentRequest.CustomValues["EMail"];
var merchantCode = payPalStandardPaymentSettings.MerchantCode;
var description = payPalStandardPaymentSettings.Description;
var phonenumber = processPaymentRequest.CustomValues["Phonenumber"];
var userSsl = payPalStandardPaymentSettings.UseSsl;
var currencyId = payPalStandardPaymentSettings.CurrencyId;
//processPaymentRequest.CustomValues.Clear();
//check configurations fileds
if (string.IsNullOrWhiteSpace(merchantCode) || string.IsNullOrWhiteSpace(description) || currencyId == 0)
{
result.AddError(
_localizationService.GetResource("Plugins.Payments.ZarinPal.ErrorOccurred"));
result.NewPaymentStatus = PaymentStatus.Voided;
return result;
}
//get base url
var baseUrl = _webHelper.GetStoreHost(userSsl);
//get currency for convert to target currency
var sourceCurrency = _currencyService.GetCurrencyById(_workContext.WorkingCurrency.Id);
var targetCurrency = _currencyService.GetCurrencyById(currencyId);
//get converted price
var finalPrice = _currencyService.ConvertCurrency(processPaymentRequest.OrderTotal, sourceCurrency, targetCurrency);
if (email == null) email = "";
if (phonenumber == null) phonenumber = "";
//send information to bank and get status
var status = zp.PaymentRequest(merchantCode, (int)finalPrice,
description, email.ToString(), phonenumber.ToString(),
baseUrl + "Plugins/PaymentZarinPal/Result", out Authority);
//retuened status from bank
if (status == 100)
result.NewPaymentStatus = PaymentStatus.Pending;
else
{
result.NewPaymentStatus = PaymentStatus.Voided;
result.AddError(_localizationService.GetResource("Plugins.Payments.ZarinPal.ErrorOccurred"));
}
result.AuthorizationTransactionCode = Authority;
return result;
}
public void PostProcessPayment(PostProcessPaymentRequest postProcessPaymentRequest)
{
_httpContext.Response.Clear();
_httpContext.Server.ClearError();
_httpContext.Session["OrderId"] = postProcessPaymentRequest.Order.Id;
_httpContext.Session["Amount"] = postProcessPaymentRequest.Order.OrderTotal;
//redirecd to bank page
_httpContext.Response
.Redirect("https://www.zarinpal.com/pg/StartPay/" + postProcessPaymentRequest.Order.AuthorizationTransactionCode);
}
public virtual int GetActiveStoreScopeConfiguration(IStoreService storeService, IWorkContext workContext)
{
//ensure that we have 2 (or more) stores
if (storeService.GetAllStores().Count < 2)
return 0;
var storeId = workContext.CurrentCustomer.GetAttribute<int>(SystemCustomerAttributeNames.AdminAreaStoreScopeConfiguration);
var store = storeService.GetStoreById(storeId);
return store != null ? store.Id : 0;
}
public bool HidePaymentMethod(IList<ShoppingCartItem> cart)
{
return false;
}
public decimal GetAdditionalHandlingFee(IList<ShoppingCartItem> cart)
{
return 0;
}
public CapturePaymentResult Capture(CapturePaymentRequest capturePaymentRequest)
{
var result = new CapturePaymentResult();
result.AddError("Capture method not supported");
return result;
}
public RefundPaymentResult Refund(RefundPaymentRequest refundPaymentRequest)
{
var result = new RefundPaymentResult();
result.AddError("Refund method not supported");
return result;
}
public VoidPaymentResult Void(VoidPaymentRequest voidPaymentRequest)
{
var result = new VoidPaymentResult();
result.AddError("Void method not supported");
return result;
}
public ProcessPaymentResult ProcessRecurringPayment(ProcessPaymentRequest processPaymentRequest)
{
var result = new ProcessPaymentResult();
result.AddError("Recurring payment not supported");
return result;
}
public CancelRecurringPaymentResult CancelRecurringPayment(CancelRecurringPaymentRequest cancelPaymentRequest)
{
var result = new CancelRecurringPaymentResult();
result.AddError("Recurring payment not supported");
return result;
}
public bool CanRePostProcessPayment(Order order)
{
if (order == null)
throw new ArgumentNullException("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;
}
public void GetConfigurationRoute(out string actionName, out string controllerName, out RouteValueDictionary routeValues)
{
actionName = "Configure";
controllerName = "PaymentZarinPal";
routeValues = new RouteValueDictionary { { "Namespaces", "Nop.Plugin.Payments.ZarinPal.Controllers" }, { "area", null } };
}
public void GetPaymentInfoRoute(out string actionName, out string controllerName, out RouteValueDictionary routeValues)
{
actionName = "PaymentInfo";
controllerName = "PaymentZarinPal";
routeValues = new RouteValueDictionary { { "Namespaces", "Nop.Plugin.Payments.ZarinPal.Controllers" }, { "area", null } };
}
public override void Install()
{
//settings
var settings = new ZarinPalPaymentSettings()
{
MerchantCode = "",
Description = "",
UseSsl = false,
CurrencyId = 0
};
//localization
this.AddOrUpdatePluginLocaleResource(_localizationService, _languageService,
"Plugins.Payments.ZarinPal.Fields.EMail", "پست الکترونیکی");
this.AddOrUpdatePluginLocaleResource(_localizationService, _languageService,
"Plugins.Payments.ZarinPal.Fields.Description", "توضیحات");
this.AddOrUpdatePluginLocaleResource(_localizationService, _languageService,
"Plugins.Payments.ZarinPal.Fields.MerchantCode", "مرچنت کد");
this.AddOrUpdatePluginLocaleResource(_localizationService, _languageService,
"Plugins.Payments.ZarinPal.Fields.Phonenumber", "شماره همراه");
this.AddOrUpdatePluginLocaleResource(_localizationService, _languageService,
"Plugins.Payments.ZarinPal.Fields.Currency", "واحد پول");
this.AddOrUpdatePluginLocaleResource(_localizationService, _languageService,
"Plugins.Payments.ZarinPal.Success", "تراکنش با موافقیت انجام شد");
this.AddOrUpdatePluginLocaleResource(_localizationService, _languageService,
"Plugins.Payments.ZarinPal.Failed", "تراکنش با موافقیت انجام نشد");
this.AddOrUpdatePluginLocaleResource(_localizationService, _languageService,
"Plugins.Payments.ZarinPal.Authority", "شناسه پرداخت");
this.AddOrUpdatePluginLocaleResource(_localizationService, _languageService,
"Plugins.Payments.ZarinPal.PaymentMethodDescription", "درگاه پرداخت زرین پال");
this.AddOrUpdatePluginLocaleResource(_localizationService, _languageService,
"Plugins.Payments.ZarinPal.Fields.RedirectionTip", "به درگاه زرین پال هدایت میشوید");
this.AddOrUpdatePluginLocaleResource(_localizationService, _languageService,
"Plugins.Payments.ZarinPal.Fields.UseSsl", "از Ssl استفاده می کنید؟");
this.AddOrUpdatePluginLocaleResource(_localizationService, _languageService,
"Plugins.Payments.ZarinPal.Fields.EMail.IsWrong", "لطفا، پست الکترونیکی را درست وارد کنید");
this.AddOrUpdatePluginLocaleResource(_localizationService, _languageService,
"Plugins.Payments.ZarinPal.Fields.Phonenumber.TypeIsNotValid", "لطفا، شماره همراه را به درستی وارد کنید");
this.AddOrUpdatePluginLocaleResource(_localizationService, _languageService,
"Plugins.Payments.ZarinPal.PleaseSelect", "لطفا، انتخاب کنید");
_settingService.SaveSetting(settings);
base.Install();
}
public override void Uninstall()
{
//settings
_settingService.DeleteSetting<ZarinPalPaymentSettings>();
//localization
this.DeletePluginLocaleResource(_localizationService, _languageService, "Plugins.Payments.ZarinPal.Fields.EMail");
this.DeletePluginLocaleResource(_localizationService, _languageService, "Plugins.Payments.ZarinPal.Fields.Description");
this.DeletePluginLocaleResource(_localizationService, _languageService, "Plugins.Payments.ZarinPal.Fields.MerchantCode");
this.DeletePluginLocaleResource(_localizationService, _languageService, "Plugins.Payments.ZarinPal.Fields.Phonenumber");
this.DeletePluginLocaleResource(_localizationService, _languageService, "Plugins.Payments.ZarinPal.Fields.Currency");
this.DeletePluginLocaleResource(_localizationService, _languageService, "Plugins.Payments.ZarinPal.Success");
this.DeletePluginLocaleResource(_localizationService, _languageService, "Plugins.Payments.ZarinPal.Failed");
this.DeletePluginLocaleResource(_localizationService, _languageService, "Plugins.Payments.ZarinPal.Authority");
this.DeletePluginLocaleResource(_localizationService, _languageService, "Plugins.Payments.ZarinPal.PaymentMethodDescription");
this.DeletePluginLocaleResource(_localizationService, _languageService, "Plugins.Payments.ZarinPal.RedirectionTip");
this.DeletePluginLocaleResource(_localizationService, _languageService, "Plugins.Payments.ZarinPal.UseSsl");
this.DeletePluginLocaleResource(_localizationService, _languageService, "Plugins.Payments.ZarinPal.Fields.EMail.IsWrong");
this.DeletePluginLocaleResource(_localizationService, _languageService, "Plugins.Payments.ZarinPal.Fields.Phonenumber.TypeIsNotValid");
this.DeletePluginLocaleResource(_localizationService, _languageService, "Plugins.Payments.ZarinPal.PleaseSelect");
base.Uninstall();
}
public Type GetControllerType()
{
return typeof(PaymentZarinPalController);
}
public bool SupportCapture
{
get { return false; }
}
public bool SupportPartiallyRefund
{
get { return false; }
}
public bool SupportRefund
{
get { return false; }
}
public bool SupportVoid
{
get { return false; }
}
public RecurringPaymentType RecurringPaymentType
{
get { return RecurringPaymentType.NotSupported; }
}
public PaymentMethodType PaymentMethodType
{
get { return PaymentMethodType.Redirection; }
}
public bool SkipPaymentInfo
{
get { return false; }
}
public string PaymentMethodDescription
{
get { return _localizationService.GetResource("Plugins.Payments.ZarinPal.PaymentMethodDescription"); }
}
}
}