Advertisement
Guest User

Untitled

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