Skip to content

Commit

Permalink
[AC-1783] PayPal IPN Changes (#3404)
Browse files Browse the repository at this point in the history
* Reverted changes to the domain for the PayPal IPN client, and added more logging

* Removing User-Agent as it wasn't in the IPN client previously
  • Loading branch information
cturnbull-bitwarden committed Nov 2, 2023
1 parent a12f361 commit 79aded8
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/Billing/Utilities/PayPalIpnClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public class PayPalIpnClient
public PayPalIpnClient(IOptions<BillingSettings> billingSettings, ILogger<PayPalIpnClient> logger)
{
var bSettings = billingSettings?.Value;
_ipnUri = new Uri(bSettings.PayPal.Production ? "https://ipnpb.paypal.com/cgi-bin/webscr" :
"https://ipnpb.sandbox.paypal.com/cgi-bin/webscr");
_logger = logger;
_ipnUri = new Uri(bSettings.PayPal.Production ? "https://www.paypal.com/cgi-bin/webscr" :
"https://www.sandbox.paypal.com/cgi-bin/webscr");
}

public async Task<bool> VerifyIpnAsync(string ipnBody)
Expand All @@ -29,19 +29,16 @@ public async Task<bool> VerifyIpnAsync(string ipnBody)
throw new ArgumentException("No IPN body.");
}

var request = new HttpRequestMessage
{
Method = HttpMethod.Post,
RequestUri = _ipnUri
};
_httpClient.DefaultRequestHeaders.Add("User-Agent", "CSharp-IPN-VerificationScript");
var request = new HttpRequestMessage { Method = HttpMethod.Post, RequestUri = _ipnUri };
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);
if (!response.IsSuccessStatusCode)
{
_logger.LogError("Failed to receive a successful response from PayPal IPN verification service. Response: {Response}", response);
throw new Exception("Failed to verify IPN, status: " + response.StatusCode);
}

var responseContent = await response.Content.ReadAsStringAsync();
if (responseContent.Equals("VERIFIED"))
{
Expand All @@ -53,6 +50,7 @@ public async Task<bool> VerifyIpnAsync(string ipnBody)
_logger.LogWarning("Received an INVALID response from PayPal: {ResponseContent}", responseContent);
return false;
}

_logger.LogError("Failed to verify IPN: {ResponseContent}", responseContent);
throw new Exception("Failed to verify IPN.");
}
Expand Down

0 comments on commit 79aded8

Please sign in to comment.