Created
September 10, 2024 22:06
-
-
Save stacklast/b6de76593482650055e0ac59a8c006de to your computer and use it in GitHub Desktop.
Handle PayPal Webhook VerifyEvent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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