Advertisement
Guest User

Untitled

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