public async Task <IHttpActionResult> AuthNetSubmit(AuthNetBindingModel model) { var processingResult = new ServiceProcessingResult <List <OrderBindingModel> > { IsSuccessful = true }; //Get the order first thing to make sure we have it. List <OrderBindingModel> Orders = new List <OrderBindingModel>(); try { var sqlQuery = "SELECT Id,OrderId,PayType,Grade,BookType,Teacher,PersText1,Studentfname,Studentlname,Emailaddress,Schcode,ItemAmount,Itemqty,Schinvoicenumber,Orddate,ItemTotal,Schname,Yr,Icon1,Icon2,Icon3,Icon4,Josicon1,Josicon2,Josicon3,Josicon4 FROM temporders where orderid=@OrderId"; MySqlParameter[] parameters = new MySqlParameter[] { new MySqlParameter("@OrderId", model.InvoiceNumber) }; var sqlQueryService = new SQLQuery(); var orderResult = await sqlQueryService.ExecuteReaderAsync <OrderBindingModel>(CommandType.Text, sqlQuery, parameters); if (!orderResult.IsSuccessful) { processingResult.IsSuccessful = false; processingResult.Error = new ProcessingError("Error retrieving order.", "Error retrieving order.", true, false); ExceptionlessClient.Default.SubmitLog(typeof(TempOrderController).FullName, orderResult.Error.UserMessage, "Error"); return(Ok(processingResult)); } if (orderResult.Data == null) { processingResult.IsSuccessful = false; processingResult.Error = new ProcessingError("Error retrieving order.", "Error retrieving order.", true, false); ExceptionlessClient.Default.SubmitLog(typeof(TempOrderController).FullName, orderResult.Error.UserMessage, "Error"); return(Ok(processingResult)); } Orders = (List <OrderBindingModel>)orderResult.Data; } catch (Exception ex) { ex.ToExceptionless().Submit(); processingResult.IsSuccessful = false; processingResult.Error = new ProcessingError("Error retrieving order.", "Error retrieving order.", true, false); return(Ok(processingResult)); } //---------------------------------------------------------------------- var authNetService = new AuthNetService(); var result = await authNetService.SubmittAsync(model); if (!result.IsSuccessful) { processingResult.IsSuccessful = false; processingResult.Error = new ProcessingError("Error submiting payment to Authorzie.net", "Error submiting payment to Authorzie.net", true, false); ExceptionlessClient.Default.SubmitLog(typeof(OrderController).FullName, result.Error.UserHelp, "Error"); return(Ok(processingResult)); } AuthNetResponse AuthNetData = new AuthNetResponse(); AuthNetData = result.Data; if (!AuthNetData.Approved) { processingResult.IsSuccessful = false; processingResult.Error = new ProcessingError("Your payment to Authorized.net failed for the following reason:" + AuthNetData.Message, "Your payment to Authorized.net failed for the following reason:" + AuthNetData.Message, true, false); return(Ok(processingResult)); } //not approved else //Is Approved { foreach (var order in Orders) { var sqlQuery1 = "INSERT INTO Orders (grade,booktype,teacher,perstext1,studentfname,studentlname,emailaddress,schcode,itemamount,itemqty,schinvoicenumber,orderid,orddate,paytype,itemtotal,schname,parentpayment,yr,icon1,icon2,icon3,icon4,josicon1,josicon2,josicon3,josicon4) VALUES(@grade,@booktype,@teacher,@perstext1,@studentfname,@studentlname,@emailaddress,@schcode,@itemamount,@itemqty,@schinvoicenumber,@orderid,@orddate,@paytype,@itemtotal,@schname,@parentpayment,@yr,@icon1,@icon2,@icon3,@icon4,@josicon1,@josicon2,@josicon3,@josicon4)"; MySqlParameter[] parameters = new MySqlParameter[] { new MySqlParameter("@grade", order.Grade), new MySqlParameter("@booktype", order.BookType), new MySqlParameter("@teacher", order.Teacher), new MySqlParameter("@perstext1", order.PersText1), new MySqlParameter("@studentfname", order.Studentfname), new MySqlParameter("@studentlname", order.Studentlname), new MySqlParameter("@emailaddress", order.Emailaddress), new MySqlParameter("@schcode", order.Schcode), new MySqlParameter("@itemamount", order.ItemAmount), new MySqlParameter("@itemqty", order.Itemqty), new MySqlParameter("@schinvoicenumber", order.Schinvoicenumber), new MySqlParameter("@orderid", order.OrderId), new MySqlParameter("@orddate", order.Orddate), new MySqlParameter("@paytype", order.PayType), new MySqlParameter("@itemtotal", order.ItemTotal), new MySqlParameter("@schname", order.Schname), new MySqlParameter("@parentpayment", 1), new MySqlParameter("@yr", order.Yr), new MySqlParameter("@icon1", order.Icon1), new MySqlParameter("@icon2", order.Icon2), new MySqlParameter("@icon3", order.Icon3), new MySqlParameter("@icon4", order.Icon4), new MySqlParameter("@josicon1", order.Josicon1), new MySqlParameter("@josicon2", order.Josicon2), new MySqlParameter("@josicon3", order.Josicon3), new MySqlParameter("@josicon4", order.Josicon4), }; try { var sqlQueryService = new SQLQuery(); var orderResult = await sqlQueryService.ExecuteNonQueryAsync(CommandType.Text, sqlQuery1, parameters); if (!orderResult.IsSuccessful) { ExceptionlessClient.Default.SubmitLog(typeof(TempOrderController).FullName, orderResult.Error.UserMessage, "Error"); //create the mail message MailMessage mail = new MailMessage(); //set the addresses mail.From = new MailAddress(ConfigurationManager.AppSettings["FromAddr"]); mail.To.Add("*****@*****.**"); //set the content mail.Subject = "Mysql Error:Inserting Order " + order.Schname.ToString() + "(" + order.Schcode.ToString() + ")"; mail.Body = "An error occured inserting a order record into the Mysql database server. The following data was not recorded in the order table.<br/>School Name:" + order.Schname + "<br/>Student Name:" + order.Studentfname + " " + order.Studentlname + "<br/>School Code:" + order.Schcode + "<br/>Order Id:" + model.InvoiceNumber + "<br/>Grade:" + order.Grade + "<br/>Teacher:" + order.Teacher + "<br/>Book Type:" + order.BookType + "<br/>EmailAddress:" + order.Emailaddress + "<br/>Perstext1:" + order.PersText1 + "<br/>Item Amount:" + order.ItemAmount + "<br/>Item Total:" + order.ItemTotal + "<br/>Item Qty:" + order.Itemqty + "<br/>icon1:" + order.Icon1 + "<br/>icon2:" + order.Icon2 + "<br/>icon3:" + order.Icon3 + "<br/>icon4:" + order.Icon4 + "<br/>Year:" + order.Yr + "<br/><br/>Mysql Exception Code:" + orderResult.Error.UserMessage; mail.IsBodyHtml = true; //send the message SmtpClient smtp = new System.Net.Mail.SmtpClient(ConfigurationManager.AppSettings["SmtpServer"]); smtp.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["smtpuser"], ConfigurationManager.AppSettings["smtppassword"]); //smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis 'only works on some serves try { smtp.Send(mail); } catch (Exception ex) { //go on don't stop because email cant be sent. } } if (orderResult.Data == 0) { ExceptionlessClient.Default.SubmitLog(typeof(TempOrderController).FullName, "Failed to insert an order", "Error"); MailMessage mail = new MailMessage(); //set the addresses mail.From = new MailAddress(ConfigurationManager.AppSettings["FromAddr"]); mail.To.Add("*****@*****.**"); //set the content mail.Subject = "Mysql Error:Inserting Order " + order.Schname.ToString() + "(" + order.Schcode.ToString() + ")"; mail.Body = "An error occured inserting a order record into the Mysql database server. The following data was not recorded in the order table.<br/>School Name:" + order.Schname + "<br/>Student Name:" + order.Studentfname + " " + order.Studentlname + "<br/>Order Id:" + model.InvoiceNumber + "<br/>School Code:" + order.Schcode + "<br/>Grade:" + order.Grade + "<br/>Teacher:" + order.Teacher + "<br/>Book Type:" + order.BookType + "<br/>EmailAddress:" + order.Emailaddress + "<br/>Perstext1:" + order.PersText1 + "<br/>Item Amount:" + order.ItemAmount + "<br/>Item Total:" + order.ItemTotal + "<br/>Item Qty:" + order.Itemqty + "<br/>icon1:" + order.Icon1 + "<br/>icon2:" + order.Icon2 + "<br/>icon3:" + order.Icon3 + "<br/>icon4:" + order.Icon4 + "<br/>Year:" + order.Yr + "<br/><br/>Mysql Exception Code:" + orderResult.Error.UserMessage; mail.IsBodyHtml = true; //send the message SmtpClient smtp = new System.Net.Mail.SmtpClient(ConfigurationManager.AppSettings["SmtpServer"]); smtp.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["smtpuser"], ConfigurationManager.AppSettings["smtppassword"]); //smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis 'only works on some serves try { smtp.Send(mail); } catch (Exception ex) { //go on don't stop because email cant be sent. } } } catch (Exception ex) { ex.ToExceptionless().Submit(); } }//endforeach //, //insert payment even if order failed try { var sqlQuery = "INSERT INTO Payment (orderid,schname,schcode,custemail,ddate,poamt,paytype,ccnum,invno,parentpay,payerfname,payerlname,addr,city,state,zip,transid,authcode) VALUES(@orderid,@schname,@schcode,@custemail,@ddate,@poamt,@paytype,@ccnum,@invno,@parentpay,@payerfname,@payerlname,@addr,@city,@state,@zip,@transid,@authcode)"; MySqlParameter[] parameters1 = new MySqlParameter[] { new MySqlParameter("@orderid", model.InvoiceNumber), new MySqlParameter("@custemail", model.EmailAddress), new MySqlParameter("@ddate", DateTime.Now), new MySqlParameter("@poamt", model.Amount), new MySqlParameter("@paytype", model.Method), new MySqlParameter("@transid", AuthNetData.TransId), new MySqlParameter("@authcode", AuthNetData.AuthCode), new MySqlParameter("@ccnum", model.Cardnum == null?"":model.Cardnum.Substring(model.Cardnum.Length - 3)), new MySqlParameter("@invno", Orders[0].Schinvoicenumber), new MySqlParameter("@schname", Orders[0].Schname), new MySqlParameter("@schcode", AuthNetData.Custid), new MySqlParameter("@parentpay", 1), new MySqlParameter("@payerfname", model.FirstName), new MySqlParameter("@payerlname", model.LastName), new MySqlParameter("@addr", model.Address), new MySqlParameter("@city", model.City), new MySqlParameter("@state", model.State.TrimEnd()), new MySqlParameter("@zip", model.Zip) }; var sqlQueryService1 = new SQLQuery(); var payResult = await sqlQueryService1.ExecuteNonQueryAsync(CommandType.Text, sqlQuery, parameters1); if (!payResult.IsSuccessful) { //fail it because we don't have the payment to create a receipt. processingResult.IsSuccessful = false; processingResult.Error = new ProcessingError("Your payment was made but an error occurred creating your receipt. To obtain a receipt contact your school adviser with this tranasaction id:" + AuthNetData.TransId, "Your payment was made but an error occurred creating your receipt. To obtain a receipt contact your school adviser with this tranasaction id:" + AuthNetData.TransId, true, false); ExceptionlessClient.Default.CreateLog(typeof(OrderController).FullName, "Error inserting parent payment.", "Error").AddObject(model).AddObject(AuthNetData); return(Ok(processingResult)); } EmailReceipt(model.InvoiceNumber); } catch (Exception ex) { ex.ToExceptionless() .SetMessage("Error inserting payment.") .AddTags("Insert Payment Error") .AddObject(model) .AddObject(AuthNetData) .Submit(); processingResult.IsSuccessful = false; processingResult.Error = new ProcessingError("Your payment was made but an error occurred creating your receipt. To obtain a receipt contact your school adviser with this tranasaction id:" + AuthNetData.TransId, "Your payment was made but an error occurred creating your receipt. To obtain a receipt contact your school adviser with this tranasaction id:" + AuthNetData.TransId, true, false); return(Ok(processingResult)); } }// End Approved return(Ok(processingResult)); }
public async Task <IHttpActionResult> SchoolAuthNetSubmit(AuthNetBindingModel model) { var processingResult = new ServiceProcessingResult <string> { IsSuccessful = true }; var authNetService = new AuthNetService(); var result = await authNetService.SubmittAsync(model); if (!result.IsSuccessful) { processingResult.IsSuccessful = false; processingResult.Error = new ProcessingError("Error submiting payment to Authorzie.net", "Error submiting payment to Authorzie.net", true, false); ExceptionlessClient.Default.SubmitLog(typeof(OrderController).FullName, result.Error.UserHelp, "Error"); return(Ok(processingResult)); } AuthNetResponse AuthNetData = new AuthNetResponse(); AuthNetData = result.Data; if (!AuthNetData.Approved) { processingResult.IsSuccessful = false; processingResult.Error = new ProcessingError("Your payment to Authorized.net failed for the following reason:" + AuthNetData.Message, "Your payment to Authorized.net failed for the following reason:" + AuthNetData.Message, true, false); return(Ok(processingResult)); } var sqlQuery = "INSERT INTO Payment (schcode,schname,custemail,ddate,poamt,paytype,ccnum,invno,parentpay,payerfname,payerlname,transid,authcode) VALUES(@schcode,@schname,@custemail,@ddate,@poamt,@paytype,@ccnum,@invno,@parentpay,@payerfname,@payerlname,@transid,@authcode)"; string fname = ""; string lname = ""; if (!String.IsNullOrEmpty(model.BankAccName)) { fname = model.BankAccName.Substring(0, model.BankAccName.IndexOf(" ") - 1); lname = model.BankAccName.Substring(model.BankAccName.IndexOf(" ") + 1); } else { fname = model.FirstName; lname = model.LastName; } MySqlParameter[] parameters = new MySqlParameter[] { new MySqlParameter("@custemail", model.EmailAddress), new MySqlParameter("@ddate", DateTime.Now), new MySqlParameter("@poamt", model.Amount), new MySqlParameter("@payerfname", fname), new MySqlParameter("@payerlname", lname), new MySqlParameter("@paytype", model.Method), new MySqlParameter("@transid", AuthNetData.TransId), new MySqlParameter("@authcode", AuthNetData.AuthCode), new MySqlParameter("@ccnum", model.Cardnum == null?"":model.Cardnum.Substring(model.Cardnum.Length - 3)), new MySqlParameter("@invno", model.InvoiceNumber), new MySqlParameter("@schcode", AuthNetData.Custid), new MySqlParameter("@schname", model.Schname), new MySqlParameter("@parentpay", "0"), }; var sqlQueryService = new SQLQuery(); var payResult = await sqlQueryService.ExecuteNonQueryAsync(CommandType.Text, sqlQuery, parameters); if (!payResult.IsSuccessful) { processingResult.IsSuccessful = false; processingResult.Error = new ProcessingError("Your payment was made but an error occurred creating your receipt. To obtain a receipt contact Memory Book with this tranasaction id:" + AuthNetData.TransId, "Your payment was made but an error occurred creating your receipt. To obtain a receipt contact Memory Book with this tranasaction id:" + AuthNetData.TransId, true, false); ExceptionlessClient.Default.CreateLog(typeof(OrderController).FullName, "Error inserting school payment.", "Error").AddObject(model).AddObject(AuthNetData); return(Ok(processingResult)); } EmailSchoolReceipt(AuthNetData.TransId); processingResult.Data = AuthNetData.TransId; return(Ok(processingResult)); }