Guest User

Untitled

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