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)
            dec = Convert.ToDecimal(TextBox1.Text);

        catch(Exception ex)
            Label1.Visible = true;

 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";




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 = "";
    public static string PayPalUrl = "";

    public string PayPalBaseUrl = "";

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 = "";
        public static string PayPalUrl = "";


Difference b/w String and String Builder

Usually, String is immutable which means once you have the content there you cannot change it. While StringBuilder is mutable because it allows you to change the content you have already placed.
Notice that a string allows you to change the content but offcourse it creates a new string reference. For example, all string methods return a new string and don't update the same string variable.
When you have huge amount of concatenation, best recommendation to go for StringBuilder!
String and StringBuilder class stores strings. But when you cannot change a String object after creating one.
eg: String name = "Prasad";
By saying you cannot change the name object means you cannot change the value in name object internally. When you change the name object value to something else, a new String object is creating in memory and assign the new value.

eg: name = "Prasad Reddy";

A new name object is creating in memory and the value "Prasad Reddy" is assinging to the newly created space.

But StringBuilder class occupies the same space even if you change the value.

If you are doing string concatenation StringBuilder class is far better in performance than String class.

You can use StringBuilder's Append() method to use concatenation.

for example

String strSen = "hello";
strSen+= "Hello";

StringBuilder sbb = new StringBuilder();
sbb = sbb.Append("goldy");
sbb = sbb.Append("gupta");

Both are doing same thing but the process is faster in stringbuilder..