簡體   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.

我現在正在嘗試實施 webhook,已經花了很多時間試圖找出如何創建 controller 來接收 PayPal webhook 以更新我的訂單狀態但無法找到解決方案。

是否有關於如何在 .NET 中創建 webhook 的 .net 文檔或示例代碼?

這是我的 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

PayPal Webhooks 指南在這里: https://developer.paypal.com/docs/api-basics/notifications/webhooks/rest/#verify-event-notifications

Webhooks API 參考在這里: https://developer.paypal.com/docs/api/webhooks/v1/

不再維護提到的用於 webhook 的 PayPal REST SDK,因此您不應使用任何 SDK。 相反,從您的環境直接調用 HTTPS API。

只需使用適當的方法和路由創建一個 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