-
Notifications
You must be signed in to change notification settings - Fork 0
/
PayooPaymentGateway.cs
252 lines (218 loc) · 11.2 KB
/
PayooPaymentGateway.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.Script.Serialization;
using EPiServer.Commerce.Order;
using EPiServer.Logging.Compatibility;
using EPiServer.ServiceLocation;
using EPiServer.Web;
using Foundation.Commerce.Payment.Payoo.Models;
using Mediachase.Commerce.Customers;
using Mediachase.Commerce.Plugins.Payment;
using RestSharp;
namespace Foundation.Commerce.Payment.Payoo
{
public class PayooPaymentGateway : AbstractPaymentGateway, IPaymentPlugin
{
private readonly ILog _logger = LogManager.GetLogger(typeof(PayooPaymentGateway));
private readonly IOrderRepository _orderRepository;
private readonly IOrderNumberGenerator _orderNumberGenerator;
private readonly IPayooCartService _payooCartService;
private readonly ICmsPaymentPropertyService _cmsPaymentPropertyService;
private PayooConfiguration _paymentMethodConfiguration;
public PayooPaymentGateway()
: this(
ServiceLocator.Current.GetInstance<IPayooCartService>(),
ServiceLocator.Current.GetInstance<IOrderNumberGenerator>(),
ServiceLocator.Current.GetInstance<IOrderRepository>(),
ServiceLocator.Current.GetInstance<ICmsPaymentPropertyService>())
{ }
public PayooPaymentGateway(
IPayooCartService payooCartService,
IOrderNumberGenerator orderNumberGenerator,
IOrderRepository orderRepository,
ICmsPaymentPropertyService cmsPaymentPropertyService)
{
_payooCartService = payooCartService;
_orderNumberGenerator = orderNumberGenerator;
_orderRepository = orderRepository;
_cmsPaymentPropertyService = cmsPaymentPropertyService;
_paymentMethodConfiguration = new PayooConfiguration(Settings);
}
/// <summary>
/// Main entry point of ECF Payment Gateway.
/// </summary>
/// <param name="payment">The payment to process</param>
/// <param name="message">The message.</param>
/// <returns>return false and set the message will make the WorkFlow activity raise PaymentExcetion(message)</returns>
public override bool ProcessPayment(Mediachase.Commerce.Orders.Payment payment, ref string message)
{
var orderGroup = payment.Parent.Parent;
var paymentProcessingResult = ProcessPayment(orderGroup, payment);
if (!string.IsNullOrEmpty(paymentProcessingResult.RedirectUrl))
{
HttpContext.Current.Response.Redirect(paymentProcessingResult.RedirectUrl);
}
message = paymentProcessingResult.Message;
return paymentProcessingResult.IsSuccessful;
}
/// <summary>
/// Processes the payment.
/// </summary>
/// <param name="orderGroup">The order group.</param>
/// <param name="payment">The payment.</param>
public PaymentProcessingResult ProcessPayment(IOrderGroup orderGroup, IPayment payment)
{
if (HttpContext.Current == null)
{
return PaymentProcessingResult.CreateSuccessfulResult("ProcessPaymentNullHttpContext");
}
if (payment == null)
{
return PaymentProcessingResult.CreateUnsuccessfulResult("PaymentNotSpecified");
}
var orderForm = orderGroup.Forms.FirstOrDefault(f => f.Payments.Contains(payment));
if (orderForm == null)
{
return PaymentProcessingResult.CreateUnsuccessfulResult("PaymentNotAssociatedOrderForm");
}
_paymentMethodConfiguration = new PayooConfiguration(Settings);
var payooOrder = CreatePayooOrder(orderGroup, payment);
var checksum = CreatePayooChecksum(payooOrder);
//Call Payoo gateway here
var message = string.Empty;
var response = ExecuteCreatePayooPreOrderRequest(payooOrder, checksum, out message);
if (response == null || !response.IsSuccess)
{
return PaymentProcessingResult.CreateUnsuccessfulResult(message);
}
UpdatePaymentPropertiesFromPayooResponse(orderGroup, payment, response);
var redirectUrl = response.order.payment_url;
message = $"---Payoo CreatePreOrder is successful. Redirect end user to {redirectUrl}";
return PaymentProcessingResult.CreateSuccessfulResult(message, redirectUrl);
}
/// <summary>
/// Processes the successful transaction, was called when Payoo Gateway redirect back.
/// </summary>
/// <param name="orderGroup">The order group that was processed.</param>
/// <param name="payment">The order payment.</param>
/// <param name="acceptUrl">The redirect url when finished.</param>
/// <param name="cancelUrl">The redirect url when error happens.</param>
/// <returns>The url redirection after process.</returns>
public string ProcessSuccessfulTransaction(IOrderGroup orderGroup, IPayment payment, string acceptUrl, string cancelUrl)
{
if (HttpContext.Current == null)
{
return cancelUrl;
}
var cart = orderGroup as ICart;
if (cart == null)
{
// return to the shopping cart page immediately and show error messages
return ProcessUnsuccessfulTransaction(cancelUrl, "Commit Tran Error Cart Null");
}
// everything is fine
var errorMessages = new List<string>();
var cartCompleted = _payooCartService.DoCompletingCart(cart, errorMessages);
if (!cartCompleted)
{
return UriUtil.AddQueryString(cancelUrl, "message", string.Join(";", errorMessages.Distinct().ToArray()));
}
// Place order
var purchaseOrder = _payooCartService.MakePurchaseOrder(cart, payment);
var redirectionUrl = CreateRedirectionUrl(purchaseOrder, acceptUrl);
_logger.Info($"Payoo transaction succeeds, redirect end user to {redirectionUrl}");
return redirectionUrl;
}
/// <summary>
/// Processes the unsuccessful transaction
/// </summary>
/// <param name="cancelUrl">The cancel url.</param>
/// <param name="errorMessage">The error message.</param>
/// <returns>The url redirection after process.</returns>
public string ProcessUnsuccessfulTransaction(string cancelUrl, string errorMessage)
{
if (HttpContext.Current == null)
{
return cancelUrl;
}
_logger.Error($"Payoo transaction failed [{errorMessage}].");
return UriUtil.AddQueryString(cancelUrl, "message", HttpUtility.UrlEncode(errorMessage));
}
private PayooOrder CreatePayooOrder(IOrderGroup orderGroup, IPayment payment)
{
var orderNumberID = _orderNumberGenerator.GenerateOrderNumber(orderGroup);
var order = new PayooOrder();
order.Session = orderNumberID;
order.BusinessUsername = _paymentMethodConfiguration.BusinessUsername;
order.OrderCashAmount = (long)payment.Amount;
order.OrderNo = orderNumberID;
order.ShippingDays = 1;
order.ShopBackUrl = $"{HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority)}{_cmsPaymentPropertyService.GetPayooPaymentProcessingPage()}";
order.ShopDomain = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority);
order.ShopID = long.Parse(_paymentMethodConfiguration.ShopID);
order.ShopTitle = _paymentMethodConfiguration.ShopTitle;
order.StartShippingDate = DateTime.Now.ToString("dd/MM/yyyy");
order.NotifyUrl = string.Empty;
order.ValidityTime = DateTime.Now.AddDays(1).ToString("yyyyMMddHHmmss");
var customer = CustomerContext.Current.GetContactById(orderGroup.CustomerId);
order.CustomerName = customer.FullName ?? $"{customer.FirstName} {customer.MiddleName} {customer.LastName}";
order.CustomerPhone = string.Empty;
order.CustomerEmail = customer.Email;
order.CustomerAddress = customer.PreferredShippingAddress?.Line1;
order.CustomerCity = customer.PreferredShippingAddress?.City;
order.OrderDescription = HttpUtility.UrlEncode(OrderDescriptionHTMLFactory.CreateOrderDescription(orderGroup));
order.Xml = PaymentXMLFactory.GetPaymentXML(order);
return order;
}
private string CreatePayooChecksum(PayooOrder payooOrder)
{
var checksumKey = _paymentMethodConfiguration.ChecksumKey;
return Utilities.EncryptSHA512($"{checksumKey}{payooOrder.Xml}");
}
private CreateOrderResponse ExecuteCreatePayooPreOrderRequest(PayooOrder payooOrder, string checksum, out string message)
{
try
{
var client = new RestClient(_paymentMethodConfiguration.ApiPayooCheckout);
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "multipart/form-data");
request.AddParameter("data", payooOrder.Xml);
request.AddParameter("checksum", checksum);
request.AddParameter("refer", payooOrder.ShopDomain);
IRestResponse response = client.Execute(request);
message = $"Excute request to create Payoo Preorder with status {response.StatusCode}";
if (response.StatusCode == HttpStatusCode.OK)
{
JavaScriptSerializer objJson = new JavaScriptSerializer();
return objJson.Deserialize<CreateOrderResponse>(response.Content);
}
return null;
}
catch (Exception e)
{
message = e.Message;
_logger.Error(e);
return null;
}
}
private void UpdatePaymentPropertiesFromPayooResponse(IOrderGroup orderGroup, IPayment payment, CreateOrderResponse response)
{
payment.Properties[Constant.PayooOrderIdPropertyName] = response.order.order_id;
payment.Properties[Constant.PayooOrderNumberPropertyName] = response.order.order_no;
payment.Properties[Constant.PayooAmountPropertyName] = response.order.amount;
payment.Properties[Constant.PayooExpiryDatePropertyName] = response.order.expiry_date;
payment.Properties[Constant.PayooPaymentCodePropertyName] = response.order.payment_code;
_orderRepository.Save(orderGroup);
}
private string CreateRedirectionUrl(IPurchaseOrder purchaseOrder, string acceptUrl)
{
var redirectionUrl = UriUtil.AddQueryString(acceptUrl, "success", "true");
redirectionUrl = UriUtil.AddQueryString(redirectionUrl, "contactId", purchaseOrder.CustomerId.ToString());
redirectionUrl = UriUtil.AddQueryString(redirectionUrl, "orderNumber", purchaseOrder.OrderLink.OrderGroupId.ToString());
return redirectionUrl;
}
}
}