private int ProcessBusinessResponse(VAT100_BusinessResponseMessage aResponse) { // The response was favourable int Result = 0; // Extract all the narrative from the response string narrative = aResponse.Body.SuccessResponse.Message + "\r\n\r\n"; // Typically "Thank you for your submission" narrative += aResponse.Body.SuccessResponse.IRmarkReceipt.IRmarkReceiptMessage + "\r\n\r\n"; // "HMRC has received the HMRC-VAT-DEC document..." narrative += aResponse.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Body.PaymentNotification.Narrative + "\r\n\r\n"; // If there is VAT to pay, then include the due date double netVat = aResponse.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Body.PaymentNotification.NetVAT; if (netVat > 0.0) { narrative += "Payment due date : " + aResponse.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Body.PaymentDueDate + "\r\n\r\n"; } // Add the narrative textNarrative.AppendText(narrative + "\r\n\r\n"); return(Result); }
//--------------------------------------------------------------------------------------------- /// <summary> /// Handle a non-error response from HMRC /// </summary> /// <param name="aResponse"></param> private int ProcessBusinessResponse(VAT100_BusinessResponseMessage aResponse) { // The response was favourable int Result = 0; // Extract all the narrative from the response. Start with an error message that should get overwritten string narrative = string.Empty; // PKR. 17/09/2015. // Now check each level exists before attempting to extract the data as it was // raising an exception. Also wrap in a try..finally for good measure. try { try { if (aResponse.Body.SuccessResponse != null) { // The SuccessResponse Message if (aResponse.Body.SuccessResponse.Message != null) { narrative += aResponse.Body.SuccessResponse.Message + "\r\n\r\n"; // Typically "Thank you for your submission..." } // The IRMarkReceipt message if (aResponse.Body.SuccessResponse.IRmarkReceipt != null) { if (aResponse.Body.SuccessResponse.IRmarkReceipt.IRmarkReceiptMessage != null) { narrative += aResponse.Body.SuccessResponse.IRmarkReceipt.IRmarkReceiptMessage + "\r\n\r\n"; // "HMRC has received the HMRC-VAT-DEC document..." } } // Acceptance time if (aResponse.Body.SuccessResponse.AcceptedTime != null) { narrative += string.Format("Submission accepted at {0}\r\n\r\n", aResponse.Body.SuccessResponse.AcceptedTime); } // if (aResponse.Body.SuccessResponse.ResponseData != null) { if (aResponse.Body.SuccessResponse.ResponseData.VATDeclarationResponse != null) { if (aResponse.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Header != null) { if (aResponse.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Header.VATPeriod != null) { narrative += string.Format("VAT Period : {0}\r\nStart Date : {1}\r\nEnd Date : {2}\r\n\r\n", aResponse.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Header.VATPeriod.PeriodId, aResponse.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Header.VATPeriod.PeriodStartDate, aResponse.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Header.VATPeriod.PeriodEndDate); } } if (aResponse.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Body != null) { if (aResponse.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Body.PaymentNotification != null) { if (aResponse.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Body.PaymentNotification.Narrative != null) { narrative += aResponse.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Body.PaymentNotification.Narrative + "\r\n\r\n"; // If there is VAT to pay, then include the due date double netVat = aResponse.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Body.PaymentNotification.NetVAT; if (netVat > 0.0) { narrative += "Payment due date : " + aResponse.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Body.PaymentDueDate + "\r\n"; narrative += string.Format("Amount due : {0}", netVat) + "\r\n\r\n"; } } } if (aResponse.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Body.InformationNotification != null) { if (aResponse.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Body.InformationNotification.Narrative != null) { narrative += aResponse.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Body.InformationNotification.Narrative + "\r\n\r\n"; } } } } } } } catch (Exception ex) { Log.Add("Error processing response from HMRC. " + ex.Message); } } // end try finally { if (narrative == string.Empty) { narrative = "Error decoding response message from HMRC"; } // Update the pending document status FPendingDocument.theDocument.status = Convert.ToInt16(SubmissionStatus.ssAccepted); // Add the narrative FPendingDocument.theDocument.hmrcNarrative = narrative; // Save the record Log.Add("Updating record in database for " + FPendingDocument.theDocument.correlationID); Result = VAT100Database.Instance.UpdateVAT100Entry(FPendingDocument.theDocument, FPendingDocument.companyCode); } return(Result); }
/// <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); }
private void btnDeserialise_Click(object sender, EventArgs e) { VAT100_BusinessResponseMessage responseMsg = null; // VAT100_BusinessErrorResponse errorMsg = null; try { XmlSerializer responseSerialiser = new XmlSerializer(typeof(VAT100_BusinessResponseMessage)); using (StringReader reader = new StringReader(editXML.Text)) { responseMsg = (VAT100_BusinessResponseMessage)(responseSerialiser.Deserialize(reader)); } // XmlSerializer errorSerialiser = new XmlSerializer(typeof(VAT100_BusinessErrorResponse)); // using (StringReader reader = new StringReader(editXML.Text)) // { // errorMsg = (VAT100_BusinessErrorResponse)(errorSerialiser.Deserialize(reader)); // } } catch (Exception ex) { MessageBox.Show("Error handling HMRC response : " + ex.Message); } string narrative = string.Empty; if (responseMsg.Body.SuccessResponse != null) { // The SuccessResponse Message if (responseMsg.Body.SuccessResponse.Message != null) { narrative += responseMsg.Body.SuccessResponse.Message + "\r\n\r\n"; // Typically "Thank you for your submission..." } // The IRMarkReceipt message if (responseMsg.Body.SuccessResponse.IRmarkReceipt != null) { if (responseMsg.Body.SuccessResponse.IRmarkReceipt.IRmarkReceiptMessage != null) { narrative += responseMsg.Body.SuccessResponse.IRmarkReceipt.IRmarkReceiptMessage + "\r\n\r\n"; // "HMRC has received the HMRC-VAT-DEC document..." } } // Acceptance time if (responseMsg.Body.SuccessResponse.AcceptedTime != null) { narrative += string.Format("Submission accepted at {0}\r\n\r\n", responseMsg.Body.SuccessResponse.AcceptedTime); } // if (responseMsg.Body.SuccessResponse.ResponseData != null) { if (responseMsg.Body.SuccessResponse.ResponseData.VATDeclarationResponse != null) { if (responseMsg.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Header != null) { if (responseMsg.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Header.VATPeriod != null) { narrative += string.Format("VAT Period : {0}\r\nStart Date : {1}\r\nEnd Date : {2}\r\n\r\n", responseMsg.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Header.VATPeriod.PeriodId, responseMsg.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Header.VATPeriod.PeriodStartDate, responseMsg.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Header.VATPeriod.PeriodEndDate); } } if (responseMsg.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Body != null) { if (responseMsg.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Body.PaymentNotification != null) { if (responseMsg.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Body.PaymentNotification.Narrative != null) { narrative += responseMsg.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Body.PaymentNotification.Narrative + "\r\n\r\n"; // If there is VAT to pay, then include the due date double netVat = responseMsg.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Body.PaymentNotification.NetVAT; if (netVat > 0.0) { narrative += "Payment due date : " + responseMsg.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Body.PaymentDueDate + "\r\n"; narrative += string.Format("Amount due : {0}", netVat) + "\r\n\r\n"; } } } if (responseMsg.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Body.InformationNotification != null) { if (responseMsg.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Body.InformationNotification.Narrative != null) { narrative += responseMsg.Body.SuccessResponse.ResponseData.VATDeclarationResponse.Body.InformationNotification.Narrative + "\r\n\r\n"; } } } } } } editNarrative.Text = narrative; }