-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPayUPaymentProcessor.cs
More file actions
317 lines (267 loc) · 15.1 KB
/
PayUPaymentProcessor.cs
File metadata and controls
317 lines (267 loc) · 15.1 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
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
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Routing;
using Nop.Core;
using Nop.Core.Domain.Directory;
using Nop.Core.Domain.Orders;
using Nop.Core.Domain.Payments;
using Nop.Core.Infrastructure;
using Nop.Core.Plugins;
using Nop.Plugin.Payments.PayuRedirect.Controllers;
using Nop.Plugin.Payments.PayuRedirect.Integration.Services;
using Nop.Services.Configuration;
using Nop.Services.Directory;
using Nop.Services.Localization;
using Nop.Services.Logging;
using Nop.Services.Orders;
using Nop.Services.Payments;
namespace Nop.Plugin.Payments.PayuRedirect
{
public class PayuPaymentProcessor : BasePlugin, IPaymentMethod, IPlugin
{
private readonly PayuPaymentSettings payuPaymentSettings;
private readonly ISettingService settingService;
private readonly ICurrencyService currencyService;
private readonly CurrencySettings currencySettings;
private readonly IWebHelper webHelper;
private readonly ILogger logger;
private readonly IPayuPaymentService payuPaymentService;
private readonly IOrderProcessingService orderProcessingService;
public bool SupportCapture => true;
public bool SupportPartiallyRefund => true;
public bool SupportRefund => true;
public bool SupportVoid => false;
public RecurringPaymentType RecurringPaymentType => RecurringPaymentType.NotSupported;
public PaymentMethodType PaymentMethodType => PaymentMethodType.Redirection;
public bool SkipPaymentInfo => false;
public bool HidePaymentMethod(IList<ShoppingCartItem> cart) => false;
public string PaymentMethodDescription => "PayU";
public PayuPaymentProcessor(PayuPaymentSettings payuPaymentSettings, ISettingService settingService, ICurrencyService currencyService, CurrencySettings currencySettings, IWebHelper webHelper, ILogger logger, IPayuPaymentService payuPaymentService, IOrderProcessingService orderProcessingService)
{
this.payuPaymentSettings = payuPaymentSettings;
this.settingService = settingService;
this.currencyService = currencyService;
this.currencySettings = currencySettings;
this.webHelper = webHelper;
this.logger = logger;
this.payuPaymentService = payuPaymentService;
this.orderProcessingService = orderProcessingService;
}
public ProcessPaymentResult ProcessPayment(ProcessPaymentRequest processPaymentRequest)
{
ProcessPaymentResult result = new ProcessPaymentResult ()
{
NewPaymentStatus = PaymentStatus.Pending
};
return result;
}
public void PostProcessPayment(PostProcessPaymentRequest postProcessPaymentRequest)
{
if (postProcessPaymentRequest == null)
throw new ArgumentException(nameof(postProcessPaymentRequest));
ValidatePaymentSettings();
var payuResponse = payuPaymentService.PlaceOrder(postProcessPaymentRequest.Order, webHelper.GetCurrentIpAddress(), GetStoreName(), new Uri(webHelper.GetStoreLocation()));
if (payuPaymentSettings.TransactMode == TransactMode.Authorize)
{
postProcessPaymentRequest.Order.AuthorizationTransactionId = payuResponse.OrderId;
postProcessPaymentRequest.Order.AuthorizationTransactionResult = payuResponse.Status.StatusCode;
orderProcessingService.MarkAsAuthorized(postProcessPaymentRequest.Order);
}
else if (payuPaymentSettings.TransactMode == TransactMode.AuthorizeAndCapture)
{
postProcessPaymentRequest.Order.CaptureTransactionId = payuResponse.OrderId;
postProcessPaymentRequest.Order.CaptureTransactionResult = payuResponse.Status.StatusCode;
orderProcessingService.MarkOrderAsPaid(postProcessPaymentRequest.Order);
}
if (!String.IsNullOrEmpty(payuResponse.RedirectUri))
{
HttpContext.Current.Response.Redirect(payuResponse.RedirectUri);
HttpContext.Current.Response.Flush();
}
else
{
throw new NopException("PayU service failed to produce redirect url.");
}
}
public decimal GetAdditionalHandlingFee(IList<ShoppingCartItem> cart) => payuPaymentSettings.AdditionalFee;
public CapturePaymentResult Capture(CapturePaymentRequest capturePaymentRequest)
{
var captureResponse = payuPaymentService.CapturePayment(capturePaymentRequest.Order);
if (!captureResponse.IsSuccess)
throw new NopException(String.Format("Capture payment failed with error: {0}", captureResponse.Status?.StatusDesc));
return new CapturePaymentResult()
{
NewPaymentStatus = PaymentStatus.Paid,
CaptureTransactionId = capturePaymentRequest.Order.AuthorizationTransactionId
};
}
public RefundPaymentResult Refund(RefundPaymentRequest refundPaymentRequest)
{
RefundPaymentResult result = new RefundPaymentResult();
var refundResult = payuPaymentService.RequestRefund(refundPaymentRequest.Order, refundPaymentRequest.AmountToRefund, refundPaymentRequest.IsPartialRefund);
if (refundResult.Success)
{
result.NewPaymentStatus = PaymentStatus.Refunded;
}
else
{
result.Errors.Add(refundResult.Status.StatusDescription);
}
return result;
}
public VoidPaymentResult Void(VoidPaymentRequest voidPaymentRequest)
{
VoidPaymentResult result = new VoidPaymentResult();
result.AddError("Void method not supported");
return result;
}
public ProcessPaymentResult ProcessRecurringPayment(ProcessPaymentRequest processPaymentRequest)
{
ProcessPaymentResult result = new ProcessPaymentResult();
result.AddError("Recurring payment not supported");
return result;
}
public CancelRecurringPaymentResult CancelRecurringPayment(CancelRecurringPaymentRequest cancelPaymentRequest)
{
CancelRecurringPaymentResult result = new CancelRecurringPaymentResult();
result.AddError("Recurring payment not supported");
return result;
}
public bool CanRePostProcessPayment(Order order)
{
if (order == null)
{
throw new ArgumentNullException("order");
}
return order.PaymentStatus == PaymentStatus.Pending && (DateTime.UtcNow - order.CreatedOnUtc).TotalMinutes >= 1.0;
}
public void GetConfigurationRoute(out string actionName, out string controllerName, out RouteValueDictionary routeValues)
{
actionName = "Configure";
controllerName = "PaymentPayu";
routeValues = new RouteValueDictionary
{
{
"Namespaces",
"Nop.Plugin.Payments.Payu.Controllers"
},
{
"area",
null
}
};
}
public void GetPaymentInfoRoute(out string actionName, out string controllerName, out RouteValueDictionary routeValues)
{
actionName = "PaymentInfo";
controllerName = "PaymentPayu";
routeValues = new RouteValueDictionary
{
{
"Namespaces",
"Nop.Plugin.Payments.Payu.Controllers"
},
{
"area",
null
}
};
}
public Type GetControllerType() => typeof(PaymentPayuController);
public override void Install()
{
PayuPaymentSettings settings = new PayuPaymentSettings
{
PosId = "",
OAuthClientSecret = "",
SecondKey = "",
BaseUrl = "",
OAuthClientId = ""
};
this.settingService.SaveSetting<PayuPaymentSettings>(settings, 0);
LocalizationExtensions.AddOrUpdatePluginLocaleResource(this, "Plugins.Payments.PayU.Instructions", @"
<p>
For plugin configuration follow these steps:<br />
<br />
1. You will need a PayU Merchant account. If you don't already have one, you can sign up here: <a href=""https://corporate.payu.com/"" target=""_blank"">https://corporate.payu.com/</a><br />
2. Copy Pos ID, OAuth Client Id, OAuth Client Secret, Second Key from your PayU store into corresponding field
3. Make sure that the code of the currency of your PayU account is the same as the active currency of yours store.
4. Fill in the remaining fields and save to complete the configuration.<br />
</p>");
LocalizationExtensions.AddOrUpdatePluginLocaleResource(this, "Plugins.Payments.Payu.RedirectionTip", "You will be redirected to Payu site to complete the order.");
LocalizationExtensions.AddOrUpdatePluginLocaleResource(this, "Plugins.Payments.Payu.PosId", "PoS ID");
LocalizationExtensions.AddOrUpdatePluginLocaleResource(this, "Plugins.Payments.Payu.PosId.Hint", "Enter PoS ID provided by PayU.");
LocalizationExtensions.AddOrUpdatePluginLocaleResource(this, "Plugins.Payments.Payu.OAuthClientId", "OAuth Client ID");
LocalizationExtensions.AddOrUpdatePluginLocaleResource(this, "Plugins.Payments.Payu.OAuthClientId.Hint", "Enter OAuth Client ID provided by PayU.");
LocalizationExtensions.AddOrUpdatePluginLocaleResource(this, "Plugins.Payments.Payu.SecondKey", "Second Key(MD5)");
LocalizationExtensions.AddOrUpdatePluginLocaleResource(this, "Plugins.Payments.Payu.SecondKey.Hint", "Enter Second Key(MD5) provided by PayU.");
LocalizationExtensions.AddOrUpdatePluginLocaleResource(this, "Plugins.Payments.Payu.BaseUrl", "PayU payment service base URL");
LocalizationExtensions.AddOrUpdatePluginLocaleResource(this, "Plugins.Payments.Payu.BaseUrl.Hint", "Enter PayU payment service base URL.");
LocalizationExtensions.AddOrUpdatePluginLocaleResource(this, "Plugins.Payments.Payu.OAuthClientSecret", "OAuth Client Secret");
LocalizationExtensions.AddOrUpdatePluginLocaleResource(this, "Plugins.Payments.Payu.OAuthClientSecret.Hint", "Enter OAuth Client Secret provided by PayU.");
LocalizationExtensions.AddOrUpdatePluginLocaleResource(this, "Plugins.Payments.Payu.AdditionalFee", "Additional Fee");
LocalizationExtensions.AddOrUpdatePluginLocaleResource(this, "Plugins.Payments.Payu.AdditionalFee.Hint", "Enter Additional.");
LocalizationExtensions.AddOrUpdatePluginLocaleResource(this, "Plugins.Payments.Payu.Currency", "Your shop currency from PayU");
LocalizationExtensions.AddOrUpdatePluginLocaleResource(this, "Plugins.Payments.Payu.Currency.Hint", "Enter currency code from your PayU account");
LocalizationExtensions.AddOrUpdatePluginLocaleResource(this, "Plugins.Payments.Payu.TransactionMode", "Transaction mode");
LocalizationExtensions.AddOrUpdatePluginLocaleResource(this, "Plugins.Payments.Payu.TransactionMode.Hint", "Select transaction mode");
base.Install();
}
public override void Uninstall()
{
LocalizationExtensions.DeletePluginLocaleResource(this, "Plugins.Payments.PayU.Instructions");
LocalizationExtensions.DeletePluginLocaleResource(this, "Plugins.Payments.Payu.RedirectionTip");
LocalizationExtensions.DeletePluginLocaleResource(this, "Plugins.Payments.Payu.PosId");
LocalizationExtensions.DeletePluginLocaleResource(this, "Plugins.Payments.Payu.PosId.Hint");
LocalizationExtensions.DeletePluginLocaleResource(this, "Plugins.Payments.Payu.OAuthClientId");
LocalizationExtensions.DeletePluginLocaleResource(this, "Plugins.Payments.Payu.OAuthClientId.Hint");
LocalizationExtensions.DeletePluginLocaleResource(this, "Plugins.Payments.Payu.SecondKey");
LocalizationExtensions.DeletePluginLocaleResource(this, "Plugins.Payments.Payu.SecondKey.Hint");
LocalizationExtensions.DeletePluginLocaleResource(this, "Plugins.Payments.Payu.BaseUrl");
LocalizationExtensions.DeletePluginLocaleResource(this, "Plugins.Payments.Payu.BaseUrl.Hint");
LocalizationExtensions.DeletePluginLocaleResource(this, "Plugins.Payments.Payu.OAuthClientSecret");
LocalizationExtensions.DeletePluginLocaleResource(this, "Plugins.Payments.Payu.OAuthClientSecret.Hint");
LocalizationExtensions.DeletePluginLocaleResource(this, "Plugins.Payments.Payu.AdditionalFee");
LocalizationExtensions.DeletePluginLocaleResource(this, "Plugins.Payments.Payu.AdditionalFee.Hint");
LocalizationExtensions.DeletePluginLocaleResource(this, "Plugins.Payments.Payu.Currency");
LocalizationExtensions.DeletePluginLocaleResource(this, "Plugins.Payments.Payu.Currency.Hint");
LocalizationExtensions.DeletePluginLocaleResource(this, "Plugins.Payments.Payu.TransactionMode");
LocalizationExtensions.DeletePluginLocaleResource(this, "Plugins.Payments.Payu.TransactionMode.Hint");
base.Uninstall();
}
private string GetStoreName()
{
var storeContext = EngineContext.Current.Resolve<IStoreContext>();
var storeName = storeContext.CurrentStore.Name;
return storeName;
}
private void ValidatePaymentSettings()
{
if (String.IsNullOrEmpty(payuPaymentSettings.Currency))
{
throw new ArgumentNullException("You must setup currency for PayU before use this payment method");
}
if (String.IsNullOrEmpty(payuPaymentSettings.BaseUrl))
{
throw new ArgumentNullException("You must setup base url for PayU before use this payment method");
}
if (String.IsNullOrEmpty(payuPaymentSettings.OAuthClientSecret))
{
throw new ArgumentNullException("You must setup oauth client secret before using this payment method");
}
if (String.IsNullOrEmpty(payuPaymentSettings.OAuthClientId))
{
throw new ArgumentNullException("You must setup oauth client id before using this payment method");
}
if (String.IsNullOrEmpty(payuPaymentSettings.PosId))
{
throw new ArgumentNullException("You must setup PoS ID before using this payment method");
}
if (String.IsNullOrEmpty(payuPaymentSettings.SecondKey))
{
throw new ArgumentNullException("You must setup second key(md5) before using this payment method");
}
}
}
}