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