Tuesday, 30 April 2013

Paypal Integration


PayPal

Here I Would like to share payapl integration with the asp.net website.
Step by step


  1. Prereqs:
    - Paypal business account with paypal websites pro enabled
    - Paypal sandbox account (you must login and accept the agreement for websites pro from this account)
    - get an API username, password, & signature from your sandbox account
- Create Seller account with 100$ balance and that seller accounts api username,password,signature use.

  1. create an ASPX form with the following fields:
    cctypeDdl, ccnumberTextbox, expdateDropDown, yearDropDown, CVVcodeTextBox, amountTextBox, firstnameTextbox, lastnameTextbox, addressTextbox, cityTextbox, regionTextbox, countryDropDown, postalTextbox .and these labels: successLabel, errLabel, errcodeLabe
  2. Create a button control and button_click event handler
  3. string strUsername = "pxxxxx.xxx_api1.xxxx.com";
string strPassword = "123456780";
string strSignature = "AegUd0h54RhcCVb8dQ2IJUh9IDp6A4MWXidjp5qgcm";
string strCredentials = "USER=" + strUsername + "&PWD=" + strPassword + "&SIGNATURE=" + strSignature;

string strNVPSandboxServer = "https://api-3t.sandbox.paypal.com/nvp";

//string strAPIVersion = "2.3";
string strAPIVersion = "98";

string strNVP = strCredentials + "&METHOD=DoDirectPayment" +
"&CREDITCARDTYPE=" + CreditCardTypeType.Visa.ToString() +
"&ACCT=" + txtCCN.Text +
"&EXPDATE=" + "42018" +
"&CVV2=" + "111" +
"&AMT=" + "7.47" +
"&FIRSTNAME=" + "Parth" +
"&LASTNAME=" + "Mehta" +
"&IPADDRESS=10.x.x.xx" +
"&STREET=452-New street" +
"&CITY=Ahmedabad" +
"&STATE=Gujarat" +
"&COUNTRY=" + "USA" +
"&ZIP=95110" + "14543" +
"&COUNTRYCODE=US" +
"&PAYMENTACTION=" + PaymentActionCodeType.Sale +
"&cancelUrl=http://www.example.com/cancel.html" +
"&returnUrl=http://www.example.com/success.html" +
"&VERSION=" + strAPIVersion;

try
{
//Create web request and web response objects, make sure you using the correct server (sandbox/live)
HttpWebRequest wrWebRequest = (HttpWebRequest)WebRequest.Create(strNVPSandboxServer);
wrWebRequest.Method = "POST";

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();

string result = Server.UrlDecode(responseData);

string[] arrResult = result.Split('&');
Hashtable htResponse = new Hashtable();
string[] responseItemArray;
foreach (string responseItem in arrResult)
{
responseItemArray = responseItem.Split('=');
htResponse.Add(responseItemArray[0], responseItemArray[1]);
}

string strAck = htResponse["ACK"].ToString();

if (strAck == "Success" || strAck == "SuccessWithWarning")
{
string strAmt = htResponse["AMT"].ToString();
string strCcy = htResponse["CURRENCYCODE"].ToString();
string strTransactionID = htResponse["TRANSACTIONID"].ToString();

string strSuccess = "Thank you, your order for: $" + strAmt + " " + strCcy + " has been processed.";
successLabel.Text = strSuccess;
}
else
{
string strErr = "Error: " + htResponse["L_LONGMESSAGE0"].ToString();
string strErrcode = "Error code: " + htResponse["L_ERRORCODE0"].ToString();
errLabel.Text = strErr;
errcodeLabel.Text = strErrcode;
return;
}
}
catch (Exception ex)
{
// do something to catch the error, like write to a log file.
Response.Write("error processing");
}

  1. once it work successfully deploy in live server . For that
    https://developer.paypal.com/webapps/developer/docs/classic/lifecycle/goingLive/

How to take classic api's username, password and signature
https://developer.paypal.com/webapps/developer/docs/classic/lifecycle/ug_sandbox/

1 comment:

  1. Great Work..... Its Very Helpful...

    Keep it up Mr.Parth..... :)

    ReplyDelete