, ,

How to integrate to PayPal payment gateway from the asp.net MVC web application?

Posted by

To integrate PayPal as a payment gateway into your ASP.NET MVC web application, you can follow these steps:

  1. Sign up for a PayPal Business Account: Visit the PayPal website (https://www.paypal.com) and sign up for a PayPal Business Account. This will provide you with the necessary credentials to interact with the PayPal API.
  2. Install PayPal SDK: In your ASP.NET MVC project, use NuGet Package Manager to install the PayPal SDK. You can search for the package named “PayPal” and install it.
  3. Configure PayPal settings: In your web.config file, add the necessary PayPal settings. You’ll need to provide your PayPal API credentials, including the Client ID and Secret. Example configuration settings:
   <appSettings>
       <add key="PayPal.ClientId" value="YourClientId" />
       <add key="PayPal.ClientSecret" value="YourClientSecret" />
   </appSettings>
  1. Create a payment form: In your MVC view, create a form to collect payment details such as amount, customer information, and any additional required fields.
  2. Process the payment request: In your MVC controller, handle the form submission and initiate the payment request to PayPal. Here’s an example of how you can initiate a PayPal payment request:
   using PayPal.Api;

   public class PaymentController : Controller
   {
       private PayPal.Api.Payment payment;

       public ActionResult MakePayment()
       {
           // Get payment details from the form submission
           decimal amount = // Get the payment amount
           string currency = // Specify the currency code (e.g., USD)

           // Set up the PayPal API context
           string clientId = ConfigurationManager.AppSettings["PayPal.ClientId"];
           string clientSecret = ConfigurationManager.AppSettings["PayPal.ClientSecret"];
           APIContext apiContext = new APIContext(new OAuthTokenCredential(clientId, clientSecret).GetAccessToken());

           // Create a new payment object
           payment = new PayPal.Api.Payment()
           {
               intent = "sale",
               payer = new Payer() { payment_method = "paypal" },
               transactions = new List<Transaction>()
               {
                   new Transaction()
                   {
                       amount = new Amount()
                       {
                           total = amount.ToString(),
                           currency = currency
                       }
                   }
               },
               redirect_urls = new RedirectUrls()
               {
                   return_url = Url.Action("PaymentSuccess", "Payment", null, Request.Url.Scheme),
                   cancel_url = Url.Action("PaymentCancel", "Payment", null, Request.Url.Scheme)
               }
           };

           // Create the payment
           Payment createdPayment = payment.Create(apiContext);

           // Redirect the user to the PayPal payment approval URL
           return Redirect(createdPayment.GetApprovalUrl());
       }

       public ActionResult PaymentSuccess(string paymentId, string token, string PayerID)
       {
           // Execute the payment
           PaymentExecution paymentExecution = new PaymentExecution() { payer_id = PayerID };
           Payment executedPayment = payment.Execute(new APIContext(new OAuthTokenCredential(ConfigurationManager.AppSettings["PayPal.ClientId"], ConfigurationManager.AppSettings["PayPal.ClientSecret"]).GetAccessToken()), paymentExecution);

           // Process the payment success
           // Update payment status in your system
           // Redirect to a success page or display a success message

           return View("PaymentSuccess");
       }

       public ActionResult PaymentCancel()
       {
           // Handle payment cancellation
           // Redirect to a cancellation page or display a cancellation message

           return View("PaymentCancel");
       }
   }

In this code, the MakePayment action method receives the payment details from the form submission. It sets up the PayPal API context using the provided client ID and secret. Then, it creates a new payment object with the specified amount

, currency, and redirect URLs for success and cancellation.

The Create method is called to create the payment object and obtain the approval URL from PayPal. The user is redirected to this URL to approve the payment.

When the payment is successful, PayPal will redirect the user back to the PaymentSuccess action method. The Execute method is called to execute the payment using the provided payer ID. You can then process the payment success, update the payment status in your system, and redirect to a success page or display a success message.

If the user cancels the payment, PayPal will redirect the user to the PaymentCancel action method. You can handle the cancellation and redirect to a cancellation page or display a cancellation message.

  1. Set up PayPal IPN (Instant Payment Notification): To handle payment notifications and update the payment status in your application, you can set up PayPal IPN. This involves configuring a URL in your PayPal account where PayPal will send notifications about payment status changes. You can then implement an endpoint in your ASP.NET MVC application to handle these IPN notifications and update your system accordingly. Refer to PayPal’s documentation for detailed instructions on setting up IPN: https://developer.paypal.com/docs/api-basics/notifications/ipn/

These are the basic steps to integrate PayPal as a payment gateway into your ASP.NET MVC web application. You may need to refer to the PayPal documentation or contact their support for more advanced integration options, customization, and handling additional features like recurring payments, refunds, or capturing additional payment information.

Please note that the above code is provided as an example and may need to be modified to fit your specific requirements and application architecture.