protected virtual CheckStatusInfo ValidateCheckStatusInfo()
        {
            Document existingDoc;

            try
            {
                existingDoc = _documentManager.GetDocumentByName(_transaction.Id, SUBMISSION_CHECK_STATUS_INFO_FILE_NAME, true);
            }
            catch (FileNotFoundException)
            {
                throw new FileNotFoundException(string.Format("The transaction is missing the required \"{0}\" attached document",
                                                              SUBMISSION_CHECK_STATUS_INFO_FILE_NAME));
            }
            CheckStatusInfo info = _serializationHelper.Deserialize <CheckStatusInfo>(existingDoc.Content);
            DateTime        now  = DateTime.Now;

            DateTime earliestSubmitTime = now.AddDays(-_checkStatusForNumDays);

            if (info.SubmitTime < earliestSubmitTime)
            {
                // Exceeded number of check days
                throw new ArgumentException("The remote transaction has not completed in the required amount of time.");
            }

            DateTime nextCheckTime = info.LastCheckStatusTime.AddMinutes(_checkStatusIntervalInMinutes);

            if (nextCheckTime > now)
            {
                // Not ready to check again yet
                return(null);
            }
            return(info);
        }
        public virtual CommonTransactionStatusCode ProcessSubmitAndReturnStatus(string transactionId,
                                                                                out string statusDetail)
        {
            Init(transactionId);

            statusDetail = string.Empty;

            if (_didSubmit)
            {
                CheckStatusInfo checkStatusInfo = ValidateCheckStatusInfo();

                if (checkStatusInfo != null)
                {
                    try
                    {
                        CommonTransactionStatusCode networkTransactionStatus;

                        AddPartnerTransactionDocumentsToTransaction(transactionId, out networkTransactionStatus);

                        checkStatusInfo.LastCheckStatusTime = DateTime.Now;
                        SaveCheckStatusInfo(checkStatusInfo);

                        if ((networkTransactionStatus == CommonTransactionStatusCode.Failed) ||
                            (networkTransactionStatus == CommonTransactionStatusCode.Completed))
                        {
                            return(networkTransactionStatus);
                        }
                    }
                    catch (Exception)
                    {
                        // Will try again on next submit call
                        // TODO: Need to check for network exception and try again
                        throw;
                    }
                }
            }
            else
            {
                SubmitTransactionDocumentToPartner(transactionId, _submitPartner, _submitFlowName,
                                                   _submitOperationName);

                SaveCheckStatusInfo();

                if (_checkStatusForNumDays == 0)
                {
                    // This means don't check status at all
                    return(CommonTransactionStatusCode.Completed);
                }
            }
            return(CommonTransactionStatusCode.Pending);
        }
        protected virtual void SaveCheckStatusInfo(CheckStatusInfo info)
        {
            byte[]   content = _serializationHelper.SerializeWithLineBreaks(info);
            Document doc     = new Document(SUBMISSION_CHECK_STATUS_INFO_FILE_NAME, CommonContentType.XML, content);

            doc.DontAutoCompress = true;
            Document existingDoc = null;

            try
            {
                existingDoc = _documentManager.GetDocumentByName(_transaction.Id, SUBMISSION_CHECK_STATUS_INFO_FILE_NAME, false);
            }
            catch (FileNotFoundException)
            {
            }
            if (existingDoc != null)
            {
                _documentManager.DeleteDocument(_transaction.Id, existingDoc.Id);
            }
            _documentManager.AddDocument(_transaction.Id, CommonTransactionStatusCode.Completed, string.Empty, doc);
        }