Hi everyone,
I am new to paypal. I have a paypal sandbox account. I want to implement IPN, so that I will not miss any transaction. I got the IPN code from Paypal site and implemented that on page and also implemented my logic to storing the info in DB. After that I hosted that page on Windows Azure Server. The code is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using Microsoft.Http;
using System.Configuration;
using System.Net;
using System.Text;
namespace SendGridService
{
public partial class IPNHandler : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
string postUrl = ConfigurationManager.AppSettings["PayPalSubmitUrl"];
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(postUrl);
//Set values for the request back
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
byte[] param = Request.BinaryRead(HttpContext.Current.Request.ContentLength);
string strRequest = Encoding.ASCII.GetString(param);
/***************Start Saving in DB****************/
HttpClient httpClientSecond = new HttpClient(ConfigurationManager.AppSettings["DBWebServiceURL"]);
httpClientSecond.DefaultHeaders.Add("inputXML", "<XMLPROCESS><Processname value=\"Paypal\"><Data>" + strRequest + "</Data><Action>IPN_Insert</Action></Processname></XMLPROCESS>");
httpClientSecond.DefaultHeaders.Add("ResponseType", "XML");
HttpResponseMessage messageNew = httpClientSecond.Post(ConfigurationManager.AppSettings["DBWebServiceURL"], HttpContent.Create("Request"));
string serviceResponseStatus = messageNew.Content.ReadAsString();
/****************End Saving*********************/
string ipnPost = strRequest;
strRequest += "&cmd=_notify-validate";
req.ContentLength = strRequest.Length;
//for proxy
//WebProxy proxy = new WebProxy(new Uri("http://url:port#"));
//req.Proxy = proxy;
//Send the request to PayPal and get the response
StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
streamOut.Write(strRequest);
streamOut.Close();
StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
string strResponse = streamIn.ReadToEnd();
streamIn.Close();
if (strResponse == "VERIFIED")
{
//check the payment_status is Completed
//check that txn_id has not been previously processed
//check that receiver_email is your Primary PayPal email
//check that payment_amount/payment_currency are correct
//process payment
}
else if (strResponse == "INVALID")
{
//log for manual investigation
}
else
{
//log response/ipn data for manual investigation
}
}
catch (Exception ex)
{
HttpClient httpClientSecond = new HttpClient(ConfigurationManager.AppSettings["DBWebServiceURL"]);
httpClientSecond.DefaultHeaders.Add("inputXML", "<XMLPROCESS><Processname value=\"Paypal\"><Data>" + ex.Message + "</Data><Action>IPN_Insert</Action></Processname></XMLPROCESS>");
httpClientSecond.DefaultHeaders.Add("ResponseType", "XML");
HttpResponseMessage messageNew = httpClientSecond.Post(ConfigurationManager.AppSettings["DBWebServiceURL"], HttpContent.Create("Request"));
string serviceResponseStatus = messageNew.Content.ReadAsString();
}
}
}
}
Please tell me If anything wrong in my coding or have to do any settings on sandbox account.
Thanks in advance.
Bikramjit