skip to Main Content

I’m using REST API SDK for Dotnet V2 github link to integrate with PayPal orders create and capture it’s working fine.

I’m trying now to implement the webhook, already spent lots of hours trying to find out how to create a controller to receive PayPal webhooks to update my orders status but not able to find solution.

Is there a .net documentation or sample code on how to create a webhook in .NET?

This is my vb.net code to create and capture orders

Private Shared Function BuildRequestBody() As OrderRequest
    Dim orderRequest As OrderRequest = New OrderRequest() With {
    .CheckoutPaymentIntent = "CAPTURE",
    .ApplicationContext = New ApplicationContext With {
        .BrandName = "EXAMPLE INC",
        .LandingPage = "BILLING",
        .CancelUrl = "https://XXX/Home/CancelUrl",
        .ReturnUrl = "https://XXX/Home/CaptureOrder",
        .UserAction = "CONTINUE",
        .ShippingPreference = "SET_PROVIDED_ADDRESS"
    },
    .PurchaseUnits = New List(Of PurchaseUnitRequest) From {
        New PurchaseUnitRequest With {
            .ReferenceId = "PUHF",
            .Description = "Sporting Goods",
            .CustomId = "CUST-HighFashions",
            .SoftDescriptor = "HighFashions",
            .AmountWithBreakdown = New AmountWithBreakdown With {
                .CurrencyCode = "USD",
                .Value = "220.00",
                .AmountBreakdown = New AmountBreakdown With {
                    .ItemTotal = New Money With {
                        .CurrencyCode = "USD",
                        .Value = "180.00"
                    },
                    .Shipping = New Money With {
                        .CurrencyCode = "USD",
                        .Value = "20.00"
                    },
                    .Handling = New Money With {
                        .CurrencyCode = "USD",
                        .Value = "10.00"
                    },
                    .TaxTotal = New Money With {
                        .CurrencyCode = "USD",
                        .Value = "20.00"
                    },
                    .ShippingDiscount = New Money With {
                        .CurrencyCode = "USD",
                        .Value = "10.00"
                    }
                }
            },
            .Items = New List(Of Item) From {
                New Item With {
                    .Name = "T-shirt",
                    .Description = "Green XL",
                    .Sku = "sku01",
                    .UnitAmount = New Money With {
                        .CurrencyCode = "USD",
                        .Value = "90.00"
                    },
                    .Tax = New Money With {
                        .CurrencyCode = "USD",
                        .Value = "10.00"
                    },
                    .Quantity = "1",
                    .Category = "PHYSICAL_GOODS"
                },
                New Item With {
                    .Name = "Shoes",
                    .Description = "Running, Size 10.5",
                    .Sku = "sku02",
                    .UnitAmount = New Money With {
                        .CurrencyCode = "USD",
                        .Value = "45.00"
                    },
                    .Tax = New Money With {
                        .CurrencyCode = "USD",
                        .Value = "5.00"
                    },
                    .Quantity = "2",
                    .Category = "PHYSICAL_GOODS"
                }
            },
            .ShippingDetail = New ShippingDetail With {
                .Name = New Name With {
                    .FullName = "John Doe"
                },
                .AddressPortable = New AddressPortable With {
                    .AddressLine1 = "123 Townsend St",
                    .AddressLine2 = "Floor 6",
                    .AdminArea2 = "San Francisco",
                    .AdminArea1 = "CA",
                    .PostalCode = "94107",
                    .CountryCode = "US"
                }
            }
        }
    }
}
    Return orderRequest
End Function

Public Shared Function CreateOrder(ByVal Optional d As Boolean = False) As HttpResponse
    Debug.WriteLine("Create Order with minimum payload..")
    Dim request = New OrdersCreateRequest()
    request.Headers.Add("prefer", "return=representation")
    request.RequestBody(BuildRequestBody())

    Dim response = Task.Run(Async Function() Await PayPalClient.client().Execute(request)).Result

    If d Then
        Dim result = response.Result(Of Order)()
        Debug.WriteLine($"Status: {result.Status}")
        Debug.WriteLine($"Order Id: {result.Id}")
        Debug.WriteLine($"Intent: {result.CheckoutPaymentIntent}")
        Debug.WriteLine("Links:")

        For Each link As LinkDescription In result.Links
            Debug.WriteLine(vbTab & $"{link.Rel}: {link.Href}" & vbTab & $"Call Type: {link.Method}")
        Next

        Dim amount As AmountWithBreakdown = result.PurchaseUnits(0).AmountWithBreakdown
        Debug.WriteLine($"Total Amount: {amount.CurrencyCode} {amount.Value}")
    End If

    Return response
End Function

Public Shared Function CaptureOrder(ByVal OrderId As String, ByVal Optional d As Boolean = False) As HttpResponse
    Dim request = New OrdersCaptureRequest(OrderId)
    request.Prefer("return=representation")
    request.RequestBody(New OrderActionRequest())
    Dim response = Task.Run(Async Function() Await PayPalClient.client().Execute(request)).Result

    If d Then
        Dim result = response.Result(Of Order)()
        Debug.WriteLine($"Status: {result.Status}")
        Debug.WriteLine($"Order Id: {result.Id}")
        Debug.WriteLine($"Intent: {result.CheckoutPaymentIntent}")
        Debug.WriteLine("Links:")

        For Each link As LinkDescription In result.Links
            Debug.WriteLine(vbTab & $"{link.Rel}: {link.Href}" & vbTab & $"Call Type: {link.Method}")
        Next

        Debug.WriteLine("Capture Ids: ")

        For Each purchaseUnit As PurchaseUnit In result.PurchaseUnits

            For Each capture As Capture In purchaseUnit.Payments.Captures
                Debug.WriteLine(vbTab & $" {capture.Id}")
            Next
        Next

        Dim amount As AmountWithBreakdown = result.PurchaseUnits(0).AmountWithBreakdown
        Debug.WriteLine("Buyer:")
        Debug.WriteLine(vbTab & $"Email Address: {result.Payer.Email}" & vbLf & vbTab & $"Name: {result.Payer.Name.GivenName} {result.Payer.Name.Surname}" & vbLf)
        Debug.WriteLine($"Response JSON:" & vbLf & $"{PayPalClient.ObjectToJSONString(result)}")
    End If

    Return response
End Function

2

Answers


  1. The PayPal Webhooks guide is here: https://developer.paypal.com/docs/api-basics/notifications/webhooks/rest/#verify-event-notifications

    The Webhooks API reference is here: https://developer.paypal.com/docs/api/webhooks/v1/

    The PayPal REST SDKs that are mentioned for webhooks are no longer maintained, so you should NOT use any SDK. Instead, implement direct HTTPS API calls from your environment.

    Login or Signup to reply.
  2. Just create a controller with the appropriate methods and routing
    e.g.

    [Route("api/[controller]")]
    [ApiController]
    public class MyController: ControllerBase 
    {
    
    
       // point the webhook at .CancelUrl = "https://XXX/api/CancelUrl" (to match the routing)
    
      [HttpPost, Route("CancelUrl")]
      public async Task<IActionResult> CancelUrlAsync() 
      {
      // do cancel stuff here
    
      }
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search