| 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_brutto, string miano)
|
|---|
| 43 | {
|
|---|
| 44 | var brutto = String.Format("{0:0.00}", kwota.ToString().Replace(",", ".")) + " PLN ";
|
|---|
| 45 |
|
|---|
| 46 | if (!String.IsNullOrEmpty(miano))
|
|---|
| 47 | if (waluta_brutto > 0 && miano.ToUpper() != "PLN")
|
|---|
| 48 | brutto += "(" + (waluta_brutto.ToString()).Replace(",", ".") + " " + miano.ToUpper() + ")";
|
|---|
| 49 |
|
|---|
| 50 | return brutto;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | public bool UserIdentity(Invoice invoice, string userName)
|
|---|
| 54 | {
|
|---|
| 55 | return invoice != null && invoice.nip == userName;
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | public Waluta SetAmount(Invoice invoice)
|
|---|
| 59 | {
|
|---|
| 60 | var waluta = new Waluta {Currency = GetCurrency(invoice.waluta_miano)};
|
|---|
| 61 |
|
|---|
| 62 | waluta.Amount = waluta.Currency == PLN ? Convert.ToInt32(invoice.Brutto * 100) : Convert.ToInt32(invoice.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 void SetLanguage(string lang)
|
|---|
| 86 | {
|
|---|
| 87 | if (lang == Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName.ToLower()) return;
|
|---|
| 88 |
|
|---|
| 89 | string culture;
|
|---|
| 90 | switch (lang)
|
|---|
| 91 | {
|
|---|
| 92 | case "en":
|
|---|
| 93 | culture = "en-GB";
|
|---|
| 94 | break;
|
|---|
| 95 | case "de":
|
|---|
| 96 | culture = "de-DE";
|
|---|
| 97 | break;
|
|---|
| 98 | default:
|
|---|
| 99 | culture = "pl-PL";
|
|---|
| 100 | break;
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfoByIetfLanguageTag(culture);
|
|---|
| 104 | Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfoByIetfLanguageTag(culture);
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | public string SetTitle()
|
|---|
| 108 | {
|
|---|
| 109 | var str = "";
|
|---|
| 110 | var css = ConfigurationManager.AppSettings["Css"];
|
|---|
| 111 |
|
|---|
| 112 | switch (css)
|
|---|
| 113 | {
|
|---|
| 114 | case "truck":
|
|---|
| 115 | str = HttpContext.GetGlobalResourceObject("tlumaczenia", "adresTruck") + " - ";
|
|---|
| 116 | break;
|
|---|
| 117 | case "admoto":
|
|---|
| 118 | str = HttpContext.GetGlobalResourceObject("tlumaczenia", "adresAdmoto") + " - ";
|
|---|
| 119 | break;
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | return str;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | public void SetUserLogger(string nip, string faktura)
|
|---|
| 126 | {
|
|---|
| 127 |
|
|---|
| 128 | var user = nip + "-" + faktura;
|
|---|
| 129 | Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(user, ""), null);
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | public ErrorViewData InitErrorViewData(string errortxt, int idFaktury)
|
|---|
| 133 | {
|
|---|
| 134 | var er = new ErrorViewData { Error = errortxt, InvoiceId = idFaktury };
|
|---|
| 135 | return er;
|
|---|
| 136 | }
|
|---|
| 137 | public PlatnosciEcard CreateAndAddNewPyment(Invoice invoice, Waluta waluta, Payer payer, string sessionId)
|
|---|
| 138 | {
|
|---|
| 139 | var payment = new PlatnosciEcard();
|
|---|
| 140 | payment.IDFaktury = invoice.ID_faktury;
|
|---|
| 141 | payment.ORDERDESCRIPTION = invoice.Faktura_Numer;
|
|---|
| 142 | payment.nip = invoice.nip;
|
|---|
| 143 | payment.nrZlecenia = "";
|
|---|
| 144 | payment.AMOUNT = waluta.Amount;
|
|---|
| 145 | payment.CURRENCY = waluta.Currency;
|
|---|
| 146 | payment.SESSIONID = sessionId;
|
|---|
| 147 | payment.NAME = payer.FirstName;
|
|---|
| 148 | payment.SURNAME = payer.LastName;
|
|---|
| 149 | payment.AUTODEPOSIT = true;
|
|---|
| 150 | payment.LANGUAGE = GetLanguage(); //ustawiamy jezyk, w ktorym ma byc wyświetlany formularz na stronie eCard
|
|---|
| 151 | payment.CHARSET = KODOWANIE;
|
|---|
| 152 | payment.COUNTRY = KOD_POLSKA;
|
|---|
| 153 | payment.JS = true;
|
|---|
| 154 | payment.PAYMENTTYPE = CARDS;
|
|---|
| 155 | payment.Data = DateTime.Now;
|
|---|
| 156 | payment.Status = null;
|
|---|
| 157 | payment.Status_data = null;
|
|---|
| 158 |
|
|---|
| 159 | _repPayment.Insert(payment);
|
|---|
| 160 | return payment;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | public ErrorViewData IsError(Invoice invoice, String userName)
|
|---|
| 164 | {
|
|---|
| 165 | var errortxt = "";
|
|---|
| 166 |
|
|---|
| 167 | if (invoice == null)
|
|---|
| 168 | errortxt = _translateManager.Translate("tlumaczenia", "brakdanych");
|
|---|
| 169 | else if (!UserIdentity(invoice, userName))
|
|---|
| 170 | errortxt = _translateManager.Translate("tlumaczenia", "weryfikacja");
|
|---|
| 171 |
|
|---|
| 172 | return InitErrorViewData(errortxt, 0);
|
|---|
| 173 | }
|
|---|
| 174 |
|
|---|
| 175 | public string GetLanguage()
|
|---|
| 176 | {
|
|---|
| 177 | var language = Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName.ToUpper();
|
|---|
| 178 |
|
|---|
| 179 | if (language != "PL" && language != "EN" && language != "DE")
|
|---|
| 180 | return "PL"; //domyślny jezyk, w ktorym ma byc wyświetlony formularz na stronie eCard
|
|---|
| 181 |
|
|---|
| 182 | return language;
|
|---|
| 183 | }
|
|---|
| 184 | }
|
|---|
| 185 | } |
|---|