protected void Insert() { Invoice invoice = UrlInvoice; int? tyro_payment_type_id = UrlTyroPaymentTypeID; decimal?amount = UrlAmount; decimal?cashout = UrlCashout; if (invoice == null) { throw new Exception("Invalid url field invoice_id"); } if (tyro_payment_type_id == null) { throw new Exception("Invalid url field tyro_payment_type_id"); } if (amount == null) { throw new Exception("Invalid url field amount"); } if (cashout == null) { throw new Exception("Invalid url field cashout"); } if (tyro_payment_type_id.Value == 1 && amount.Value > invoice.TotalDue) { Response.Write("OVERPAYMENT:$" + invoice.TotalDue); return; } if (tyro_payment_type_id.Value == 2 && amount.Value > invoice.ReceiptsTotal) { Response.Write("OVERREFUND:$" + invoice.ReceiptsTotal); return; } if (tyro_payment_type_id == 2) // make it negative for refunds { amount = amount.Value * -1; } Tuple <int, string> dbFieds = TyroPaymentPendingDB.Insert(invoice.InvoiceID, Session["DB"].ToString(), tyro_payment_type_id.Value, amount.Value, cashout.Value); Response.Write(dbFieds.Item2); // tyro_transaction_id }
protected void btnProcessResults_Click(object sender, EventArgs e) { try { lblErrorMessage.Text = string.Empty; txtSearchDate.Text = hiddenDate.Value; string resultXML = "<table style=\"text-align:left;white-space:pre;vertical-align:top;\">"; resultXML += "<tr><td>Result </td><td style=\"width:10px;\"></td><td>" + hiddenResultXML.Value + "</td><td style=\"width:55px;\"></td><td>Error </td><td></td><td>" + hiddenErrorXML.Value + "</td></tr>"; resultXML += "<tr><td>Type </td><td></td><td>" + hiddenTypeXML.Value + "</td><td></td><td>Format </td><td></td><td>" + hiddenFormatXML.Value + "</td></tr>"; resultXML += "</table>"; if (hiddenDataXML.Value.Length > 0) { resultXML += "Raw Data: <a href=\"javascript:void(0)\" onclick=\"document.getElementById('xml_data').style.display='';\">Show</a> <a href=\"javascript:void(0)\" onclick=\"document.getElementById('xml_data').style.display='none';\">Hide</a>"; resultXML += "<div id=\"xml_data\" style=\"display:none;\">"; resultXML += "<table><tr><td><pre style=\"white-space:nowrap;text-align:left;\">" + hiddenDataXML.Value.Replace("<", "<").Replace(">", ">").Replace(Environment.NewLine, "<br />") + "</pre></td></tr></table>"; resultXML += "</div>"; } string resultText = "<table style=\"text-align:left;white-space:pre;\">"; resultText += "<tr><td>Result </td><td style=\"width:10px;\"></td><td>" + hiddenResultText.Value + "</td><td style=\"width:55px;\"></td><td>Error </td><td></td><td>" + hiddenErrorText.Value + "</td></tr>"; resultText += "<tr><td>Type </td><td></td><td>" + hiddenTypeText.Value + "</td><td></td><td>Format </td><td></td><td>" + hiddenFormatText.Value + "</td></tr>"; resultText += "</table>"; if (hiddenDataText.Value.Length > 0) { resultText += "<table><tr><td style=\"white-space:nowrap;text-align:left;\"><pre>" + hiddenDataText.Value + "</pre></td></tr></table>"; } lblXMLOutput.Text = Request["debug"] != null && Request["debug"] == "1" ? resultXML : string.Empty; lblTextOutput.Text = resultText; string errorMsg = string.Empty; if (hiddenResultXML.Value != "success") { errorMsg += (errorMsg.Length > 0 ? "<br />" : "") + "Result: " + hiddenResultXML.Value; } if (hiddenErrorXML.Value != string.Empty && hiddenErrorXML.Value != "undefined") { errorMsg += (errorMsg.Length > 0 ? "<br />" : "") + "Error: " + hiddenErrorXML.Value; } if (errorMsg.Length > 0) { throw new CustomMessageException(errorMsg); } DateTime dateTime; if (!DateTime.TryParseExact(txtSearchDate.Text, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime)) { throw new CustomMessageException("DateTime is not in correct format: " + txtSearchDate.Text); } TyroPaymentPendingDB.Reconcile( Session["DB"].ToString(), dateTime, hiddenDataXML.Value); } catch (Exception ex) { lblErrorMessage.Text += (lblErrorMessage.Text.Length == 0 ? "" : "<br /><br />") + (ex is CustomMessageException ? ex.Message : ex.ToString()); //Email myself } }
protected void Update() { string tyro_transaction_id = UrlTyroTransactionID; string out_result = UrlOutResult; string out_cardType = UrlOutCardType; string out_transactionReference = UrlOutTransactionReference; string out_authorisationCode = UrlOutAuthorisationCode; string out_issuerActionCode = UrlOutIssuerActionCode; if (tyro_transaction_id == null) { throw new Exception("Invalid url field tyro_transaction_id"); } if (out_result == null) { throw new Exception("Invalid url field out_result"); } if (out_cardType == null) { throw new Exception("Invalid url field out_cardType"); } if (out_transactionReference == null) { throw new Exception("Invalid url field out_transactionReference"); } if (out_authorisationCode == null) { throw new Exception("Invalid url field out_authorisationCode"); } if (out_issuerActionCode == null) { throw new Exception("Invalid url field out_issuerActionCode"); } TyroPaymentPendingDB.UpdateByTyroTransactionID(null, tyro_transaction_id, out_result, out_cardType, out_transactionReference, out_authorisationCode, out_issuerActionCode, DateTime.Now); if (out_result == "APPROVED") { TyroPaymentPending tyroPaymentPending = TyroPaymentPendingDB.GetByByTyroTransactionID(null, tyro_transaction_id); Invoice invoice = InvoiceDB.GetByID(tyroPaymentPending.InvoiceID); int staffID = Session == null || Session["StaffID"] == null ? -8 : Convert.ToInt32(Session["StaffID"]); if (tyroPaymentPending.TyroPaymentTypeID == 1) // payment { decimal totalOwed = invoice.TotalDue - tyroPaymentPending.Amount; bool isOverPaid = totalOwed < 0; bool isPaid = totalOwed <= 0; ReceiptDB.Insert(null, 364, tyroPaymentPending.InvoiceID, tyroPaymentPending.Amount, 0, false, isOverPaid, DateTime.MaxValue, staffID); if (tyroPaymentPending.Cashout > 0) { CreditDB.Insert_Cashout(tyroPaymentPending.Cashout, tyroPaymentPending.TyroPaymentPendingID, staffID); } if (isPaid) { InvoiceDB.UpdateIsPaid(null, invoice.InvoiceID, true); } if (isOverPaid) { // send email to someone .. to fix up the overpayment.... Emailer.SimpleAlertEmail( "Invoice tyro payment added and is overpaid.<br />tyro_payment_pending_id: " + tyroPaymentPending.TyroPaymentPendingID + "<br />Invoice: " + invoice.InvoiceID + "<br />DB: " + (Session == null || Session["DB"] == null ? "" : Session["DB"]), "Tyro Invoice OverPaid: " + invoice.InvoiceID, true); } } if (tyroPaymentPending.TyroPaymentTypeID == 2) // refund { decimal totalOwed = invoice.TotalDue + tyroPaymentPending.Amount; bool isPaid = totalOwed <= 0; RefundDB.Insert(tyroPaymentPending.InvoiceID, tyroPaymentPending.Amount, 308, "", staffID); if (totalOwed > 0) { InvoiceDB.UpdateIsPaid(null, tyroPaymentPending.InvoiceID, false); } } } Response.Write("1"); // to indicate it was successful }
public static void Reconcile(string DB, DateTime date, string xml) { /* * string xmlString = * @"<?xml version=""1.0"" encoding=""UTF-8""?> * <reconciliation-detail mid=""200"" tid=""200"" terminal-business-day=""2015-04-14"" total=""1620.00""> * <transaction type=""purchase"" card-type=""visa"" amount=""100.00"" tip=""10.00"" transaction-local-date-time=""2015-04-14 12:30:30"" tyro-reference=""870020"" merchant-reference=""071061048231306351219677"" settlement-date=""2015-04-16""/> * <transaction type=""purchase"" card-type=""mastercard"" amount=""200.00"" transaction-local-date-time=""2015-04-14 12:31:20"" tyro-reference=""885214"" merchant-reference=""071061048231306351219678"" settlement-date=""2015-04-16""/> * <transaction type=""purchase"" card-type=""jcb"" amount=""40.00"" transaction-local-date-time=""2015-04-14 12:33:23"" tyro-reference=""896534"" merchant-reference=""071061048231306351219679""/> * <transaction type=""purchase"" card-type=""amex"" amount=""30.00"" transaction-local-date-time=""2015-04-14 12:36:35"" tyro-reference=""905845"" merchant-reference=""071061048231306351219680""/> * <transaction type=""purchase"" card-type=""eftpos"" amount=""650.00"" cash-out=""50.00"" transaction-local-date-time=""2015-04-14 12:40:30"" tyro-reference=""912556"" merchant-reference=""071061048231306351219681"" settlement-date=""2015-04-16""/> * <transaction type=""purchase"" card-type=""eftpos"" amount=""450.00"" transaction-local-date-time=""2015-04-14 12:50:30"" tyro-reference=""920187"" merchant-reference=""071061048231306351219682"" settlement-date=""2015-04-16""/> * <transaction type=""purchase"" card-type=""diners"" amount=""70.00"" transaction-local-date-time=""2015-04-14 13:30:30"" tyro-reference=""935587"" merchant-reference=""071061048231306351219683""/> * <transaction type=""void"" card-type=""mastercard"" amount=""-80.00"" transaction-local-date-time=""2015-04-14 13:50:30"" tyro-reference=""946585"" merchant-reference=""071061048231306351219684"" settlement-date=""2015-04-16""/> * <transaction type=""purchase"" card-type=""visa"" amount=""170.00"" transaction-local-date-time=""2015-04-14 14:23:25"" tyro-reference=""953594"" merchant-reference=""071061048231306351219685"" settlement-date=""2015-04-16""/> * <transaction type=""refund"" card-type=""visa"" amount=""-70.00"" transaction-local-date-time=""2015-04-14 15:41:12"" tyro-reference=""962548"" merchant-reference=""071061048231306351219685"" settlement-date=""2015-04-16""/> * </reconciliation-detail>"; */ StringBuilder debutOutput = new StringBuilder(); using (XmlReader reader = XmlReader.Create(new StringReader(xml))) { reader.ReadToFollowing("reconciliation-detail"); string mid = reader.GetAttribute("mid"); string tid = reader.GetAttribute("tid"); string terminal_business_day = reader.GetAttribute("terminal-business-day"); string total = reader.GetAttribute("total"); debutOutput.AppendLine("reconciliation-detail : " + " mid:" + mid + " tid:" + tid + " terminal_business_day:" + terminal_business_day + " total:" + total); while (reader.ReadToFollowing("transaction")) { string type = reader.GetAttribute("type"); string card_type = reader.GetAttribute("card-type"); string _amount = reader.GetAttribute("amount"); string cash_out = reader.GetAttribute("cash-out"); string tip = reader.GetAttribute("tip"); string transaction_local_date_time = reader.GetAttribute("transaction-local-date-time"); string tyro_reference = reader.GetAttribute("tyro-reference"); string merchant_reference = reader.GetAttribute("merchant-reference"); // transactionId that we sent in = tyro_payment_pending_id string settlement_date = reader.GetAttribute("settlement-date"); debutOutput.AppendLine("transaction : " + " type:" + type + " card_type:" + card_type + " amount:" + _amount + " cash_out:" + cash_out + " tip:" + tip + " transaction_local_date_time:" + transaction_local_date_time + " tyro_reference:" + tyro_reference + " merchant_reference:" + merchant_reference + " settlement_date:" + settlement_date ); /* * select * from TyroPaymentPending * * update TyroPaymentPending * set * tyro_transaction_id = '071061048231306351219677', * amount = 101.00, * out_date_processed = NULL, * out_result = '', * out_cardType = '', * out_transactionReference = '', * out_authorisationCode = '', * out_issuerActionCode = '' * WHERE tyro_payment_pending_id = 1 */ TyroPaymentPending tyroPaymentPending = TyroPaymentPendingDB.GetByByTyroTransactionID(DB, merchant_reference); if (tyroPaymentPending == null) { continue; } if (tyroPaymentPending.OutDateProcessed != DateTime.MinValue) { continue; } int tyro_payment_type_id = -1; if (type == "purchase") { tyro_payment_type_id = 1; } else if (type == "refund") { tyro_payment_type_id = 2; } bool sucessfulTransaction = (tyro_payment_type_id == 1 || tyro_payment_type_id == 2); if (sucessfulTransaction) { decimal amount; if (!Decimal.TryParse(_amount, out amount)) { Emailer.SimpleAlertEmail( "Tyro invoice late payment added but amount is not decimal type (" + _amount + ")<br />tyro_payment_pending_id: " + tyroPaymentPending.TyroPaymentPendingID + "<br />Invoice: " + tyroPaymentPending.InvoiceID + "<br />DB: " + DB + "<br /><br />" + xml.Replace("<", "<").Replace(">", ">").Replace(Environment.NewLine, "<br />"), "Tyro Reconcilliation Amount Not Decimal Type. Invoice : " + tyroPaymentPending.InvoiceID, true); continue; } DateTime transactionDateTime; if (!DateTime.TryParseExact(transaction_local_date_time, "yyyy-dd-MM hh:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out transactionDateTime)) { Emailer.SimpleAlertEmail( "Tyro invoice late payment added but transaction_local_date_time is not parsable (" + transaction_local_date_time + ")<br />tyro_payment_pending_id: " + tyroPaymentPending.TyroPaymentPendingID + "<br />Invoice: " + tyroPaymentPending.InvoiceID + "<br />DB: " + DB + "<br /><br />" + xml.Replace("<", "<").Replace(">", ">").Replace(Environment.NewLine, "<br />"), "Tyro Reconcilliation Amount Not Decimal Type. Invoice : " + tyroPaymentPending.InvoiceID, true); continue; } TyroPaymentPendingDB.UpdateByTyroTransactionID(DB, merchant_reference, "APPROVED", card_type, tyro_reference, "", "", transactionDateTime); if (amount != tyroPaymentPending.Amount) { Emailer.SimpleAlertEmail( "Tyro invoice late payment added but initial payment amount and reconcilliation ammount differ (" + tyroPaymentPending.Amount + ", " + amount + ")<br />tyro_payment_pending_id: " + tyroPaymentPending.TyroPaymentPendingID + "<br />Invoice: " + tyroPaymentPending.InvoiceID + "<br />DB: " + DB, "Tyro Reconcilliation Amounts Differ. Invoice : " + tyroPaymentPending.InvoiceID, true); } if (tyroPaymentPending.TyroPaymentTypeID != tyro_payment_type_id) { Emailer.SimpleAlertEmail( "Tyro invoice late payment added but payment types differ (" + tyroPaymentPending.TyroPaymentTypeID + ", " + tyro_payment_type_id + ")<br />tyro_payment_pending_id: " + tyroPaymentPending.TyroPaymentPendingID + "<br />Invoice: " + tyroPaymentPending.InvoiceID + "<br />DB: " + DB, "Tyro Reconcilliation Types Differ. Invoice : " + tyroPaymentPending.InvoiceID, true); } Invoice invoice = InvoiceDB.GetByID(tyroPaymentPending.InvoiceID, DB); if (tyroPaymentPending.TyroPaymentTypeID == 1) // payment { decimal totalOwed = invoice.TotalDue - tyroPaymentPending.Amount; bool isOverPaid = totalOwed < 0; bool isPaid = totalOwed <= 0; ReceiptDB.Insert(DB, 364, tyroPaymentPending.InvoiceID, tyroPaymentPending.Amount, 0, false, isOverPaid, DateTime.MaxValue, -8); if (isPaid) { InvoiceDB.UpdateIsPaid(DB, invoice.InvoiceID, true); } if (isOverPaid) { // send email to someone .. to fix up the overpayment.... Emailer.SimpleAlertEmail( "Tyro invoice late payment added and is overpaid.<br />tyro_payment_pending_id: " + tyroPaymentPending.TyroPaymentPendingID + "<br />Invoice: " + invoice.InvoiceID + "<br />DB: " + DB, "Tyro Invoice OverPaid: " + invoice.InvoiceID, true); } } if (tyroPaymentPending.TyroPaymentTypeID == 2) // refund { amount = amount * -1; // reconcilliation report shows refund amount as negative amounts decimal totalOwed = invoice.TotalDue + tyroPaymentPending.Amount; bool isPaid = totalOwed <= 0; RefundDB.Insert(tyroPaymentPending.InvoiceID, tyroPaymentPending.Amount, 308, "", -8, DB); if (totalOwed > 0) { InvoiceDB.UpdateIsPaid(DB, tyroPaymentPending.InvoiceID, false); } } Emailer.SimpleAlertEmail( "Tyro Invoice Payment Updated Asynchonously (ie Late). <br />tyro_payment_pending_id: " + tyroPaymentPending.TyroPaymentPendingID + "<br />Invoice: " + invoice.InvoiceID + "<br />DB: " + DB, "Tyro Invoice Payment Updated Asynchonously (ie Late). Invoice: " + invoice.InvoiceID, true); } } } Logger.LogQuery(debutOutput.ToString()); }