Paypal Payment Gateway Integration using ASP.NET


First Create Paypal sandbox account
 https://developer.paypal.com/

For Sandbox :-  "https://www.sandbox.paypal.com/cgi-bin/webscr?";
For Live :-  "https://www.paypal.com/cgi-bin/webscr?";

==================On Pay Button Click
--------------------------------Check whether item in list
if (dt.Rows.Count != 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (i == 0)
                {
                    url = "https://www.sandbox.paypal.com/cgi-bin/webscr?";
                    url += "cmd=_cart";
                    url += "&upload=" + i.ToString() + "";
                    url += "&business=sandbox@wantola.com";
                    url += "&item_number_1=" + dt.Rows[i]["Product_Id"].ToString();
                    url += "&item_name_1=" + dt.Rows[i]["Product"].ToString();
                    url += "&amount_1=" + Convert.ToUInt32(dt.Rows[i]["Product_Unit_Cost"]).ToString();
                    url += "&quantity_1=" + dt.Rows[i]["Product_Cnt"].ToString();
                }
                else
                {
                    url += "&item_number_" + (i + 1).ToString() + "=" + dt.Rows[i]["Product_Id"].ToString();
                    url += "&item_name_" + (i + 1).ToString() + "=" + dt.Rows[i]["Product"].ToString();
                    url += "&amount_" + (i + 1).ToString() + "=" + Convert.ToUInt32(dt.Rows[i]["Product_Unit_Cost"]).ToString();
                    url += "&quantity_" + (i + 1).ToString() + "=" + dt.Rows[i]["Product_Cnt"].ToString();
                }
            }

            url += "&return=http://Domainname/return url";
            url += "&notify_url=http:// Domainname/noftifyUrl";
            url += "&cancel_return=CancelURL";
            url += "&currency_code=USD";
            url += "&custom=" + custom;
            url += "&image_url=LOGO path";



====================On Notify Page Load Event


protected void Page_Load(object sender, EventArgs e)
    {       
        //Post back to either sandbox or live
        string strSandbox = "https://www.sandbox.paypal.com/cgi-bin/webscr";
        string strLive = "https://www.paypal.com/cgi-bin/webscr";
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strSandbox);

        //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);
        strRequest += "&cmd=_notify-validate";
        req.ContentLength = strRequest.Length;
 //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")
        {
  //==== SuccessFull
//============Get All Item Data

 foreach (string key in Request.Form.AllKeys)
                {
                 
                    if (key.StartsWith("item_name") && "item_name".Length < key.Length)
                    {
                        item_names = item_names + "~" + Convert.ToString(Request.Form["" + key + ""]);
                        item_namesKeys = item_namesKeys + "~" + Convert.ToString(key);
                    }
                    else if (key.StartsWith("item_number") && "item_number".Length < key.Length)
                    {
                        item_numbers = item_numbers + "~" + Convert.ToString(Request.Form["" + key + ""]);
                        item_numbersKey = item_numbersKey + "~" + Convert.ToString(key);
                    }
                    else if (key.StartsWith("quantity") && "quantity".Length < key.Length)
                    {
                        quantitys = quantitys + "~" + Convert.ToString(Request.Form["" + key + ""]);
                        quantitysKeys = quantitysKeys + "~" + Convert.ToString(key);
                    }
                    else if (key.StartsWith("mc_gross") && "mc_gross".Length < key.Length)
                    {
                        mc_gross_charges = mc_gross_charges + "~" + Convert.ToString(Request.Form["" + key + ""]);
                        mc_gross_chargesKeys = mc_gross_chargesKeys + "~" + Convert.ToString(key);
                    }
                    else if (key.StartsWith("mc_handling") && "mc_handling".Length < key.Length)
                    {
                        mc_handling_charges = mc_handling_charges + "~" + Convert.ToString(Request.Form["" + key + ""]);
                        mc_handling_chargesKeys = mc_handling_chargesKeys + "~" + Convert.ToString(key);
                    }
                    else if (key.StartsWith("mc_shipping") && "mc_shipping".Length < key.Length)
                    {
                        mc_shipping_charges = mc_shipping_charges + "~" + Convert.ToString(Request.Form["" + key + ""]);
                        mc_shipping_chargesKeys = mc_shipping_chargesKeys + "~" + Convert.ToString(key);
                    }
                }


string test_ipn=Convert.ToString(Request.Form["test_ipn"]);
               string ipn_track_Id= Convert.ToString(Request.Form["ipn_track_Id"]);
              string txn_type= Convert.ToString(Request.Form["txn_type"]);
            string  txn_id=Convert.ToString(Request.Form["txn_id"]);



}

Comments

  1. Thanks Amol.
    Your imformation is really helpfull for me. i confuse about notify url.
    i got my mistake. actually i use test account but in notify url i use valid paypal account. thanks.

    ReplyDelete
  2. This is awesome!! Really helpful for me and developers. Thanks for sharing with us. Following links also helped me to complete my task.

    http://www.mindstick.com/Articles/41de052a-37e1-41db-aa4c-123313be444a/?How%20to%20integrate%20PayPal%20in%20Asp%20Net

    http://www.codeproject.com/Tips/474197/PayPal-Gateway-Integration-in-ASP-NET

    ReplyDelete

Post a Comment

Popular posts from this blog

GROUP BY, CUBE, ROLLUP and SQL SERVER 2005

How to get content of Ckeditor & Fckeditor using Javascript

How to Fix Error- Sys.WebForms.PageRequestManagerTimeoutException - The server request timed out