Advertisement
Guest User

Untitled

a guest
Oct 11th, 2015
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.82 KB | None | 0 0
  1. View:
  2. @inherits UmbracoViewPage<UmbracoSite.Models.PaymentFormModel>
  3. @{
  4. Layout = null;
  5. }
  6.  
  7. Зачекайте, будь ласка. Якщо нічого не відбулося, то для того щоб продовжити - натисніть зелену кнопку
  8.  
  9. <form method="POST" accept-charset="utf-8" action="https://www.liqpay.com/api/pay">
  10. <input type="hidden" name="public_key" value="@Model.PublicKey" />
  11. <input type="hidden" name="amount" value="@Model.Amount" />
  12. <input type="hidden" name="currency" value="@Model.Currency" />
  13. <input type="hidden" name="description" value="@Model.Description" />
  14. <input type="hidden" name="order_id" value="@Model.OrderId" />
  15. <input type="hidden" name="type" value="@Model.Type" />
  16. @if (Model.Sandbox)
  17. {
  18. <input type="hidden" name="sandbox" value="1" />
  19. }
  20. <input type="hidden" name="language" value="en" />
  21. <input type="hidden" name="result_url" value="@Model.ResultUrl" />
  22. <input type="hidden" name="server_url" value="@Model.ServiceUrl" />
  23. <input type="hidden" name="signature" value="@Model.Signature" />
  24. @if (Model.Type == "subsribe")
  25. {
  26. <input type="hidden" name="subscribe_date_start" value="@Model.SubscribeDateStart" />
  27. <input type="hidden" name="subscribe_periodicity" value="month" />
  28. }
  29. <input type="image" src="//static.liqpay.com/buttons/d1en.radius.png" name="btn_text" />
  30. </form>
  31.  
  32. <script>
  33. document.getElementsByTagName("form")[0].submit();
  34. </script>
  35.  
  36. Controller:
  37. using System;
  38. using System.Collections.Generic;
  39. using System.Linq;
  40. using System.Security.Cryptography;
  41. using System.Text;
  42. using System.Web;
  43. using System.Web.Mvc;
  44. using MySql.Data.MySqlClient;
  45. using umbraco;
  46. using umbraco.BusinessLogic;
  47. using umbraco.cms.businesslogic.packager;
  48. using Umbraco.Core;
  49. using Umbraco.Core.Logging;
  50. using Umbraco.Web.Mvc;
  51. using UmbracoSite.Models;
  52. using System.Configuration;
  53.  
  54. namespace UmbracoSite.Controllers
  55. {
  56. public class PaymentSurfaceController : SurfaceController
  57. {
  58. protected UmbracoLPMLDatabase Database = new UmbracoLPMLDatabase();
  59.  
  60. [HttpPost]
  61. public ActionResult Donate(DonateFormModel model)
  62. {
  63. if (!model.Amount.HasValue)
  64. {
  65. return Content("Error. Amount cannot be null");
  66. }
  67.  
  68. var orderId = Database.AddDonate(model);
  69.  
  70. var description = "Добровільна пожертва у фонд випускників ліцею. ";
  71. if (!model.Subscribe)
  72. {
  73. var node = uQuery.GetNode(model.ProjectId);
  74. description += node.Name;
  75. }
  76.  
  77. var paymentModel = new PaymentFormModel
  78. {
  79. Amount = model.Amount.Value.ToString(),
  80. Currency = "UAH",
  81. Description = description,
  82. OrderId = orderId,
  83. PublicKey = ConfigurationManager.AppSettings["publicKey"],
  84. Type = model.Subscribe ? "subscribe" : "donate",
  85. ServiceUrl = ConfigurationManager.AppSettings["callbackUrl"],
  86. ResultUrl = ConfigurationManager.AppSettings["resultUrl"] + "?id=" + orderId,
  87. Sandbox = ConfigurationManager.AppSettings["sandbox"] == "1",
  88. SubscribeDateStart = DateTime.UtcNow.AddHours(3).ToString("yyyy-MM-dd HH:mm:ss")
  89. };
  90.  
  91. var signature = ConfigurationManager.AppSettings["privateKey"] + paymentModel;
  92. signature = CalculateSignature(signature);
  93. paymentModel.Signature = signature;
  94.  
  95. return View("PaymentHidden", paymentModel);
  96. }
  97.  
  98. [HttpPost]
  99. public ActionResult Callback(CallbackModel model, string public_key, int order_id, string transaction_id, string sender_phone)
  100. {
  101. try
  102. {
  103. if (model != null)
  104. {
  105. model.PublicKey = public_key;
  106. model.OrderId = order_id;
  107. model.TransactionId = transaction_id;
  108. model.SenderPhone = sender_phone;
  109. }
  110. else
  111. {
  112. throw new Exception("Model is null");
  113. }
  114.  
  115. var paymentId = Database.AddPayment(model);
  116.  
  117. var signature = ConfigurationManager.AppSettings["privateKey"] + model;
  118.  
  119. signature = CalculateSignature(signature);
  120.  
  121. if (signature == model.Signature)
  122. {
  123. var donate = Database.UpdateDonate(model.OrderId, true);
  124.  
  125. var amount = double.Parse(model.Amount);
  126.  
  127. if (donate.ProjectId > 0 && donate.DonateStatusChanged)
  128. {
  129. var node = uQuery.GetNode(donate.ProjectId);
  130. var collected = 0;
  131. int.TryParse(node.GetProperty("collected").Value, out collected);
  132. collected += (int)amount;
  133. node.SetProperty("collected", collected);
  134.  
  135. var donatesCount = 0;
  136. int.TryParse(node.GetProperty("donatesCount").Value, out donatesCount);
  137. donatesCount++;
  138. node.SetProperty("donatesCount", donatesCount);
  139.  
  140. node.Publish(true);
  141. }
  142. }
  143.  
  144. return Content("PaymentId: " + paymentId);
  145. }
  146. catch (Exception ex)
  147. {
  148. LogHelper.Error(this.GetType(), "CALLBACK ERROR: " + model, ex);
  149. throw;
  150. }
  151.  
  152. }
  153.  
  154. private string CalculateSignature(string data)
  155. {
  156. var bytes = Encoding.UTF8.GetBytes(data);
  157. var sha1 = SHA1Managed.Create();
  158. bytes = sha1.ComputeHash(bytes);
  159. return Convert.ToBase64String(bytes);
  160. }
  161. }
  162. }
  163.  
  164. Model:
  165. using System;
  166. using System.Collections.Generic;
  167. using System.Linq;
  168. using System.Web;
  169.  
  170. namespace UmbracoSite.Models
  171. {
  172. public class PaymentFormModel
  173. {
  174. public string PublicKey { get; set; }
  175. public string Amount { get; set; }
  176. public string Currency { get; set; }
  177. public int OrderId { get; set; }
  178. public string Type { get; set; }
  179. public string Description { get; set; }
  180. public string ResultUrl { get; set; }
  181. public string ServiceUrl { get; set; }
  182. public string Signature { get; set; }
  183. public string SubscribeDateStart { get; set; }
  184. public bool Sandbox { get; set; }
  185.  
  186. public override string ToString()
  187. {
  188. return Amount + Currency + PublicKey + OrderId + Type + Description + ResultUrl + ServiceUrl;
  189. }
  190. }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement