问题描述
我需要实现 webhook 控制器来处理完整的支付事件,所以我该如何处理或订阅。 `公共 IActionResult Webhook() { // APIContext 对象可以包含可信证书的可选覆盖。 var apiContext = PayPalConfiguration.GetAPIContext();
// Get the received request's headers
var requestheaders = HttpContext.Request.Headers;
// Get the received request's body
var requestBody = string.Empty;
using (var reader = new System.IO.StreamReader(HttpContext.Request.Body))
{
requestBody = reader.ReadToEnd();
}
dynamic jsonBody = JObject.Parse(requestBody);
string webhookId = jsonBody.id;
var ev = WebhookEvent.Get(apiContext,webhookId);
// We have all the information the SDK needs,so perform the validation.
// Note: at least on SandBox environment this returns false.
// var isValid = WebhookEvent.ValidateReceivedEvent(apiContext,ToNameValueCollection(requestheaders),requestBody,webhookId);
switch (ev.event_type)
{
case "PAYMENT.CAPTURE.COMPLETED":
// Handle payment completed
break;
case "PAYMENT.CAPTURE.DENIED":
// Handle payment denied
break;
// Handle other webhooks
default:
break;
}
return new HttpStatusCodeResult(200);
}
这是我得到的 javascript 示例,但与我想在 java 中处理实现相同,以及当控制器被命中时需要哪些参数。
解决方法
对于 PayPal Subscriptions,Webhook 事件名称列表如下: https://developer.paypal.com/docs/api-basics/notifications/webhooks/event-names/#subscriptions ,特别是您想要的是 PAYMENT.SALE.COMPLETED
(不是“.CAPTURE.”)
您可能会发现这里的信息很有帮助:https://stackoverflow.com/a/65139331/2069605