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

Wersja 870, 7.0 KB (wprowadzona przez alina, 16 years temu)

re #215

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