| 1 | using System;
|
|---|
| 2 | using System.Linq;
|
|---|
| 3 | using System.Web.Mvc;
|
|---|
| 4 | using Platnosci.Models;
|
|---|
| 5 | using Platnosci.Core.Linq;
|
|---|
| 6 | using Platnosci.Core.Interface;
|
|---|
| 7 |
|
|---|
| 8 | namespace Platnosci.Controllers
|
|---|
| 9 | {
|
|---|
| 10 |
|
|---|
| 11 | [Authorize]
|
|---|
| 12 | public class PlatnoscController : Controller
|
|---|
| 13 | {
|
|---|
| 14 | public const string ISPAID = "payment_deposited"; //transakcja potwierdzona do rozliczenia
|
|---|
| 15 | private readonly IRepository<vPlatnosciEcard> _repVPayment;
|
|---|
| 16 | private readonly IRepository<PlatnosciEcard> _repPayment;
|
|---|
| 17 | private readonly IRepository<PotwierdzeniaEcard> _repConfirm;
|
|---|
| 18 | private readonly ITranslateManager _translateManager;
|
|---|
| 19 | private readonly FunkcjePlatnosci _funkcjePlatnosci;
|
|---|
| 20 |
|
|---|
| 21 | public PlatnoscController()
|
|---|
| 22 | {
|
|---|
| 23 | _repVPayment = new Repository<vPlatnosciEcard>(new DataContext());
|
|---|
| 24 | _repPayment = new Repository<PlatnosciEcard>(new DataContext());
|
|---|
| 25 | _repConfirm = new Repository<PotwierdzeniaEcard>(new DataContext());
|
|---|
| 26 | _funkcjePlatnosci = new FunkcjePlatnosci(_repPayment);
|
|---|
| 27 | _translateManager = new Translation();
|
|---|
| 28 | }
|
|---|
| 29 | public PlatnoscController(IRepository<vPlatnosciEcard> repVPayment, IRepository<PlatnosciEcard> repPayment, IRepository<PotwierdzeniaEcard> repConfirm, ITranslateManager translate)
|
|---|
| 30 | {
|
|---|
| 31 | _repVPayment = repVPayment;
|
|---|
| 32 | _repPayment = repPayment;
|
|---|
| 33 | _repConfirm = repConfirm;
|
|---|
| 34 | _translateManager = translate;
|
|---|
| 35 | _funkcjePlatnosci = new FunkcjePlatnosci(_repPayment, _translateManager);
|
|---|
| 36 |
|
|---|
| 37 | }
|
|---|
| 38 | public ActionResult Show(string id, string language)
|
|---|
| 39 | {
|
|---|
| 40 | language = _funkcjePlatnosci.SetLanguage(language);
|
|---|
| 41 | var id1 = ConvertId(id);
|
|---|
| 42 |
|
|---|
| 43 | var platnosc = _repVPayment.Find(p => p.ID_faktury == id1).SingleOrDefault();
|
|---|
| 44 |
|
|---|
| 45 | var errorViewData = _funkcjePlatnosci.IsError(platnosc, HttpContext.User.Identity.Name);
|
|---|
| 46 | if (!String.IsNullOrEmpty(errorViewData.Error)) return View("Error1", errorViewData);
|
|---|
| 47 |
|
|---|
| 48 | var invoiceDeatailsViewData = InitInvoiceDetailsViewData(platnosc);
|
|---|
| 49 |
|
|---|
| 50 | var tablicaPotwierdzenia = _repConfirm.FindItemsByIdFaktury(id1);
|
|---|
| 51 | if (tablicaPotwierdzenia.Count > 0) //platnosc za fakture zostala uregulowana
|
|---|
| 52 | {
|
|---|
| 53 | var dataZaplaty = String.Format("{0:dd-MM-yyyy}", tablicaPotwierdzenia[0].AUTHTIME);
|
|---|
| 54 | invoiceDeatailsViewData.info = String.Format(_translateManager.Translate("tlumaczenia","zaplacono"), platnosc.Faktura_Numer, dataZaplaty);
|
|---|
| 55 | invoiceDeatailsViewData.termin = dataZaplaty;
|
|---|
| 56 | return View("Paid", invoiceDeatailsViewData);
|
|---|
| 57 | }
|
|---|
| 58 | return View(invoiceDeatailsViewData);
|
|---|
| 59 | }
|
|---|
| 60 | [Authorize]
|
|---|
| 61 | [AcceptVerbs(HttpVerbs.Post)]
|
|---|
| 62 | public ActionResult Show(Payer payer, string language)
|
|---|
| 63 | {
|
|---|
| 64 | language = _funkcjePlatnosci.SetLanguage(language);
|
|---|
| 65 |
|
|---|
| 66 | if (String.IsNullOrEmpty(payer.FirstName))
|
|---|
| 67 | ModelState.AddModelError("Payer.FirstName", _translateManager.Translate("tlumaczenia", "err_imieWK"));
|
|---|
| 68 | else if (payer.FirstName.Length > 25)
|
|---|
| 69 | ModelState.AddModelError("Payer.FirstName", String.Format(_translateManager.Translate("tlumaczenia", "ToLongValue"), "25"));
|
|---|
| 70 |
|
|---|
| 71 | if (String.IsNullOrEmpty(payer.LastName))
|
|---|
| 72 | ModelState.AddModelError("Payer.LastName", _translateManager.Translate("tlumaczenia", "err_nazwiskoWK"));
|
|---|
| 73 | else if (payer.LastName.Length > 30)
|
|---|
| 74 | ModelState.AddModelError("Payer.LastName", String.Format(_translateManager.Translate("tlumaczenia", "ToLongValue"), "30"));
|
|---|
| 75 |
|
|---|
| 76 | if (ModelState.IsValid == false)
|
|---|
| 77 | {
|
|---|
| 78 | var platnosc = _repVPayment.Find(p => p.ID_faktury == payer.Id_faktury).SingleOrDefault();
|
|---|
| 79 | var errorViewData = _funkcjePlatnosci.IsError(platnosc, HttpContext.User.Identity.Name);
|
|---|
| 80 |
|
|---|
| 81 | if (!String.IsNullOrEmpty(errorViewData.Error))
|
|---|
| 82 | return View("Error1", errorViewData);
|
|---|
| 83 |
|
|---|
| 84 | return View("Show", InitInvoiceDetailsViewData(platnosc));
|
|---|
| 85 | }
|
|---|
| 86 | return RedirectToAction("Merchant", "Merchant", payer);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | public ActionResult Ok(string id, string language, string o)
|
|---|
| 90 | {
|
|---|
| 91 | var order = ConvertId(o);
|
|---|
| 92 | language = _funkcjePlatnosci.SetLanguage(language);
|
|---|
| 93 | var id1 = ConvertId(id);
|
|---|
| 94 | var platnosc = _repVPayment.Find(p => p.ID_faktury == id1).SingleOrDefault();
|
|---|
| 95 |
|
|---|
| 96 | if (platnosc == null)
|
|---|
| 97 | return View("Error1", _funkcjePlatnosci.InitErrorViewData(_translateManager.Translate("tlumaczenia", "brakdanych"), 0));
|
|---|
| 98 |
|
|---|
| 99 | var invoiceDeatailsViewData = InitInvoiceDetailsViewData(platnosc);
|
|---|
| 100 |
|
|---|
| 101 | //sprawdzamy czy dla kombinacji ordernumber i idfaktury istnieje platnosc,
|
|---|
| 102 | //jesli tak, to sprawdzamy czy przyszlo potwierdzenie z eCardu.
|
|---|
| 103 |
|
|---|
| 104 | if (CheckConfirm(id1, order) == 0) //nie ma potwierdzenia z eCardu
|
|---|
| 105 | invoiceDeatailsViewData.info = String.Format(_translateManager.Translate("tlumaczenia", "blad1"), invoiceDeatailsViewData.vPlatnosciEcard.Faktura_Numer);
|
|---|
| 106 |
|
|---|
| 107 | else if (CheckConfirm(id1, order) == 2)
|
|---|
| 108 | invoiceDeatailsViewData.info = _translateManager.Translate("tlumaczenia", "weryfikacja");
|
|---|
| 109 |
|
|---|
| 110 | return View(invoiceDeatailsViewData);
|
|---|
| 111 | }
|
|---|
| 112 | public ActionResult Fail(string id, string language)
|
|---|
| 113 | {
|
|---|
| 114 | language = _funkcjePlatnosci.SetLanguage(language);
|
|---|
| 115 | var id1 = ConvertId(id);
|
|---|
| 116 | var platnosc = _repVPayment.Find(p => p.ID_faktury == id1).SingleOrDefault();
|
|---|
| 117 |
|
|---|
| 118 | if (platnosc == null)
|
|---|
| 119 | return View("Error1", _funkcjePlatnosci.InitErrorViewData(_translateManager.Translate("tlumaczenia", "brakdanych"), 0));
|
|---|
| 120 |
|
|---|
| 121 | var invoiceDeatailsViewData = InitInvoiceDetailsViewData(platnosc);
|
|---|
| 122 | return View(invoiceDeatailsViewData);
|
|---|
| 123 | }
|
|---|
| 124 | public ActionResult Form()
|
|---|
| 125 | {
|
|---|
| 126 | return View();
|
|---|
| 127 | }
|
|---|
| 128 | public ActionResult Status()
|
|---|
| 129 | {
|
|---|
| 130 | var potwierdzenie = new PotwierdzeniaEcard();
|
|---|
| 131 | var content = new ContentResult();
|
|---|
| 132 | try
|
|---|
| 133 | {
|
|---|
| 134 | if (!String.IsNullOrEmpty(Request.Form["APPROVALCODE"])) potwierdzenie.APPROVALCODE = Request.Form["APPROVALCODE"];
|
|---|
| 135 | if (!String.IsNullOrEmpty(Request.Form["AUTHTIME"])) potwierdzenie.AUTHTIME = Convert.ToDateTime(Request.Form["AUTHTIME"]);
|
|---|
| 136 | if (!String.IsNullOrEmpty(Request.Form["BIN"])) potwierdzenie.BIN = Request.Form["BIN"];
|
|---|
| 137 | if (!String.IsNullOrEmpty(Request.Form["COMMTYPE"])) potwierdzenie.COMMTYPE = Request.Form["COMMTYPE"];
|
|---|
| 138 | if (!String.IsNullOrEmpty(Request.Form["CURRENTSTATE"])) potwierdzenie.CURRENTSTATE = Request.Form["CURRENTSTATE"];
|
|---|
| 139 | if (!String.IsNullOrEmpty(Request.Form["DATATRANSMISJI"])) potwierdzenie.DATATRANSMISJI = Convert.ToDateTime(Request.Form["DATATRANSMISJI"]);
|
|---|
| 140 | if (!String.IsNullOrEmpty(Request.Form["EVENTTYPE"])) potwierdzenie.EVENTTYPE = Convert.ToBoolean(Request.Form["EVENTTYPE"]);
|
|---|
| 141 | if (!String.IsNullOrEmpty(Request.Form["MERCHANTNUMBER"])) potwierdzenie.MERCHANTNUMBER = Request.Form["MERCHANTNUMBER"];
|
|---|
| 142 | if (!String.IsNullOrEmpty(Request.Form["ORDERNUMBER"])) potwierdzenie.ORDERNUMBER = Convert.ToInt32(Request.Form["ORDERNUMBER"]);
|
|---|
| 143 | if (!String.IsNullOrEmpty(Request.Form["PAYMENTNUMBER"])) potwierdzenie.PAYMENTNUMBER = Convert.ToBoolean(Request.Form["PAYMENTNUMBER"]);
|
|---|
| 144 | if (!String.IsNullOrEmpty(Request.Form["PAYMENTTYPE"])) potwierdzenie.PAYMENTTYPE = Convert.ToBoolean(Request.Form["PAYMENTTYPE"]);
|
|---|
| 145 | if (!String.IsNullOrEmpty(Request.Form["PREVIOUSSTATE"])) potwierdzenie.PREVIOUSSTATE = Request.Form["PREVIOUSSTATE"];
|
|---|
| 146 | if (!String.IsNullOrEmpty(Request.Form["TYPE"])) potwierdzenie.TYPE = Request.Form["TYPE"];
|
|---|
| 147 | if (!String.IsNullOrEmpty(Request.Form["VALIDATIONCODE"])) potwierdzenie.VALIDATIONCODE = Request.Form["VALIDATIONCODE"];
|
|---|
| 148 | if (!String.IsNullOrEmpty(Request.Form["WITHCVC"])) potwierdzenie.WITHCVC = Request.Form["WITHCVC"];
|
|---|
| 149 |
|
|---|
| 150 | _repConfirm.Insert(potwierdzenie);
|
|---|
| 151 |
|
|---|
| 152 | if (potwierdzenie.ORDERNUMBER.HasValue)
|
|---|
| 153 | UpdateStatus(potwierdzenie.ORDERNUMBER.Value, potwierdzenie.CURRENTSTATE);
|
|---|
| 154 |
|
|---|
| 155 | content.Content = "OK";
|
|---|
| 156 | }
|
|---|
| 157 | catch(Exception ex)
|
|---|
| 158 | {
|
|---|
| 159 | content.Content = "FALSE " + ex.Message + " " + ex.GetType();
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | return content;
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | private static Payer InitPayer(int idFaktury)
|
|---|
| 166 | {
|
|---|
| 167 | var payer = new Payer {Id_faktury = idFaktury};
|
|---|
| 168 | return payer;
|
|---|
| 169 | }
|
|---|
| 170 |
|
|---|
| 171 | private InvoiceDetailsViewData InitInvoiceDetailsViewData(vPlatnosciEcard platnosc)
|
|---|
| 172 | {
|
|---|
| 173 | var invoiceDeatailsViewData = new InvoiceDetailsViewData();
|
|---|
| 174 | invoiceDeatailsViewData.vPlatnosciEcard = platnosc;
|
|---|
| 175 | invoiceDeatailsViewData.Payer = InitPayer(platnosc.ID_faktury);
|
|---|
| 176 | invoiceDeatailsViewData.brutto = _funkcjePlatnosci.BruttoToString(platnosc.Brutto, platnosc.waluta_brutto, platnosc.waluta_miano);
|
|---|
| 177 | return invoiceDeatailsViewData;
|
|---|
| 178 | }
|
|---|
| 179 |
|
|---|
| 180 | public int ConvertId(string id)
|
|---|
| 181 | {
|
|---|
| 182 | int id1;
|
|---|
| 183 | return Int32.TryParse(id, out id1) ? id1 : 0;
|
|---|
| 184 | }
|
|---|
| 185 |
|
|---|
| 186 | public void UpdateStatus(int ordernumber, string currentstate)
|
|---|
| 187 | {
|
|---|
| 188 | var platnosc = _repPayment.Find(p => p.ORDERNUMBER == ordernumber).SingleOrDefault();
|
|---|
| 189 |
|
|---|
| 190 | if (platnosc != null && currentstate == ISPAID)
|
|---|
| 191 | {
|
|---|
| 192 | platnosc.Status = true;
|
|---|
| 193 | platnosc.Status_data = DateTime.Now;
|
|---|
| 194 | _repPayment.SubmitChanges();
|
|---|
| 195 | }
|
|---|
| 196 | }
|
|---|
| 197 |
|
|---|
| 198 | public int CheckConfirm(int idfaktury, int order)
|
|---|
| 199 | {
|
|---|
| 200 | var pl = _repPayment.Find(p => p.ORDERNUMBER == order && p.IDFaktury == idfaktury).SingleOrDefault();
|
|---|
| 201 |
|
|---|
| 202 | if (pl != null)
|
|---|
| 203 | {
|
|---|
| 204 | var confirm = _repConfirm.Find(p => p.ORDERNUMBER == order).FirstOrDefault();
|
|---|
| 205 | if (confirm == null) return 0; //potwierdzenie nie przyszlo z eCardu
|
|---|
| 206 | }
|
|---|
| 207 | else
|
|---|
| 208 | {
|
|---|
| 209 | return 2; //nie ma platnosci o takim idfaktury i ordernumber
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | return 1; //potwierdzenie przyszlo z eCardu
|
|---|
| 213 | }
|
|---|
| 214 | }
|
|---|
| 215 | }
|
|---|