PayPal.IPNMessage.Validate C# (CSharp) Method

Validate() public method

Returns the IPN request validation
public Validate ( ) : bool
return bool
        public bool Validate()
        {
            /// If ipn has been previously validated, do not repeat the validation process.
            if (this.ipnValidationResult != null)
            {
                return this.ipnValidationResult.Value;
            }
            else
            {
                // Get the IPN endpoint to use in order to validate the received IPN.
                // NOTE: This call will throw an exception if the config is not setup properly.
                string ipnEndpoint = this.GetIPNEndpoint();

                try
                {
                    // Setup the HTTP request to use for posting the IPN back to PayPal.
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(ipnEndpoint);
                    request.Method = "POST";
                    request.ContentType = "application/x-www-form-urlencoded";
                    request.ContentLength = this.ipnRequest.Length;

                    using (StreamWriter streamOut = new StreamWriter(request.GetRequestStream(), ipnEncoding))
                    {
                        streamOut.Write(this.ipnRequest);
                    }

                    // Send the request to PayPal and get the response
                    string strResponse = string.Empty;
                    using (StreamReader streamIn = new StreamReader(request.GetResponse().GetResponseStream()))
                    {
                        strResponse = streamIn.ReadToEnd();
                    }

                    // If the IPN is valid, the response from PayPal will be 'VERIFIED'.
                    if (strResponse.Equals("VERIFIED"))
                    {
                        this.ipnValidationResult = true;
                    }
                    else
                    {
                        logger.InfoFormat("IPN validation failed. Got response: " + strResponse);
                        this.ipnValidationResult = false;
                    }
                }
                catch (System.Exception ex)
                {
                    logger.InfoFormat(this.GetType().Name + " : " + ex.Message);

                }
                return this.ipnValidationResult.HasValue ? this.ipnValidationResult.Value : false;
            }
        }

Usage Example

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                byte[] parameters = Request.BinaryRead(HttpContext.Current.Request.ContentLength);

                if (parameters.Length > 0)
                {
                    // Configuration map containing signature credentials and other required configuration.
                    // For a full list of configuration parameters refer in wiki page
                    // (https://github.com/paypal/sdk-core-dotnet/wiki/SDK-Configuration-Parameters)
                    Dictionary<string, string> configurationMap = Configuration.GetConfig();
                    IPNMessage ipn = new IPNMessage(configurationMap, parameters);

                    bool isIpnValidated = ipn.Validate();
                    string transactionType = ipn.TransactionType;
                    NameValueCollection map = ipn.IpnMap;

                    logger.Info("----------Type-------------------" + this.GetType().Name + "\n"
                               + "*********IPN Name Value Pair****" + map + "\n"
                               + "#########IPN Transaction Type###" + transactionType + "\n"
                               + "=========IPN Validation=========" + isIpnValidated);
                }
            }
            catch (System.Exception ex)
            {
                logger.Debug("Exception in class " + this.GetType().Name + ": " + ex.Message);
                return;
            }
        }
All Usage Examples Of PayPal.IPNMessage::Validate