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