4

I have paypal integration application which receives webhook notification from paypal and I want to verify the signature as per docs:

Verify signature rest api link

Here is code which I have written:

public async Task<ActionResult> Index()
    {
        var stream = this.Request.InputStream;

        var requestheaders = HttpContext.Request.Headers;
        var reader = new StreamReader(stream);
        var jsonReader = new JsonTextReader(reader);
        var serializer = new JsonSerializer();
        var webhook = serializer.Deserialize<Models.Event>(jsonReader); 

        var webhookSignature = new WebhookSignature();
        webhookSignature.TransmissionId = requestheaders["PAYPAL-TRANSMISSION-ID"];
        webhookSignature.TransmissionTime = requestheaders["PAYPAL-TRANSMISSION-TIME"];
        webhookSignature.TransmissionSig = requestheaders["PAYPAL-TRANSMISSION-SIG"];
        webhookSignature.WebhookId = "My actual webhookid from paypal account";
        webhookSignature.CertUrl = requestheaders["PAYPAL-CERT-URL"];
        webhookSignature.AuthAlgo = requestheaders["PAYPAL-AUTH-ALGO"];
        webhookSignature.WebhookEvent = webhook;
        var jsonStr2 = JsonConvert.SerializeObject(webhookSignature);
        var result = await _webhookService.VerifyWebhookSignatureAsync(webhookSignature);
        var jsonStr3 = JsonConvert.SerializeObject(result);

        return Content(jsonStr3, "application/json");
    }


  public async Task<Models.SignatureResponse> VerifyWebhookSignatureAsync(Models.WebhookSignature webhook, CancellationToken cancellationToken = default(CancellationToken))
    {
        var accessTokenDetails = await this.CreateAccessTokenAsync();
        _httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + accessTokenDetails.AccessToken);
        try
        {
            string jsonStr = JsonConvert.SerializeObject(webhook);
            var content = new StringContent(jsonStr, Encoding.UTF8, "application/json");
            string url = $"{_baseUrl}notifications/verify-webhook-signature";
            var response = await _httpClient.PostAsync(url, content);
            if (!response.IsSuccessStatusCode)
            {
                var error = await response.Content.ReadAsStringAsync();
                throw new Exception(error);
            }
            string jsonContent = response.Content.ReadAsStringAsync().Result;
            return JsonConvert.DeserializeObject<Models.SignatureResponse>(jsonContent);
        }
        catch (Exception ex)
        {
            throw new InvalidOperationException("Request to Create payment Service failed.", ex);
        }

    }

Webhook signature verification response :

{"verification_status":"FAILURE"}

I am getting 200K ok response from api but verification status in response is always FAILURE.I tried many different request.

I am not sure if something is wrong from my request. Looking for help.

0 Answers0