简体   繁体   English

ASP.NET MVC 创建 paypal webhook

[英]ASP.NET MVC creating paypal webhook

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 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.我现在正在尝试实施 webhook,已经花了很多时间试图找出如何创建 controller 来接收 PayPal webhook 以更新我的订单状态但无法找到解决方案。

Is there a .net documentation or sample code on how to create a webhook in .NET?是否有关于如何在 .NET 中创建 webhook 的 .net 文档或示例代码?

This is my vb.net code to create and capture orders这是我的 vb.net 代码,用于创建和捕获订单

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

The PayPal Webhooks guide is here: https://developer.paypal.com/docs/api-basics/notifications/webhooks/rest/#verify-event-notifications PayPal Webhooks 指南在这里: 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/ Webhooks API 参考在这里: 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.不再维护提到的用于 webhook 的 PayPal REST SDK,因此您不应使用任何 SDK。 Instead, implement direct HTTPS API calls from your environment.相反,从您的环境直接调用 HTTPS API。

Just create a controller with the appropriate methods and routing eg只需使用适当的方法和路由创建一个 controller,例如

[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

  }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM