Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- View:
- @inherits UmbracoViewPage<UmbracoSite.Models.PaymentFormModel>
- @{
- Layout = null;
- }
- Зачекайте, будь ласка. Якщо нічого не відбулося, то для того щоб продовжити - натисніть зелену кнопку
- <form method="POST" accept-charset="utf-8" action="https://www.liqpay.com/api/pay">
- <input type="hidden" name="public_key" value="@Model.PublicKey" />
- <input type="hidden" name="amount" value="@Model.Amount" />
- <input type="hidden" name="currency" value="@Model.Currency" />
- <input type="hidden" name="description" value="@Model.Description" />
- <input type="hidden" name="order_id" value="@Model.OrderId" />
- <input type="hidden" name="type" value="@Model.Type" />
- @if (Model.Sandbox)
- {
- <input type="hidden" name="sandbox" value="1" />
- }
- <input type="hidden" name="language" value="en" />
- <input type="hidden" name="result_url" value="@Model.ResultUrl" />
- <input type="hidden" name="server_url" value="@Model.ServiceUrl" />
- <input type="hidden" name="signature" value="@Model.Signature" />
- @if (Model.Type == "subsribe")
- {
- <input type="hidden" name="subscribe_date_start" value="@Model.SubscribeDateStart" />
- <input type="hidden" name="subscribe_periodicity" value="month" />
- }
- <input type="image" src="//static.liqpay.com/buttons/d1en.radius.png" name="btn_text" />
- </form>
- <script>
- document.getElementsByTagName("form")[0].submit();
- </script>
- Controller:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Security.Cryptography;
- using System.Text;
- using System.Web;
- using System.Web.Mvc;
- using MySql.Data.MySqlClient;
- using umbraco;
- using umbraco.BusinessLogic;
- using umbraco.cms.businesslogic.packager;
- using Umbraco.Core;
- using Umbraco.Core.Logging;
- using Umbraco.Web.Mvc;
- using UmbracoSite.Models;
- using System.Configuration;
- namespace UmbracoSite.Controllers
- {
- public class PaymentSurfaceController : SurfaceController
- {
- protected UmbracoLPMLDatabase Database = new UmbracoLPMLDatabase();
- [HttpPost]
- public ActionResult Donate(DonateFormModel model)
- {
- if (!model.Amount.HasValue)
- {
- return Content("Error. Amount cannot be null");
- }
- var orderId = Database.AddDonate(model);
- var description = "Добровільна пожертва у фонд випускників ліцею. ";
- if (!model.Subscribe)
- {
- var node = uQuery.GetNode(model.ProjectId);
- description += node.Name;
- }
- var paymentModel = new PaymentFormModel
- {
- Amount = model.Amount.Value.ToString(),
- Currency = "UAH",
- Description = description,
- OrderId = orderId,
- PublicKey = ConfigurationManager.AppSettings["publicKey"],
- Type = model.Subscribe ? "subscribe" : "donate",
- ServiceUrl = ConfigurationManager.AppSettings["callbackUrl"],
- ResultUrl = ConfigurationManager.AppSettings["resultUrl"] + "?id=" + orderId,
- Sandbox = ConfigurationManager.AppSettings["sandbox"] == "1",
- SubscribeDateStart = DateTime.UtcNow.AddHours(3).ToString("yyyy-MM-dd HH:mm:ss")
- };
- var signature = ConfigurationManager.AppSettings["privateKey"] + paymentModel;
- signature = CalculateSignature(signature);
- paymentModel.Signature = signature;
- return View("PaymentHidden", paymentModel);
- }
- [HttpPost]
- public ActionResult Callback(CallbackModel model, string public_key, int order_id, string transaction_id, string sender_phone)
- {
- try
- {
- if (model != null)
- {
- model.PublicKey = public_key;
- model.OrderId = order_id;
- model.TransactionId = transaction_id;
- model.SenderPhone = sender_phone;
- }
- else
- {
- throw new Exception("Model is null");
- }
- var paymentId = Database.AddPayment(model);
- var signature = ConfigurationManager.AppSettings["privateKey"] + model;
- signature = CalculateSignature(signature);
- if (signature == model.Signature)
- {
- var donate = Database.UpdateDonate(model.OrderId, true);
- var amount = double.Parse(model.Amount);
- if (donate.ProjectId > 0 && donate.DonateStatusChanged)
- {
- var node = uQuery.GetNode(donate.ProjectId);
- var collected = 0;
- int.TryParse(node.GetProperty("collected").Value, out collected);
- collected += (int)amount;
- node.SetProperty("collected", collected);
- var donatesCount = 0;
- int.TryParse(node.GetProperty("donatesCount").Value, out donatesCount);
- donatesCount++;
- node.SetProperty("donatesCount", donatesCount);
- node.Publish(true);
- }
- }
- return Content("PaymentId: " + paymentId);
- }
- catch (Exception ex)
- {
- LogHelper.Error(this.GetType(), "CALLBACK ERROR: " + model, ex);
- throw;
- }
- }
- private string CalculateSignature(string data)
- {
- var bytes = Encoding.UTF8.GetBytes(data);
- var sha1 = SHA1Managed.Create();
- bytes = sha1.ComputeHash(bytes);
- return Convert.ToBase64String(bytes);
- }
- }
- }
- Model:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- namespace UmbracoSite.Models
- {
- public class PaymentFormModel
- {
- public string PublicKey { get; set; }
- public string Amount { get; set; }
- public string Currency { get; set; }
- public int OrderId { get; set; }
- public string Type { get; set; }
- public string Description { get; set; }
- public string ResultUrl { get; set; }
- public string ServiceUrl { get; set; }
- public string Signature { get; set; }
- public string SubscribeDateStart { get; set; }
- public bool Sandbox { get; set; }
- public override string ToString()
- {
- return Amount + Currency + PublicKey + OrderId + Type + Description + ResultUrl + ServiceUrl;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement