| 1 | using System;
|
|---|
| 2 | using System.Web;
|
|---|
| 3 | using System.Threading;
|
|---|
| 4 | using System.Globalization;
|
|---|
| 5 | using System.Configuration;
|
|---|
| 6 | using System.Security.Principal;
|
|---|
| 7 | using adMoto.Payments.Core;
|
|---|
| 8 | using adMoto.Payments.Core.Data;
|
|---|
| 9 | using adMoto.Payments.Core.Interfaces;
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 | namespace adMoto.Payments.Web.Models
|
|---|
| 13 | {
|
|---|
| 14 | public class PaymentsUtils
|
|---|
| 15 | {
|
|---|
| 16 | public const string EUR = "978";
|
|---|
| 17 | public const string GBP = "826";
|
|---|
| 18 | public const string USD = "789";
|
|---|
| 19 | public const string PLN = "985";
|
|---|
| 20 | public const string CARDS = "CARDS"; //obsługa tylko kart płatniczych
|
|---|
| 21 | public const string KOD_POLSKA = "616"; //kod kraju Akceptanta - Polska
|
|---|
| 22 | public const string KODOWANIE = "ISO-8859-2";
|
|---|
| 23 | private readonly IRepository<PlatnosciEcard> _repPayment;
|
|---|
| 24 | private readonly ITranslateManager _translateManager;
|
|---|
| 25 |
|
|---|
| 26 | public PaymentsUtils()
|
|---|
| 27 | {
|
|---|
| 28 | _repPayment = new Repository<PlatnosciEcard>(new DataContext());
|
|---|
| 29 | _translateManager = new Translation();
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | public PaymentsUtils(IRepository<PlatnosciEcard> repPayment)
|
|---|
| 33 | {
|
|---|
| 34 | _repPayment = repPayment;
|
|---|
| 35 | _translateManager = new Translation();
|
|---|
| 36 | }
|
|---|
| 37 | public PaymentsUtils(IRepository<PlatnosciEcard> repPayment, ITranslateManager translate)
|
|---|
| 38 | {
|
|---|
| 39 | _repPayment = repPayment;
|
|---|
| 40 | _translateManager = translate;
|
|---|
| 41 | }
|
|---|
| 42 | public string BruttoToString(decimal? kwota, decimal? waluta, string miano)
|
|---|
| 43 | {
|
|---|
| 44 | var brutto = String.Format("{0:0.00}", kwota.ToString().Replace(",", ".")) + " PLN ";
|
|---|
| 45 |
|
|---|
| 46 | if (!String.IsNullOrEmpty(miano))
|
|---|
| 47 | if (waluta > 0 && miano.ToUpper() != "PLN")
|
|---|
| 48 | brutto += "(" + (waluta.ToString()).Replace(",", ".") + " " + miano.ToUpper() + ")";
|
|---|
| 49 |
|
|---|
| 50 | return brutto;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | public bool UserIdentity(Invoice platnosc, string userName)
|
|---|
| 54 | {
|
|---|
| 55 | return platnosc != null && platnosc.nip == userName;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | public Waluta SetAmount(Invoice platnosc)
|
|---|
| 59 | {
|
|---|
| 60 | var waluta = new Waluta {Currency = GetCurrency(platnosc.waluta_miano)};
|
|---|
| 61 |
|
|---|
| 62 | waluta.Amount = waluta.Currency == PLN ? Convert.ToInt32(platnosc.Brutto * 100) : Convert.ToInt32(platnosc.waluta_brutto * 100);
|
|---|
| 63 |
|
|---|
| 64 | return waluta;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | public string GetCurrency(string currency)
|
|---|
| 68 | {
|
|---|
| 69 | if (string.IsNullOrEmpty(currency))
|
|---|
| 70 | return PLN;
|
|---|
| 71 |
|
|---|
| 72 | switch (currency.ToUpper())
|
|---|
| 73 | {
|
|---|
| 74 | case "EUR":
|
|---|
| 75 | return EUR;
|
|---|
| 76 | case "GBP":
|
|---|
| 77 | return GBP;
|
|---|
| 78 | case "USD":
|
|---|
| 79 | return USD;
|
|---|
| 80 | default:
|
|---|
| 81 | return PLN;
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|
| 84 |
|
|---|
| 85 | public string SetLanguage(string lang)
|
|---|
| 86 | {
|
|---|
| 87 | switch (lang)
|
|---|
| 88 | {
|
|---|
| 89 | case "de":
|
|---|
| 90 | case "en":
|
|---|
| 91 | case "pl":
|
|---|
| 92 | if (lang != Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName.ToLower())
|
|---|
| 93 | {
|
|---|
| 94 | var culture = "pl-PL";
|
|---|
| 95 | switch (lang)
|
|---|
| 96 | {
|
|---|
| 97 | case "pl":
|
|---|
| 98 | culture = "pl-PL";
|
|---|
| 99 | break;
|
|---|
| 100 | case "en":
|
|---|
| 101 | culture = "en-US";
|
|---|
| 102 | break;
|
|---|
| 103 | case "de":
|
|---|
| 104 | culture = "de-DE";
|
|---|
| 105 | break;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(culture);
|
|---|
| 109 | Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);
|
|---|
| 110 | }
|
|---|
| 111 | break;
|
|---|
| 112 | }
|
|---|
| 113 | return CultureInfo.CurrentCulture.TwoLetterISOLanguageName.ToLower();
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | public string SetTitle()
|
|---|
| 117 | {
|
|---|
| 118 | var str = "";
|
|---|
| 119 | var css = ConfigurationManager.AppSettings["Css"];
|
|---|
| 120 |
|
|---|
| 121 | switch (css)
|
|---|
| 122 | {
|
|---|
| 123 | case "truck":
|
|---|
| 124 | str = HttpContext.GetGlobalResourceObject("tlumaczenia", "adresTruck") + " - ";
|
|---|
| 125 | break;
|
|---|
| 126 | case "admoto":
|
|---|
| 127 | str = HttpContext.GetGlobalResourceObject("tlumaczenia", "adresAdmoto") + " - ";
|
|---|
| 128 | break;
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | return str;
|
|---|
| 132 | }
|
|---|
| 133 |
|
|---|
| 134 | public void SetUserLogger(string nip, string faktura)
|
|---|
| 135 | {
|
|---|
| 136 |
|
|---|
| 137 | var user = nip + "-" + faktura;
|
|---|
| 138 | Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(user, ""), null);
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | public ErrorViewData InitErrorViewData(string errortxt, int idFaktury)
|
|---|
| 142 | {
|
|---|
| 143 | var er = new ErrorViewData { Error = errortxt, InvoiceId = idFaktury };
|
|---|
| 144 | return er;
|
|---|
| 145 | }
|
|---|
| 146 | public PlatnosciEcard CreateAndAddNewPyment(Invoice platnosc, Waluta waluta, Payer payer, string sessionId)
|
|---|
| 147 | {
|
|---|
| 148 | var newPayment = new PlatnosciEcard();
|
|---|
| 149 | newPayment.IDFaktury = platnosc.ID_faktury;
|
|---|
| 150 | newPayment.ORDERDESCRIPTION = platnosc.Faktura_Numer;
|
|---|
| 151 | newPayment.nip = platnosc.nip;
|
|---|
| 152 | newPayment.nrZlecenia = "";
|
|---|
| 153 | newPayment.AMOUNT = waluta.Amount;
|
|---|
| 154 | newPayment.CURRENCY = waluta.Currency;
|
|---|
| 155 | newPayment.SESSIONID = sessionId;
|
|---|
| 156 | newPayment.NAME = payer.FirstName;
|
|---|
| 157 | newPayment.SURNAME = payer.LastName;
|
|---|
| 158 | newPayment.AUTODEPOSIT = true;
|
|---|
| 159 | newPayment.LANGUAGE = GetLanguage(); //ustawiamy jezyk, w ktorym ma byc wyświetlany formularz na stronie eCard
|
|---|
| 160 | newPayment.CHARSET = KODOWANIE;
|
|---|
| 161 | newPayment.COUNTRY = KOD_POLSKA;
|
|---|
| 162 | newPayment.JS = true;
|
|---|
| 163 | newPayment.PAYMENTTYPE = CARDS;
|
|---|
| 164 | newPayment.Data = DateTime.Now;
|
|---|
| 165 | newPayment.Status = null;
|
|---|
| 166 | newPayment.Status_data = null;
|
|---|
| 167 |
|
|---|
| 168 | _repPayment.Insert(newPayment);
|
|---|
| 169 | return newPayment;
|
|---|
| 170 | }
|
|---|
| 171 |
|
|---|
| 172 | public ErrorViewData IsError(Invoice platnosc, String userName)
|
|---|
| 173 | {
|
|---|
| 174 | var errortxt = "";
|
|---|
| 175 |
|
|---|
| 176 | if (platnosc == null)
|
|---|
| 177 | errortxt = _translateManager.Translate("tlumaczenia", "brakdanych");
|
|---|
| 178 | else if (!UserIdentity(platnosc, userName))
|
|---|
| 179 | errortxt = _translateManager.Translate("tlumaczenia", "weryfikacja");
|
|---|
| 180 |
|
|---|
| 181 | return InitErrorViewData(errortxt, 0);
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | public string GetLanguage()
|
|---|
| 185 | {
|
|---|
| 186 | var language = Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName.ToUpper();
|
|---|
| 187 |
|
|---|
| 188 | if (language != "PL" && language != "EN" && language != "DE")
|
|---|
| 189 | return "PL"; //domyślny jezyk, w ktorym ma byc wyświetlony formularz na stronie eCard
|
|---|
| 190 |
|
|---|
| 191 | return language;
|
|---|
| 192 | }
|
|---|
| 193 | }
|
|---|
| 194 | } |
|---|