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

Wersja 896, 8.4 KB (wprowadzona przez alina, 16 years temu)

re #215 zmiana parametrow w konstruktorze PlatnosciController?, dostosowanie testow do zmiany

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";
[877]31        private int ORDERNUMBER = 122;
[896]32        private string merchantId;
[866]33
[896]34        private readonly IRepository<vPlatnosciEcard> _repVPayment;
35        private readonly IRepository<PlatnosciEcard> _repPayment;       
[866]36        private FunkcjePlatnosci _func;
37
38        public MerchantController()
[896]39        {
40            _repVPayment = new Repository<vPlatnosciEcard>(new DataContext1());
41            _repPayment = new Repository<PlatnosciEcard>(new DataContext1());
[866]42            _func = new FunkcjePlatnosci();
43        }
44        public ActionResult Merchant(Payer payer, string language)
45        {
[877]46            System.Diagnostics.Debug.WriteLine("MerchantController:Merchant:" + language);
[866]47            language = _func.setLanguage(language);
48
49            int id1 = Convert.ToInt32(payer.Id_faktury);
[896]50            vPlatnosciEcard platnosc = _repVPayment.Find(p => p.ID_faktury == id1).SingleOrDefault();
[880]51                 
52            if (platnosc == null)
[866]53            {
[880]54                ErrorViewData errorViewData = _func.InitErrorViewData(HttpContext.GetGlobalResourceObject("tlumaczenia", "brakdanych").ToString());
[866]55                return View("Error1", errorViewData);
56            }
[880]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            }
[868]62           
[866]63            Waluta waluta = _func.setAmount(platnosc);
[868]64            var newPayment = InitNewPayment(id1, platnosc, waluta, payer );           
[866]65
66            string systemKs = platnosc.SystemKsiegowyId.ToString();
67
[868]68            bool createPayment = AddNewPayment(newPayment);
[866]69            if (createPayment == false) return View("Error");
70
[868]71            string hash = GetHash(newPayment, systemKs);
[866]72            hash = hash.Replace("\n","");
[870]73            if (hash == BAD_HASH  || hash == "" ) return View("Error");
[866]74
[870]75            if (systemKs == "1") merchantId = "170906000";
76                else merchantId = "171485000";
77
[881]78            string LinkFail = ConfigurationManager.AppSettings["Strona"];
79            LinkFail += "/" + language + ConfigurationManager.AppSettings["LinkFail"];
80            LinkFail += "/" + newPayment.IDFaktury;
[866]81
[881]82            string LinkOk = ConfigurationManager.AppSettings["Strona"];
83            LinkOk += "/" + language + ConfigurationManager.AppSettings["LinkOk"];
84            LinkOk += "/" + newPayment.IDFaktury; ;
85
[868]86            var merchantViewData = InitMerchantViewData(newPayment, hash, merchantId, LinkFail, LinkOk);
[877]87            wyslij(merchantViewData, hash, merchantId);
[868]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;
[877]103            newPayment.LANGUAGE = Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName.ToUpper();
[868]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            {               
[896]117                //_repPayment.Insert(platnosc);
[868]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           
[866]131            //wartosci testowe
[877]132            merchantViewData.nowaPlatnosc.ORDERNUMBER = ORDERNUMBER;
133            merchantViewData.nowaPlatnosc.ORDERDESCRIPTION = "222";
[866]134            merchantViewData.nowaPlatnosc.AMOUNT = 300;
135            merchantViewData.nowaPlatnosc.CURRENCY = "985";
[877]136            merchantViewData.nowaPlatnosc.SESSIONID = "ff";
[866]137            merchantViewData.merchantId = "171485000";
138            merchantViewData.LinkFail = "";
139            merchantViewData.LinkOk = "";
[868]140            return merchantViewData;
[866]141        }
142        private string GetHash(PlatnosciEcard p, string ks)
143        {
144            string strResponse;
[868]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;
[866]147            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(adres);
148            string dane = "&orderDescription=&amount=" + platnosc.AMOUNT;
149            dane += "&currency=" + platnosc.CURRENCY;
150            if (ks == "1") dane += "&merchantId=171485000&password=ashSeth2";
[868]151                else dane += "&merchantId=170906000&password=JaYpqfs0"; */
152
[866]153            //dane testowe
[877]154            string adres = "https://pay.ecard.pl/servlet/HS?orderNumber="+ORDERNUMBER;
[868]155            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(adres);
[866]156            string dane = "&orderDescription=&amount=300&currency=985&merchantId=171485000&password=ashSeth2";               
[868]157           
[866]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;
[877]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        }
[866]182    }
183}
Notatka: Zobacz TracBrowser aby uzyskać więcej informacji.