ASP.NET MVC 创建 paypal webhook
Posted
技术标签:
【中文标题】ASP.NET MVC 创建 paypal webhook【英文标题】:ASP.NET MVC creating paypal webhook 【发布时间】:2020-11-09 02:28:17 【问题描述】:我正在使用 REST API SDK for Dotnet V2 github link 与 PayPal 订单集成创建和捕获它工作正常。
我现在正在尝试实现 webhook,已经花了很多时间试图找出如何创建一个控制器来接收 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
【问题讨论】:
这是一个有趣的问题,但是现在如何制定它有关闭的风险,因为您要求为您找到一些外部资源。而是提供您解决 webhook 问题的尝试,即使它不起作用。有人可以发现问题并帮助您 说实话,当涉及到付款时,我正在寻找从贝宝接收消息的最佳实践,我可以创建自己的 webhook 类,但我正在寻找创建的最佳实践它,所以我现在的代码只是创建订单并捕获返回响应 我添加了创建和捕获订单的代码 你想要的是一个双端口控制器。一个端口连接到客户端,第二个端口将客户端数据转发到 SDK 并将 SDK 的响应发送到客户端。它与普通控制器没有什么不同,只是来自客户端的请求将被转发到 SDK,而来自 SDK 的响应将被发送到客户端。 【参考方案1】: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 调用。
【讨论】:
感谢您的回复,我能够找到这些文档,实际上我正在寻找的是如何使用 .net 构建 webhook 的示例代码【参考方案2】:只需使用适当的方法和路由创建一个控制器 例如
[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
【讨论】:
以上是关于ASP.NET MVC 创建 paypal webhook的主要内容,如果未能解决你的问题,请参考以下文章
ASP.NET MVC C# PayPal Rest API - UNAUTHORIZED_PAYMENT
在 ASP.NET MVC 站点上集成 PayPal 的简单解决方案
在 ASP.NET MVC 站点上集成 PayPal 的简单解决方案