/// <summary> /// Handles the response returned from the HMRC Gateway for the VAT Submission /// that is currently being processed. Returns false if the response is either /// that the VAT Return was in error or if it was accepted successfully, and /// therefore that polling should be stopped. Otherwise it returns true, and /// polling should continue. /// </summary> /// <param name="responseXML"></param> /// <returns></returns> private int HandleHMRCResponse(string responseXML) { string filename; int Result = 0; // Deserialise the response. At this point we don't know if its a business response or an error response. VAT100_BusinessResponseMessage responseMsg = null; VAT100_BusinessErrorResponse errorMsg = null; try { XmlSerializer responseSerialiser = new XmlSerializer(typeof(VAT100_BusinessResponseMessage)); using (StringReader reader = new StringReader(responseXML)) { responseMsg = (VAT100_BusinessResponseMessage)(responseSerialiser.Deserialize(reader)); } XmlSerializer errorSerialiser = new XmlSerializer(typeof(VAT100_BusinessErrorResponse)); using (StringReader reader = new StringReader(responseXML)) { errorMsg = (VAT100_BusinessErrorResponse)(errorSerialiser.Deserialize(reader)); } } catch (Exception ex) { LogText("Error handling HMRC response : " + ex.Message); } //........................................................................................... // Determine the response type. // For a pending submission, this will be "acknowledgement" // For a successful submission, this will be "response" // For a submission failure, this will be "error" string responseType = responseMsg.Header.MessageDetails.Qualifier; // response, error, acknowledgement string function = responseMsg.Header.MessageDetails.Function; switch (responseType.ToLower()) { case "acknowledgement": // Nothing to do. We'll continue polling until we get a business response or an error. Log.Add("Acknowledgement received"); Result = 0; break; case "response": // Business Response. We can stop polling, save the data and then delete the request (unless it was a delete). Log.Add("Response received"); // CJS 2015-09-24 - ABSEXCH-16922 - HMRC Filing VAT Submissions xml folder // Save the XML filename = string.Format("response{0}.xml", responseMsg.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Header.VATPeriod.PeriodId); XMLWrite.ToReceivedFolder(FDatabase.tToolkit.Configuration.EnterpriseDirectory, filename, responseXML); Result = ProcessBusinessResponse(responseMsg); if (function.ToLower() != "delete") { Delete(FPendingDocument.theDocument.correlationID); } break; case "error": Log.Add("Error response received"); // CJS 2015-09-24 - ABSEXCH-16922 - HMRC Filing VAT Submissions xml folder // Save the XML DateTime timenow = DateTime.Now; filename = string.Format("error{0:yyyyMMdd_hhmm}.xml", timenow); XMLWrite.ToReceivedFolder(FDatabase.tToolkit.Configuration.EnterpriseDirectory, filename, responseXML); // Error Response. We can stop polling, save the data and then delete the request (unless it was a delete). Result = ProcessErrorResponse(errorMsg); if (function.ToLower() != "delete") { Delete(FPendingDocument.theDocument.correlationID); } break; default: // Unrecognised response qualifier throw new Exception("Unexpected response received from HMRC"); } return(Result); }
//--------------------------------------------------------------------------------------------- /// <summary> /// Submit an XML file to HMRC and adds it to the polling service to /// wait for a response. This method returns the immediate response, /// which will be an XML string containing either an acknowledgement /// that the submission was received by HMRC, or an error. /// </summary> /// <returns></returns> public string SubmitToHMRC(string companycode, string doctype, string xmldoc, string filename, string suburl, string username, string email) { string Result = string.Empty; Console.WriteLine(""); Console.WriteLine("SubmitToHMRC request"); Log.Add("SubmitToHMRC request received for company " + companycode); // Can only accept a submission if none are in progress for this company. if (FPollingService.Status(companycode) == ServiceStatus.ssIdle) { // Add the IRMark xmldoc = ApplyIRMark(xmldoc); // CJS 2015-09-24 - ABSEXCH-16922 - HMRC Filing VAT Submissions xml folder // Save a copy of the submitted file XMLWrite.ToSentFolder(dbHandler.tToolkit.Configuration.EnterpriseDirectory, filename, xmldoc); // Store the parameters locally in the pending document FDocType = doctype; FXMLDoc = xmldoc; //........................................................................................... // Prepare for the submission string hmrcURL = string.Empty; if (string.IsNullOrEmpty(username)) { username = "******"; } // Deserialize the XML to an hierarchical class structure so we can save the details VAT100_GovTalkMessage outMessage = DeserializeFromXmlString <VAT100_GovTalkMessage>(xmldoc); // Save the message class (HMRC-VAT-DEC or HMRC-VAT-DEC-TIL) FDocClass = outMessage.Header.MessageDetails.Class; // Determine where we want to send the data (Live or Dev) // The message class (HMRC-VAT-DEC or HMRC-VAT-DEC-TIL) will already be in the message sent by Exchequer. hmrcURL = suburl; //........................................................................................... // Submit the XML to HMRC and await the acknowledgement response // Create a request var request = (HttpWebRequest)WebRequest.Create(hmrcURL); var data = Encoding.ASCII.GetBytes(xmldoc); string responseXML = string.Empty; // LogText(PrettyPrinter.PrettyPrintXML(XMLDoc)); request.Method = "POST"; request.ContentType = "application/x-www-form-urlencoded"; request.ContentLength = data.Length; // Write the data to the request parameters using (var stream = request.GetRequestStream()) { stream.Write(data, 0, data.Length); } // Log.Add("Submitting request to " + hmrcURL); // Console.WriteLine("Submitting request to " + hmrcURL); // Send the request and get the response. var response = (HttpWebResponse)request.GetResponse(); // Get the response in a stream. var responseData = new StreamReader(response.GetResponseStream()).ReadToEnd(); // Convert the stream to a string responseXML = responseData.ToString(); // LogText(PrettyPrinter.PrettyPrintXML(responseXML)); // Clipboard.SetData(System.Windows.Forms.DataFormats.Text, responseXML); // Deserialise the response VAT100_Acknowledgement acknowledgementMsg; XmlSerializer serialiser = new XmlSerializer(typeof(VAT100_Acknowledgement)); using (StringReader reader = new StringReader(responseXML)) { acknowledgementMsg = (VAT100_Acknowledgement)(serialiser.Deserialize(reader)); } //........................................................................................... // Determine the response type. // For a successful submission, this will be "acknowledgement" // For a submission failure, this will be "error" string responseType = acknowledgementMsg.Header.MessageDetails.Qualifier; switch (responseType.ToLower()) { case "acknowledgement": Log.Add("Acknowledgement received"); FPollingService.Add(companycode, doctype, username, outMessage, acknowledgementMsg); break; case "error": Log.Add("Error response received from HMRC"); break; default: Log.Add("Unrecognised response from HMRC"); // Unrecognised response qualifier break; } Result = responseXML; } else { // Tried to submit a return while one is in progress. FErrorString = "A VAT 100 return submission is currently being processed"; Log.Add(FErrorString); Result = string.Empty; } return(Result); }