Introduction
This the era of online. In the present time of the world everyone works online. In today's time people purchase everthing online because it is time-saving and provides more options compared to shopping in person. Everyone wants more and more interaction with the online shopping but there is a problem related to payments on online shopping sites. So in this article we learn about the online shopping payment system known as PayPal.
PayPal
PayPal is a very famous online payment gateway or system. PayPal was founded in 1998 in America. Using the PayPal payment system we can pay for our purchases and get payment from the users that want to pay online. One more advanced feature of PayPal is that using it we can also do money transactions, in other words to send or receive money from one place to another place. PayPal is also known as a digital wallet because it works just like a wallet. The use of PayPal is very easy. Here the user enters his/her credit card number and the trnsfer is done by email. Here a password is required for the transaction. One thing is clear here, any financial details related to your transaction is never disclosed to anyone. For more information about PayPal, visit the PayPal official site PAYPAL.
So now here we discuss how to add the PayPal system into our e-commerce website. There are procedures to follow to easily integrate the PayPal payment system into your e-commerce site.
PayPal Implementation
Step 1
Open your Visual Studio, go to the file menu, select New, go to Project, choose ASP.NET MVC application then provide it an appropriate name and click on the OK button.
Step 2
Now from the Template field select internet application and click on the OK button.
Step 3
Then our solution is ready. It has some folders like Controller, Views, Models and so on. It looks as in the following picture.
Step 4
Now go to the Models folder and add a new model to it named this model as you want to. Here I used the model name PayPal. This model contains the parameters related to the payment systems. To add it simply add a new class file named PayPal and add the following code into this file.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Configuration;
-
- namespace paypalintegrationinapp.Models
- {
- public class PayPalModel
- {
- public string cmd { get; set; }
- public string business { get; set; }
- public string no_shipping { get; set; }
- public string @return { get; set; }
- public string cancel_return { get; set; }
- public string notify_url { get; set; }
- public string currency_code { get; set; }
- public string item_name { get; set; }
- public string item_quantity { get; set; }
- public string amount { get; set; }
- public string actionURL { get; set; }
-
- public PayPalModel(bool useSandbox)
- {
- this.cmd = "_xclick";
- this.business = ConfigurationManager.AppSettings["business"];
- this.cancel_return = ConfigurationManager.AppSettings["cancel_return"];
- this.@return = ConfigurationManager.AppSettings["return"];
- if (useSandbox)
- {
- this.actionURL = ConfigurationManager.AppSettings["test_url"];
- }
- else
- {
- this.actionURL = ConfigurationManager.AppSettings["Prod_url"];
- }
-
- this.notify_url = ConfigurationManager.AppSettings["notify_url"];
-
- this.currency_code = ConfigurationManager.AppSettings["currency_code"];
- }
- }
- }
Step 5
Then go to the Controller folder and add a new controller into your application and name it PayPal controller and add the following code into the controller.
- using System;
- using System.Collections.Generic;
- using System.Configuration;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
-
- using paypalintegrationinapp.Models;
-
- namespace paypalintegrationinapp.Controllers
- {
- public class PayPalController : Controller
- {
-
- public ActionResult ValidateCommand(string product, string totalPrice,string quantity)
- {
- bool useSandbox = Convert.ToBoolean(ConfigurationManager.AppSettings["IsSandbox"]);
- var paypal = new PayPalModel(useSandbox);
-
- paypal.item_name = product;
- paypal.amount = totalPrice;
- paypal.item_quantity = quantity;
- return View(paypal);
- }
-
- public ActionResult RedirectFromPaypal()
- {
- return View();
- }
-
- public ActionResult CancelFromPaypal()
- {
- return View();
- }
-
- public ActionResult NotifyFromPaypal()
- {
- return View();
- }
-
- }
- }
Step 6
Now we add a view to our project and in this view all the fields should be in the hidden format because it contains the user information. So add a folder into the view folder and then add a view named viewofvalidate and the following code to that view.
- @model paypalintegrationinapp.Models.PayPalModel
-
- <body>
- <form id="hiddenform" [email protected]>
- @Html.HiddenFor(model => model.cmd)
- @Html.HiddenFor(model => model.business)
- @Html.HiddenFor(model => model.no_shipping)
- @Html.HiddenFor(model => model.@return)
- @Html.HiddenFor(model => model.cancel_return)
- @Html.HiddenFor(model => model.notify_url)
- @Html.HiddenFor(model => model.currency_code)
- @Html.HiddenFor(model => model.item_name)
- @Html.HiddenFor(model => model.item_quantity)
- @Html.HiddenFor(model => model.amount)
- </form>
-
- <p style="text-align: center">
- <h3>
- Now you are connectin to the PayPal Application
- </h3>
- </p>
- </body>
- @Scripts.Render("~/bundles/jquery")
-
- <script type="text/javascript" language="javascript">
- $(this.document).ready(function () {
- var form = $("form");
- form.submit();
- });
- </script>
Step 7
We use the following code to validate our action in the PayPal controller.
- public ActionResult ValidateCommand(string product, string totalPrice,string quantity)
- {
- bool useSandbox = Convert.ToBoolean(ConfigurationManager.AppSettings["IsSandbox"]);
- var paypal = new PayPalModel(useSandbox);
-
- paypal.item_name = product;
- paypal.amount = totalPrice;
- paypal.item_quantity = quantity;
- return View(paypal);
- }
Step 8
Now we do our final step to simply go to the view folder, open the home folder and go into the index.html file and replace the code of this file with the following code.
- @{
- ViewBag.Title = "Home Page";
- }
- @using (Html.BeginForm("ValidateCommand", "PayPal"))
- {
- <div>
- <table >
- <tr>
- <td>
- product Name:
- </td>
- <td>
- <input type="text" name="product" value="Car" readonly />
- </td>
- </tr>
- <tr>
- <td>
- Total Price:
- </td>
- <td>
- $<input type="text" name="totalPrice" value="250000" readonly />
- </td>
- </tr>
- <tr>
- <td>
- product quantity:
- </td>
- <td>
- <input type="text" name="quantity" value="10" readonly />
- </td>
- </tr>
- <tr>
- <td>
- </td>
- <td>
- <input type="submit" name="btnConfirm" value="Click here for payment with Paypal" />
- </td>
- </tr>
- </table>
- </div>
- }
Step 9
Now we need to perform some changes in the web configuration file through which we integrate the PayPal application into the project.
- <appSettings>
- <add key="webpages:Version" value="2.0.0.0" />
- <add key="webpages:Enabled" value="false" />
- <add key="PreserveLoginUrl" value="true" />
- <add key="ClientValidationEnabled" value="true" />
- <add key="UnobtrusiveJavaScriptEnabled" value="true" />
-
- <add key="business" value="[email protected]"/>
- <add key="IsSandbox" value="true" />
- <add key="currency_code" value="USD" />
- <add key="return" value="http://localhost/PayPal/RedirectFromPaypal" />
- <add key="cancel_return" value="http://localhost/PayPal/CancelFromPaypal" />
- <add key="notify_url" value="http://localhost/PayPal/NotifyFromPaypal" />
-
- <add key="test_url" value="https://www.sandbox.paypal.com/cgi-bin/webscr" />
- <add key="Prod_url" value="https://www.sandbox.paypal.com/cgi-bin/webscr" />
- </appSettings>
Step 10
Now build your application and run it. You will then have output, something like this.
Step 11
Now in the given output when you click on the button you will have a window like this
.
Now in this form enter your email id and the PayPal Password to make the payment using the PayPal payment gateway. It is very easy, just fill in some information and then you make the payment successfully. This is the overall view to add PayPal into your application.
Summary
So this is the overall view and procedure that makes your application with PayPal integration. As I said above, PayPal is a very famous and powerful payment gateway system for online shopping and transfer of money from one place to another so usually an e-commerce site must use the PayPal gateway for the payment system. I hope using this article you can easily add the PayPal payment gateway system into your application.