Advertisement
Guest User

Untitled

a guest
Apr 18th, 2016
498
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.25 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Specialized; using System.IO;
  4. using System.Net; using System.Text; using System.Data;
  5. using System.Configuration; using System.Web;
  6. using WingtipToys;
  7. using WingtipToys.Models;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10.  
  11. public class NVPAPICaller
  12. {
  13. //Flag that determines the PayPal environment (live or sandbox)
  14. private const bool bSandbox = true;
  15. private const string CVV2 = "CVV2";
  16.  
  17. // Live strings.
  18. private string pEndPointURL = "https://api-3t.paypal.com/nvp";
  19. private string host = "www.paypal.com";
  20.  
  21. // Sandbox strings.
  22. private string pEndPointURL_SB = "https://api-3t.sandbox.paypal.com/nvp";
  23. private string host_SB = "www.sandbox.paypal.com";
  24.  
  25. private const string SIGNATURE = "SIGNATURE";
  26. private const string PWD = "PWD";
  27. private const string ACCT = "ACCT";
  28.  
  29. //Replace <Your API Username> with your API Username
  30. //Replace <Your API Password> with your API Password
  31. //Replace <Your Signature> with your Signature public string APIUsername = "<Your API Username>"; private string APIPassword = "<Your API Password>"; private string APISignature = "<Your Signature>"; private string Subject = "";
  32.  
  33. public string APIUsername = "nathaniel.fischer - facilitator_api1.southeasttech.edu";
  34. private string APIPassword = "UBCNLZM7EVN28BM4";
  35. private string Subject = "";
  36. private string APISignature = "AFcWxV21C7fd0v3bYYYRCpSSRl31A4WZlOsS1Z0P5fOHXQ4a4uNPAHMS";
  37. private string BNCode = "PP-ECWizard";
  38.  
  39. //HttpWebRequest Timeout specified in milliseconds
  40.  
  41. private const int Timeout = 15000;
  42. private static readonly string[] SECURED_NVPS = new string[] { ACCT, CVV2, SIGNATURE, PWD };
  43.  
  44. public void SetCredentials(string Userid, string Pwd, string Signature)
  45. {
  46. APIUsername = Userid;
  47. APIPassword = Pwd;
  48. APISignature = Signature;
  49. }
  50.  
  51. public bool ShortcutExpressCheckout(string amt, ref string token, ref string retMsg)
  52. {
  53. if (bSandbox)
  54. {
  55. pEndPointURL = pEndPointURL_SB;
  56. host = host_SB;
  57. }
  58.  
  59. string returnURL = "http://localhost:34368/Checkout/CheckoutReview.aspx";
  60. string cancelURL = "http://localhost:34368/Checkout/CheckoutCancel.aspx";
  61.  
  62. NVPCodec encoder = new NVPCodec();
  63. encoder["METHOD"] = "SetExpressCheckout";
  64. encoder["RETURNURL"] = returnURL;
  65. encoder["CANCELURL"] = cancelURL;
  66. encoder["BRANDNAME"] = "Wingtip Toys Sample Application";
  67. encoder["PAYMENTREQUEST_0_AMT"] = amt;
  68. encoder["PAYMENTREQUEST_0_ITEMAMT"] = amt;
  69. encoder["PAYMENTREQUEST_0_PAYMENTACTION"] = "Sale";
  70. encoder["PAYMENTREQUEST_0_CURRENCYCODE"] = "USD";
  71.  
  72. // Get the Shopping Cart Products
  73. using (WingtipToys.Logic.ShoppingCartActions myCartOrders = new WingtipToys.Logic.ShoppingCartActions())
  74. {
  75. List<CartItem> myOrderList = myCartOrders.GetCartItems();
  76.  
  77. for (int i = 0; i < myOrderList.Count; i++)
  78. {
  79. encoder["L_PAYMENTREQUEST_0_NAME" + i] = myOrderList[i].Product.ProductName.ToString();
  80. encoder["L_PAYMENTREQUEST_0_AMT" + i] = myOrderList[i].Product.UnitPrice.ToString();
  81. encoder["L_PAYMENTREQUEST_0_QTY" + i] = myOrderList[i].Quantity.ToString();
  82. }
  83. }
  84.  
  85. string pStrrequestforNvp = encoder.Encode();
  86. string pStresponsenvp = HttpCall(pStrrequestforNvp);
  87.  
  88. NVPCodec decoder = new NVPCodec(); decoder.Decode(pStresponsenvp);
  89.  
  90. string strAck = decoder["ACK"].ToLower();
  91. if (strAck != null && (strAck == "success" || strAck == "successwithwarning"))
  92. {
  93. token = decoder["TOKEN"];
  94. string ECURL = "https://" + host + "/cgi-bin/webscr?cmd=_express- checkout" + "&token=" + token;
  95. retMsg = ECURL; return true;
  96. }
  97. else
  98. {
  99. retMsg = "ErrorCode=" + decoder["L_ERRORCODE0"] + "&" + "Desc=" + decoder["L_SHORTMESSAGE0"] + "&" + "Desc2=" + decoder["L_LONGMESSAGE0"];
  100. return false;
  101. }
  102. }
  103.  
  104. public bool GetCheckoutDetails(string token, ref string PayerID, ref NVPCodec decoder, ref string retMsg)
  105. {
  106. if (bSandbox)
  107. {
  108. pEndPointURL = pEndPointURL_SB;
  109. }
  110.  
  111. NVPCodec encoder = new NVPCodec(); encoder["METHOD"] = "GetExpressCheckoutDetails"; encoder["TOKEN"] = token;
  112.  
  113. string pStrrequestforNvp = encoder.Encode();
  114. string pStresponsenvp = HttpCall(pStrrequestforNvp);
  115.  
  116. decoder = new NVPCodec(); decoder.Decode(pStresponsenvp);
  117.  
  118. string strAck = decoder["ACK"].ToLower();
  119. if (strAck != null && (strAck == "success" || strAck == "successwithwarning"))
  120. {
  121. PayerID = decoder["PAYERID"]; return true;
  122. }
  123. else
  124. {
  125. retMsg = "ErrorCode=" + decoder["L_ERRORCODE0"] + "&" + "Desc=" + decoder["L_SHORTMESSAGE0"] + "&" + "Desc2=" + decoder["L_LONGMESSAGE0"];
  126.  
  127. return false;
  128. }
  129. }
  130.  
  131. public bool DoCheckoutPayment(string finalPaymentAmount, string token, string PayerID, ref NVPCodec decoder, ref string retMsg)
  132. {
  133. if (bSandbox)
  134. {
  135. pEndPointURL = pEndPointURL_SB;
  136. }
  137.  
  138. NVPCodec encoder = new NVPCodec(); encoder["METHOD"] = "DoExpressCheckoutPayment"; encoder["TOKEN"] = token;
  139. encoder["PAYERID"] = PayerID; encoder["PAYMENTREQUEST_0_AMT"] = finalPaymentAmount; encoder["PAYMENTREQUEST_0_CURRENCYCODE"] = "USD"; encoder["PAYMENTREQUEST_0_PAYMENTACTION"] = "Sale";
  140.  
  141. string pStrrequestforNvp = encoder.Encode();
  142. string pStresponsenvp = HttpCall(pStrrequestforNvp);
  143.  
  144. decoder = new NVPCodec(); decoder.Decode(pStresponsenvp);
  145.  
  146. string strAck = decoder["ACK"].ToLower();
  147. if (strAck != null && (strAck == "success" || strAck == "successwithwarning"))
  148. {
  149. return true;
  150. }
  151. else
  152. {
  153. retMsg = "ErrorCode=" + decoder["L_ERRORCODE0"] + "&" + "Desc=" + decoder["L_SHORTMESSAGE0"] + "&" + "Desc2=" + decoder["L_LONGMESSAGE0"];
  154.  
  155. return false;
  156. }
  157. }
  158.  
  159. public string HttpCall(string NvpRequest)
  160. {
  161. string url = pEndPointURL;
  162.  
  163. string strPost = NvpRequest + "&" + buildCredentialsNVPString(); strPost = strPost + "&BUTTONSOURCE=" + HttpUtility.UrlEncode(BNCode);
  164.  
  165. HttpWebRequest objRequest = (HttpWebRequest)WebRequest.Create(url); objRequest.Timeout = Timeout;
  166. objRequest.Method = "POST"; objRequest.ContentLength = strPost.Length;
  167.  
  168. try
  169. {
  170. using (StreamWriter myWriter = new StreamWriter(objRequest.GetRequestStream()))
  171. {
  172. myWriter.Write(strPost);
  173. }
  174. }
  175. catch (Exception e)
  176. {
  177. // Log the exception.
  178. WingtipToys.Logic.ExceptionUtility.LogException(e, "HttpCall in PayPalFunction.cs");
  179. }
  180.  
  181. //Retrieve the Response returned from the NVP API call to PayPal.
  182. HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse(); string result;
  183. using (StreamReader sr = new StreamReader(objResponse.GetResponseStream()))
  184. {
  185. result = sr.ReadToEnd();
  186. }
  187.  
  188. return result;
  189. }
  190.  
  191. private string buildCredentialsNVPString()
  192. {
  193. NVPCodec codec = new NVPCodec();
  194.  
  195. if (!IsEmpty(APIUsername)) codec["USER"] = APIUsername;
  196.  
  197. if (!IsEmpty(APIPassword)) codec[PWD] = APIPassword;
  198.  
  199. if (!IsEmpty(APISignature)) codec[SIGNATURE] = APISignature;
  200.  
  201. if (!IsEmpty(Subject)) codec["SUBJECT"] = Subject;
  202.  
  203. codec["VERSION"] = "88.0";
  204.  
  205. return codec.Encode();
  206. }
  207.  
  208. public static bool IsEmpty(string s)
  209. {
  210. return s == null || s.Trim() == string.Empty;
  211. }
  212. }
  213.  
  214. public sealed class NVPCodec : NameValueCollection
  215. {
  216. private const string AMPERSAND = "&"; private const string EQUALS = "=";
  217. private static readonly char[] AMPERSAND_CHAR_ARRAY = AMPERSAND.ToCharArray();
  218. private static readonly char[] EQUALS_CHAR_ARRAY = EQUALS.ToCharArray();
  219.  
  220. public string Encode()
  221. {
  222. StringBuilder sb = new StringBuilder(); bool firstPair = true;
  223. foreach (string kv in AllKeys)
  224. {
  225. string name = HttpUtility.UrlEncode(kv);
  226. string value = HttpUtility.UrlEncode(this[kv]); if (!firstPair)
  227. {
  228. sb.Append(AMPERSAND);
  229. }
  230. sb.Append(name).Append(EQUALS).Append(value); firstPair = false;
  231. }
  232. return sb.ToString();
  233. }
  234.  
  235. public void Decode(string nvpstring)
  236. {
  237. Clear();
  238. foreach (string nvp in nvpstring.Split(AMPERSAND_CHAR_ARRAY))
  239. {
  240. string[] tokens = nvp.Split(EQUALS_CHAR_ARRAY); if (tokens.Length >= 2)
  241. {
  242. string name = HttpUtility.UrlDecode(tokens[0]); string value = HttpUtility.UrlDecode(tokens[1]); Add(name, value);
  243. }
  244. }
  245. }
  246.  
  247. public void Add(string name, string value, int index)
  248. {
  249. this.Add(GetArrayName(index, name), value);
  250. }
  251.  
  252. public void Remove(string arrayName, int index)
  253. {
  254. this.Remove(GetArrayName(index, arrayName));
  255. }
  256.  
  257. public string this[string name, int index]
  258. {
  259. get
  260. {
  261. return this[GetArrayName(index, name)];
  262. }
  263. set
  264. {
  265. this[GetArrayName(index, name)] = value;
  266. }
  267. }
  268.  
  269. private static string GetArrayName(int index, string name)
  270. {
  271. if (index < 0)
  272. {
  273. throw new ArgumentOutOfRangeException("index", "index cannot be negative: " + index);
  274. }
  275. return name + index;
  276. }
  277. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement