Skip to content

Instantly share code, notes, and snippets.

@stacklast
Created September 10, 2024 22:06
Show Gist options
  • Select an option

  • Save stacklast/b6de76593482650055e0ac59a8c006de to your computer and use it in GitHub Desktop.

Select an option

Save stacklast/b6de76593482650055e0ac59a8c006de to your computer and use it in GitHub Desktop.
Handle PayPal Webhook VerifyEvent
private const string ENDPOINT_VERIFY_WEBHOOK = "v1/notifications/verify-webhook-signature";
public static bool VerifyEvent(string json, NameValueCollection headerDictionary, string webhookId)
{
var transmissionId = headerDictionary["PAYPAL-TRANSMISSION-ID"];
var transmissionTime = headerDictionary["PAYPAL-TRANSMISSION-TIME"];
var certificateUrl = headerDictionary["PAYPAL-CERT-URL"];
var authAlgorithm = headerDictionary["PAYPAL-AUTH-ALGO"];
var transmissionSignature = headerDictionary["PAYPAL-TRANSMISSION-SIG"];
var request = $@"{{""transmission_id"":""{transmissionId}"",""transmission_time"": ""{transmissionTime}"",""cert_url"": ""{certificateUrl}"",""auth_algo"":""{authAlgorithm}"",""transmission_sig"": ""{transmissionSignature}"",""webhook_id"": ""{webhookId}"",""webhook_event"":{json}}}";
try
{
var webApi = SetUpWebApi();
var response = webApi.PostJson<WebHookVerificationResponse>($"{BaseUrl}/{ENDPOINT_VERIFY_WEBHOOK}", request);
if (response.VerificationStatus == VerificationStatusType.SUCCESS)
{
return true;
}
bLogging.AddError(null, null, JObject.Parse(request), "Failed to verify PayPal webhook");
return false;
}
catch (Exception exception)
{
bLogging.AddError(null, null, "Failed to verify paypal webhook due to an exception", exception, request);
return false;
}
}
public class WebHookVerificationResponse
{
[JsonProperty("verification_status")]
public VerificationStatusType VerificationStatus { get; set; }
}
public enum VerificationStatusType
{
SUCCESS, FAILURE
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment