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