Search code examples
c#.netpaypalpaypal-sandboxpaypal-rest-sdk

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.


Solution

  • 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.