Base type definition of a response payload that can carry any type of payload content with following optional elements: - timestamp of response message, - application level acknowledgement, and - application-level errors and warnings.
 /// <summary>
 /// Checks response
 /// </summary>
 /// <param name="abstractResponse">response</param>
 /// <param name="errorMsg">Error message if exists</param>
 /// <returns>True - response OK; otherwise, false</returns>
 public static bool CheckSuccess(AbstractResponseType abstractResponse, out string errorMsg)
 {
     bool success = false;
     var sb = new StringBuilder();
     switch (abstractResponse.Ack)
     {
         case AckCodeType.SUCCESS:
         case AckCodeType.SUCCESSWITHWARNING:
             success = true;
             break;
         default:
             break;
     }
     if (null != abstractResponse.Errors)
     {
         foreach (ErrorType errorType in abstractResponse.Errors)
         {
             if (sb.Length <= 0)
             {
                 sb.Append(Environment.NewLine);
             }
             sb.Append("LongMessage: ").Append(errorType.LongMessage).Append(Environment.NewLine);
             sb.Append("ShortMessage: ").Append(errorType.ShortMessage).Append(Environment.NewLine);
             sb.Append("ErrorCode: ").Append(errorType.ErrorCode).Append(Environment.NewLine);
         }
     }
     errorMsg = sb.ToString();
     return success;
 }
Beispiel #2
0
        /// <summary>
        /// Parses paypal response and returns true if operation succeeds. False otherwise.
        /// </summary>
        /// <param name="response"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        public static bool ParseResponseSuccess(AbstractResponseType response, out string error)
        {
            var success = false;
            if (response.Ack.HasValue)
            {
                switch (response.Ack)
                {
                    case AckCodeType.SUCCESS:
                    case AckCodeType.SUCCESSWITHWARNING:
                        success = true;
                        break;
                }
            }

            var builder = new StringBuilder();
            var errorFormat = "LongMessage: {0}" + Environment.NewLine + "ShortMessage: {1}" + Environment.NewLine + "ErrorCode: {2}" + Environment.NewLine;
            if (response.Errors != null)
            {
                foreach (var errorType in response.Errors)
                {
                    if (builder.Length <= 0)
                    {
                        builder.Append(Environment.NewLine);
                    }

                    builder.Append(string.Format(errorFormat, errorType.LongMessage, errorType.ShortMessage,
                        errorType.ErrorCode));
                }
            }
            error = builder.ToString();

            return success;
        }
        private bool CheckResponse(AbstractResponseType response, out string error)
        {
            var retVal = false;

            if (response != null)
            {
                if ((response.Errors != null && response.Errors.Count > 0) || !(response.Ack.Equals(AckCodeType.SUCCESS) || response.Ack.Equals(AckCodeType.SUCCESSWITHWARNING)))
                {
                    StringBuilder sb = new StringBuilder();
                    foreach (var err in response.Errors)
                    {
                        sb.AppendLine(err.LongMessage);
                    }

                    error = sb.ToString();
                }
                else
                {
                    error = string.Empty;
                }

                retVal = true;
            }
            else
            {
                error = "response in null";
            }

            return retVal;
        }
		private bool CheckResponse(AbstractResponseType response)
		{
			if (response != null)
			{
				if (response.Ack.Equals(AckCodeType.FAILURE) || (response.Errors != null && response.Errors.Count > 0))
				{
					StringBuilder sb = new StringBuilder();
					foreach (var error in response.Errors)
					{
						sb.AppendLine(error.LongMessage);
					}

					throw new NullReferenceException(sb.ToString());
				}
			}
			else
			{
				throw new NullReferenceException("response in null");
			}

			return true;
		}
		private bool CheckResponse(AbstractResponseType response)
		{
			if (response != null)
			{
				if (response.Errors != null && response.Errors.Count > 0)
				{
					StringBuilder sb = new StringBuilder();
					foreach (var error in response.Errors)
					{
						sb.Append("LongMessage: ").Append(error.LongMessage).Append(Environment.NewLine);
						sb.Append("ShortMessage: ").Append(error.ShortMessage).Append(Environment.NewLine);
						sb.Append("ErrorCode: ").Append(error.ErrorCode).Append(Environment.NewLine);
					}

					throw new NullReferenceException(sb.ToString());
				}
				else if(!(response.Ack == AckCodeType.SUCCESS) && !(response.Ack == AckCodeType.SUCCESSWITHWARNING))
				{
					throw new NullReferenceException("Paypal error without detail info");
				}
			}
			else
			{
				throw new NullReferenceException("response in null");
			}

			return true;
		}