PayPal Checkout in ASP.NET MVC: Follow the Payment State

Editorial illustration.

Originally published 23 June 2023; rewritten and reviewed on 15 October 2026.

The browser returns to /Checkout/Success, displays a green checkmark, and the developer moves on to the next ticket. That is usually how silent payment bugs enter production. In PayPal integrations, a browser return is only a signal that the buyer finished clicking through PayPal’s UI—it is not a settled receipt, and an approved order still has to be captured and verified on the server before you ship anything.

In Malaysia, PayPal is rarely the primary gateway for local RM retail, where FPX and local e-wallets dominate. Where it still shows up consistently is for Malaysian SaaS products, agencies, or digital stores billing overseas clients in USD or SGD. That cross-border context makes state tracking even more important, because a mismatched currency or an uncaptured authorisation hurts far more when FX fees and support timezones are involved.

Move past legacy SDKs and let the server own the order

The original 2023 version of this post referenced an older PayPal.Api NuGet pattern. For any active ASP.NET MVC or .NET project today, build against the PayPal Orders v2 REST API rather than copying legacy NVP/SOAP or deprecated SDK constructors. Using HttpClient directly against the REST endpoints also keeps your dependencies lean and makes it obvious what JSON payload is crossing the wire.

Calculate the order total, tax, and currency strictly from your database on the server. Store your ClientId and ClientSecret in protected environment configuration—never commit them to Git or expose the secret in client-side JavaScript. Keep Sandbox (https://api-m.sandbox.paypal.com) and Live (https://api-m.paypal.com) base URLs and credentials strictly separated.

A clean checkout flow follows a predictable state machine:

Local order created (Status = Pending, Amount = 49.00, Currency = USD)
  → Server requests OAuth2 token & creates PayPal Order (/v2/checkout/orders)
  → Buyer approves payment in PayPal UI
  → Server captures payment (/v2/checkout/orders/{id}/capture)
  → Server verifies status == "COMPLETED", amount, and currency match
  → Fulfilment runs once inside an idempotent transaction

Capture and verify on the server

Whether you use PayPal’s JavaScript SDK buttons (createOrder and onApprove calling your MVC endpoints) or a server redirect, the capture step should happen from your C# backend. Do not mark an order paid just because onApprove fired in the browser.

When the capture response returns from PayPal, inspect the actual capture object—not just the HTTP 200/201 status code:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> CaptureOrder(string payPalOrderId)
{
    var localOrder = _orderRepo.GetByPayPalOrderId(payPalOrderId);
    if (localOrder == null)
    {
        return HttpNotFound();
    }

    // Already settled by an earlier request or webhook
    if (localOrder.Status == OrderStatus.Paid)
    {
        return Json(new { status = "COMPLETED", orderRef = localOrder.Reference });
    }

    var captureResult = await _payPalClient.CaptureOrderAsync(payPalOrderId);
    var capture = captureResult?.PurchaseUnits?
        .FirstOrDefault()?.Payments?.Captures?.FirstOrDefault();

    if (captureResult?.Status != "COMPLETED" || capture == null || capture.Status != "COMPLETED")
    {
        _orderRepo.RecordAttemptState(localOrder.Id, captureResult?.Status ?? "UNKNOWN");
        return new HttpStatusCodeResult(400, "Payment not completed");
    }

    if (!decimal.TryParse(capture.Amount.Value, NumberStyles.Number, CultureInfo.InvariantCulture, out var capturedAmount)
        || capturedAmount != localOrder.TotalAmount
        || !string.Equals(capture.Amount.CurrencyCode, localOrder.Currency, StringComparison.OrdinalIgnoreCase))
    {
        _logger.Error($"PayPal mismatch on {localOrder.Reference}: expected {localOrder.TotalAmount} {localOrder.Currency}, got {capture.Amount.Value} {capture.Amount.CurrencyCode}");
        return new HttpStatusCodeResult(400, "Amount or currency mismatch");
    }

    _orderRepo.MarkAsPaid(localOrder.Id, capture.Id, DateTime.UtcNow);
    return Json(new { status = "COMPLETED", orderRef = localOrder.Reference });
}

Checking both captureResult.Status and capture.Status matters. An order can occasionally report COMPLETED at the top level while an underlying capture is PENDING (for example, an eCheck or a manual compliance review). Treating PENDING as Paid is how digital goods get downloaded before funds clear.

Handle webhooks and browser returns as convergent paths

A buyer’s browser can crash or lose mobile data the exact millisecond after they click “Pay Now”, leaving your CaptureOrder endpoint uncalled, or a PAYMENT.CAPTURE.COMPLETED webhook can arrive before the user’s browser finishes redirecting. Both paths need to converge safely on the same database record.

Verify incoming PayPal webhooks using PayPal’s signature verification endpoint (/v1/notifications/verify-webhook-signature) with your configured WebhookId, rather than blindly trusting any JSON posted to your webhook URL. Once verified, look up the local order by the PayPal order ID or custom reference, check whether it is already Paid, and only trigger fulfilment (license keys, confirmation emails, invoice generation) if this transition actually moved the order from Pending to Paid.

Before shipping to production, test three failure cases in Sandbox: a duplicate webhook delivery, a network timeout right after CaptureOrderAsync succeeds, and a tampered amount or currency mismatch. When every state change is backed by stored PayPal order and capture IDs, your thank-you page can simply read the truth from your database instead of guessing from a query string.