VSERVERWS/Controllers/PaymentController.cs
Cameron Taylor 6bc14b88c9 Initial Commit.
New site in .NET 6 replacing .net framwk 4.8
2022-07-27 16:02:36 -04:00

353 lines
12 KiB
C#

using System.Net.Mail;
using System.Printing;
using System.Text.Json;
using System.Reflection;
using System.Drawing.Printing;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using CVRLIB.CVR;
using CVRLIB.Printing;
using CVRLIB.External.Communications;
using CVRLIB.Customers;
using static CVRLIB.Customers.PFUNCS;
using CVRLIB_NF.Printing;
using CVRLIB_NF.Customers;
using static VSERVERWS.Global.Globals;
namespace VSERVERWS.Controllers {
[Route("api/Payment/[action]")]
[ApiController]
public class PaymentController : ControllerBase {
public const bool PAY_STATUS_FORCE_OFFLINE = false;
[HttpGet]
public IActionResult PaymentStatus() {
CVRPRINTER_MF264dw CVP2 = CVP_MF264dw;
CVRPRINTER_MFC_L8900CDW CVP3 = CVP_MFC_L8900CDW;
NF_CVRPrinterCollection CVPC = new NF_CVRPrinterCollection(new[] { (CVRPRINTER)CVP2, (CVRPRINTER)CVP3 });
object? robj = null;
switch (true) {
//case true when CVP2.IsOnline && !PAY_STATUS_FORCE_OFFLINE:
//case true when CVP2.IsPrintReady && !PAY_STATUS_FORCE_OFFLINE:
case true when CVPC.IsOnline && !PAY_STATUS_FORCE_OFFLINE:
case true when CVPC.IsPrintReady && !PAY_STATUS_FORCE_OFFLINE:
Console.WriteLine("CVP1 -> OK");
robj = new { PRINTER_NAME = CVPC.ToString(), STATUS = "OK" };
break;
case true when CVPC.IsInError:
Console.WriteLine("CVPC -> ERROR");
robj = new {PRINTER_NAME = CVPC.ToString(), STATUS = "ERROR"};
AlertCVRtoPrinterERROR();
break;
case true when PAY_STATUS_FORCE_OFFLINE:
#pragma warning disable CS0162 // Unreachable code detected
Console.WriteLine("CVP1 -> OFFLINE");
robj = new {PRINTER_NAME = CVPC.ToString(), STATUS = "OFFLINE"};
AlertCVRtoPrinterERROR();
#pragma warning restore CS0162 // Unreachable code detected
break;
default:
Console.WriteLine("CVP1 -> ELSE");
robj = new {PRINTER_NAME = CVPC.ToString(), STATUS = "UNK"};
break;
}
return new JsonResult(robj);
}
private void AlertCVRtoPrinterERROR() {
try {
CVRPrinterCollection CVRPC = new CVRPrinterCollection(new[] { (CVRPRINTER)CVP_MF264dw, (CVRPRINTER)CVP_MFC_L8900CDW });
foreach (ICVRPrinter CVRP in CVRPC) {
string tS = "";
tS = CVRP.LastRawStatus;
CVRCO.TryWriteToLog("Raw Printer Status (" + CVRP.PrinterName + ") is: " + tS, "PRINTERS", "PRINTER_STATUS");
}
} catch {}
try {
string[] SA = CVRCO.get_SYSPARAM("PayStatusLastError");
DateTime LastError = DateTime.MinValue;
if (!string.IsNullOrEmpty(SA[1])) {
LastError = DateTime.Parse(SA[1]);
}
if (DateTime.Now.Subtract(LastError).Hours > 2) {
Email E = new Email();
E.SEND_FROM = Email.SEND_FROM_ENUM.OFFICE365;
string B = "Payment System Printer error / offline: Check applicable printer(s) for error or offline!";
string S = "<cc> Payment System Printer Error: Check Printer(s)!";
var eV = new List<EmailImage>();
E.TrySendEmail(B, true, new MailAddressCollection() { new MailAddress("query@cvrco.ca")}, new MailAddress("info@cvrco.ca", "CVRBOT"), S, null, null, ref eV);
CVRCO.set_SYSPARAM("PayStatusLastError", null, new[] { DateTime.Now.ToString() });
}
} catch { }
}
[HttpGet]
public IActionResult PaymentInfo() {
JsonResult JR = new JsonResult(new { RESULT = "TEST OK!", PATH = Assembly.GetExecutingAssembly()?.FullName });
return JR;
}
[HttpPost]
public IActionResult PaymentInfo([FromForm] PayInfo inPI) {
CI_NF CI_N = inPI.ToCI();
object? rOBJ = null;
Email E = new Email();
E.SEND_FROM = Email.SEND_FROM_ENUM.OFFICE365;
bool DupRetry = false;
CI? tCI = null;
CCRETRY:
try {
CI_N.SaveToDB();
try {
CI_N.Print();
} catch (Exception ex) {
CVRPrinterCollection CVRPC = new CVRPrinterCollection(new[] { (CVRPRINTER)CVP_MF264dw, (CVRPRINTER)CVP_MFC_L8900CDW });
foreach (CVRPRINTER CVRP in CVRPC) {
string tS = "";
if (!CVRP.TryGetRawStatus(ref tS)) {
tS = "<unk>";
}
CVRCO.TryWriteToLog("Raw Printer Status (" + CVRP.PrinterName + ") is: " + tS, "PRINTERS", "PRINTER_STATUS");
}
var ev1 = new List<EmailImage>();
E.TrySendEmail("Web Credit Card printer error; check printer(s).",
false,
new MailAddressCollection() { new MailAddress("query@cvrco.ca")}, new MailAddress("info@cvrco.ca", "CVRBOT"),
"<cc> Web CC Printer Failure, Check Printer(s)",
null, null, ref ev1);
if (rOBJ == null) {
rOBJ = new { SUCCESS = false, DATA = "P" };
}
try {
CI_N.Delete();
} catch (Exception pex) {
throw;
}
throw;
}
string W = "Provided";
if (DupRetry) { W = "Updated"; }
string B = "CC Info " + W + ", Use: " + CI_N.UID + @" / LAST5: " + CI_N.LAST5 + " FOR: " + CI_N.REF_NUM;
string S = "<cc> CC Info " + W + ", Use: " + CI_N.UID + " for: " + CI_N.REF_NUM;
if (DupRetry) {
B += "<br>REPLACE CC SHEET!";
S += " & Replace Sheet!";
}
var ev = new List<EmailImage>();
E.TrySendEmail(B,
true,
new MailAddressCollection() { new MailAddress("query@cvrco.ca")},
new MailAddress("info@cvrco.ca", "CVRBOT"),
S,
null, null, ref ev);
if (rOBJ == null) {
rOBJ = new { SUCCESS = true, DATA = "N-" + CI_N.UID };
}
} catch (AttemptedToAddDuplicateCIException ex) {
//email to store with dup meta details ie ref num / name
CICollection CIC = new CICollection(CI_N.CHASH);
if (CIC.Count == 1) {
foreach (string K in CIC.Keys) {
tCI = CIC[K];
break;
}
}
string B = "";
string S = "";
string D = "S";
if (tCI != null) {
if ((!DupRetry && tCI.EXPIRY.Year != CI_N.EXPIRY.Year || tCI.EXPIRY.Month != CI_N.EXPIRY.Month) &&
(CI_N.FNAME.ToLower().Trim() == tCI.FNAME.ToLower().Trim() ||
CI_N.LNAME.ToLower().Trim() == tCI.LNAME.ToLower().Trim() ||
CI_N.PHONE.Trim() == tCI.PHONE.Trim() ||
CI_N.EMAIL.ToLower().Trim() == tCI.EMAIL.ToLower().Trim()
)) {
CI_N.UID = tCI.UID;
CI_N.LNAME = tCI.LNAME;
DupRetry = true;
goto CCRETRY;
}
D += "-" + tCI.UID;
B = "Attempted to Add duplicate Credit Card from WEB. <br><br><b>Details (OLD | NEW):</b><br> Ref: " + CI_N.REF_NUM + "<br> USE: " + tCI.UID + "<br>CVNUM: " + tCI.CUST_UID + " | " + CI_N.CUST_UID + "<br>FNAME: " + tCI.FNAME + " | " + CI_N.FNAME + "<br>LNAME: " + tCI.LNAME + " | " + CI_N.LNAME + "<br>PHONE: " + tCI.PHONE + " | " + CI_N.PHONE + "<br>EMAIL: " + tCI.EMAIL + " | " + CI_N.EMAIL + "<br>LAST5: " + tCI.LAST5 + " | " + CI_N.LAST5 + "<br>EXPIRY: " + tCI.EXPIRY.ToString("MM / yyyy") + " | " + CI_N.EXPIRY.ToString("MM / yyyy") + "<br>CVV (NEW ONLY): " + CI_N.SD;
S = "<cc> Duplicate CC received for Ref Num: " + CI_N.REF_NUM + " Use Card: " + tCI.UID;
} else {
B = "Attempted to Add duplicate Credit Card from WEB.<br><br><b>Details (OLD | NEW):</b><br> Ref: " + CI_N.REF_NUM + "<br><br>ERROR FETCHING EXISTING CARD DETAILS!";
S = "<cc> Duplicate CC received for Ref Num: " + CI_N.REF_NUM + "Use Card: UNK";
}
var ev = new List<EmailImage>();
E.TrySendEmail(B,
true,
new MailAddressCollection() { new MailAddress("query@cvrco.ca")},
new MailAddress("info@cvrco.ca", "CVRBOT"),
S,
null,null, ref ev);
if (rOBJ == null) { rOBJ = new { SUCCESS = true, DATA = D }; }
} catch (Exception ex) {
string B = "General Web CC add exception, MSG: " + ex.Message + "<br><br>STACK: " + ex.StackTrace;
string S = "<cc> Web CC General Failure";
var ev = new List<EmailImage>();
E.TrySendEmail(B,
true,
new MailAddressCollection() { new MailAddress("query@cvrco.ca")},
new MailAddress("info@cvrco.ca", "CVRBOT"),
S,
null, null, ref ev);
if (rOBJ == null) { rOBJ = new { SUCCESS = false, DATA = "F" }; }
}
var JR = new JsonResult(rOBJ);
return JR;
}
public class PayInfo {
public string RFNUM { get; set; } = "";
public string? CVNUM { get; set; } = "";
public string FNAME { get; set; } = "";
public string LNAME { get; set; } = "";
public string PHONE { get; set; } = "";
public string EMAIL { get; set; } = "";
public string CDNAM { get; set; } = "";
public string CDNUM { get; set; } = "";
public string EXPDT { get; set; } = "";
public string CVDIG { get; set; } = "";
public string CRDTY { get; set; } = "";
public string CDDLD { get; set; } = "";
public CI_NF ToCI() {
var rCI = new CI_NF();
rCI.CN = CDNUM;
rCI.EXPIRY = DateTime.Parse(EXPDT);
rCI.SD = CVDIG;
rCI.CT = StoCTE(CRDTY);
rCI.FNAME = FNAME;
rCI.LNAME = LNAME;
rCI.CUST_UID = CVNUM;
rCI.PHONE = PHONE;
rCI.EMAIL = EMAIL;
DateTime tDLD = DateTime.Now.AddDays(14);
if (!DateTime.TryParse(CDDLD, out tDLD)) {
tDLD = DateTime.Now.AddDays(14);
}
rCI.DELETE_DATE = tDLD;
rCI.REF_NUM = RFNUM;
return rCI;
}
}
}
}