| 1 | using System;
|
|---|
| 2 | using System.Linq;
|
|---|
| 3 | using System.Web.Mvc;
|
|---|
| 4 | using Platnosci.Models;
|
|---|
| 5 | using Platnosci.Core.Linq;
|
|---|
| 6 | using Platnosci.Core.Interface;
|
|---|
| 7 | using System.Configuration;
|
|---|
| 8 | using System.Net;
|
|---|
| 9 | using System.IO;
|
|---|
| 10 | using System.Threading;
|
|---|
| 11 |
|
|---|
| 12 | namespace Platnosci.Controllers
|
|---|
| 13 | {
|
|---|
| 14 | [Authorize]
|
|---|
| 15 | public class MerchantController : Controller
|
|---|
| 16 | {
|
|---|
| 17 | public const string BAD_HASH = "zlyHash"; //b³êdne has³o - odpowied z eCard
|
|---|
| 18 | public const string CARDS = "CARDS"; //obs³uga tylko kart p³atniczych
|
|---|
| 19 | public const string KOD_POLSKA = "616"; //kod kraju Akceptanta - Polska
|
|---|
| 20 | public const string KODOWANIE = "ISO-8859-2";
|
|---|
| 21 | public const string HASH_ERROR_INFO = "payment not exist";
|
|---|
| 22 | private string _merchantId;
|
|---|
| 23 | private string _merchantPassword;
|
|---|
| 24 |
|
|---|
| 25 | private readonly IRepository<vPlatnosciEcard> _repVPayment;
|
|---|
| 26 | private readonly IRepository<PlatnosciEcard> _repPayment;
|
|---|
| 27 | private readonly FunkcjePlatnosci _funkcjePlatnosci;
|
|---|
| 28 | private readonly ITranslateManager _translateManager;
|
|---|
| 29 |
|
|---|
| 30 | public MerchantController()
|
|---|
| 31 | {
|
|---|
| 32 | _repVPayment = new Repository<vPlatnosciEcard>(new DataContext1());
|
|---|
| 33 | _repPayment = new Repository<PlatnosciEcard>(new DataContext1());
|
|---|
| 34 | _funkcjePlatnosci = new FunkcjePlatnosci();
|
|---|
| 35 | _translateManager = new Translation();
|
|---|
| 36 | }
|
|---|
| 37 |
|
|---|
| 38 | public ActionResult Merchant(Payer payer, string language)
|
|---|
| 39 | {
|
|---|
| 40 | System.Diagnostics.Debug.WriteLine("MerchantController:Merchant:" + language);
|
|---|
| 41 | language = _funkcjePlatnosci.setLanguage(language);
|
|---|
| 42 |
|
|---|
| 43 | var id1 = Convert.ToInt32(payer.Id_faktury);
|
|---|
| 44 | var platnosc = _repVPayment.Find(p => p.ID_faktury == id1).SingleOrDefault();
|
|---|
| 45 |
|
|---|
| 46 | if (platnosc == null)
|
|---|
| 47 | {
|
|---|
| 48 | ErrorViewData errorViewData = _funkcjePlatnosci.InitErrorViewData(_translateManager.Translate("tlumaczenia", "brakdanych"));
|
|---|
| 49 | return View("Error1", errorViewData);
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|
| 52 | if (!_funkcjePlatnosci.UserIdentity(platnosc, ControllerContext.HttpContext.User.Identity.Name))
|
|---|
| 53 | {
|
|---|
| 54 | ErrorViewData errorViewData = _funkcjePlatnosci.InitErrorViewData(_translateManager.Translate("tlumaczenia", "weryfikacja"));
|
|---|
| 55 | return View("Error1", errorViewData);
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | var waluta = _funkcjePlatnosci.setAmount(platnosc);
|
|---|
| 59 | var newPayment = InitNewPayment(id1, platnosc, waluta, payer );
|
|---|
| 60 |
|
|---|
| 61 | var systemKs = platnosc.SystemKsiegowyId.ToString();
|
|---|
| 62 |
|
|---|
| 63 | var createPayment = AddNewPayment(newPayment);
|
|---|
| 64 |
|
|---|
| 65 | if (createPayment == false)
|
|---|
| 66 | {
|
|---|
| 67 | ErrorViewData errorViewData = _funkcjePlatnosci.InitErrorViewData(_translateManager.Translate("tlumaczenia", "error_hash"));
|
|---|
| 68 | errorViewData.InvoiceId = payer.Id_faktury;
|
|---|
| 69 | return View("Error1", errorViewData);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | _merchantId = systemKs == "1" ? "171485000" : "170906000";
|
|---|
| 73 | _merchantPassword = systemKs == "1" ? "ashSeth2" : "JaYpqfs0";
|
|---|
| 74 |
|
|---|
| 75 | var hash = GetHash(newPayment, _merchantId, _merchantPassword);
|
|---|
| 76 | hash = hash.Replace("\n","");
|
|---|
| 77 |
|
|---|
| 78 | if (hash == BAD_HASH || hash == "" || hash == HASH_ERROR_INFO)
|
|---|
| 79 | {
|
|---|
| 80 | ErrorViewData errorViewData = _funkcjePlatnosci.InitErrorViewData(_translateManager.Translate("tlumaczenia", "error_hash"));
|
|---|
| 81 | errorViewData.InvoiceId = payer.Id_faktury;
|
|---|
| 82 | return View("Error1", errorViewData);
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | //przeslanie w linku ordernumber potrzebnego do wyswietlenia potwierdzenia
|
|---|
| 86 | var orderek = _repPayment.GetOrdernumber(newPayment.ORDERDESCRIPTION, newPayment.IDFaktury, newPayment.Data);
|
|---|
| 87 |
|
|---|
| 88 | var linkFail = ConfigurationManager.AppSettings["Strona"];
|
|---|
| 89 | linkFail += "/" + language + ConfigurationManager.AppSettings["LinkFail"];
|
|---|
| 90 | linkFail += "/" + newPayment.IDFaktury + "?o=" + orderek;
|
|---|
| 91 |
|
|---|
| 92 | var linkOk = ConfigurationManager.AppSettings["Strona"];
|
|---|
| 93 | linkOk += "/" + language + ConfigurationManager.AppSettings["LinkOk"];
|
|---|
| 94 | linkOk += "/" + newPayment.IDFaktury + "?o=" + orderek;
|
|---|
| 95 |
|
|---|
| 96 | SendRequest(newPayment, hash, _merchantId, linkFail, linkOk);
|
|---|
| 97 | return new EmptyResult();
|
|---|
| 98 | }
|
|---|
| 99 | private PlatnosciEcard InitNewPayment(int id, vPlatnosciEcard platnosc, Waluta waluta, Payer payer)
|
|---|
| 100 | {
|
|---|
| 101 | var newPayment = new PlatnosciEcard();
|
|---|
| 102 | newPayment.IDFaktury = id;
|
|---|
| 103 | newPayment.ORDERDESCRIPTION = platnosc.Faktura_Numer;
|
|---|
| 104 | newPayment.nip = platnosc.nip;
|
|---|
| 105 | newPayment.nrZlecenia = "";
|
|---|
| 106 | newPayment.AMOUNT = waluta.Amount;
|
|---|
| 107 | newPayment.CURRENCY = waluta.Currency;
|
|---|
| 108 | newPayment.SESSIONID = Session.SessionID;
|
|---|
| 109 | newPayment.NAME = payer.FirstName;
|
|---|
| 110 | newPayment.SURNAME = payer.LastName;
|
|---|
| 111 | newPayment.AUTODEPOSIT = true;
|
|---|
| 112 | newPayment.LANGUAGE = Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName.ToUpper();
|
|---|
| 113 | newPayment.CHARSET = KODOWANIE;
|
|---|
| 114 | newPayment.COUNTRY = KOD_POLSKA;
|
|---|
| 115 | newPayment.JS = true;
|
|---|
| 116 | newPayment.PAYMENTTYPE = CARDS;
|
|---|
| 117 | newPayment.Data = DateTime.Now;
|
|---|
| 118 | newPayment.Status = null;
|
|---|
| 119 | newPayment.Status_data = null;
|
|---|
| 120 | return newPayment;
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | private bool AddNewPayment(PlatnosciEcard platnosc)
|
|---|
| 124 | {
|
|---|
| 125 | if (platnosc != null)
|
|---|
| 126 | {
|
|---|
| 127 | _repPayment.Insert(platnosc);
|
|---|
| 128 | return true;
|
|---|
| 129 | }
|
|---|
| 130 | return false;
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | private string GetHash(PlatnosciEcard p, string merchantId, string merchantPassword)
|
|---|
| 134 | {
|
|---|
| 135 | var platnosc = _repPayment.Find(i => i.ORDERDESCRIPTION == p.ORDERDESCRIPTION && i.IDFaktury == p.IDFaktury && i.Data == p.Data).First();
|
|---|
| 136 |
|
|---|
| 137 | if (platnosc == null) return HASH_ERROR_INFO;
|
|---|
| 138 |
|
|---|
| 139 | var adres = "https://pay.ecard.pl/servlet/HS?orderNumber=" + platnosc.ORDERNUMBER;
|
|---|
| 140 | var req = (HttpWebRequest)WebRequest.Create(adres);
|
|---|
| 141 | var dane = "&orderDescription=&amount=" + platnosc.AMOUNT;
|
|---|
| 142 | dane += "¤cy=" + platnosc.CURRENCY;
|
|---|
| 143 | dane += string.Format("&merchantId={0}&password={1}", merchantId, merchantPassword);
|
|---|
| 144 |
|
|---|
| 145 | var bdata = System.Text.Encoding.ASCII.GetBytes(dane);
|
|---|
| 146 | req.Method = "POST";
|
|---|
| 147 | req.ContentType = "application/x-www-form-urlencoded";
|
|---|
| 148 | req.ContentLength = dane.Length;
|
|---|
| 149 |
|
|---|
| 150 | var reqStream = req.GetRequestStream();
|
|---|
| 151 | reqStream.Write(bdata, 0, bdata.Length);
|
|---|
| 152 | reqStream.Close();
|
|---|
| 153 |
|
|---|
| 154 | var streamResponse = new StreamReader(req.GetResponse().GetResponseStream());
|
|---|
| 155 | string strResponse = streamResponse.ReadToEnd();
|
|---|
| 156 | streamResponse.Close();
|
|---|
| 157 |
|
|---|
| 158 | return strResponse;
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | private void SendRequest(PlatnosciEcard m, string hash, string merchantid, string linkok, string linkfail)
|
|---|
| 162 | {
|
|---|
| 163 | var adres = ConfigurationManager.AppSettings["eCard.Url"] + "?ORDERDESCRIPTION=" + m.ORDERDESCRIPTION;
|
|---|
| 164 |
|
|---|
| 165 | var dane = "&AMOUNT=" + m.AMOUNT + "&CURRENCY=" + m.CURRENCY + "&ORDERNUMBER=" + m.ORDERNUMBER;
|
|---|
| 166 | dane += "&NAME=" + m.NAME + "&SURNAME=" + m.SURNAME + "&LANGUAGE=" + m.LANGUAGE + "&CHARSET=ISO-8859-2";
|
|---|
| 167 | dane += "&COUNTRY=616&PAYMENTTYPE=CARDS&JS=1&HASH=" + hash + "&MERCHANTID=" + merchantid + "&AUTODEPOSIT=" + m.AUTODEPOSIT;
|
|---|
| 168 | dane += "&LINKFAIL=" + linkfail + "&LINKOK=" + linkok + "&SESSIONID=" + m.SESSIONID;
|
|---|
| 169 | Response.Redirect(adres + dane);
|
|---|
| 170 | }
|
|---|
| 171 | }
|
|---|
| 172 | }
|
|---|