I was trying to get the NVP sdk for asp.net from the
https://www.paypal.com/IntegrationCenter/ic_nvp.html
as described in the pdf guide for NVP integration
but this link is broken.
you may try to download the files from:
https://www.sandbox.paypal.com/IntegrationCenter/ic_nvp.html
Saturday, May 19, 2007
Paypal Express checkout NVP -- Really simple
Create a class file for the paypal operation:
In this class file this is a three step process:
#region declare Global variables
public const string strUsername = "--API User Name--";
public const string strPassword = "--API Pasword--";
public const string strSignature = "--API Signature--"
public const string strNVPSandboxServer = "https://api-3t.sandbox.paypal.com/nvp";
public const string strNVPLiveServer = "https://api-3t.paypal.com/nvp";
public const string strAPIVersion = "2.3";
#endregion
#region Paypal Payment Step -> One ::connect to PayPal an get the token number
public string SetExpressCheckout(string Amount)
{
string returnURL = "http://localhost/ppexpcheckout/ReviewOrder.aspx" + "?amount=" + Amount + "¤cy=USD";
string cancelURL = returnURL.Replace("ReviewOrder","ExpCheckOut");
string strCredentials = "USER=" + strUsername + "&PWD=" + strPassword + "&SIGNATURE=" + strSignature;
string strNVP = strCredentials;
strNVP += "&PAYMENTACTION=Sale&AMT="+Amount+"&RETURNURL=" + returnURL;
strNVP += "&CANCELURL="+ cancelURL;
strNVP += "&METHOD=SetExpressCheckout&VERSION=" + strAPIVersion+"&NOSHIPPING=1";
//Create web request and web response objects, make sure you using the correct server (sandbox/live)
HttpWebRequest wrWebRequest = (HttpWebRequest)WebRequest.Create(strNVPSandboxServer);
//Set WebRequest Properties
wrWebRequest.Method = "POST";
// write the form values into the request message
StreamWriter requestWriter = new StreamWriter(wrWebRequest.GetRequestStream());
requestWriter.Write(strNVP);
requestWriter.Close();
// Get the response.
HttpWebResponse hwrWebResponse = (HttpWebResponse)wrWebRequest.GetResponse();
StreamReader responseReader = new StreamReader(wrWebRequest.GetResponse().GetResponseStream());
// and read the response
string responseData = responseReader.ReadToEnd();
responseReader.Close();
return responseData;
}
#endregion
#region Paypal Payment Step -> Two :: Supply the token and get the payer details to process
public string GetExpressCheckoutDetails(string Token)
{
string strCredentials = "USER=" + strUsername + "&PWD=" + strPassword + "&SIGNATURE=" + strSignature;
string strNVP = strCredentials;
strNVP += "&METHOD=GetExpressCheckoutDetails&VERSION=" + strAPIVersion+"&TOKEN="+Token ;
//Create web request and web response objects, make sure you using the correct server (sandbox/live)
HttpWebRequest wrWebRequest = (HttpWebRequest)WebRequest.Create(strNVPSandboxServer);
//Set WebRequest Properties
wrWebRequest.Method = "POST";
// write the form values into the request message
StreamWriter requestWriter = new StreamWriter(wrWebRequest.GetRequestStream());
requestWriter.Write(strNVP);
requestWriter.Close();
// Get the response.
HttpWebResponse hwrWebResponse = (HttpWebResponse)wrWebRequest.GetResponse();
StreamReader responseReader = new StreamReader(wrWebRequest.GetResponse().GetResponseStream());
// and read the response
string responseData = responseReader.ReadToEnd();
responseReader.Close();
return responseData;
}
#endregion
#region Paypal Payment Step -> Three :: Supply token, payerID and Amount to process final payment
public string DoExpressCheckoutPayment(string token, string payerID,string Amount)
{
string strCredentials = "USER=" + strUsername + "&PWD=" + strPassword + "&SIGNATURE=" + strSignature;
string strNVP = strCredentials;
strNVP += "&METHOD=DoExpressCheckoutPayment&VERSION=" + strAPIVersion+"&TOKEN="+token;
strNVP += "&PAYERID="+payerID;
strNVP += "&PAYMENTACTION=Sale";
strNVP += "&AMT="+Amount;
//Create web request and web response objects, make sure you using the correct server (sandbox/live)
HttpWebRequest wrWebRequest = (HttpWebRequest)WebRequest.Create(strNVPSandboxServer);
//Set WebRequest Properties
wrWebRequest.Method = "POST";
// write the form values into the request message
StreamWriter requestWriter = new StreamWriter(wrWebRequest.GetRequestStream());
requestWriter.Write(strNVP);
requestWriter.Close();
// Get the response.
HttpWebResponse hwrWebResponse = (HttpWebResponse)wrWebRequest.GetResponse();
StreamReader responseReader = new StreamReader(wrWebRequest.GetResponse().GetResponseStream());
// and read the response
string responseData = responseReader.ReadToEnd();
responseReader.Close();
return responseData;
}
#endregion
Create an express checkout page that will initiate the process:
The code for the call from this page is ::
PP paypal = new PP(); // PP is the class that you have generated
string responseData = paypal.SetExpressCheckout(TextBoxAmt.Text);
string token = responseData.Substring(responseData.IndexOf("TOKEN=")+6);
Session["token"] = token;
string host = "www.sandbox.paypal.com";
Response.Redirect("https://" + host + "/cgi-bin/webscr?cmd=_express-checkout&token=" +token );
Now Create a ReviewOrder page that will take the call from the PayPal:
the code for the call from this page is ::
#region page load
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
getdetails(Request.QueryString["TOKEN"].ToString());
}
#endregion
#region get the payer details to process the order payment
private void getdetails(string token,object sender, System.EventArgs e)
{
PP paypal = new PP();
//read the response
string responseData = paypal.GetExpressCheckoutDetails(token);
string payerID = responseData.Substring(responseData.IndexOf("PAYERID=")+8);
payerID = payerID.Substring(0,payerID.IndexOf("&"));
Session["payerID"] = payerID;
responseData= responseData.Replace("%2e",".");
responseData= responseData.Replace("%3a",":");
responseData= responseData.Replace("%2d","-");
responseData= responseData.Replace("%40","@");
responseData= responseData.Replace("%3f","?");
Response.Write(responseData);
}
#endregion
#region Process the order payment
private void BtnPay_Click(object sender, System.EventArgs e)
{
BtnPay.Visible = false;
x.Visible=false;
string token = Request.QueryString["TOKEN"].ToString();
string amount = Request.QueryString["amount"].ToString();
PP paypal = new PP();
// read the response
string responseData = paypal.DoExpressCheckoutPayment(token,Session["payerID"].ToString(),amount);
responseData= responseData.Replace("%2e",".");
responseData= responseData.Replace("%3a",":");
responseData= responseData.Replace("%2d","-");
responseData= responseData.Replace("%40","@");
responseData= responseData.Replace("%3f","?");
Response.Write(responseData);
}
#endregion
In this class file this is a three step process:
#region declare Global variables
public const string strUsername = "--API User Name--";
public const string strPassword = "--API Pasword--";
public const string strSignature = "--API Signature--"
public const string strNVPSandboxServer = "https://api-3t.sandbox.paypal.com/nvp";
public const string strNVPLiveServer = "https://api-3t.paypal.com/nvp";
public const string strAPIVersion = "2.3";
#endregion
#region Paypal Payment Step -> One ::connect to PayPal an get the token number
public string SetExpressCheckout(string Amount)
{
string returnURL = "http://localhost/ppexpcheckout/ReviewOrder.aspx" + "?amount=" + Amount + "¤cy=USD";
string cancelURL = returnURL.Replace("ReviewOrder","ExpCheckOut");
string strCredentials = "USER=" + strUsername + "&PWD=" + strPassword + "&SIGNATURE=" + strSignature;
string strNVP = strCredentials;
strNVP += "&PAYMENTACTION=Sale&AMT="+Amount+"&RETURNURL=" + returnURL;
strNVP += "&CANCELURL="+ cancelURL;
strNVP += "&METHOD=SetExpressCheckout&VERSION=" + strAPIVersion+"&NOSHIPPING=1";
//Create web request and web response objects, make sure you using the correct server (sandbox/live)
HttpWebRequest wrWebRequest = (HttpWebRequest)WebRequest.Create(strNVPSandboxServer);
//Set WebRequest Properties
wrWebRequest.Method = "POST";
// write the form values into the request message
StreamWriter requestWriter = new StreamWriter(wrWebRequest.GetRequestStream());
requestWriter.Write(strNVP);
requestWriter.Close();
// Get the response.
HttpWebResponse hwrWebResponse = (HttpWebResponse)wrWebRequest.GetResponse();
StreamReader responseReader = new StreamReader(wrWebRequest.GetResponse().GetResponseStream());
// and read the response
string responseData = responseReader.ReadToEnd();
responseReader.Close();
return responseData;
}
#endregion
#region Paypal Payment Step -> Two :: Supply the token and get the payer details to process
public string GetExpressCheckoutDetails(string Token)
{
string strCredentials = "USER=" + strUsername + "&PWD=" + strPassword + "&SIGNATURE=" + strSignature;
string strNVP = strCredentials;
strNVP += "&METHOD=GetExpressCheckoutDetails&VERSION=" + strAPIVersion+"&TOKEN="+Token ;
//Create web request and web response objects, make sure you using the correct server (sandbox/live)
HttpWebRequest wrWebRequest = (HttpWebRequest)WebRequest.Create(strNVPSandboxServer);
//Set WebRequest Properties
wrWebRequest.Method = "POST";
// write the form values into the request message
StreamWriter requestWriter = new StreamWriter(wrWebRequest.GetRequestStream());
requestWriter.Write(strNVP);
requestWriter.Close();
// Get the response.
HttpWebResponse hwrWebResponse = (HttpWebResponse)wrWebRequest.GetResponse();
StreamReader responseReader = new StreamReader(wrWebRequest.GetResponse().GetResponseStream());
// and read the response
string responseData = responseReader.ReadToEnd();
responseReader.Close();
return responseData;
}
#endregion
#region Paypal Payment Step -> Three :: Supply token, payerID and Amount to process final payment
public string DoExpressCheckoutPayment(string token, string payerID,string Amount)
{
string strCredentials = "USER=" + strUsername + "&PWD=" + strPassword + "&SIGNATURE=" + strSignature;
string strNVP = strCredentials;
strNVP += "&METHOD=DoExpressCheckoutPayment&VERSION=" + strAPIVersion+"&TOKEN="+token;
strNVP += "&PAYERID="+payerID;
strNVP += "&PAYMENTACTION=Sale";
strNVP += "&AMT="+Amount;
//Create web request and web response objects, make sure you using the correct server (sandbox/live)
HttpWebRequest wrWebRequest = (HttpWebRequest)WebRequest.Create(strNVPSandboxServer);
//Set WebRequest Properties
wrWebRequest.Method = "POST";
// write the form values into the request message
StreamWriter requestWriter = new StreamWriter(wrWebRequest.GetRequestStream());
requestWriter.Write(strNVP);
requestWriter.Close();
// Get the response.
HttpWebResponse hwrWebResponse = (HttpWebResponse)wrWebRequest.GetResponse();
StreamReader responseReader = new StreamReader(wrWebRequest.GetResponse().GetResponseStream());
// and read the response
string responseData = responseReader.ReadToEnd();
responseReader.Close();
return responseData;
}
#endregion
Create an express checkout page that will initiate the process:
The code for the call from this page is ::
PP paypal = new PP(); // PP is the class that you have generated
string responseData = paypal.SetExpressCheckout(TextBoxAmt.Text);
string token = responseData.Substring(responseData.IndexOf("TOKEN=")+6);
Session["token"] = token;
string host = "www.sandbox.paypal.com";
Response.Redirect("https://" + host + "/cgi-bin/webscr?cmd=_express-checkout&token=" +token );
Now Create a ReviewOrder page that will take the call from the PayPal:
the code for the call from this page is ::
#region page load
private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack)
getdetails(Request.QueryString["TOKEN"].ToString());
}
#endregion
#region get the payer details to process the order payment
private void getdetails(string token,object sender, System.EventArgs e)
{
PP paypal = new PP();
//read the response
string responseData = paypal.GetExpressCheckoutDetails(token);
string payerID = responseData.Substring(responseData.IndexOf("PAYERID=")+8);
payerID = payerID.Substring(0,payerID.IndexOf("&"));
Session["payerID"] = payerID;
responseData= responseData.Replace("%2e",".");
responseData= responseData.Replace("%3a",":");
responseData= responseData.Replace("%2d","-");
responseData= responseData.Replace("%40","@");
responseData= responseData.Replace("%3f","?");
Response.Write(responseData);
}
#endregion
#region Process the order payment
private void BtnPay_Click(object sender, System.EventArgs e)
{
BtnPay.Visible = false;
x.Visible=false;
string token = Request.QueryString["TOKEN"].ToString();
string amount = Request.QueryString["amount"].ToString();
PP paypal = new PP();
// read the response
string responseData = paypal.DoExpressCheckoutPayment(token,Session["payerID"].ToString(),amount);
responseData= responseData.Replace("%2e",".");
responseData= responseData.Replace("%3a",":");
responseData= responseData.Replace("%2d","-");
responseData= responseData.Replace("%40","@");
responseData= responseData.Replace("%3f","?");
Response.Write(responseData);
}
#endregion
Labels:
Paypal with .Net
Subscribe to:
Posts (Atom)