| 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 | var platnosc = _repVPayment.Find(p => p.ID_faktury == payer.Id_faktury).SingleOrDefault();
|
|---|
| 67 | var errorViewData = _funkcjePlatnosci.IsError(platnosc, HttpContext.User.Identity.Name);
|
|---|
| 68 | if (!String.IsNullOrEmpty(errorViewData.Error)) return View("Error1", errorViewData);
|
|---|
| 69 |
|
|---|
| 70 | if (String.IsNullOrEmpty(payer.FirstName))
|
|---|
| 71 | ModelState.AddModelError("Payer.FirstName", _translateManager.Translate("tlumaczenia", "err_imieWK"));
|
|---|
| 72 |
|
|---|
| 73 | if (String.IsNullOrEmpty(payer.LastName))
|
|---|
| 74 | ModelState.AddModelError("Payer.LastName", _translateManager.Translate("tlumaczenia", "err_nazwiskoWK"));
|
|---|
| 75 |
|
|---|
| 76 | if (payer.FirstName.Length > 25)
|
|---|
| 77 | ModelState.AddModelError("Payer.FirstName", String.Format(_translateManager.Translate("tlumaczenia", "ToLongValue"),"25"));
|
|---|
| 78 |
|
|---|
| 79 | if (payer.LastName.Length > 30)
|
|---|
| 80 | ModelState.AddModelError("Payer.LastName", String.Format(_translateManager.Translate("tlumaczenia", "ToLongValue"),"30"));
|
|---|
| 81 |
|
|---|
| 82 | if (ModelState.IsValid == false)
|
|---|
| 83 | {
|
|---|
| 84 | var kwota = _funkcjePlatnosci.BruttoToString(platnosc.Brutto, platnosc.waluta_brutto, platnosc.waluta_miano);
|
|---|
| 85 | var viewData = InitInvoiceDetailsViewData(platnosc);
|
|---|
| 86 | return View("Show",viewData);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | return RedirectToAction("Merchant", "Merchant", payer);
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | public ActionResult Ok(string id, string language, string o)
|
|---|
| 93 | {
|
|---|
| 94 | var order = ConvertId(o);
|
|---|
| 95 |
|
|---|
| 96 | language = _funkcjePlatnosci.SetLanguage(language);
|
|---|
| 97 |
|
|---|
| 98 | var id1 = ConvertId(id);
|
|---|
| 99 |
|
|---|
| 100 | var platnosc = _repVPayment.Find(p => p.ID_faktury == id1).SingleOrDefault();
|
|---|
| 101 |
|
|---|
| 102 | if (!String.IsNullOrEmpty(IsError(platnosc).Error)) return View("Error1", IsError(platnosc));
|
|---|
| 103 |
|
|---|
| 104 | var invoiceDeatailsViewData = InitInvoiceDetailsViewData(platnosc);
|
|---|
| 105 |
|
|---|
| 106 | //sprawdzamy czy dla kombinacji ordernumber i idfaktury istnieje platnosc,
|
|---|
| 107 | //jesli tak, to sprawdzamy czy przyszlo potwierdzenie z eCardu.
|
|---|
| 108 | if (order > 0)
|
|---|
| 109 | if (CheckConfirm(id1, order) == 0) //nie ma potwierdzenia z eCardu
|
|---|
| 110 | invoiceDeatailsViewData.info = String.Format(_translateManager.Translate("tlumaczenia", "blad1"), invoiceDeatailsViewData.vPlatnosciEcard.Faktura_Numer);
|
|---|
| 111 |
|
|---|
| 112 | if (order == 0 || (CheckConfirm(id1, order) == 2) && order > 0 )
|
|---|
| 113 | invoiceDeatailsViewData.info = _translateManager.Translate("tlumaczenia", "weryfikacja");
|
|---|
| 114 |
|
|---|
| 115 | return View(invoiceDeatailsViewData);
|
|---|
| 116 | }
|
|---|
| 117 | public ActionResult Fail(string id, string language)
|
|---|
| 118 | {
|
|---|
| 119 | language = _funkcjePlatnosci.SetLanguage(language);
|
|---|
| 120 | var id1 = ConvertId(id);
|
|---|
| 121 | var platnosc = _repVPayment.Find(p => p.ID_faktury == id1).SingleOrDefault();
|
|---|
| 122 | if (!String.IsNullOrEmpty(IsError(platnosc).Error)) return View("Error1", IsError(platnosc));
|
|---|
| 123 |
|
|---|
| 124 | var invoiceDeatailsViewData = InitInvoiceDetailsViewData(platnosc);
|
|---|
| 125 | return View(invoiceDeatailsViewData);
|
|---|
| 126 | }
|
|---|
| 127 | public ActionResult Form()
|
|---|
| 128 | {
|
|---|
| 129 | return View();
|
|---|
| 130 | }
|
|---|
| 131 | public ActionResult Status()
|
|---|
| 132 | {
|
|---|
| 133 | var potwierdzenie = new PotwierdzeniaEcard();
|
|---|
| 134 | var content = new ContentResult();
|
|---|
| 135 | try
|
|---|
| 136 | {
|
|---|
| 137 | if (!String.IsNullOrEmpty(Request.Form["APPROVALCODE"])) potwierdzenie.APPROVALCODE = Request.Form["APPROVALCODE"];
|
|---|
| 138 | if (!String.IsNullOrEmpty(Request.Form["AUTHTIME"])) potwierdzenie.AUTHTIME = Convert.ToDateTime(Request.Form["AUTHTIME"]);
|
|---|
| 139 | if (!String.IsNullOrEmpty(Request.Form["BIN"])) potwierdzenie.BIN = Request.Form["BIN"];
|
|---|
| 140 | if (!String.IsNullOrEmpty(Request.Form["COMMTYPE"])) potwierdzenie.COMMTYPE = Request.Form["COMMTYPE"];
|
|---|
| 141 | if (!String.IsNullOrEmpty(Request.Form["CURRENTSTATE"])) potwierdzenie.CURRENTSTATE = Request.Form["CURRENTSTATE"];
|
|---|
| 142 | if (!String.IsNullOrEmpty(Request.Form["DATATRANSMISJI"])) potwierdzenie.DATATRANSMISJI = Convert.ToDateTime(Request.Form["DATATRANSMISJI"]);
|
|---|
| 143 | if (!String.IsNullOrEmpty(Request.Form["EVENTTYPE"])) potwierdzenie.EVENTTYPE = Convert.ToBoolean(Request.Form["EVENTTYPE"]);
|
|---|
| 144 | if (!String.IsNullOrEmpty(Request.Form["MERCHANTNUMBER"])) potwierdzenie.MERCHANTNUMBER = Request.Form["MERCHANTNUMBER"];
|
|---|
| 145 | if (!String.IsNullOrEmpty(Request.Form["ORDERNUMBER"])) potwierdzenie.ORDERNUMBER = Convert.ToInt32(Request.Form["ORDERNUMBER"]);
|
|---|
| 146 | if (!String.IsNullOrEmpty(Request.Form["PAYMENTNUMBER"])) potwierdzenie.PAYMENTNUMBER = Convert.ToBoolean(Request.Form["PAYMENTNUMBER"]);
|
|---|
| 147 | if (!String.IsNullOrEmpty(Request.Form["PAYMENTTYPE"])) potwierdzenie.PAYMENTTYPE = Convert.ToBoolean(Request.Form["PAYMENTTYPE"]);
|
|---|
| 148 | if (!String.IsNullOrEmpty(Request.Form["PREVIOUSSTATE"])) potwierdzenie.PREVIOUSSTATE = Request.Form["PREVIOUSSTATE"];
|
|---|
| 149 | if (!String.IsNullOrEmpty(Request.Form["TYPE"])) potwierdzenie.TYPE = Request.Form["TYPE"];
|
|---|
| 150 | if (!String.IsNullOrEmpty(Request.Form["VALIDATIONCODE"])) potwierdzenie.VALIDATIONCODE = Request.Form["VALIDATIONCODE"];
|
|---|
| 151 | if (!String.IsNullOrEmpty(Request.Form["WITHCVC"])) potwierdzenie.WITHCVC = Request.Form["WITHCVC"];
|
|---|
| 152 |
|
|---|
| 153 | _repConfirm.Insert(potwierdzenie);
|
|---|
| 154 |
|
|---|
| 155 | if (potwierdzenie.ORDERNUMBER.HasValue)
|
|---|
| 156 | UpdateStatus(potwierdzenie.ORDERNUMBER.Value, potwierdzenie.CURRENTSTATE);
|
|---|
| 157 |
|
|---|
| 158 | content.Content = "OK";
|
|---|
| 159 | }
|
|---|
| 160 | catch(Exception ex)
|
|---|
| 161 | {
|
|---|
| 162 | content.Content = "FALSE " + ex.Message + " " + ex.GetType();
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | return content;
|
|---|
| 166 | }
|
|---|
| 167 |
|
|---|
| 168 | private static Payer InitPayer(int idFaktury)
|
|---|
| 169 | {
|
|---|
| 170 | var payer = new Payer {Id_faktury = idFaktury};
|
|---|
| 171 | return payer;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | private InvoiceDetailsViewData InitInvoiceDetailsViewData(vPlatnosciEcard platnosc)
|
|---|
| 175 | {
|
|---|
| 176 | var invoiceDeatailsViewData = new InvoiceDetailsViewData();
|
|---|
| 177 | invoiceDeatailsViewData.vPlatnosciEcard = platnosc;
|
|---|
| 178 | invoiceDeatailsViewData.Payer = InitPayer(platnosc.ID_faktury);
|
|---|
| 179 | invoiceDeatailsViewData.brutto = _funkcjePlatnosci.BruttoToString(platnosc.Brutto, platnosc.waluta_brutto, platnosc.waluta_miano);
|
|---|
| 180 | return invoiceDeatailsViewData;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | public int ConvertId(string id)
|
|---|
| 184 | {
|
|---|
| 185 | int id1;
|
|---|
| 186 | return Int32.TryParse(id, out id1) ? id1 : 0;
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | public ErrorViewData IsError(vPlatnosciEcard platnosc)
|
|---|
| 190 | {
|
|---|
| 191 | var errortxt = "";
|
|---|
| 192 |
|
|---|
| 193 | if (platnosc == null)
|
|---|
| 194 | errortxt = _translateManager.Translate("tlumaczenia", "brakdanych");
|
|---|
| 195 | else if (!_funkcjePlatnosci.UserIdentity(platnosc, HttpContext.User.Identity.Name))
|
|---|
| 196 | errortxt = _translateManager.Translate("tlumaczenia","weryfikacja");
|
|---|
| 197 |
|
|---|
| 198 | return _funkcjePlatnosci.InitErrorViewData(errortxt,0);
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | public void UpdateStatus(int ordernumber, string currentstate)
|
|---|
| 202 | {
|
|---|
| 203 | var platnosc = _repPayment.Find(p => p.ORDERNUMBER == ordernumber).SingleOrDefault();
|
|---|
| 204 |
|
|---|
| 205 | if (platnosc == null || currentstate != ISPAID) return;
|
|---|
| 206 |
|
|---|
| 207 | platnosc.Status = true;
|
|---|
| 208 | platnosc.Status_data = DateTime.Now;
|
|---|
| 209 | _repPayment.SubmitChanges();
|
|---|
| 210 | }
|
|---|
| 211 |
|
|---|
| 212 | public int CheckConfirm(int idfaktury, int order)
|
|---|
| 213 | {
|
|---|
| 214 | var pl = _repPayment.Find(p => p.ORDERNUMBER == order && p.IDFaktury == idfaktury).SingleOrDefault();
|
|---|
| 215 |
|
|---|
| 216 | if (pl != null)
|
|---|
| 217 | {
|
|---|
| 218 | var confirm = _repConfirm.Find(p => p.ORDERNUMBER == order).FirstOrDefault();
|
|---|
| 219 | if (confirm == null) return 0; //potwierdzenie nie przyszlo z eCardu
|
|---|
| 220 | }
|
|---|
| 221 | else
|
|---|
| 222 | {
|
|---|
| 223 | return 2; //nie ma platnosci o takim idfaktury i ordernumber
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | return 1; //potwierdzenie przyszlo z eCardu
|
|---|
| 227 | }
|
|---|
| 228 | }
|
|---|
| 229 | }
|
|---|