root/trunk/eCard/eCardMVC/Platnosci/Controllers/MerchantController.cs @ 927

Wersja 927, 8.2 KB (wprowadzona przez alina, 16 years temu)

re #215 zmiana nazwy plikow

RevLine 
[866]1using System;
2using System.Text;
3using System.Linq;
4using System.Web;
5using System.Web.UI;
6
7using System.Web.UI.WebControls;
8using System.Web.UI.WebControls.WebParts;
9using System.Web.UI.HtmlControls;
10using System.Web.Configuration;
11using System.Web.Mvc;
12using System.Web.Mvc.Ajax;
13using System.Collections.Generic;
14using Platnosci.Models;
15using Platnosci.Core.Linq;
16using Platnosci.Core.Interface;
17using System.Configuration;
18using System.Net;
19using System.IO;
20using System.Threading;
21
22namespace 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
[868]30        public const string KODOWANIE = "ISO-8859-2";
[917]31        public const string HASH_ERROR_INFO = "payment not exist";
[896]32        private string merchantId;
[866]33
[896]34        private readonly IRepository<vPlatnosciEcard> _repVPayment;
[919]35        private readonly IRepository<PlatnosciEcard> _repPayment;
[866]36        private FunkcjePlatnosci _func;
[927]37        private ITranslateManager _t;
[866]38
39        public MerchantController()
[896]40        {
41            _repVPayment = new Repository<vPlatnosciEcard>(new DataContext1());
42            _repPayment = new Repository<PlatnosciEcard>(new DataContext1());
[866]43            _func = new FunkcjePlatnosci();
[927]44            _t = new Translation();
[866]45        }
46        public ActionResult Merchant(Payer payer, string language)
47        {
[877]48            System.Diagnostics.Debug.WriteLine("MerchantController:Merchant:" + language);
[866]49            language = _func.setLanguage(language);
50
51            int id1 = Convert.ToInt32(payer.Id_faktury);
[896]52            vPlatnosciEcard platnosc = _repVPayment.Find(p => p.ID_faktury == id1).SingleOrDefault();
[880]53                 
54            if (platnosc == null)
[866]55            {
[927]56                ErrorViewData errorViewData = _func.InitErrorViewData(_t.Translate("tlumaczenia", "brakdanych"));
[866]57                return View("Error1", errorViewData);
58            }
[880]59            else if (!_func.UserIdentity(platnosc, ControllerContext.HttpContext.User.Identity.Name))
60            {
[927]61                ErrorViewData errorViewData = _func.InitErrorViewData(_t.Translate("tlumaczenia", "weryfikacja"));
[880]62                return View("Error1", errorViewData);
63            }
[868]64           
[866]65            Waluta waluta = _func.setAmount(platnosc);
[868]66            var newPayment = InitNewPayment(id1, platnosc, waluta, payer );           
[866]67
68            string systemKs = platnosc.SystemKsiegowyId.ToString();
69
[868]70            bool createPayment = AddNewPayment(newPayment);
[917]71            if (createPayment == false)
72            {
[927]73                ErrorViewData errorViewData = _func.InitErrorViewData(_t.Translate("tlumaczenia", "error_hash"));
[917]74                errorViewData.idfaktury = payer.Id_faktury;
75                return View("Error1", errorViewData);
76            }
[866]77
[868]78            string hash = GetHash(newPayment, systemKs);
[866]79            hash = hash.Replace("\n","");
[917]80            if (hash == BAD_HASH || hash == "" || hash == HASH_ERROR_INFO)
81            {
[927]82                ErrorViewData errorViewData = _func.InitErrorViewData(_t.Translate("tlumaczenia", "error_hash"));
[917]83                errorViewData.idfaktury = payer.Id_faktury;
84                return View("Error1", errorViewData);
85            }
[866]86
[870]87            if (systemKs == "1") merchantId = "170906000";
88                else merchantId = "171485000";
89
[919]90            //przeslanie w linku ordernumber potrzebnego do wyswietlenia potwierdzenia
91            int orderek = 0;
92            orderek = _repPayment.GetOrdernumber(newPayment.ORDERDESCRIPTION, newPayment.IDFaktury, newPayment.Data);
93
[881]94            string LinkFail = ConfigurationManager.AppSettings["Strona"];
95            LinkFail += "/" + language + ConfigurationManager.AppSettings["LinkFail"];
[919]96            LinkFail += "/" + newPayment.IDFaktury + "?o=" + orderek;
[866]97
[881]98            string LinkOk = ConfigurationManager.AppSettings["Strona"];
99            LinkOk += "/" + language + ConfigurationManager.AppSettings["LinkOk"];
[919]100            LinkOk += "/" + newPayment.IDFaktury + "?o=" + orderek; ;
[881]101
[919]102            //merchantid dla srodowiska testowego
103            merchantId = "171485000";
[917]104            wyslij(newPayment, hash, merchantId, LinkFail, LinkOk);
105            return new EmptyResult();         
[868]106        }
107        private PlatnosciEcard InitNewPayment(int id, vPlatnosciEcard platnosc, Waluta waluta, Payer payer)
108        {
109            PlatnosciEcard newPayment = new PlatnosciEcard();
110            newPayment.IDFaktury = id;
111            newPayment.ORDERDESCRIPTION = platnosc.Faktura_Numer;
112            newPayment.nip = platnosc.nip;
113            newPayment.nrZlecenia = "";
114            newPayment.AMOUNT = waluta.Amount;
115            newPayment.CURRENCY = waluta.Currency;
116            newPayment.SESSIONID = Session.SessionID;
117            newPayment.NAME = payer.FirstName;
118            newPayment.SURNAME = payer.LastName;
119            newPayment.AUTODEPOSIT = true;
[877]120            newPayment.LANGUAGE = Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName.ToUpper();
[868]121            newPayment.CHARSET = KODOWANIE;
122            newPayment.COUNTRY = KOD_POLSKA;
123            newPayment.JS = true;
124            newPayment.PAYMENTTYPE = CARDS;
125            newPayment.Data = DateTime.Now;
126            newPayment.Status = null;
127            newPayment.Status_data = null;
128            return newPayment;
129        }
130        private bool AddNewPayment(PlatnosciEcard platnosc)
131        {
132            if (platnosc != null)
133            {               
[917]134                _repPayment.Insert(platnosc);
[868]135                return true;
136            }
137            return false;
138        }
[866]139        private string GetHash(PlatnosciEcard p, string ks)
140        {
141            string strResponse;
[917]142            PlatnosciEcard platnosc = _repPayment.Find(i => i.ORDERDESCRIPTION == p.ORDERDESCRIPTION && i.IDFaktury == p.IDFaktury && i.Data == p.Data).First();
143
144            if (platnosc == null) return HASH_ERROR_INFO;
145
146            string adres = "https://pay.ecard.pl/servlet/HS?orderNumber=" + platnosc.ORDERNUMBER;
[866]147            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(adres);
148            string dane = "&orderDescription=&amount=" + platnosc.AMOUNT;
149            dane += "&currency=" + platnosc.CURRENCY;
[917]150            dane += "&merchantId=171485000&password=ashSeth2";
[868]151
[917]152          //  if (ks == "1") dane += "&merchantId=171485000&password=ashSeth2";
153            //    else dane += "&merchantId=170906000&password=JaYpqfs0";
154
[866]155            byte[] bdata = System.Text.ASCIIEncoding.ASCII.GetBytes(dane);
156            req.Method = "POST";
157            req.ContentType = "application/x-www-form-urlencoded";
158            req.ContentLength = dane.Length;
159
160            Stream reqStream = req.GetRequestStream();
161            reqStream.Write(bdata, 0, bdata.Length);
162            reqStream.Close();
163
164            StreamReader streamResponse = new StreamReader(req.GetResponse().GetResponseStream());
165            strResponse = streamResponse.ReadToEnd();
166            streamResponse.Close();
167                     
168            return strResponse;
[877]169        }
[919]170        private void wyslij(PlatnosciEcard m, string hash, string merchantid, string linkok, string linkfail)
[877]171        {
[919]172            //string adres = "https://pay.ecard.pl/payment/PS?ORDERDESCRIPTION=" + m.ORDERDESCRIPTION;
173           
[917]174            string adres = "https://pay.ecard.pl/servlet/PSTEST?ORDERDESCRIPTION=" + m.ORDERDESCRIPTION;
[877]175            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(adres);
[917]176            string dane = "&AMOUNT=" + m.AMOUNT + "&CURRENCY=" + m.CURRENCY + "&ORDERNUMBER=" + m.ORDERNUMBER;
177            dane += "&NAME=" + m.NAME + "&SURNAME=" + m.SURNAME + "&LANGUAGE=" + m.LANGUAGE + "&CHARSET=ISO-8859-2";
[919]178            dane += "&COUNTRY=616&PAYMENTTYPE=CARDS&JS=1&HASH=" + hash + "&MERCHANTID=" + merchantid + "&AUTODEPOSIT=" + m.AUTODEPOSIT;
[917]179            dane += "&LINKFAIL=" + linkfail + "&LINKOK=" + linkok + "&SESSIONID=" + m.SESSIONID;
[877]180            Response.Redirect(adres + dane);
181        }
[866]182    }
183}
Notatka: Zobacz TracBrowser aby uzyskać więcej informacji.