首页 > 解决方案 > java paypal webhook控制器处理支付事件

问题描述

我需要实现 webhook 控制器来处理完整的支付事件,所以我该如何处理或订阅。`public 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中处理实现相同。以及当控制器被击中时需要哪些参数。

标签: javapaypalpayment-gatewaywebhookspaypal-sandbox

解决方案


对于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


推荐阅读