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