| 1 | using System;
|
|---|
| 2 | using System.Linq;
|
|---|
| 3 | using System.Web.Mvc;
|
|---|
| 4 | using adMoto.Payments.Core;
|
|---|
| 5 | using adMoto.Payments.Core.Data;
|
|---|
| 6 | using adMoto.Payments.Core.Interfaces;
|
|---|
| 7 | using Elmah;
|
|---|
| 8 | using Platnosci.Models;
|
|---|
| 9 |
|
|---|
| 10 | namespace Platnosci.Controllers
|
|---|
| 11 | {
|
|---|
| 12 | public class PlatnoscController : Controller
|
|---|
| 13 | {
|
|---|
| 14 | public const string ISPAID = "payment_deposited"; //transakcja potwierdzona do rozliczenia
|
|---|
| 15 | private readonly IRepository<Invoice> _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<Invoice>(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<Invoice> 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 |
|
|---|
| 39 | [Authorize]
|
|---|
| 40 | public ActionResult Show(string id, string language)
|
|---|
| 41 | {
|
|---|
| 42 | _funkcjePlatnosci.SetLanguage(language);
|
|---|
| 43 | var id1 = ConvertId(id);
|
|---|
| 44 |
|
|---|
| 45 | var platnosc = _repVPayment.Find(p => p.ID_faktury == id1).SingleOrDefault();
|
|---|
| 46 |
|
|---|
| 47 | var errorViewData = _funkcjePlatnosci.IsError(platnosc, HttpContext.User.Identity.Name);
|
|---|
| 48 | if (!String.IsNullOrEmpty(errorViewData.Error))
|
|---|
| 49 | return View("Error1", errorViewData);
|
|---|
| 50 |
|
|---|
| 51 | var invoiceDeatailsViewData = InitInvoiceDetailsViewData(platnosc);
|
|---|
| 52 |
|
|---|
| 53 | var tablicaPotwierdzenia = _repConfirm.FindItemsByIdFaktury(id1);
|
|---|
| 54 | if (tablicaPotwierdzenia.Count > 0) //platnosc za fakture zostala uregulowana
|
|---|
| 55 | {
|
|---|
| 56 | var dataZaplaty = String.Format("{0:dd-MM-yyyy}", tablicaPotwierdzenia[0].AUTHTIME);
|
|---|
| 57 | invoiceDeatailsViewData.info = String.Format(_translateManager.Translate("tlumaczenia", "zaplacono"), platnosc.Faktura_Numer, dataZaplaty);
|
|---|
| 58 | invoiceDeatailsViewData.termin = dataZaplaty;
|
|---|
| 59 | return View("Paid", invoiceDeatailsViewData);
|
|---|
| 60 | }
|
|---|
| 61 | return View(invoiceDeatailsViewData);
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | [Authorize]
|
|---|
| 65 | [AcceptVerbs(HttpVerbs.Post)]
|
|---|
| 66 | public ActionResult Show(Payer payer, string language)
|
|---|
| 67 | {
|
|---|
| 68 | _funkcjePlatnosci.SetLanguage(language);
|
|---|
| 69 |
|
|---|
| 70 | if (String.IsNullOrEmpty(payer.FirstName))
|
|---|
| 71 | ModelState.AddModelError("Payer.FirstName", _translateManager.Translate("tlumaczenia", "err_imieWK"));
|
|---|
| 72 | else if (payer.FirstName.Length > 25)
|
|---|
| 73 | ModelState.AddModelError("Payer.FirstName", String.Format(_translateManager.Translate("tlumaczenia", "ToLongValue"), "25"));
|
|---|
| 74 |
|
|---|
| 75 | if (String.IsNullOrEmpty(payer.LastName))
|
|---|
| 76 | ModelState.AddModelError("Payer.LastName", _translateManager.Translate("tlumaczenia", "err_nazwiskoWK"));
|
|---|
| 77 | else if (payer.LastName.Length > 30)
|
|---|
| 78 | ModelState.AddModelError("Payer.LastName", String.Format(_translateManager.Translate("tlumaczenia", "ToLongValue"), "30"));
|
|---|
| 79 |
|
|---|
| 80 | if (ModelState.IsValid == false)
|
|---|
| 81 | {
|
|---|
| 82 | var platnosc = _repVPayment.Find(p => p.ID_faktury == payer.Id_faktury).SingleOrDefault();
|
|---|
| 83 | var errorViewData = _funkcjePlatnosci.IsError(platnosc, HttpContext.User.Identity.Name);
|
|---|
| 84 |
|
|---|
| 85 | if (!String.IsNullOrEmpty(errorViewData.Error))
|
|---|
| 86 | return View("Error1", errorViewData);
|
|---|
| 87 |
|
|---|
| 88 | return View("Show", InitInvoiceDetailsViewData(platnosc));
|
|---|
| 89 | }
|
|---|
| 90 | return RedirectToAction("Merchant", "Merchant", payer);
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | public ActionResult Ok(string id, string order, string language)
|
|---|
| 94 | {
|
|---|
| 95 | var orderId = ConvertId(order);
|
|---|
| 96 | _funkcjePlatnosci.SetLanguage(language);
|
|---|
| 97 | var id1 = ConvertId(id);
|
|---|
| 98 | var platnosc = _repVPayment.Find(p => p.ID_faktury == id1).SingleOrDefault();
|
|---|
| 99 |
|
|---|
| 100 | if (platnosc == null)
|
|---|
| 101 | return View("Error1", _funkcjePlatnosci.InitErrorViewData(_translateManager.Translate("tlumaczenia", "brakdanych"), 0));
|
|---|
| 102 |
|
|---|
| 103 | var invoiceDeatailsViewData = InitInvoiceDetailsViewData(platnosc);
|
|---|
| 104 |
|
|---|
| 105 | //sprawdzamy czy dla kombinacji ordernumber i idfaktury istnieje platnosc,
|
|---|
| 106 | //jesli tak, to sprawdzamy czy przyszlo potwierdzenie z eCardu.
|
|---|
| 107 |
|
|---|
| 108 | if (CheckConfirm(id1, orderId) == 0) //nie ma potwierdzenia z eCardu
|
|---|
| 109 | invoiceDeatailsViewData.info = String.Format(_translateManager.Translate("tlumaczenia", "blad1"), invoiceDeatailsViewData.vPlatnosciEcard.Faktura_Numer);
|
|---|
| 110 |
|
|---|
| 111 | else if (CheckConfirm(id1, orderId) == 2)
|
|---|
| 112 | invoiceDeatailsViewData.info = _translateManager.Translate("tlumaczenia", "weryfikacja");
|
|---|
| 113 |
|
|---|
| 114 | return View(invoiceDeatailsViewData);
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | public ActionResult Fail(string id, string language)
|
|---|
| 118 | {
|
|---|
| 119 | _funkcjePlatnosci.SetLanguage(language);
|
|---|
| 120 | var id1 = ConvertId(id);
|
|---|
| 121 | var platnosc = _repVPayment.Find(p => p.ID_faktury == id1).SingleOrDefault();
|
|---|
| 122 |
|
|---|
| 123 | if (platnosc == null)
|
|---|
| 124 | return View("Error1",
|
|---|
| 125 | _funkcjePlatnosci.InitErrorViewData(
|
|---|
| 126 | _translateManager.Translate("tlumaczenia", "brakdanych"), 0));
|
|---|
| 127 |
|
|---|
| 128 | var invoiceDeatailsViewData = InitInvoiceDetailsViewData(platnosc);
|
|---|
| 129 | return View(invoiceDeatailsViewData);
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | public ActionResult Form()
|
|---|
| 133 | {
|
|---|
| 134 | return View();
|
|---|
| 135 | }
|
|---|
| 136 |
|
|---|
| 137 | public ActionResult Status()
|
|---|
| 138 | {
|
|---|
| 139 | if (System.Web.HttpContext.Current != null)
|
|---|
| 140 | ErrorSignal.FromCurrentContext().Raise(new Exception(), System.Web.HttpContext.Current);
|
|---|
| 141 |
|
|---|
| 142 | var potwierdzenie = new PotwierdzeniaEcard();
|
|---|
| 143 | var content = new ContentResult();
|
|---|
| 144 | try
|
|---|
| 145 | {
|
|---|
| 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"];
|
|---|
| 172 |
|
|---|
| 173 | if (!potwierdzenie.ORDERNUMBER.HasValue || potwierdzenie.ORDERNUMBER.Value <= 0)
|
|---|
| 174 | {
|
|---|
| 175 | content.Content = "FALSE";
|
|---|
| 176 | return content;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | _repConfirm.Insert(potwierdzenie);
|
|---|
| 180 |
|
|---|
| 181 | if (potwierdzenie.ORDERNUMBER.HasValue)
|
|---|
| 182 | UpdateStatus(potwierdzenie.ORDERNUMBER.Value, potwierdzenie.CURRENTSTATE);
|
|---|
| 183 |
|
|---|
| 184 | content.Content = "OK";
|
|---|
| 185 | }
|
|---|
| 186 | catch (Exception ex)
|
|---|
| 187 | {
|
|---|
| 188 | ErrorSignal.FromCurrentContext().Raise(ex);
|
|---|
| 189 |
|
|---|
| 190 | content.Content = "FALSE " + ex.Message + " " + ex.GetType();
|
|---|
| 191 | }
|
|---|
| 192 |
|
|---|
| 193 | return content;
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | private static Payer InitPayer(int idFaktury)
|
|---|
| 197 | {
|
|---|
| 198 | var payer = new Payer { Id_faktury = idFaktury };
|
|---|
| 199 | return payer;
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 | private InvoiceDetailsViewData InitInvoiceDetailsViewData(Invoice platnosc)
|
|---|
| 203 | {
|
|---|
| 204 | var invoiceDeatailsViewData = new InvoiceDetailsViewData();
|
|---|
| 205 | invoiceDeatailsViewData.vPlatnosciEcard = platnosc;
|
|---|
| 206 | invoiceDeatailsViewData.Payer = InitPayer(platnosc.ID_faktury);
|
|---|
| 207 | invoiceDeatailsViewData.brutto = _funkcjePlatnosci.BruttoToString(platnosc.Brutto, platnosc.waluta_brutto, platnosc.waluta_miano);
|
|---|
| 208 | return invoiceDeatailsViewData;
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | public int ConvertId(string id)
|
|---|
| 212 | {
|
|---|
| 213 | int id1;
|
|---|
| 214 | return Int32.TryParse(id, out id1) ? id1 : 0;
|
|---|
| 215 | }
|
|---|
| 216 |
|
|---|
| 217 | public void UpdateStatus(int ordernumber, string currentstate)
|
|---|
| 218 | {
|
|---|
| 219 | var platnosc = _repPayment.Find(p => p.ORDERNUMBER == ordernumber).SingleOrDefault();
|
|---|
| 220 |
|
|---|
| 221 | if (platnosc != null && currentstate == ISPAID)
|
|---|
| 222 | {
|
|---|
| 223 | platnosc.Status = true;
|
|---|
| 224 | platnosc.Status_data = DateTime.Now;
|
|---|
| 225 | _repPayment.SubmitChanges();
|
|---|
| 226 | }
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | public int CheckConfirm(int idfaktury, int order)
|
|---|
| 230 | {
|
|---|
| 231 | var pl = _repPayment.Find(p => p.ORDERNUMBER == order && p.IDFaktury == idfaktury).SingleOrDefault();
|
|---|
| 232 |
|
|---|
| 233 | if (pl != null)
|
|---|
| 234 | {
|
|---|
| 235 | var confirm = _repConfirm.Find(p => p.ORDERNUMBER == order).FirstOrDefault();
|
|---|
| 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 | }
|
|---|
| 242 |
|
|---|
| 243 | return 1; //potwierdzenie przyszlo z eCardu
|
|---|
| 244 | }
|
|---|
| 245 | }
|
|---|
| 246 | } |
|---|