How to Add PayPal Application Into a Project

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.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6. using System.Configuration;  
  7.   
  8. namespace paypalintegrationinapp.Models  
  9. {  
  10.     public class PayPalModel  
  11.     {  
  12.         public string cmd { getset; }  
  13.         public string business { getset; }  
  14.         public string no_shipping { getset; }  
  15.         public string @return { getset; }  
  16.         public string cancel_return { getset; }  
  17.         public string notify_url { getset; }  
  18.         public string currency_code { getset; }  
  19.         public string item_name { getset; }  
  20.         public string item_quantity { getset; }  
  21.         public string amount { getset; }  
  22.         public string actionURL { getset; }  
  23.   
  24.         public PayPalModel(bool useSandbox)  
  25.         {  
  26.             this.cmd = "_xclick";  
  27.             this.business = ConfigurationManager.AppSettings["business"];  
  28.             this.cancel_return = ConfigurationManager.AppSettings["cancel_return"];  
  29.             this.@return = ConfigurationManager.AppSettings["return"];  
  30.             if (useSandbox)  
  31.             {  
  32.                 this.actionURL = ConfigurationManager.AppSettings["test_url"];  
  33.             }  
  34.             else  
  35.             {  
  36.                 this.actionURL = ConfigurationManager.AppSettings["Prod_url"];  
  37.             }  
  38.             // We can add parameters here, for example OrderId, CustomerId, etc....  
  39.             this.notify_url = ConfigurationManager.AppSettings["notify_url"];  
  40.             // We can add parameters here, for example OrderId, CustomerId, etc....  
  41.             this.currency_code = ConfigurationManager.AppSettings["currency_code"];  
  42.         }  
  43.     }  
  44. }  
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.
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Configuration;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.Mvc;  
  7.   
  8. using paypalintegrationinapp.Models;  
  9.   
  10. namespace paypalintegrationinapp.Controllers  
  11. {  
  12.     public class PayPalController : Controller  
  13.     {  
  14.         
  15.         public ActionResult ValidateCommand(string product, string totalPrice,string quantity)  
  16.         {  
  17.             bool useSandbox = Convert.ToBoolean(ConfigurationManager.AppSettings["IsSandbox"]);  
  18.             var paypal = new PayPalModel(useSandbox);  
  19.   
  20.             paypal.item_name = product;  
  21.             paypal.amount = totalPrice;  
  22.             paypal.item_quantity = quantity;  
  23.             return View(paypal);  
  24.         }  
  25.   
  26.         public ActionResult RedirectFromPaypal()  
  27.         {  
  28.             return View();  
  29.         }  
  30.   
  31.         public ActionResult CancelFromPaypal()  
  32.         {  
  33.             return View();  
  34.         }  
  35.   
  36.         public ActionResult NotifyFromPaypal()  
  37.         {  
  38.             return View();  
  39.         }  
  40.   
  41.     }  
  42. }  
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.
  1. @model paypalintegrationinapp.Models.PayPalModel  
  2.   
  3. <body>  
  4.     <form id="hiddenform" [email protected]>  
  5.     @Html.HiddenFor(model => model.cmd)  
  6.     @Html.HiddenFor(model => model.business)  
  7.     @Html.HiddenFor(model => model.no_shipping)  
  8.     @Html.HiddenFor(model => model.@return)  
  9.     @Html.HiddenFor(model => model.cancel_return)  
  10.     @Html.HiddenFor(model => model.notify_url)  
  11.     @Html.HiddenFor(model => model.currency_code)  
  12.     @Html.HiddenFor(model => model.item_name)  
  13.     @Html.HiddenFor(model => model.item_quantity)  
  14.     @Html.HiddenFor(model => model.amount)  
  15.     </form>  
  16.   
  17.     <p style="text-align: center">  
  18.         <h3>  
  19.             Now you are connectin to the PayPal Application  
  20.         </h3>  
  21.     </p>  
  22. </body>  
  23.  @Scripts.Render("~/bundles/jquery")  
  24.   
  25. <script type="text/javascript" language="javascript">  
  26.     $(this.document).ready(function () {  
  27.         var form = $("form");  
  28.         form.submit();  
  29.     });     
  30. </script>  
Step 7

We use the following code to validate our action in the PayPal controller.
  1. public ActionResult ValidateCommand(string product, string totalPrice,string quantity)  
  2.        {  
  3.            bool useSandbox = Convert.ToBoolean(ConfigurationManager.AppSettings["IsSandbox"]);  
  4.            var paypal = new PayPalModel(useSandbox);  
  5.   
  6.            paypal.item_name = product;  
  7.            paypal.amount = totalPrice;  
  8.            paypal.item_quantity = quantity;  
  9.            return View(paypal);  
  10.        }  
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.
  1. @{  
  2.     ViewBag.Title = "Home Page";  
  3. }  
  4. @using (Html.BeginForm("ValidateCommand", "PayPal"))  
  5. {  
  6.     <div>  
  7.         <table >  
  8.             <tr>  
  9.                 <td>  
  10.                     product Name:  
  11.                 </td>  
  12.                 <td>  
  13.                     <input type="text" name="product" value="Car" readonly />  
  14.                 </td>  
  15.             </tr>  
  16.             <tr>  
  17.                 <td>  
  18.                    Total Price:  
  19.                 </td>  
  20.                 <td>  
  21.                     $<input type="text" name="totalPrice" value="250000" readonly />  
  22.                 </td>  
  23.             </tr>  
  24.              <tr>  
  25.                 <td>  
  26.                     product quantity:  
  27.                 </td>  
  28.                 <td>  
  29.                     <input type="text" name="quantity" value="10" readonly />  
  30.                 </td>  
  31.             </tr>  
  32.             <tr>  
  33.                 <td>  
  34.                 </td>  
  35.                 <td>  
  36.                     <input type="submit" name="btnConfirm" value="Click here for payment with Paypal" />  
  37.                 </td>  
  38.             </tr>  
  39.         </table>  
  40.     </div>  
  41. }  
Step 9

Now we need to perform some changes in the web configuration file through which we integrate the PayPal application into the project.
  1. <appSettings>  
  2.     <add key="webpages:Version" value="2.0.0.0" />  
  3.     <add key="webpages:Enabled" value="false" />  
  4.     <add key="PreserveLoginUrl" value="true" />  
  5.     <add key="ClientValidationEnabled" value="true" />  
  6.     <add key="UnobtrusiveJavaScriptEnabled" value="true" />  
  7.     
  8.    <add key="business" value="[email protected]"/> <!--here add that account whic is availavle at the paypal account-->  
  9.     <add key="IsSandbox" value="true" />  
  10.     <add key="currency_code" value="USD" />  
  11.     <add key="return" value="http://localhost/PayPal/RedirectFromPaypal" />  
  12.     <add key="cancel_return" value="http://localhost/PayPal/CancelFromPaypal" />  
  13.     <add key="notify_url" value="http://localhost/PayPal/NotifyFromPaypal" />  
  14.       
  15.    <add key="test_url" value="https://www.sandbox.paypal.com/cgi-bin/webscr" />  
  16.    <add key="Prod_url" value="https://www.sandbox.paypal.com/cgi-bin/webscr" />  
  17. </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.

Up Next
    Ebook Download
    View all
    Learn
    View all