-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProviderUtils.cs
165 lines (143 loc) · 6.83 KB
/
ProviderUtils.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
using DotNetNuke.Entities.Portals;
using NBrightCore.common;
using NBrightDNN;
using Nevoweb.DNN.NBrightBuy.Components;
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
namespace Nevoweb.DNN.NBrightBuyPayPal
{
public class ProviderUtils
{
public static String GetTemplateData(String templatename)
{
var controlMapPath = HttpContext.Current.Server.MapPath("/DesktopModules/NBright/NBrightPayPal");
var templCtrl = new NBrightCore.TemplateEngine.TemplateGetter(PortalSettings.Current.HomeDirectoryMapPath, controlMapPath, "Themes\\config", "");
var templ = templCtrl.GetTemplateData(templatename, Utils.GetCurrentCulture());
templ = Utils.ReplaceSettingTokens(templ, StoreSettings.Current.Settings());
templ = Utils.ReplaceUrlTokens(templ);
return templ;
}
public static NBrightInfo GetProviderSettings(String ctrlkey)
{
var info = (NBrightInfo)Utils.GetCache("NBrightPayPalPaymentProvider" + PortalSettings.Current.PortalId.ToString(""));
if (info == null)
{
var modCtrl = new NBrightBuyController();
info = modCtrl.GetByGuidKey(PortalSettings.Current.PortalId, -1, "NBrightPayPalPAYMENT", ctrlkey);
if (info == null)
{
info = new NBrightInfo(true);
info.GUIDKey = ctrlkey;
info.TypeCode = "NBrightPayPalPAYMENT";
info.ModuleId = -1;
info.PortalId = PortalSettings.Current.PortalId;
}
Utils.SetCache("NBrightPayPalPaymentProvider" + PortalSettings.Current.PortalId.ToString(""), info);
}
return info;
}
public static String GetBankRemotePost(OrderData orderData)
{
// use this class to build up the post html
var rPost = new RemotePost();
// get the gateway settings which have been entered into the back office page (settings.html template)
var settings = ProviderUtils.GetProviderSettings("NBrightPayPalpayment");
// get the order data
var payData = new PayData(orderData);
rPost.Url = payData.PostUrl;
rPost.Add("cmd", "_xclick");
rPost.Add("item_number", payData.ItemId);
rPost.Add("return", payData.ReturnUrl);
rPost.Add("currency_code", payData.CurrencyCode);
rPost.Add("cancel_return", payData.ReturnCancelUrl);
rPost.Add("notify_url", payData.NotifyUrl);
rPost.Add("custom", Utils.GetCurrentCulture());
rPost.Add("business", payData.PayPalId);
rPost.Add("item_name", orderData.PurchaseInfo.GetXmlProperty("genxml/ordernumber"));
rPost.Add("amount", payData.Amount);
rPost.Add("shipping", payData.ShippingAmount);
rPost.Add("tax", payData.TaxAmount);
rPost.Add("lc", Utils.GetCurrentCulture().Substring(3, 2));
var extrafields = settings.GetXmlProperty("genxml/textbox/extrafields");
var fields = extrafields.Split(',');
foreach (var f in fields)
{
var ary = f.Split('=');
if (ary.Count() == 2)
{
var n = ary[0];
var v = ary[1];
var d = orderData.PurchaseInfo.GetXmlProperty(v);
rPost.Add(n, d);
}
}
//Build the re-direct html
var rtnStr = rPost.GetPostHtml("/DesktopModules/NBright/NBrightPayPal/Themes/config/img/paypal.gif");
if (settings.GetXmlPropertyBool("genxml/checkbox/debug.mode"))
{
File.WriteAllText(PortalSettings.Current.HomeDirectoryMapPath + "\\debug_NBrightPayPalpost.html", rtnStr);
}
return rtnStr;
}
public static bool VerifyPayment(PayPalIpnParameters ipn, string verifyURL)
{
try
{
bool isVerified = false;
if (ipn.IsValid)
{
System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Tls12;
HttpWebRequest PPrequest = (HttpWebRequest)WebRequest.Create(verifyURL);
if ((PPrequest != null))
{
PPrequest.Method = "POST";
PPrequest.ContentLength = ipn.PostString.Length;
PPrequest.ContentType = "application/x-www-form-urlencoded";
StreamWriter writer = new StreamWriter(PPrequest.GetRequestStream());
writer.Write(ipn.PostString);
writer.Close();
HttpWebResponse response = (HttpWebResponse)PPrequest.GetResponse();
if ((response != null))
{
StreamReader reader = new StreamReader(response.GetResponseStream());
string responseString = reader.ReadToEnd();
reader.Close();
if (string.Compare(responseString, "VERIFIED", true) == 0)
{
isVerified = true;
}
}
}
}
return isVerified;
}
catch (Exception ex)
{
return false;
}
}
private string PayPalEncode(string value)
{
//a single accentuated/special character matches a single non acc/spec character:
value = StringListReplace(value, "ŠŽŸÀÁÂÃÅÇÈÉÊËÌÍÎÏÐÑÒÓÔÕØÙÚÛÝÞØ", "SZYAAAAACEEEEIIIIDNOOOOOUUUYPO");
value = StringListReplace(value, "šžÿàáâãåçèéêëìíîïðñòóôõøùúûýþµ", "szyaaaaaceeeeiiiidnooooouuuypu");
//a single accentuated/special character matches a couple of non acc/spec character:
value = value.Replace("Œ", "OE");
value = value.Replace("Æ", "AE");
value = value.Replace("œ", "oe");
value = value.Replace("æ", "ae");
return HttpUtility.UrlEncode(value);
}
private string StringListReplace(string value, string searchfor, string replacewith)
{
for (var x = 1; x <= searchfor.Length; x++)
{
value = value.Replace(searchfor.Substring(x - 1, 1), replacewith.Substring(x - 1, 1));
}
return value;
}
}
}