-
Notifications
You must be signed in to change notification settings - Fork 15
/
Payment.ascx.cs
312 lines (252 loc) · 12.8 KB
/
Payment.ascx.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
// --- Copyright (c) notice NevoWeb ---
// Copyright (c) 2014 SARL NevoWeb. www.nevoweb.com. The MIT License (MIT).
// Author: D.C.Lee
// ------------------------------------------------------------------------
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
// ------------------------------------------------------------------------
// This copyright notice may NOT be removed, obscured or modified without written consent from the author.
// --- End copyright notice ---
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Web.UI.WebControls;
using DotNetNuke.Common;
using DotNetNuke.Entities.Portals;
using NBrightCore.common;
using NBrightCore.render;
using NBrightDNN;
using Nevoweb.DNN.NBrightBuy.Base;
using Nevoweb.DNN.NBrightBuy.Components;
using Nevoweb.DNN.NBrightBuy.Components.Interfaces;
using DataProvider = DotNetNuke.Data.DataProvider;
namespace Nevoweb.DNN.NBrightBuy
{
/// -----------------------------------------------------------------------------
/// <summary>
/// The ViewNBrightGen class displays the content
/// </summary>
/// -----------------------------------------------------------------------------
public partial class Payment : NBrightBuyFrontOfficeBase
{
private GenXmlTemplate _templateHeader;//this is used to pickup the meta data on page load.
private CartData _cartInfo;
private Dictionary<String, NBrightInfo> _provList;
private OrderData _orderData;
private PaymentsInterface _prov;
#region Event Handlers
override protected void OnInit(EventArgs e)
{
base.OnInit(e);
if (ModSettings.Get("themefolder") == "") // if we don't have module setting jump out
{
rpPaymentGateways.ItemTemplate = new GenXmlTemplate("NO MODULE SETTINGS");
return;
}
try
{
var pluginData = new PluginData(PortalSettings.Current.PortalId);
_provList = pluginData.GetPaymentProviders();
_cartInfo = new CartData(PortalId);
var orderid = Utils.RequestQueryStringParam(Context, "orderid");
var templOk = ModSettings.Get("paymentoktemplate");
var templFail = ModSettings.Get("paymentfailtemplate");
var templHeader = "";
var templFooter = "";
var templText = "";
if (_provList.Count == 0 && ModSettings.Get("orderingsystem") == "True")
{
#region "No Payment providers, so process as a ordering system"
var displayTempl = templOk;
if (!_cartInfo.IsValidated()) displayTempl = templFail;
rpDetailDisplay.ItemTemplate = NBrightBuyUtils.GetGenXmlTemplate(ModCtrl.GetTemplateData(ModSettings, displayTempl, Utils.GetCurrentCulture(), DebugMode), ModSettings.Settings(), PortalSettings.HomeDirectory);
_templateHeader = (GenXmlTemplate) rpDetailDisplay.ItemTemplate;
// we may have voucher discounts that give a zero appliedtotal, so process.
var discountprov = DiscountCodeInterface.Instance();
if (discountprov != null)
{
discountprov.UpdatePercentUsage(PortalId, UserId, _cartInfo.PurchaseInfo);
discountprov.UpdateVoucherAmount(PortalId, UserId, _cartInfo.PurchaseInfo);
}
#endregion
}
else
{
#region "Payment Details"
// display the payment method by default
templHeader = ModSettings.Get("paymentordersummary");
templFooter = ModSettings.Get("paymentfooter");
var templPaymentText = "";
var msg = "";
if (Utils.IsNumeric(orderid))
{
// orderid exists, so must be return from bank; Process it!!
_orderData = new OrderData(PortalId, Convert.ToInt32(orderid));
_prov = PaymentsInterface.Instance(_orderData.PaymentProviderKey);
msg = _prov.ProcessPaymentReturn(Context);
if (msg == "") // no message so successful
{
_orderData = new OrderData(PortalId, Convert.ToInt32(orderid)); // get the updated order.
_orderData.PaymentOk("050");
templText = templOk;
}
else
{
_orderData = new OrderData(PortalId, Convert.ToInt32(orderid)); // reload the order, becuase the status and typecode may have changed by the payment provider.
_orderData.AddAuditMessage(msg, "paymsg", "payment.ascx", "False");
_orderData.Save();
templText = templFail;
}
templFooter = ""; // return from bank, hide footer
}
else
{
// not returning from bank, so display list of payment providers.
rpPaymentGateways.ItemTemplate = NBrightBuyUtils.GetGenXmlTemplate(GetPaymentProviderTemplates(), ModSettings.Settings(), PortalSettings.HomeDirectory);
}
if (templText == "") templText = templHeader; // if we are NOT returning from bank, then display normal header summary template
templPaymentText = ModCtrl.GetTemplateData(ModSettings, templText, Utils.GetCurrentCulture(), DebugMode);
rpDetailDisplay.ItemTemplate = NBrightBuyUtils.GetGenXmlTemplate(templPaymentText, ModSettings.Settings(), PortalSettings.HomeDirectory);
_templateHeader = (GenXmlTemplate) rpDetailDisplay.ItemTemplate;
if (templFooter != "")
{
var templPaymentFooterText = ModCtrl.GetTemplateData(ModSettings, templFooter, Utils.GetCurrentCulture(), DebugMode);
rpDetailFooter.ItemTemplate = NBrightBuyUtils.GetGenXmlTemplate(templPaymentFooterText, ModSettings.Settings(), PortalSettings.HomeDirectory);
}
#endregion
}
// insert page header text
NBrightBuyUtils.IncludePageHeaders(ModCtrl, ModuleId, Page, _templateHeader, ModSettings.Settings(), null, DebugMode);
}
catch (Exception exc)
{
//display the error on the template (don;t want to log it here, prefer to deal with errors directly.)
var l = new Literal();
l.Text = exc.ToString();
phData.Controls.Add(l);
}
}
protected override void OnLoad(EventArgs e)
{
try
{
base.OnLoad(e);
if (Page.IsPostBack == false)
{
PageLoad();
}
}
catch (Exception exc) //Module failed to load
{
//display the error on the template (don;t want to log it here, prefer to deal with errors directly.)
var l = new Literal();
l.Text = exc.ToString();
phData.Controls.Add(l);
}
}
private void PageLoad()
{
if (UserId > 0 && _cartInfo.UserId == -1) // user may have just logged in.
{
_cartInfo.UserId = UserId;
_cartInfo.Save();
}
var orderid = Utils.RequestQueryStringParam(Context, "orderid");
if (_provList.Count == 0 && ModSettings.Get("orderingsystem") == "True")
{
#region "No Payment providers, so process as a ordering system"
if (_cartInfo != null && _cartInfo.IsValidated())
{
_cartInfo.SaveModelTransQty(); // move qty into trans
_cartInfo.ConvertToOrder(DebugMode);
_cartInfo.ApplyModelTransQty();
// Send emails
NBrightBuyUtils.SendOrderEmail("OrderCreatedClient", _cartInfo.PurchaseInfo.ItemID, "ordercreatedemailsubject");
// update status to completed
_orderData = new OrderData(PortalId, _cartInfo.PurchaseInfo.ItemID);
_orderData.CreatedDate = DateTime.Now.ToString("O");
_orderData.SavePurchaseData();
var cartL = new List<NBrightInfo>();
cartL.Add(_cartInfo.GetInfo());
// display payment OK for order
rpDetailDisplay.DataSource = cartL;
rpDetailDisplay.DataBind();
}
#endregion
}
else
{
#region "Payment Details"
if (Utils.IsNumeric(orderid))
{
// orderid exists, so must be return from bank; Process it!!
_orderData = new OrderData(PortalId, Convert.ToInt32(orderid));
DoDetail(rpDetailDisplay, _orderData.PurchaseInfo);
}
else
{
// display return page
DoDetail(rpDetailDisplay, _cartInfo.PurchaseInfo);
DoDetail(rpPaymentGateways, _cartInfo.PurchaseInfo);
DoDetail(rpDetailFooter, _cartInfo.PurchaseInfo);
}
#endregion
}
}
#endregion
#region "Events "
protected void CtrlItemCommand(object source, RepeaterCommandEventArgs e)
{
var cArg = e.CommandArgument.ToString();
var param = new string[3];
if (Utils.RequestParam(Context, "eid") != "") param[0] = "eid=" + Utils.RequestParam(Context, "eid");
switch (e.CommandName.ToLower())
{
case "pay":
if (_cartInfo != null)
{
_cartInfo.SaveModelTransQty(); // move qty into trans
_cartInfo.ConvertToOrder(DebugMode);
var orderData = new OrderData(_cartInfo.PurchaseInfo.ItemID);
orderData.payselectionXml = GenXmlFunctions.GetGenXml(rpPaymentGateways, "", "");
orderData.PaymentProviderKey = cArg.ToLower(); // provider keys should always be lowecase
orderData.SavePurchaseData();
var redirecturl = PaymentsInterface.Instance(orderData.PaymentProviderKey).RedirectForPayment(orderData);
if (redirecturl != "") Response.Redirect(redirecturl, true);
}
Response.Redirect(Globals.NavigateURL(TabId, "", param), true);
break;
}
}
#endregion
#region "private methods"
private String GetPaymentProviderTemplates()
{
var strRtn = "";
foreach (var d in _provList)
{
var p = d.Value;
var key = p.GetXmlProperty("genxml/textbox/ctrl");
var prov = PaymentsInterface.Instance(key);
if (prov != null)
{
var templ = prov.GetTemplate(_cartInfo.PurchaseInfo);
if (templ == "")
{
var msgcode = "noproviderdata_" + NotifyCode.warning.ToString();
templ = "<div>" + key + "</div>";
templ += NBrightBuyUtils.GetResxMessage(msgcode);
}
strRtn += templ;
}
}
return strRtn;
}
#endregion
}
}