| [866] | 1 | using System;
|
|---|
| 2 | using System.Linq;
|
|---|
| 3 | using System.Web.Mvc;
|
|---|
| [970] | 4 | using adMoto.Payments.Core;
|
|---|
| 5 | using adMoto.Payments.Core.Data;
|
|---|
| 6 | using adMoto.Payments.Core.Interfaces;
|
|---|
| [971] | 7 | using adMoto.Payments.Web.Models;
|
|---|
| [969] | 8 | using Elmah;
|
|---|
| [866] | 9 |
|
|---|
| [971] | 10 | namespace adMoto.Payments.Web.Controllers
|
|---|
| [866] | 11 | {
|
|---|
| 12 | public class PlatnoscController : Controller
|
|---|
| 13 | {
|
|---|
| [883] | 14 | public const string ISPAID = "payment_deposited"; //transakcja potwierdzona do rozliczenia
|
|---|
| [970] | 15 | private readonly IRepository<Invoice> _repVPayment;
|
|---|
| [933] | 16 | private readonly IRepository<PlatnosciEcard> _repPayment;
|
|---|
| [903] | 17 | private readonly IRepository<PotwierdzeniaEcard> _repConfirm;
|
|---|
| [933] | 18 | private readonly ITranslateManager _translateManager;
|
|---|
| [971] | 19 | private readonly PaymentsUtils _paymentsUtils;
|
|---|
| [969] | 20 |
|
|---|
| [866] | 21 | public PlatnoscController()
|
|---|
| 22 | {
|
|---|
| [970] | 23 | _repVPayment = new Repository<Invoice>(new DataContext());
|
|---|
| [949] | 24 | _repPayment = new Repository<PlatnosciEcard>(new DataContext());
|
|---|
| 25 | _repConfirm = new Repository<PotwierdzeniaEcard>(new DataContext());
|
|---|
| [971] | 26 | _paymentsUtils = new PaymentsUtils(_repPayment);
|
|---|
| [969] | 27 | _translateManager = new Translation();
|
|---|
| [866] | 28 | }
|
|---|
| [970] | 29 | public PlatnoscController(IRepository<Invoice> repVPayment, IRepository<PlatnosciEcard> repPayment, IRepository<PotwierdzeniaEcard> repConfirm, ITranslateManager translate)
|
|---|
| [872] | 30 | {
|
|---|
| [896] | 31 | _repVPayment = repVPayment;
|
|---|
| 32 | _repPayment = repPayment;
|
|---|
| 33 | _repConfirm = repConfirm;
|
|---|
| [933] | 34 | _translateManager = translate;
|
|---|
| [971] | 35 | _paymentsUtils = new PaymentsUtils(_repPayment, _translateManager);
|
|---|
| [969] | 36 |
|
|---|
| [870] | 37 | }
|
|---|
| [967] | 38 |
|
|---|
| 39 | [Authorize]
|
|---|
| [866] | 40 | public ActionResult Show(string id, string language)
|
|---|
| 41 | {
|
|---|
| [971] | 42 | _paymentsUtils.SetLanguage(language);
|
|---|
| [933] | 43 | var id1 = ConvertId(id);
|
|---|
| [873] | 44 |
|
|---|
| [984] | 45 | var invoice = _repVPayment.Find(p => p.ID_faktury == id1).SingleOrDefault();
|
|---|
| [952] | 46 |
|
|---|
| [984] | 47 | var errorViewData = _paymentsUtils.IsError(invoice, HttpContext.User.Identity.Name);
|
|---|
| [969] | 48 | if (!String.IsNullOrEmpty(errorViewData.Error))
|
|---|
| [957] | 49 | return View("Error1", errorViewData);
|
|---|
| [883] | 50 |
|
|---|
| [984] | 51 | var invoiceDeatailsViewData = InitInvoiceDetailsViewData(invoice);
|
|---|
| [969] | 52 |
|
|---|
| [984] | 53 | var tablicaPotwierdzenia = _repConfirm.FindItemsByIdFaktury(id1); //szukamy uregulowanej faktury
|
|---|
| [933] | 54 | if (tablicaPotwierdzenia.Count > 0) //platnosc za fakture zostala uregulowana
|
|---|
| [866] | 55 | {
|
|---|
| [933] | 56 | var dataZaplaty = String.Format("{0:dd-MM-yyyy}", tablicaPotwierdzenia[0].AUTHTIME);
|
|---|
| [984] | 57 | invoiceDeatailsViewData.Info = String.Format(_translateManager.Translate("tlumaczenia", "zaplacono"), invoice.Faktura_Numer, dataZaplaty);
|
|---|
| [971] | 58 | invoiceDeatailsViewData.Termin = dataZaplaty;
|
|---|
| [883] | 59 | return View("Paid", invoiceDeatailsViewData);
|
|---|
| [969] | 60 | }
|
|---|
| 61 | return View(invoiceDeatailsViewData);
|
|---|
| 62 | }
|
|---|
| [970] | 63 |
|
|---|
| [866] | 64 | [Authorize]
|
|---|
| 65 | [AcceptVerbs(HttpVerbs.Post)]
|
|---|
| 66 | public ActionResult Show(Payer payer, string language)
|
|---|
| 67 | {
|
|---|
| [971] | 68 | _paymentsUtils.SetLanguage(language);
|
|---|
| [969] | 69 |
|
|---|
| [866] | 70 | if (String.IsNullOrEmpty(payer.FirstName))
|
|---|
| [933] | 71 | ModelState.AddModelError("Payer.FirstName", _translateManager.Translate("tlumaczenia", "err_imieWK"));
|
|---|
| [956] | 72 | else if (payer.FirstName.Length > 25)
|
|---|
| 73 | ModelState.AddModelError("Payer.FirstName", String.Format(_translateManager.Translate("tlumaczenia", "ToLongValue"), "25"));
|
|---|
| [969] | 74 |
|
|---|
| [866] | 75 | if (String.IsNullOrEmpty(payer.LastName))
|
|---|
| [933] | 76 | ModelState.AddModelError("Payer.LastName", _translateManager.Translate("tlumaczenia", "err_nazwiskoWK"));
|
|---|
| [956] | 77 | else if (payer.LastName.Length > 30)
|
|---|
| 78 | ModelState.AddModelError("Payer.LastName", String.Format(_translateManager.Translate("tlumaczenia", "ToLongValue"), "30"));
|
|---|
| 79 |
|
|---|
| [866] | 80 | if (ModelState.IsValid == false)
|
|---|
| 81 | {
|
|---|
| [984] | 82 | var invoice = _repVPayment.Find(p => p.ID_faktury == payer.Id_faktury).SingleOrDefault();
|
|---|
| 83 | var errorViewData = _paymentsUtils.IsError(invoice, HttpContext.User.Identity.Name);
|
|---|
| [933] | 84 |
|
|---|
| [956] | 85 | if (!String.IsNullOrEmpty(errorViewData.Error))
|
|---|
| 86 | return View("Error1", errorViewData);
|
|---|
| 87 |
|
|---|
| [984] | 88 | return View("Show", InitInvoiceDetailsViewData(invoice));
|
|---|
| [956] | 89 | }
|
|---|
| [933] | 90 | return RedirectToAction("Merchant", "Merchant", payer);
|
|---|
| [866] | 91 | }
|
|---|
| [933] | 92 |
|
|---|
| [967] | 93 | public ActionResult Ok(string id, string order, string language)
|
|---|
| [866] | 94 | {
|
|---|
| [969] | 95 | var orderId = ConvertId(order);
|
|---|
| [971] | 96 | _paymentsUtils.SetLanguage(language);
|
|---|
| [969] | 97 | var id1 = ConvertId(id);
|
|---|
| [984] | 98 | var invoice = _repVPayment.Find(p => p.ID_faktury == id1).SingleOrDefault();
|
|---|
| [955] | 99 |
|
|---|
| [984] | 100 | if (invoice == null)
|
|---|
| [971] | 101 | return View("Error1", _paymentsUtils.InitErrorViewData(_translateManager.Translate("tlumaczenia", "brakdanych"), 0));
|
|---|
| [969] | 102 |
|
|---|
| [984] | 103 | var invoiceDeatailsViewData = InitInvoiceDetailsViewData(invoice);
|
|---|
| [926] | 104 |
|
|---|
| [956] | 105 | //sprawdzamy czy dla kombinacji ordernumber i idfaktury istnieje platnosc,
|
|---|
| [926] | 106 | //jesli tak, to sprawdzamy czy przyszlo potwierdzenie z eCardu.
|
|---|
| [969] | 107 |
|
|---|
| [967] | 108 | if (CheckConfirm(id1, orderId) == 0) //nie ma potwierdzenia z eCardu
|
|---|
| [971] | 109 | invoiceDeatailsViewData.Info = String.Format(_translateManager.Translate("tlumaczenia", "blad1"), invoiceDeatailsViewData.Invoice.Faktura_Numer);
|
|---|
| [969] | 110 |
|
|---|
| 111 | else if (CheckConfirm(id1, orderId) == 2)
|
|---|
| [971] | 112 | invoiceDeatailsViewData.Info = _translateManager.Translate("tlumaczenia", "weryfikacja");
|
|---|
| [969] | 113 |
|
|---|
| 114 | return View(invoiceDeatailsViewData);
|
|---|
| [866] | 115 | }
|
|---|
| [967] | 116 |
|
|---|
| 117 | public ActionResult Fail(string id, string language)
|
|---|
| [871] | 118 | {
|
|---|
| [971] | 119 | _paymentsUtils.SetLanguage(language);
|
|---|
| [933] | 120 | var id1 = ConvertId(id);
|
|---|
| [984] | 121 | var invoice = _repVPayment.Find(p => p.ID_faktury == id1).SingleOrDefault();
|
|---|
| [967] | 122 |
|
|---|
| [984] | 123 | if (invoice == null)
|
|---|
| [967] | 124 | return View("Error1",
|
|---|
| [971] | 125 | _paymentsUtils.InitErrorViewData(
|
|---|
| [967] | 126 | _translateManager.Translate("tlumaczenia", "brakdanych"), 0));
|
|---|
| 127 |
|
|---|
| [984] | 128 | var invoiceDeatailsViewData = InitInvoiceDetailsViewData(invoice);
|
|---|
| [967] | 129 | return View(invoiceDeatailsViewData);
|
|---|
| [871] | 130 | }
|
|---|
| [967] | 131 |
|
|---|
| [969] | 132 | public ActionResult Form()
|
|---|
| 133 | {
|
|---|
| 134 | return View();
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| [933] | 137 | private static Payer InitPayer(int idFaktury)
|
|---|
| [868] | 138 | {
|
|---|
| [969] | 139 | var payer = new Payer { Id_faktury = idFaktury };
|
|---|
| [868] | 140 | return payer;
|
|---|
| 141 | }
|
|---|
| [933] | 142 |
|
|---|
| [984] | 143 | private InvoiceDetailsViewData InitInvoiceDetailsViewData(Invoice invoice)
|
|---|
| [868] | 144 | {
|
|---|
| 145 | var invoiceDeatailsViewData = new InvoiceDetailsViewData();
|
|---|
| [984] | 146 | invoiceDeatailsViewData.Invoice = invoice;
|
|---|
| 147 | invoiceDeatailsViewData.Payer = InitPayer(invoice.ID_faktury);
|
|---|
| 148 | invoiceDeatailsViewData.Brutto = _paymentsUtils.BruttoToString(invoice.Brutto, invoice.waluta_brutto, invoice.waluta_miano);
|
|---|
| [868] | 149 | return invoiceDeatailsViewData;
|
|---|
| 150 | }
|
|---|
| [933] | 151 |
|
|---|
| [873] | 152 | public int ConvertId(string id)
|
|---|
| 153 | {
|
|---|
| [933] | 154 | int id1;
|
|---|
| 155 | return Int32.TryParse(id, out id1) ? id1 : 0;
|
|---|
| [873] | 156 | }
|
|---|
| [933] | 157 |
|
|---|
| [883] | 158 | public void UpdateStatus(int ordernumber, string currentstate)
|
|---|
| [873] | 159 | {
|
|---|
| [984] | 160 | var payment = _repPayment.Find(p => p.ORDERNUMBER == ordernumber).SingleOrDefault();
|
|---|
| [933] | 161 |
|
|---|
| [984] | 162 | if (payment != null && currentstate == ISPAID)
|
|---|
| [956] | 163 | {
|
|---|
| [984] | 164 | payment.Status = true;
|
|---|
| 165 | payment.Status_data = DateTime.Now;
|
|---|
| [956] | 166 | _repPayment.SubmitChanges();
|
|---|
| 167 | }
|
|---|
| [911] | 168 | }
|
|---|
| [933] | 169 |
|
|---|
| [926] | 170 | public int CheckConfirm(int idfaktury, int order)
|
|---|
| 171 | {
|
|---|
| [984] | 172 | var payment = _repPayment.Find(p => p.ORDERNUMBER == order && p.IDFaktury == idfaktury).SingleOrDefault();
|
|---|
| [933] | 173 |
|
|---|
| [984] | 174 | if (payment != null)
|
|---|
| [926] | 175 | {
|
|---|
| [933] | 176 | var confirm = _repConfirm.Find(p => p.ORDERNUMBER == order).FirstOrDefault();
|
|---|
| [926] | 177 | if (confirm == null) return 0; //potwierdzenie nie przyszlo z eCardu
|
|---|
| 178 | }
|
|---|
| 179 | else
|
|---|
| 180 | {
|
|---|
| 181 | return 2; //nie ma platnosci o takim idfaktury i ordernumber
|
|---|
| 182 | }
|
|---|
| [969] | 183 |
|
|---|
| [926] | 184 | return 1; //potwierdzenie przyszlo z eCardu
|
|---|
| [969] | 185 | }
|
|---|
| [866] | 186 | }
|
|---|
| [969] | 187 | } |
|---|