root/branches/Emisje/eCard/eCardMVC/Platnosci/Controllers/MerchantController.cs @ 869

Wersja 868, 7.1 KB (wprowadzona przez alina, 16 years temu)

re #215

Line 
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
30        public const string KODOWANIE = "ISO-8859-2";
31
32        private string merchantId;
33        private readonly PlatnosciDataContext _context;
34        private readonly IRepositoryPE _rep;
35        private FunkcjePlatnosci _func;
36
37        public MerchantController()
38        {   
39            _rep = new RepositoryPlatnosciEcard();
40            _context = new PlatnosciDataContext();
41            _func = new FunkcjePlatnosci();
42        }
43        public ActionResult Merchant(Payer payer, string language)
44        {
45            language = _func.setLanguage(language);
46
47            int id1 = Convert.ToInt32(payer.Id_faktury);
48            vPlatnosciEcard platnosc = _context.FindInvoiceById(id1).SingleOrDefault();
49           
50            if (!_func.UserIdentity(platnosc, ControllerContext.HttpContext.User.Identity.Name))
51            {
52                ErrorViewData errorViewData = new ErrorViewData();
53                errorViewData.error = HttpContext.GetGlobalResourceObject("tlumaczenia", "weryfikacja").ToString();
54                return View("Error1", errorViewData);
55            }
56           
57            Waluta waluta = _func.setAmount(platnosc);
58            var newPayment = InitNewPayment(id1, platnosc, waluta, payer );           
59
60            string systemKs = platnosc.SystemKsiegowyId.ToString();
61
62            bool createPayment = AddNewPayment(newPayment);
63            if (createPayment == false) return View("Error");
64
65            string hash = GetHash(newPayment, systemKs);
66
67            hash = hash.Replace("\n","");
68            if (hash == BAD_HASH  || hash == "" )
69            {
70                return View("Error"); //nie mo¿na po³¹czyæ siê z serverem p³atnoœci. Proszê spróbowaæ jeszcze raz.
71            }
72            if (platnosc.SystemKsiegowyId == 1)
73                merchantId = "170906000";
74            else
75                merchantId = "171485000";
76
77            string link = ConfigurationManager.AppSettings["StatusLink"];
78            string LinkFail = link + "?status=fail";
79            string LinkOk = link + "?status=ok";
80
81            var merchantViewData = InitMerchantViewData(newPayment, hash, merchantId, LinkFail, LinkOk);
82            return View(merchantViewData);           
83        }
84        private PlatnosciEcard InitNewPayment(int id, vPlatnosciEcard platnosc, Waluta waluta, Payer payer)
85        {
86            PlatnosciEcard newPayment = new PlatnosciEcard();
87            newPayment.IDFaktury = id;
88            newPayment.ORDERDESCRIPTION = platnosc.Faktura_Numer;
89            newPayment.nip = platnosc.nip;
90            newPayment.nrZlecenia = "";
91            newPayment.AMOUNT = waluta.Amount;
92            newPayment.CURRENCY = waluta.Currency;
93            newPayment.SESSIONID = Session.SessionID;
94            newPayment.NAME = payer.FirstName;
95            newPayment.SURNAME = payer.LastName;
96            newPayment.AUTODEPOSIT = true;
97            newPayment.LANGUAGE = Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName.ToUpper(); //"PL";
98            newPayment.CHARSET = KODOWANIE;
99            newPayment.COUNTRY = KOD_POLSKA;
100            newPayment.JS = true;
101            newPayment.PAYMENTTYPE = CARDS;
102            newPayment.Data = DateTime.Now;
103            newPayment.Status = null;
104            newPayment.Status_data = null;
105            return newPayment;
106        }
107        private bool AddNewPayment(PlatnosciEcard platnosc)
108        {
109            if (platnosc != null)
110            {               
111               // _rep.Insert(platnosc);
112                return true;
113            }
114            return false;
115        }
116        private MerchantViewData InitMerchantViewData(PlatnosciEcard newPayment, string hash, string Id, string LinkFail, string LinkOk)
117        {
118            MerchantViewData merchantViewData = new MerchantViewData();
119            merchantViewData.nowaPlatnosc = newPayment;
120            merchantViewData.Hash = hash;
121            merchantViewData.merchantId = Id;
122            merchantViewData.LinkFail = LinkFail;
123            merchantViewData.LinkOk = LinkOk;
124           
125            //wartosci testowe
126            merchantViewData.nowaPlatnosc.ORDERNUMBER = 4;
127            merchantViewData.nowaPlatnosc.ORDERDESCRIPTION = "22";
128            merchantViewData.nowaPlatnosc.AMOUNT = 300;
129            merchantViewData.nowaPlatnosc.CURRENCY = "985";
130            merchantViewData.nowaPlatnosc.SESSIONID = "";
131            merchantViewData.merchantId = "171485000";
132            merchantViewData.LinkFail = "";
133            merchantViewData.LinkOk = "";
134            return merchantViewData;
135        }
136        private string GetHash(PlatnosciEcard p, string ks)
137        {
138            string strResponse;
139            /*PlatnosciEcard platnosc = _rep.FindOne(i => i.ORDERDESCRIPTION == p.ORDERDESCRIPTION && i.IDFaktury == p.IDFaktury && i.Data == p.Data);
140            string adres = "https://pay.ecard.pl/servlet/HS?orderNumber="+p.ORDERNUMBER;
141            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(adres);
142            string dane = "&orderDescription=&amount=" + platnosc.AMOUNT;
143            dane += "&currency=" + platnosc.CURRENCY;
144            if (ks == "1") dane += "&merchantId=171485000&password=ashSeth2";
145                else dane += "&merchantId=170906000&password=JaYpqfs0"; */
146
147            //dane testowe
148            string adres = "https://pay.ecard.pl/servlet/HS?orderNumber=4";
149            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(adres);
150            string dane = "&orderDescription=&amount=300&currency=985&merchantId=171485000&password=ashSeth2";               
151           
152            byte[] bdata = System.Text.ASCIIEncoding.ASCII.GetBytes(dane);
153            req.Method = "POST";
154            req.ContentType = "application/x-www-form-urlencoded";
155            req.ContentLength = dane.Length;
156
157            Stream reqStream = req.GetRequestStream();
158            reqStream.Write(bdata, 0, bdata.Length);
159            reqStream.Close();
160
161            StreamReader streamResponse = new StreamReader(req.GetResponse().GetResponseStream());
162            strResponse = streamResponse.ReadToEnd();
163            streamResponse.Close();
164                     
165            return strResponse;
166        }               
167    }
168}
Notatka: Zobacz TracBrowser aby uzyskać więcej informacji.