简体   繁体   中英

Validating PayPal Webhook calls with PayPal .NET SDK in Sandbox

I've been trying to set up PayPal Webhooks (in Sandbox mode) to receive notifications about declined and successful payments. My problem is that I can't get validation working. Some details about my attempts:

  1. The app is an OWIN self-hosted Web API 2.
  2. Hosted as an Azure Web App, tested on Azure as well.
  3. I set the Paypal Webhook receiver URL in the Paypal dashboard to the URL of my endpoint on Azure.
  4. I used the Paypal Webhooks simulator from the Paypal dashboard to send a message to the Azure endpoint.

I tried listening for Webhook calls two ways:

  1. ASP.NET Webhook Receivers ( http://blogs.msdn.com/b/webdev/archive/2015/09/04/introducing-microsoft-asp.net-webhooks-preview.aspx ), which didn't work. I get the error message "WebHook validation failed: "
  2. Tried creating a Web API endpoint to receive and validate the request, didn't work either. Code here:

     [HttpPost] public async Task<IHttpActionResult> PaymentCaptureCompleted() { // Get the received request's headers NameValueCollection nvc = new NameValueCollection(); foreach (var item in Request.Headers) { nvc.Add(item.Key, string.Join(",", item.Value)); } // Get the received request's body var requestBody = await Request.Content.ReadAsStringAsync(); var isValid = WebhookEvent.ValidateReceivedEvent(Api, nvc, requestBody, ConfigurationManager.AppSettings["paypal.webhook.id"]); if (isValid) { return Ok(); } else { return BadRequest("Could not validate request"); } }

There are a lot more details to this of course, but I'm not sure how much information is required to answer my question. Just let me know what you need and I'll edit this question.

Please refer PayPal Dot Net SDK for code samples. https://github.com/paypal/PayPal-NET-SDK

Also if your simulator is not working, to rule out if there is something wrong with the configuration of webhook or the azure, you may want to use Runscope. Ypu can configure a Runscope bucket as a webhook endpoint and If you are getting a webhook notification there, you may need to make changes in the Azure config.

Really don`t go to the documentation. It is old but objects is still good. You can use WebhookEvent class for it. Just use this action in your controller.

public JsonResult Index(WebhookEvent event)
{
    // event has all the data 
    return Json(new { success = true });
}

Validation does not work for either IPN sandbox nor Webhook events from the mock. It's stated in the PayPal documentation. Validation only works on the production environment of PayPal.

Wrap WebhookEvent.ValidateReceivedEvent in a try catch. If the call fails, it can just hang without that. The exception will show you the error.

  try
  {
    var isValid = WebhookEvent.ValidateReceivedEvent(apiContext, nv, requestBody, hookId);
  }
  catch(Exception e)
  {

  }

In my case the exception said "Unable to load trusted certificate"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM