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;
}
}