private readonly HttpClient _httpClient;
private readonly ClientConfig _clientConfig;
private readonly Serilog.ILogger _logger;
public Client(HttpClient httpClient, ClientConfig clientConfig, Serilog.ILogger logger)
_httpClient = httpClient;
_clientConfig = clientConfig;
public async ValueTask<T> SendRequest<T>(DTO.Processors.PayPal.MerchantAccountConfig merchantConfig, string url, PayPalRequest obj)
where T : PayPalResponse, new()
var auth = Convert.ToBase64String(Encoding.UTF8.GetBytes(merchantConfig.clientId + ":" + merchantConfig.clientSecret));
var json = Newtonsoft.Json.JsonConvert.SerializeObject(obj, Global._jsonSettings);
using var rq = new HttpRequestMessage()
Method = HttpMethod.Post,
RequestUri = new(_clientConfig.BaseUrl + url),
Content = new StringContent(json, Encoding.UTF8, "application/json"),
rq.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Basic", auth);
rq.Headers.Add("PayPal-Request-Id", obj.payPalRequestId??Guid.NewGuid().ToString("N"));
using HttpResponseMessage response = await _httpClient.SendAsync(rq);
using var contentStream = await response.Content.ReadAsStreamAsync();
var rp = await System.Text.Json.JsonSerializer.DeserializeAsync<T>(contentStream);
if (!response.IsSuccessStatusCode)
_logger.Error(ex, "Can not communicate to PayPal");
message = "Can not communicate to PayPal",
public class PayPalRequest
public string payPalRequestId;
public class PayPalResponse
public bool error { get; set; }
public string message { get; set; }
public class CreateOrderRequest : PayPalRequest
public Purchase_Units[] purchase_units { get; set; }
public string intent { get; set; }
public Payer payer { get; set; }
public Payment_Source payment_source { get; set; }
public Application_Context application_context { get; set; }
public class CreateOrderResponse : PayPalResponse
public string id { get; set; }
public string status { get; set; }
public string intent { get; set; }
public Payment_Source payment_source { get; set; }
public Purchase_Units[] purchase_units { get; set; }
public DateTime create_time { get; set; }
public DateTime update_time { get; set; }
public Link[] links { get; set; }