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