From 95bf85dc6e95cfd274918834a2e24855a07707a7 Mon Sep 17 00:00:00 2001 From: cyprain-okeke <108260115+cyprain-okeke@users.noreply.github.com> Date: Wed, 18 Oct 2023 17:06:15 +0100 Subject: [PATCH] [AC-1705] Update PayPal Verification URL to point to updated domain (#3338) * change the url to match the paypal documentation * Adding user-agent to the header * Add a missing using statement * Add more logs * Resolving pr comments --- src/Billing/Utilities/PayPalIpnClient.cs | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/Billing/Utilities/PayPalIpnClient.cs b/src/Billing/Utilities/PayPalIpnClient.cs index 15f7a2f156..1762f92a11 100644 --- a/src/Billing/Utilities/PayPalIpnClient.cs +++ b/src/Billing/Utilities/PayPalIpnClient.cs @@ -10,18 +10,22 @@ public class PayPalIpnClient { private readonly HttpClient _httpClient = new HttpClient(); private readonly Uri _ipnUri; + private readonly ILogger _logger; - public PayPalIpnClient(IOptions billingSettings) + public PayPalIpnClient(IOptions billingSettings, ILogger logger) { var bSettings = billingSettings?.Value; - _ipnUri = new Uri(bSettings.PayPal.Production ? "https://www.paypal.com/cgi-bin/webscr" : - "https://www.sandbox.paypal.com/cgi-bin/webscr"); + _ipnUri = new Uri(bSettings.PayPal.Production ? "https://ipnpb.paypal.com/cgi-bin/webscr" : + "https://ipnpb.sandbox.paypal.com/cgi-bin/webscr"); + _logger = logger; } public async Task VerifyIpnAsync(string ipnBody) { + _logger.LogInformation("Verifying IPN with PayPal at {Timestamp}: {VerificationUri}", DateTime.UtcNow, _ipnUri); if (ipnBody == null) { + _logger.LogError("No IPN body."); throw new ArgumentException("No IPN body."); } @@ -30,6 +34,7 @@ public class PayPalIpnClient Method = HttpMethod.Post, RequestUri = _ipnUri }; + _httpClient.DefaultRequestHeaders.Add("User-Agent", "CSharp-IPN-VerificationScript"); var cmdIpnBody = string.Concat("cmd=_notify-validate&", ipnBody); request.Content = new StringContent(cmdIpnBody, Encoding.UTF8, "application/x-www-form-urlencoded"); var response = await _httpClient.SendAsync(request); @@ -42,14 +47,14 @@ public class PayPalIpnClient { return true; } - else if (responseContent.Equals("INVALID")) + + if (responseContent.Equals("INVALID")) { + _logger.LogWarning("Received an INVALID response from PayPal: {ResponseContent}", responseContent); return false; } - else - { - throw new Exception("Failed to verify IPN."); - } + _logger.LogError("Failed to verify IPN: {ResponseContent}", responseContent); + throw new Exception("Failed to verify IPN."); } public class IpnTransaction