PayPal Gateway Integration in ASP.NET
Introduction
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.
Description
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.
THE DESIGN PAGE :
This is the test page ...you can create as per your requirement.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PaymentIntegration.aspx.cs" Inherits="PaymentIntegration" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>PAYPAL INTEGRATION IN ASP.NET Page</title>
</head>
<body>
<form id="form1" runat="server">
<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;"
ID="txtEmailId"></asp:TextBox>
<asp:RegularExpressionValidator ID="regexEmailValid" runat="server"
ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
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>
<asp:ListItem>INR</asp:ListItem>
<asp:ListItem>USD</asp:ListItem>
<asp:ListItem>EURO</asp:ListItem>
<asp:ListItem>Pound</asp:ListItem>
</asp:DropDownList>
<br />
<br />
<span style="font-size: small;">Your Purpose:</span><asp:TextBox TextMode="MultiLine"
Rows="10" runat="server" ID="txtPurpose"
Height="50px"
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>
</div>
</form>
</body>
</html>
THE CODE BEHIND:
HERE BEFORE ADDING CODE YOU NEED TO HAVE AN AUTHORIZED ACCOUNT OR TRY WITH TESTING VERSION AS I HAVE DID::::
//<!--Here i used sandbox site url only if you hosted in live change sandbox to live paypal URL-->
//paypal account sandbox URL: https://www.sandbox.paypal.com/cgi-bin/webscr
// <add key="PayPalSubmitUrl" value="https://www.paypal.com/cgi-bin/webscr"/>
// <add key="FailedURL" value="http://www.mrsoft.co.in/ProceedToPayment.aspx"/>
// <add key="SuccessURL" value="http://www.mrsoft.co.in/ProceedToPayment.aspx"/>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class PaymentIntegration : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
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 += "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_xclick&business=" +
ConfigurationManager.AppSettings["paypalemail"].ToString();
//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=BANGALORE";
//State i assign static based on login user detail you change this value
redirecturl += "&state=YELHANKA NEW TOWM";
//Product Name
redirecturl += "&item_name=" + itemInfo;
//Product Name
redirecturl += "&amount=" + amount;
//Phone No
redirecturl += "&night_phone_a=" + phone;
//Product Name
redirecturl += "&item_name=" + itemInfo;
//Address
redirecturl += "&address1=" + email;
//Business contact id
// redirecturl += "&business=piyushrana1991@gmail.com";
//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 += "¤cy=" + currency;
//Success return page url
redirecturl += "&return=" +
ConfigurationManager.AppSettings["SuccessURL"].ToString();
//Failed return page url
redirecturl += "&cancel_return=" +
ConfigurationManager.AppSettings["FailedURL"].ToString();
Response.Redirect(redirecturl);
}
protected void btnPay_AsPerYourChoice(object sender, EventArgs e)
{
PayWithPayPal(txtAmount.Text.ToString(),txtPurpose.Text.ToString(), txtName.Text.ToString(), txtPhone.Text.ToString(), txtEmailId.Text.ToString(), ddlCurrency.SelectedItem.Text.ToString());
}
}
//The COMPLETE URL GENERATED AFTER EXECTION
// https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_xclick&business=piyushrana1991@live.com&first_name=piyush&city=BANGALORE&state=YELHANKA NEW TOWM&item_name=TESTING PURPOSE&amount=500&night_phone_a=9538774446&item_name=TESTING PURPOSE&address1=piyushrana1991@gmail.com&shipping=0&handling=0&tax=0&quantity=1¤cy=INR&return=http://www.mrsoft.co.in/ProceedToPayment.aspx&cancel_return=http://www.mrsoft.co.in/ProceedToPayment.aspx
// THE URL BEEN SHOWN BY PAYPAL(HTTPS)
//https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_flow&SESSION=vvkc3WmzlBWoW6IQujCZ4L-egHt00STTbbNLkH4ZK-kf33C46xxdUCf4ZWC&dispatch=50a222a57771920b6a3d7b606239e4d529b525e0b7e69bf0224adecfb0124e9b61f737ba21b081986471f9b93cfa01e00b63629be0164db1