Skip to main content
Login Register
Code2night
  • Home
  • Blog Archive
  • Learn
    • Tutorials
    • Videos
  • Interview Q&A
  • Resources
    • Cheatsheets
    • Tech Comparisons
  • Languages
    • Angular Angular js ASP.NET Asp.net Core ASP.NET Core, C# ASP.NET MVC ASP.NET Web Forms C C# C#, ASP.NET Core, Dapper
      C#, ASP.NET Core, Dapper, Entity Framework DotNet General Web Development HTML, CSS HTML/CSS Java JavaScript JavaScript, HTML, CSS JavaScript, Node.js Node.js
      Python Python 3.11, Pandas, SQL Python 3.11, SQL Python 3.11, SQLAlchemy Python 3.11, SQLAlchemy, SQL Python 3.11, SQLite React Security SQL Server TypeScript
  • Post Blog
  • Tools
    • Beautifiers
      JSON Beautifier HTML Beautifier XML Beautifier CSS Beautifier JS Beautifier SQL Formatter
      Dev Utilities
      JWT Decoder Regex Tester Diff Checker Cron Explainer String Escape Hash Generator Password Generator
      Converters
      Base64 Encode/Decode URL Encoder/Decoder JSON to CSV CSV to JSON JSON to TypeScript Markdown to HTML Number Base Converter Timestamp Converter Case Converter
      Generators
      UUID / GUID Generator Lorem Ipsum QR Code Generator Meta Tag Generator
      Image Tools
      Image Converter Image Resizer Image Compressor Image to Base64 PNG to ICO Background Remover Color Picker
      Text & Content
      Word Counter PDF Editor
      SEO & Web
      SEO Analyzer URL Checker World Clock
  1. Home
  2. Blog
  3. ASP.NET Core
  4. Integrating PayPal REST API with ASP.NET Core: A Deep Dive into Orders, Subscriptions, and IPN

Integrating PayPal REST API with ASP.NET Core: A Deep Dive into Orders, Subscriptions, and IPN

Date- Apr 22,2026 102
paypal rest api

Overview

The PayPal REST API is a powerful tool that allows developers to integrate payment processing capabilities into their applications. It offers various services such as creating orders, managing subscriptions, and handling notifications through IPN (Instant Payment Notification). This integration streamlines the payment process, enabling businesses to accept payments online efficiently and securely.

Real-world use cases for the PayPal REST API include e-commerce platforms, subscription services, and donation websites. For instance, an online store can utilize the Orders API to create and manage customer transactions, while a subscription-based service can leverage the Subscriptions API to handle recurring billing seamlessly. This flexibility makes the PayPal REST API a popular choice among developers.

Prerequisites

  • ASP.NET Core: Familiarity with building web applications using ASP.NET Core framework.
  • PayPal Developer Account: An account to access the PayPal Developer Dashboard and create API credentials.
  • NuGet Package Manager: Understanding how to manage packages in ASP.NET Core projects.
  • Basic REST API Concepts: Knowledge of HTTP methods, JSON format, and API endpoints.

Setting Up PayPal SDK in ASP.NET Core

Before integrating the PayPal REST API, it is essential to set up the PayPal SDK in your ASP.NET Core application. The PayPal SDK simplifies the process of making API calls and managing responses. To get started, you need to install the PayPal SDK NuGet package.

dotnet add package PayPal

This command will add the PayPal SDK to your project, allowing you to access its functionalities. After installation, configure the SDK in your application by adding the necessary credentials obtained from your PayPal Developer account.

public void ConfigureServices(IServiceCollection services)
{
    services.AddHttpClient();
    services.Configure<PayPalSettings>(Configuration.GetSection("PayPal"));
}

This code snippet demonstrates how to register the PayPal configuration in the Startup class. The PayPalSettings should be a class that holds your client ID and secret, which you will retrieve from the appsettings.json file.

public class PayPalSettings
{
    public string ClientId { get; set; }
    public string ClientSecret { get; set; }
}

By creating a PayPalSettings class, you ensure that your API credentials are easily accessible throughout your application without hardcoding sensitive information.

Creating Orders with PayPal REST API

Once the PayPal SDK is set up, you can start creating orders. The Orders API allows you to define payment transactions, specifying the amount and currency, and redirect users to PayPal for approval.

public async Task<string> CreateOrder()
{
    var order = new Order
    {
        Intent = "CAPTURE",
        PurchaseUnits = new List<PurchaseUnit>
        {
            new PurchaseUnit
            {
                Amount = new Amount
                {
                    CurrencyCode = "USD",
                    Value = "100.00"
                }
            }
        }
    };

    var request = new OrdersCreateRequest();
    request.RequestBody(order);

    var response = await _client.Execute(request);
    var createdOrder = response.Result();

    return createdOrder.Id;
}

This method constructs an order object with the desired payment intent and purchase unit details. The OrdersCreateRequest is instantiated, and the order information is sent to PayPal. Upon successful execution, the method returns the created order ID.

Redirecting to PayPal for Approval

After creating the order, the next step is to redirect the user to PayPal for approval. This is achieved by obtaining the approval URL from the created order.

var approvalUrl = createdOrder.Links.FirstOrDefault(x => x.Rel == "approval_url")?.Href;
return Redirect(approvalUrl);

This code extracts the approval link from the created order's response and redirects the user to PayPal, where they can complete the payment process.

Capturing Payments

Once the user approves the payment on PayPal, they are redirected back to your application. At this point, you need to capture the payment to finalize the transaction. This is done using the order ID obtained earlier.

public async Task<string> CaptureOrder(string orderId)
{
    var request = new OrdersCaptureRequest(orderId);
    request.RequestBody(new OrderActionRequest());

    var response = await _client.Execute(request);
    var result = response.Result<Order>();

    return result.Status;
}

The CaptureOrder method creates a capture request for the specified order ID and finalizes the payment. The payment status is returned, allowing you to verify whether the transaction was successful.

Managing Subscriptions with PayPal

In addition to one-time payments, PayPal provides a robust Subscriptions API that allows businesses to manage recurring payments. This is particularly useful for SaaS applications and membership sites that require a subscription model.

public async Task<string> CreateSubscription()
{
    var plan = new Plan
    {
        ProductId = "productId",
        Name = "Basic Plan",
        Description = "Basic subscription plan",
        BillingCycles = new List<BillingCycle>
        {
            new BillingCycle
            {
                Frequency = new Frequency
                {
                    IntervalUnit = "MONTH",
                    IntervalCount = "1"
                },
                TotalCycles = "12",
                PricingScheme = new PricingScheme
                {
                    FixedPrice = new Money
                    {
                        CurrencyCode = "USD",
                        Value = "10.00"
                    }
                }
            }
        }
    };

    var request = new PlansCreateRequest();
    request.RequestBody(plan);

    var response = await _client.Execute(request);
    var createdPlan = response.Result<Plan>();

    return createdPlan.Id;
}

This code creates a subscription plan that bills the user monthly for a year. The plan includes a product ID, name, description, and billing cycle details. After executing the request, the method returns the ID of the created subscription plan.

Subscribing a Customer

After creating a subscription plan, the next step is to subscribe a customer to this plan. This involves creating a subscription object and associating it with the plan.

public async Task<string> SubscribeCustomer(string planId)
{
    var subscription = new Subscription
    {
        Name = "Customer Subscription",
        StartTime = DateTime.UtcNow.AddDays(1).ToString("o"),
        PlanId = planId,
        Subscriber = new SubscriptionSubscriber
        {
            EmailAddress = "customer@example.com"
        }
    };

    var request = new SubscriptionsCreateRequest();
    request.RequestBody(subscription);

    var response = await _client.Execute(request);
    var createdSubscription = response.Result<Subscription>();

    return createdSubscription.Id;
}

This method creates a new subscription for a specified customer, associating it with the previously created plan. The subscription ID returned can be used for future references and management purposes.

Instant Payment Notifications (IPN)

IPN is a message service that sends notifications about changes in transaction status. This is crucial for maintaining accurate records of transactions and ensuring that your application responds appropriately to payment events.

[HttpPost]
public async Task<IActionResult> IPNHandler([FromBody] IPNNotification notification)
{
    // Validate the IPN message
    var isValid = await ValidateIPN(notification);
    if (!isValid)
    {
        return BadRequest();
    }

    // Process the notification
    // Update order status, send confirmation email, etc.
    return Ok();
}

This endpoint receives IPN messages and validates them before processing. Validating the IPN message ensures that it originated from PayPal, protecting your application from fraudulent notifications.

Validating IPN Messages

To ensure the authenticity of the IPN message, you should validate it by sending it back to PayPal for verification.

private async Task<bool> ValidateIPN(IPNNotification notification)
{
    var request = new IPNValidateRequest();
    request.RequestBody(notification);

    var response = await _client.Execute(request);
    return response.StatusCode == HttpStatusCode.OK;
}

This method constructs a validation request and sends it back to PayPal. If the response status is OK, the notification is considered valid.

Edge Cases & Gotchas

When working with payment integrations, several pitfalls can arise. One common issue is handling user cancellations during the payment process. It's essential to manage state effectively and ensure that the application reflects the correct status of the transaction.

if (userCancelled)
{
    // Handle cancellation
    await UpdateOrderStatus(orderId, "CANCELLED");
}

This code snippet illustrates how to handle a cancellation event. Failing to manage cancellations can lead to discrepancies in your payment records.

Performance & Best Practices

For optimal performance, consider implementing a caching strategy for frequently accessed payment data. This can reduce the number of API calls made to PayPal, improving response times and minimizing the load on your application.

services.AddMemoryCache();

By integrating memory caching, you can store results of API calls temporarily, improving performance for repeated requests. Additionally, ensure that you log all payment transactions and IPN notifications for auditing purposes.

Real-World Scenario: Building a Subscription-Based E-Commerce Application

Let's create a simple subscription-based e-commerce application that allows users to subscribe to a monthly product delivery service. We will utilize both the Orders and Subscriptions APIs.

public class SubscriptionController : Controller
{
    private readonly PayPalService _payPalService;

    public SubscriptionController(PayPalService payPalService)
    {
        _payPalService = payPalService;
    }

    [HttpPost]
    public async Task<IActionResult> Subscribe([FromBody] SubscriptionRequest request)
    {
        var planId = await _payPalService.CreateSubscriptionPlan();
        var subscriptionId = await _payPalService.SubscribeCustomer(planId, request.Email);
        return Ok(subscriptionId);
    }
}

This controller handles subscription requests by creating a subscription plan and subscribing a customer to it. The PayPalService encapsulates the logic for interacting with PayPal APIs, promoting separation of concerns.

Conclusion

  • Understanding the PayPal REST API is essential for integrating payment solutions in ASP.NET Core applications.
  • Proper handling of orders, subscriptions, and IPN notifications ensures a smooth payment experience for users.
  • Implementing best practices and performance optimizations can lead to a more robust application.
  • Explore additional features of the PayPal API such as refunds and disputes to enhance your payment integration.

S
Shubham Saini
Programming author at Code2Night — sharing tutorials on ASP.NET, C#, and more.
View all posts →

Related Articles

Braintree Payments Integration in ASP.NET Core: Cards, PayPal, and Vault Management
Apr 19, 2026
Facebook Login Integration in ASP.NET Core with OAuth 2.0: A Comprehensive Guide
Apr 29, 2026
Integrating CCAvenue Payment Gateway in ASP.NET Core Applications
Apr 24, 2026
Integrating PayU Payment Gateway in ASP.NET Core: A Comprehensive Guide
Apr 23, 2026
Previous in ASP.NET Core
Stripe Payment Gateway Integration in ASP.NET Core: Comprehensive…
Next in ASP.NET Core
Mastering Stripe Subscription Billing in ASP.NET Core: Plans, Tri…
Buy me a pizza

Comments

🔥 Trending This Month

  • 1
    Complete Guide to C++ Classes: Explained with Examples 4,212 views
  • 2
    Implementing an End-to-End CI/CD Pipeline for ASP.NET Core… 367 views
  • 3
    Create Database and CRUD operation 3,388 views
  • 4
    Mastering TypeScript Utility Types: Partial, Required, Rea… 675 views
  • 5
    Responsive Slick Slider 23,373 views
  • 6
    Integrating Azure Cognitive Search into ASP.NET Core Appli… 156 views
  • 7
    Integrating Anthropic Claude API in ASP.NET Core for AI Ch… 141 views

On this page

🎯

Interview Prep

Ace your ASP.NET Core interview with curated Q&As for all levels.

View ASP.NET Core Interview Q&As

More in ASP.NET Core

  • How to Encrypt and Decrypt Password in Asp.Net 26191 views
  • Exception Handling Asp.Net Core 20938 views
  • HTTP Error 500.31 Failed to load ASP NET Core runtime 20391 views
  • How to implement Paypal in Asp.Net Core 19753 views
  • Task Scheduler in Asp.Net core 17705 views
View all ASP.NET Core posts →

Tags

AspNet C# programming AspNet MVC c programming AspNet Core C software development tutorial MVC memory management Paypal coding coding best practices data structures programming tutorial tutorials object oriented programming Slick Slider StripeNet
Free Download for Youtube Subscribers!

First click on Subscribe Now and then subscribe the channel and come back here.
Then Click on "Verify and Download" button for download link

Subscribe Now | 1770
Download
Support Us....!

Please Subscribe to support us

Thank you for Downloading....!

Please Subscribe to support us

Continue with Downloading
Be a Member
Join Us On Whatsapp
Code2Night

A community platform for sharing programming knowledge, tutorials, and blogs. Learn, write, and grow with developers worldwide.

Panipat, Haryana, India
info@code2night.com
Quick Links
  • Home
  • Blog Archive
  • Tutorials
  • About Us
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Guest Posts
  • SEO Analyzer
Dev Tools
  • JSON Beautifier
  • HTML Beautifier
  • CSS Beautifier
  • JS Beautifier
  • SQL Formatter
  • Diff Checker
  • Regex Tester
  • Markdown to HTML
  • Word Counter
More Tools
  • Password Generator
  • QR Code Generator
  • Hash Generator
  • Base64 Encoder
  • JWT Decoder
  • UUID Generator
  • Image Converter
  • PNG to ICO
  • SEO Analyzer
By Language
  • Angular
  • Angular js
  • ASP.NET
  • Asp.net Core
  • ASP.NET Core, C#
  • ASP.NET MVC
  • ASP.NET Web Forms
  • C
  • C#
  • C#, ASP.NET Core, Dapper
  • C#, ASP.NET Core, Dapper, Entity Framework
  • DotNet
  • General Web Development
  • HTML, CSS
  • HTML/CSS
  • Java
  • JavaScript
  • JavaScript, HTML, CSS
  • JavaScript, Node.js
  • Node.js
  • Python
  • Python 3.11, Pandas, SQL
  • Python 3.11, SQL
  • Python 3.11, SQLAlchemy
  • Python 3.11, SQLAlchemy, SQL
  • Python 3.11, SQLite
  • React
  • Security
  • SQL Server
  • TypeScript
© 2026 Code2Night. All Rights Reserved.
Made with for developers  |  Privacy  ·  Terms
Translate Page
We use cookies to improve your experience and analyze site traffic. By clicking Accept, you consent to our use of cookies. Privacy Policy
Accessibility
Text size
High contrast
Grayscale
Dyslexia font
Highlight links
Pause animations
Large cursor