首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ASP.NET MVC创建paypal webhook

ASP.NET MVC创建paypal webhook
EN

Stack Overflow用户
提问于 2020-07-20 04:14:52
回答 2查看 338关注 0票数 1

我正在使用REST API SDK for Dotnet V2 github link来集成PayPal订单,创建和捕获它工作得很好。

我现在正在尝试实现webhook,我已经花了很多时间尝试找出如何创建一个控制器来接收PayPal webhook来更新我的订单状态,但无法找到解决方案。

有没有关于如何在.NET中创建webhook的.net文档或示例代码?

这是我用来创建和捕获订单的vb.net代码

代码语言:javascript
运行
复制
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
EN

回答 2

Stack Overflow用户

发布于 2020-07-20 04:26:58

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/

这里提到的webhooks的PayPal REST SDK不再维护,所以您不应该使用任何SDK。相反,应从您的环境中实现直接HTTPS API调用。

票数 1
EN

Stack Overflow用户

发布于 2020-07-23 11:40:13

只需创建一个具有适当方法和路由的控制器,例如

代码语言:javascript
运行
复制
[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

  }

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62985194

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档