| 1 | using System;
|
|---|
| 2 | using System.Net;
|
|---|
| 3 | using System.IO;
|
|---|
| 4 | using System.Linq;
|
|---|
| 5 | using System.Configuration;
|
|---|
| 6 | using Platnosci.Core.Linq;
|
|---|
| 7 | using Platnosci.Core.Interface;
|
|---|
| 8 |
|
|---|
| 9 | namespace Platnosci.Models
|
|---|
| 10 | {
|
|---|
| 11 | public class eCardData
|
|---|
| 12 | {
|
|---|
| 13 | public const string HASH_ERROR_INFO = "payment not exist";
|
|---|
| 14 | private readonly FunkcjePlatnosci _funkcjePlatnosci;
|
|---|
| 15 | private IRepository<PlatnosciEcard> _repPayment;
|
|---|
| 16 |
|
|---|
| 17 | public eCardData(IRepository<PlatnosciEcard> repPayment)
|
|---|
| 18 | {
|
|---|
| 19 | _repPayment = repPayment;
|
|---|
| 20 | _funkcjePlatnosci = new FunkcjePlatnosci(_repPayment);
|
|---|
| 21 | }
|
|---|
| 22 |
|
|---|
| 23 | public String GetUrl(Merchant merchant)
|
|---|
| 24 | {
|
|---|
| 25 | if (merchant == null)
|
|---|
| 26 | throw new ArgumentNullException("merchant");
|
|---|
| 27 |
|
|---|
| 28 | var dane = ConfigurationManager.AppSettings["eCard.Url"] + "?ORDERDESCRIPTION=" + merchant.Payment.ORDERDESCRIPTION;
|
|---|
| 29 | dane += "&AMOUNT=" + merchant.Payment.AMOUNT + "&CURRENCY=" + merchant.Payment.CURRENCY;
|
|---|
| 30 | dane += "&ORDERNUMBER=" + merchant.Payment.ORDERNUMBER;
|
|---|
| 31 | dane += "&NAME=" + merchant.Payment.NAME + "&SURNAME=" + merchant.Payment.SURNAME;
|
|---|
| 32 | dane += "&LANGUAGE=" + merchant.Payment.LANGUAGE + "&CHARSET=ISO-8859-2";
|
|---|
| 33 | dane += "&COUNTRY=616&PAYMENTTYPE=CARDS&JS=1&HASH=" + merchant.Hash;
|
|---|
| 34 | dane += "&MERCHANTID=" + merchant.Id + "&AUTODEPOSIT=" + merchant.Payment.AUTODEPOSIT;
|
|---|
| 35 | dane += "&LINKFAIL=" + merchant.LinkFail + "&LINKOK=" + merchant.LinkOk;
|
|---|
| 36 | dane += "&SESSIONID=" + merchant.Payment.SESSIONID;
|
|---|
| 37 | return dane;
|
|---|
| 38 | }
|
|---|
| 39 | public Merchant CreateMerchantData(vPlatnosciEcard invoice, Payer payer, string lang, string sessionId)
|
|---|
| 40 | {
|
|---|
| 41 | var merchant = new Merchant();
|
|---|
| 42 |
|
|---|
| 43 | var waluta = _funkcjePlatnosci.SetAmount(invoice);
|
|---|
| 44 | var newPayment = _funkcjePlatnosci.CreateAndAddNewPyment(invoice, waluta, payer, sessionId);
|
|---|
| 45 |
|
|---|
| 46 | merchant.SystemKsiegowy = invoice.SystemKsiegowyId.ToString();
|
|---|
| 47 |
|
|---|
| 48 | #if DEBUG
|
|---|
| 49 | //TODO: find better method to set SystemKsiegowy when testing or developing
|
|---|
| 50 | merchant.SystemKsiegowy = "2";
|
|---|
| 51 | #endif
|
|---|
| 52 |
|
|---|
| 53 | merchant.Payment = newPayment;
|
|---|
| 54 | GetMerchantInfo(merchant);
|
|---|
| 55 | merchant.Hash = SetHash(merchant);
|
|---|
| 56 |
|
|---|
| 57 | //przeslanie w linku ordernumber potrzebnego do wyswietlenia potwierdzenia
|
|---|
| 58 | var orderek = _repPayment.GetOrdernumber(newPayment.ORDERDESCRIPTION, newPayment.IDFaktury, newPayment.Data);
|
|---|
| 59 |
|
|---|
| 60 | var linkFail = ConfigurationManager.AppSettings["Strona"];
|
|---|
| 61 | linkFail += "/" + lang + ConfigurationManager.AppSettings["LinkFail"];
|
|---|
| 62 | linkFail += "/" + newPayment.IDFaktury + "?o=" + orderek + "&";
|
|---|
| 63 |
|
|---|
| 64 | var linkOk = ConfigurationManager.AppSettings["Strona"];
|
|---|
| 65 | linkOk += "/" + lang + ConfigurationManager.AppSettings["LinkOk"];
|
|---|
| 66 | linkOk += "/" + newPayment.IDFaktury + "?o=" + orderek + "&";
|
|---|
| 67 |
|
|---|
| 68 | merchant.LinkOk = linkOk;
|
|---|
| 69 | merchant.LinkFail = linkFail;
|
|---|
| 70 |
|
|---|
| 71 | merchant.IsValid();
|
|---|
| 72 |
|
|---|
| 73 | return merchant;
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | public void GetMerchantInfo(Merchant merchant)
|
|---|
| 77 | {
|
|---|
| 78 | if (merchant.SystemKsiegowy == "2")
|
|---|
| 79 | {
|
|---|
| 80 | merchant.Id = "171485000";
|
|---|
| 81 | merchant.Password = "ashSeth2";
|
|---|
| 82 | }
|
|---|
| 83 | else
|
|---|
| 84 | {
|
|---|
| 85 | merchant.Id = "170906000";
|
|---|
| 86 | merchant.Password = "JaYpqfs0";
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| 89 | public string SetHash(Merchant merchant)
|
|---|
| 90 | {
|
|---|
| 91 | if (merchant == null)
|
|---|
| 92 | throw new ArgumentNullException("merchant");
|
|---|
| 93 |
|
|---|
| 94 | var platnosc = _repPayment.Find(i => i.ORDERDESCRIPTION == merchant.Payment.ORDERDESCRIPTION && i.IDFaktury == merchant.Payment.IDFaktury && i.Data == merchant.Payment.Data).SingleOrDefault();
|
|---|
| 95 |
|
|---|
| 96 | if (platnosc == null)
|
|---|
| 97 | merchant.Hash = HASH_ERROR_INFO;
|
|---|
| 98 | else
|
|---|
| 99 | {
|
|---|
| 100 | var adres = "https://pay.ecard.pl/servlet/HS?orderNumber=" + platnosc.ORDERNUMBER;
|
|---|
| 101 | var req = (HttpWebRequest)WebRequest.Create(adres);
|
|---|
| 102 | var dane = "&orderDescription=&amount=" + platnosc.AMOUNT;
|
|---|
| 103 | dane += "¤cy=" + platnosc.CURRENCY;
|
|---|
| 104 | dane += string.Format("&merchantId={0}&password={1}", merchant.Id, merchant.Password);
|
|---|
| 105 |
|
|---|
| 106 | var bdata = System.Text.Encoding.ASCII.GetBytes(dane);
|
|---|
| 107 | req.Method = "POST";
|
|---|
| 108 | req.ContentType = "application/x-www-form-urlencoded";
|
|---|
| 109 | req.ContentLength = dane.Length;
|
|---|
| 110 |
|
|---|
| 111 | var reqStream = req.GetRequestStream();
|
|---|
| 112 | reqStream.Write(bdata, 0, bdata.Length);
|
|---|
| 113 | reqStream.Close();
|
|---|
| 114 |
|
|---|
| 115 | var streamResponse = new StreamReader(req.GetResponse().GetResponseStream());
|
|---|
| 116 | string strResponse = streamResponse.ReadToEnd();
|
|---|
| 117 | streamResponse.Close();
|
|---|
| 118 | strResponse = strResponse.Replace("\n", "");
|
|---|
| 119 |
|
|---|
| 120 | merchant.Hash = strResponse;
|
|---|
| 121 | }
|
|---|
| 122 | return merchant.Hash;
|
|---|
| 123 | }
|
|---|
| 124 | }
|
|---|
| 125 | } |
|---|