| 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 DataContext1());
|
|---|
| 24 | _repPayment = new Repository<PlatnosciEcard>(new DataContext1());
|
|---|
| 25 | _repConfirm = new Repository<PotwierdzeniaEcard>(new DataContext1());
|
|---|
| 26 | _funkcjePlatnosci = new FunkcjePlatnosci();
|
|---|
| 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 | _funkcjePlatnosci = new FunkcjePlatnosci();
|
|---|
| 35 | _translateManager = translate;
|
|---|
| 36 | }
|
|---|
| 37 | public ActionResult Show(string id, string language)
|
|---|
| 38 | {
|
|---|
| 39 | language = _funkcjePlatnosci.setLanguage(language);
|
|---|
| 40 | var id1 = ConvertId(id);
|
|---|
| 41 |
|
|---|
| 42 | var platnosc = _repVPayment.Find(p => p.ID_faktury == id1).SingleOrDefault();
|
|---|
| 43 | if (!String.IsNullOrEmpty(IsError(platnosc).Error)) return View("Error1", IsError(platnosc));
|
|---|
| 44 |
|
|---|
| 45 | var kwota = _funkcjePlatnosci.BruttoToString(platnosc.Brutto, platnosc.waluta_brutto, platnosc.waluta_miano);
|
|---|
| 46 |
|
|---|
| 47 | var payer = InitPayer(platnosc.ID_faktury);
|
|---|
| 48 |
|
|---|
| 49 | var invoiceDeatailsViewData = InitInvoiceDetailsViewData(platnosc, payer, "", kwota);
|
|---|
| 50 |
|
|---|
| 51 | var tablicaPotwierdzenia = _repConfirm.FindItemsByIdFaktury(id1);
|
|---|
| 52 | if (tablicaPotwierdzenia.Count > 0) //platnosc za fakture zostala uregulowana
|
|---|
| 53 | {
|
|---|
| 54 | var dataZaplaty = String.Format("{0:dd-MM-yyyy}", tablicaPotwierdzenia[0].AUTHTIME);
|
|---|
| 55 | invoiceDeatailsViewData.info = String.Format(_translateManager.Translate("tlumaczenia","zaplacono"), platnosc.Faktura_Numer, dataZaplaty);
|
|---|
| 56 | invoiceDeatailsViewData.termin = dataZaplaty;
|
|---|
| 57 | return View("Paid", invoiceDeatailsViewData);
|
|---|
| 58 | }
|
|---|
| 59 | return View(invoiceDeatailsViewData);
|
|---|
| 60 | }
|
|---|
| 61 | [Authorize]
|
|---|
| 62 | [AcceptVerbs(HttpVerbs.Post)]
|
|---|
| 63 | public ActionResult Show(Payer payer, string language)
|
|---|
| 64 | {
|
|---|
| 65 | language = _funkcjePlatnosci.setLanguage(language);
|
|---|
| 66 |
|
|---|
| 67 | var platnosc = _repVPayment.Find(p => p.ID_faktury == payer.Id_faktury).SingleOrDefault();
|
|---|
| 68 |
|
|---|
| 69 | if (!String.IsNullOrEmpty(IsError(platnosc).Error)) return View("Error1", IsError(platnosc));
|
|---|
| 70 |
|
|---|
| 71 | if (String.IsNullOrEmpty(payer.FirstName))
|
|---|
| 72 | {
|
|---|
| 73 | ModelState.AddModelError("Payer.FirstName", _translateManager.Translate("tlumaczenia", "err_imieWK"));
|
|---|
| 74 | }
|
|---|
| 75 | if (String.IsNullOrEmpty(payer.LastName))
|
|---|
| 76 | {
|
|---|
| 77 | ModelState.AddModelError("Payer.LastName", _translateManager.Translate("tlumaczenia", "err_nazwiskoWK"));
|
|---|
| 78 | }
|
|---|
| 79 | if (ModelState.IsValid == false)
|
|---|
| 80 | {
|
|---|
| 81 | var kwota = _funkcjePlatnosci.BruttoToString(platnosc.Brutto, platnosc.waluta_brutto, platnosc.waluta_miano);
|
|---|
| 82 | var viewData = InitInvoiceDetailsViewData(platnosc, payer, "", kwota );
|
|---|
| 83 | return View("Show",viewData);
|
|---|
| 84 | }
|
|---|
| 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 |
|
|---|
| 93 | language = _funkcjePlatnosci.setLanguage(language);
|
|---|
| 94 |
|
|---|
| 95 | var id1 = ConvertId(id);
|
|---|
| 96 |
|
|---|
| 97 | var platnosc = _repVPayment.Find(p => p.ID_faktury == id1).SingleOrDefault();
|
|---|
| 98 |
|
|---|
| 99 | if (!String.IsNullOrEmpty(IsError(platnosc).Error)) return View("Error1", IsError(platnosc));
|
|---|
| 100 |
|
|---|
| 101 | var invoiceDeatailsViewData = InitInvoiceDetailsViewData(platnosc, null, "" , "");
|
|---|
| 102 |
|
|---|
| 103 | //sprawdzamy czy dla kombinacji ordernumber i idfaktury istnieje platnosc,
|
|---|
| 104 | //jesli tak, to sprawdzamy czy przyszlo potwierdzenie z eCardu.
|
|---|
| 105 | if (order > 0)
|
|---|
| 106 | if (CheckConfirm(id1, order) == 0) //nie ma potwierdzenia z eCardu
|
|---|
| 107 | invoiceDeatailsViewData.info = String.Format(_translateManager.Translate("tlumaczenia", "blad1"), invoiceDeatailsViewData.vPlatnosciEcard.Faktura_Numer);
|
|---|
| 108 |
|
|---|
| 109 | if (order == 0 || (CheckConfirm(id1, order) == 2) && order > 0 )
|
|---|
| 110 | invoiceDeatailsViewData.info = _translateManager.Translate("tlumaczenia", "weryfikacja");
|
|---|
| 111 |
|
|---|
| 112 | return View(invoiceDeatailsViewData);
|
|---|
| 113 | }
|
|---|
| 114 | public ActionResult Fail(string id, string language)
|
|---|
| 115 | {
|
|---|
| 116 | language = _funkcjePlatnosci.setLanguage(language);
|
|---|
| 117 | var id1 = ConvertId(id);
|
|---|
| 118 | var platnosc = _repVPayment.Find(p => p.ID_faktury == id1).SingleOrDefault();
|
|---|
| 119 | if (!String.IsNullOrEmpty(IsError(platnosc).Error)) return View("Error1", IsError(platnosc));
|
|---|
| 120 |
|
|---|
| 121 | var invoiceDeatailsViewData = InitInvoiceDetailsViewData(platnosc, null, "", "");
|
|---|
| 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 static InvoiceDetailsViewData InitInvoiceDetailsViewData(vPlatnosciEcard platnosc, Payer payer, string status, string brutto )
|
|---|
| 172 | {
|
|---|
| 173 | var invoiceDeatailsViewData = new InvoiceDetailsViewData();
|
|---|
| 174 | invoiceDeatailsViewData.vPlatnosciEcard = platnosc;
|
|---|
| 175 | invoiceDeatailsViewData.Payer = payer;
|
|---|
| 176 | invoiceDeatailsViewData.Status = status;
|
|---|
| 177 | invoiceDeatailsViewData.brutto = brutto;
|
|---|
| 178 | return invoiceDeatailsViewData;
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | public int ConvertId(string id)
|
|---|
| 182 | {
|
|---|
| 183 | int id1;
|
|---|
| 184 | return Int32.TryParse(id, out id1) ? id1 : 0;
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | public ErrorViewData IsError(vPlatnosciEcard platnosc)
|
|---|
| 188 | {
|
|---|
| 189 | var errortxt = "";
|
|---|
| 190 |
|
|---|
| 191 | if (platnosc == null)
|
|---|
| 192 | errortxt = _translateManager.Translate("tlumaczenia", "brakdanych");
|
|---|
| 193 | else if (!_funkcjePlatnosci.UserIdentity(platnosc, HttpContext.User.Identity.Name))
|
|---|
| 194 | errortxt = _translateManager.Translate("tlumaczenia","weryfikacja");
|
|---|
| 195 |
|
|---|
| 196 | return _funkcjePlatnosci.InitErrorViewData(errortxt,0);
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | public void UpdateStatus(int ordernumber, string currentstate)
|
|---|
| 200 | {
|
|---|
| 201 | var platnosc = _repPayment.Find(p => p.ORDERNUMBER == ordernumber).SingleOrDefault();
|
|---|
| 202 |
|
|---|
| 203 | if (platnosc == null || currentstate != ISPAID) return;
|
|---|
| 204 |
|
|---|
| 205 | platnosc.Status = true;
|
|---|
| 206 | platnosc.Status_data = DateTime.Now;
|
|---|
| 207 | _repPayment.SubmitChanges();
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | public int CheckConfirm(int idfaktury, int order)
|
|---|
| 211 | {
|
|---|
| 212 | var pl = _repPayment.Find(p => p.ORDERNUMBER == order && p.IDFaktury == idfaktury).SingleOrDefault();
|
|---|
| 213 |
|
|---|
| 214 | if (pl != null)
|
|---|
| 215 | {
|
|---|
| 216 | var confirm = _repConfirm.Find(p => p.ORDERNUMBER == order).FirstOrDefault();
|
|---|
| 217 | if (confirm == null) return 0; //potwierdzenie nie przyszlo z eCardu
|
|---|
| 218 | }
|
|---|
| 219 | else
|
|---|
| 220 | {
|
|---|
| 221 | return 2; //nie ma platnosci o takim idfaktury i ordernumber
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | return 1; //potwierdzenie przyszlo z eCardu
|
|---|
| 225 | }
|
|---|
| 226 | }
|
|---|
| 227 | }
|
|---|