Monday, December 20, 2010

payment through paypal in Asp.Net

A simple Way to payment through Paypal..
For this it is compulsory to login in your pay pal account.



Now come to the coding part ..  Take one text-box and a button

 protected void Button1_Click(object sender, EventArgs e)
    {
       protected decimal dec;(take this globally)
        try
        {
            dec = Convert.ToDecimal(TextBox1.Text);
                       
        }

        catch(Exception ex)
        {
            Label1.Visible = true;
        }
        paypalquery();
    }


 protected void paypalquery()
    {

//Make a class named it as "paypal"
        paypal pay = new paypal();
        pay.AccountEmail = paypal.Email;
        pay.PayPalBaseUrl = paypal.PayPalUrl;
        pay.Amount = dec;
        pay.ItemName = "New Item Added";

        pay.SuccessUrl = Request.Url + "?paypal=success";
        pay.CancelUrl = Request.Url + "?paypal=cancel";

        Response.Redirect(pay.submiturl());

        return;


    }



this is cs class

public class paypal
{


    public string LogoUrl = "";
    public string AccountEmail = "";
    public string BuyerEmail = "";
    public string SuccessUrl = "";
    public string CancelUrl = "";
    public string ItemName = "";
    public decimal Amount = 0.00M;
    public string InvoiceNo = "";


    public static string Email = "PayPalEmailAccount@YourCompany.com";
    public static string PayPalUrl = "https://www.sandbox.paypal.com/us/cgi-bin/webscr?";

    public string PayPalBaseUrl = "https://www.paypal.com/cgi-bin/webscr?";


public paypal()
{
//
// TODO: Add constructor logic here
//
}

    public string submiturl()
    {
        StringBuilder buil = new StringBuilder();

        buil.Append(PayPalBaseUrl + "cmd=_xclick&business=" + HttpUtility.UrlEncode(AccountEmail));

       if (Amount != null)
           buil.AppendFormat("&amount={0:f2}", Amount);

       if (ItemName != null && ItemName != "")
           buil.AppendFormat("&item_name={0}", HttpUtility.UrlEncode(ItemName));

       if (SuccessUrl != null && SuccessUrl != "")
           buil.AppendFormat("&return={0}", HttpUtility.UrlEncode(SuccessUrl));

       if (CancelUrl != null && CancelUrl != "")
           buil.AppendFormat("&cancel_return={0}", HttpUtility.UrlEncode(CancelUrl));

        return buil.ToString();
    }


    public class configuration
    {
        public static string email = "PayPalEmailAccount@YourCompany.com";
        public static string PayPalUrl = "https://www.sandbox.paypal.com/us/cgi-bin/webscr?";
    }


}

No comments:

Post a Comment