Guest User

Untitled

a guest
Sep 25th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. public static class MyGlobalVariables
  2. {
  3. public static int TokenChoice
  4. {
  5. get
  6. {
  7. if (System.Web.HttpContext.Current.Session["TokenChoice"] == null)
  8. {
  9. return -1;
  10. }
  11. else
  12. {
  13. return (int)System.Web.HttpContext.Current.Session["TokenChoice"];
  14. }
  15. }
  16. set
  17. {
  18. System.Web.HttpContext.Current.Session["TokenChoice"] = value;
  19. }
  20. }
  21. public static int PaymentChoice
  22. {
  23. get
  24. {
  25. if (System.Web.HttpContext.Current.Session["PaymentChoice"] == null)
  26. {
  27. return -1;
  28. }
  29. else
  30. {
  31. return (int)System.Web.HttpContext.Current.Session["PaymentChoice"];
  32. }
  33. }
  34. set
  35. {
  36. System.Web.HttpContext.Current.Session["PaymentChoice"] = value;
  37. }
  38. }
  39. public static string CurrentUser
  40. {
  41. get
  42. {
  43. System.Web.HttpContext.Current.Session["CurrentUser"] = System.Web.HttpContext.Current.User.Identity.Name;
  44. return (string)System.Web.HttpContext.Current.Session["CurrentUser"];
  45. }
  46. }
  47.  
  48. }
  49.  
  50. public ActionResult Payment(int tokenChoice, int paymentChoice)
  51. {
  52. ViewData["_CurrentUser"] =
  53. System.Web.HttpContext.Current.Session["_CurrentUser"] = User.Identity.Name;
  54. ViewBag.Payment = paymentChoice;
  55. MyGlobalVariables.PaymentChoice = paymentChoice;
  56. MyGlobalVariables.TokenChoice = tokenChoice;
  57.  
  58. return View();
  59. }
  60.  
  61. [HttpPost]
  62. public ActionResult Payment()
  63. {
  64. NameValueCollection nvc = Request.Form;
  65.  
  66. Trace.WriteLine("Http_Hmac: " + HTTP_HMAC);
  67. Trace.WriteLine("ipn_mode: " + nvc["ipn_mode"]);
  68. Trace.WriteLine("currency1: " + nvc["currency1"]);
  69. Trace.WriteLine("txn id: " + nvc["txn_id"]);
  70. var merchant_id = "9750af22c5fb6321c05fb0b4befb9ac1";
  71. var ipn_secret = "xxkuki1212xx";
  72. var order_total = MyGlobalVariables.PaymentChoice;
  73.  
  74. if (String.IsNullOrEmpty(nvc["ipn_mode"]) || nvc["ipn_mode"] != "hmac")
  75. {
  76. Trace.WriteLine("IPN Mode is not HMAC");
  77. return View();
  78. }
  79.  
  80. if (String.IsNullOrEmpty(HTTP_HMAC))
  81. {
  82. Trace.WriteLine("No HMAC signature sent");
  83. return View();
  84. }
  85.  
  86. if (String.IsNullOrEmpty(nvc["merchant"]) || nvc["merchant"] != merchant_id.Trim())
  87. {
  88. Trace.WriteLine("No or incorrect Merchant ID passed");
  89. return View();
  90. }
  91.  
  92. //var hmac = hash_hmac("sha512", request, ipn_secret.Trim());
  93.  
  94. var txn_id = nvc["txn_id"];
  95. var item_name = nvc["item_name"];
  96. var item_number = nvc["item_number"];
  97. var amount1 = nvc["amount1"];
  98. var amount2 = float.Parse(nvc["amount2"], CultureInfo.InvariantCulture.NumberFormat);
  99. var currency1 = nvc["currency1"];
  100. var currency2 = nvc["currency2"];
  101. var status = Convert.ToInt32(nvc["status"]);
  102. var status_text = nvc["status_text"];
  103. Trace.WriteLine(status);
  104. if (currency1 != "USD") {
  105. Trace.WriteLine("Original currency mismatch!");
  106. return View();
  107. }
  108.  
  109. if (Convert.ToInt32(amount1) < Convert.ToInt32(order_total))
  110. {
  111. Trace.WriteLine("Amount is less than order total!");
  112. return View();
  113. }
  114.  
  115. if (status >= 100 || status == 2) {
  116.  
  117. using (MyDatabaseEntities1 dc = new MyDatabaseEntities1())
  118. {
  119. var account = dc.Users.Where(a => a.Username == MyGlobalVariables.CurrentUser).FirstOrDefault();
  120. if (account != null && account.Paid == 0)
  121. {
  122. Trace.WriteLine("Payment Completed");
  123. Trace.WriteLine("Tokens to add: " + MyGlobalVariables.TokenChoice);
  124. account.Tokens += MyGlobalVariables.TokenChoice;
  125. account.Paid = 1;
  126. dc.Configuration.ValidateOnSaveEnabled = false;
  127. dc.SaveChanges();
  128. }
  129. }
  130. } else if (status < 0)
  131. {
  132. Trace.WriteLine(
  133. "payment error, this is usually final but payments will sometimes be reopened if there was no exchange rate conversion or with seller consent");
  134. } else {
  135. using (MyDatabaseEntities1 dc = new MyDatabaseEntities1())
  136. {
  137. var account = dc.Users.Where(a => a.Username == MyGlobalVariables.CurrentUser).FirstOrDefault();
  138. if (account != null)
  139. {
  140. account.Paid = 0;
  141. dc.Configuration.ValidateOnSaveEnabled = false;
  142. dc.SaveChanges();
  143. }
  144. }
  145. Trace.WriteLine("Payment is pending");
  146. }
  147.  
  148. return View();
  149. }
Add Comment
Please, Sign In to add comment