Advertisement
Guest User

Untitled

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