1
0
mirror of https://github.com/bitwarden/server synced 2025-12-28 06:03:29 +00:00
Files
server/src/Billing/Services/Implementations/PayPalIPNClient.cs
Alex Morask 6cc53b4739 Fix PayPal IPN Logging (#3768)
* Remove request logging, fix txn_id correlation

* Respond 400 when txn_id is missing

* More cleanup
2024-02-08 15:37:41 +00:00

85 lines
2.7 KiB
C#

using System.Text;
using Microsoft.Extensions.Options;
namespace Bit.Billing.Services.Implementations;
public class PayPalIPNClient : IPayPalIPNClient
{
private readonly HttpClient _httpClient;
private readonly Uri _ipnEndpoint;
private readonly ILogger<PayPalIPNClient> _logger;
public PayPalIPNClient(
IOptions<BillingSettings> billingSettings,
HttpClient httpClient,
ILogger<PayPalIPNClient> logger)
{
_httpClient = httpClient;
_ipnEndpoint = new Uri(billingSettings.Value.PayPal.Production
? "https://www.paypal.com/cgi-bin/webscr"
: "https://www.sandbox.paypal.com/cgi-bin/webscr");
_logger = logger;
}
public async Task<bool> VerifyIPN(string transactionId, string formData)
{
LogInfo(transactionId, $"Verifying IPN against {_ipnEndpoint}");
if (string.IsNullOrEmpty(formData))
{
throw new ArgumentNullException(nameof(formData));
}
var requestMessage = new HttpRequestMessage { Method = HttpMethod.Post, RequestUri = _ipnEndpoint };
var requestContent = string.Concat("cmd=_notify-validate&", formData);
requestMessage.Content = new StringContent(requestContent, Encoding.UTF8, "application/x-www-form-urlencoded");
var response = await _httpClient.SendAsync(requestMessage);
var responseContent = await response.Content.ReadAsStringAsync();
if (response.IsSuccessStatusCode)
{
return responseContent switch
{
"VERIFIED" => Verified(),
"INVALID" => Invalid(),
_ => Unhandled(responseContent)
};
}
LogError(transactionId, $"Unsuccessful Response | Status Code: {response.StatusCode} | Content: {responseContent}");
return false;
bool Verified()
{
LogInfo(transactionId, "Verified");
return true;
}
bool Invalid()
{
LogError(transactionId, "Verification Invalid");
return false;
}
bool Unhandled(string content)
{
LogWarning(transactionId, $"Unhandled Response Content: {content}");
return false;
}
}
private void LogInfo(string transactionId, string message)
=> _logger.LogInformation("Verify PayPal IPN ({Id}) | {Message}", transactionId, message);
private void LogWarning(string transactionId, string message)
=> _logger.LogWarning("Verify PayPal IPN ({Id}) | {Message}", transactionId, message);
private void LogError(string transactionId, string message)
=> _logger.LogError("Verify PayPal IPN ({Id}) | {Message}", transactionId, message);
}