Click here to Skip to main content
11,923,676 members (62,245 online)
Click here to Skip to main content
Add your own
alternative version


37 bookmarked

PayPal Gateway Integration in ASP.NET

, 11 Oct 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
In this article I will describe how to integrate paypal in your web application


If you are developing an ASP.NET web application and you require some payment gateway integration, then here is a simplified option to integrate PayPal with your application.

** A PayPal account can be used only for international transactions. You can not use it for domestic transactions. If you want the solution for domestic purpose then integrate CCAvenue or some other gateway, those provide domestic transactions.


In this article I will explain thoroughly all the requirements and techniques for integrating PayPal in your web application.

Nowadays PayPal is the most popular payment gateway worldwide because it is totally free to integrate and PayPal does not charge anything for opening an account, you will pay PayPal when you get paid. And the amount is also lower than other payment gateways. 

Things to Do

First add a webform to your web application. Name it ProceedToPayment (as per your requirements). Then add the below shown div inside the form tag (where you want to put it).

In this div I have implemented all the details that a user should enter while making the payment, such as Name, Address, Subject, Mobile No, Email Address, Amount, Currency etc.

<div style="color: #324143; margin: 30px 0 0 60px; font-family: Arial;">
    <span style="font-size: small;">Your Name:</span>
    <asp:TextBox runat="server" ValidationGroup="save" ID="txtName" Style="margin-left: 30px; width: 200px;
        background-image: url('../images/txtBoxbg.jpg') no-repeat;"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" ControlToValidate="txtPurpose"
        ErrorMessage="Please enter your Name" runat="server" 
        ValidationGroup="save" ForeColor="red"></asp:RequiredFieldValidator>
    <br />
    <br />
    <span style="font-size: small;">Your Email Id:</span><asp:TextBox runat="server" ValidationGroup="save"
        Style="margin-left: 20px;width: 200px; background-image: url('../images/txtBoxbg.jpg') no-repeat;"
    <asp:RegularExpressionValidator ID="regexEmailValid" runat="server" 
        ControlToValidate="txtEmailId" ValidationGroup="save" 
        ErrorMessage="Invalid Email Format" 
        ForeColor="red"></asp:RegularExpressionValidator><br />
    <br />
    <span style="font-size: small;">Your Phone No:</span>
    <asp:TextBox runat="server" ID="txtPhone" ValidationGroup="save" Style="margin-left: 6px;
        width: 200px; background-image: transparent url('../images/txtBoxbg.jpg') no-repeat;"></asp:TextBox>
    <asp:RegularExpressionValidator ID="RegularExpressionValidator4" runat="server" ControlToValidate="txtPhone"
        ForeColor="red" ErrorMessage="Invalid Phone No"
        ValidationGroup="save" ValidationExpression="^([0-9\(\)\/\+ \-]*)$"></asp:RegularExpressionValidator>
    <br />
    <br />
    <span style="font-size: small;">Enter Amount:</span><asp:TextBox runat="server" ID="txtAmount" ValidationGroup="save"
        Style="margin-left: 16px; width: 200px; background-image: url('../images/txtBoxbg.jpg') no-repeat;"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txtAmount"
        runat="server" ForeColor="red" ErrorMessage="Please enter the amount."></asp:RequiredFieldValidator>
    <br />
    <br />
    <span style="font-size: small;">Currency:</span>
    <asp:DropDownList runat="server" ID="ddlCurrency" Style="margin-left: 42px; 
        width: 204px; background-image: transparent url('../images/txtBoxbg.jpg') no-repeat;">
        <asp:ListItem>- Select -</asp:ListItem>
    <br />
    <br />
    <span style="font-size: small;">Your Purpose:</span><asp:TextBox TextMode="MultiLine" 
        Rows="10" runat="server" ID="txtPurpose"
        Style="margin-left: 17px; margin-left: 19px; width: 200px; 
               background-image: url('../images/txtBoxbg.jpg') no-repeat;"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator6" ControlToValidate="txtPurpose"
        ErrorMessage="Can not be left blank" ValidationGroup="save" 
        runat="server" ForeColor="red"></asp:RequiredFieldValidator>
    <br />
    <asp:Button ID="btnPay" runat="server" Text="Pay Now" CssClass="button" Style="font-size: 12px;
        cursor: pointer; height: 27px; margin-left: 207px; margin-top: 10px; width: 93px;"
        OnClick="btnPay_AsPerYourChoice" ValidationGroup="save"></asp:Button>

Now go to the code behind page of the webform and add the below method in it.

An important thing to notice is I am using the live URL of PayPal as this functionality is implemented in my live server. You can use the Sandbox URL of PayPal for testing purposes.

protected void PayWithPayPal(string amount, string itemInfo, string name, 
          string phone, string email, string currency)
    string redirecturl = "";

    //Mention URL to redirect content to paypal site
    redirecturl += "" + 

    //First name i assign static based on login details assign this value
    redirecturl += "&first_name=" + name;

    //City i assign static based on login user detail you change this value
    redirecturl += "&city=bhubaneswar";

    //State i assign static based on login user detail you change this value
    redirecturl += "&state=Odisha";

    //Product Name
    redirecturl += "&item_name=" + itemInfo;

    //Product Name
    redirecturl += "&amount=" + amount;

    //Phone No
    redirecturl += "&night_phone_a=" + phone;

    //Product Name
    redirecturl += "&item_name=" + itemInfo;

    redirecturl += "&address1=" + email;

    //Business contact id
    // redirecturl += "&";

    //Shipping charges if any
    redirecturl += "&shipping=0";

    //Handling charges if any
    redirecturl += "&handling=0";

    //Tax amount if any
    redirecturl += "&tax=0";

    //Add quatity i added one only statically 
    redirecturl += "&quantity=1";

    //Currency code 
    redirecturl += "&currency=" + currency;

    //Success return page url
    redirecturl += "&return=" + 

    //Failed return page url
    redirecturl += "&cancel_return=" + 


OK, that is fine. Now before running your application you need to add something in your web.config file.

Just add the below shown appsetting code snippet to your web config file under the system.web tag.

Here you have to give information regarding your PayPal account, your PayPal email ID , PayPal submit URL, your website success URL, failure URL, etc.

    <add key="token" value="PW1BDVNqVPVanwduF_Tb2Ey91aT1Uhx1kL7HPc-7e8S-6AnUwSSHyasolSe"/>
    <add key="paypalemail" value=""/>

    <!--Here i used sandbox site url only if you hosted in live change sandbox to live paypal URL-->
    <add key="PayPalSubmitUrl" value=""/>

    <add key="FailedURL" value=""/>

    <add key="SuccessURL" value=""/>


Now you can run your application to see if its working or not.


I have this application in my live server and have also tested it properly. It works fine at my end. If you have any problems in understanding it or anything else, then please revert back. If you want to see it live, you may visit Payment Gateway



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

You may also be interested in...

Comments and Discussions

QuestionNo source code Pin
Philip Sam15-Oct-15 4:59
memberPhilip Sam15-Oct-15 4:59 
QuestionPayPal Pin
Ml Res17-Apr-15 1:50
memberMl Res17-Apr-15 1:50 
QuestionWhat is "token" in this paypal configuration setting in web.config file? Pin
Jignesh Khokhariya10-Apr-15 2:46
memberJignesh Khokhariya10-Apr-15 2:46 
QuestionPaypal integration Pin
Member 111823815-Nov-14 1:47
memberMember 111823815-Nov-14 1:47 
QuestionPaypal integration Pin
Member 1118238128-Oct-14 20:06
memberMember 1118238128-Oct-14 20:06 
Questioncustomize our own company header Pin
Member 1005310126-Sep-14 1:15
memberMember 1005310126-Sep-14 1:15 
QuestionIntegrating PayPal in Pin
Member 110711679-Sep-14 21:21
memberMember 110711679-Sep-14 21:21 
QuestionHow to integrate ASP .net C# mysql e-commerce website with online Payment gateway other than Paypal using SOAP XML request and response Pin
Member 1097112225-Jul-14 4:24
memberMember 1097112225-Jul-14 4:24 
QuestionError in tutorial Pin
heemanshubhalla1-Feb-14 10:11
memberheemanshubhalla1-Feb-14 10:11 
BugIf you want to see it live, you may visit Payment Gateway Pin
Member 954018924-Oct-13 9:06
memberMember 954018924-Oct-13 9:06 
QuestionMultiple Products Pin
Suraj aka Consigliere10-Oct-13 22:20
memberSuraj aka Consigliere10-Oct-13 22:20 
Bugcurrency variable error Pin
Muhammad Umair Irshad3-Sep-13 2:02
memberMuhammad Umair Irshad3-Sep-13 2:02 
GeneralMy vote of 2 Pin
i_islamian25-Aug-13 1:52
memberi_islamian25-Aug-13 1:52 
GeneralMy vote of 5 Pin
Sravan S16-Jul-13 23:42
memberSravan S16-Jul-13 23:42 
GeneralMy vote of 5 Pin
Shailesh Kalasariya21-Apr-13 21:01
memberShailesh Kalasariya21-Apr-13 21:01 
Questionimplemented your code; good in my program but PayPal says "???" Pin
janwayne9-Apr-13 3:38
memberjanwayne9-Apr-13 3:38 
Questionfor testing Pin
degrader40417-Mar-13 7:24
memberdegrader40417-Mar-13 7:24 
Suggestionİf you need to avoid stringbuilder Pin
azizhocaoglu_7021-Nov-12 6:31
memberazizhocaoglu_7021-Nov-12 6:31 
GeneralNice One Pin
Gun Gun Febrianza23-Oct-12 22:30
memberGun Gun Febrianza23-Oct-12 22:30 
GeneralRe: Nice One Pin
Tapan Kumar ( Unleashed )11-Nov-12 20:36
memberTapan Kumar ( Unleashed )11-Nov-12 20:36 
GeneralMy vote of 5 Pin
Gun Gun Febrianza23-Oct-12 22:29
memberGun Gun Febrianza23-Oct-12 22:29 
GeneralRe: My vote of 5 Pin
Tapan Kumar ( Unleashed )11-Nov-12 20:37
memberTapan Kumar ( Unleashed )11-Nov-12 20:37 
Generalnot right way Pin
Sachin Pisal17-Oct-12 11:30
memberSachin Pisal17-Oct-12 11:30 
GeneralRe: not right way Pin
Mr. Tapan Kumar18-Oct-12 1:59
memberMr. Tapan Kumar18-Oct-12 1:59 
GeneralRe: not right way Pin
sacp.net19-Oct-12 10:39
membersacp.net19-Oct-12 10:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151125.3 | Last Updated 11 Oct 2012
Article Copyright 2012 by Mr. Tapan Kumar
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid