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