322 lines
11 KiB
C#
322 lines
11 KiB
C#
using AyCode.Core.Loggers;
|
|
using System.Net.Http.Headers;
|
|
using System.Text;
|
|
using System.Text.Json;
|
|
using System.Text.Json.Serialization;
|
|
using TIAMWebApp.Shared.Application.Models.ClientSide.Payment;
|
|
|
|
namespace TIAMWebApp.Server.Services
|
|
{
|
|
|
|
public class SumupService
|
|
{
|
|
private readonly HttpClient _httpClient;
|
|
private readonly TIAM.Core.Loggers.ILogger _logger;
|
|
private readonly string _clientId;
|
|
private readonly string _clientSecret;
|
|
|
|
public SumupService(HttpClient httpClient, string clientId, string clientSecret, IEnumerable<IAcLogWriterBase> logWriters)
|
|
{
|
|
_httpClient = httpClient;
|
|
_clientId = clientId;
|
|
_clientSecret = clientSecret;
|
|
_logger = new TIAM.Core.Loggers.Logger<SumupService>(logWriters.ToArray());
|
|
}
|
|
|
|
public async Task<string> GetAccessTokenAsync()
|
|
{
|
|
var authToken = Convert.ToBase64String(Encoding.UTF8.GetBytes($"{_clientId}:{_clientSecret}"));
|
|
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authToken);
|
|
|
|
var requestContent = new StringContent("grant_type=client_credentials", Encoding.UTF8, "application/x-www-form-urlencoded");
|
|
|
|
var response = await _httpClient.PostAsync("https://api.sumup.com/token", requestContent);
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
var responseContent = await response.Content.ReadAsStringAsync();
|
|
var tokenResponse = JsonSerializer.Deserialize<SumUpTokenResponse>(responseContent);
|
|
|
|
return tokenResponse.AccessToken;
|
|
}
|
|
|
|
public async Task<PaymentResponse> CreatePaymentLinkAsync(SumupPaymentRequest paymentRequest)
|
|
{
|
|
var accessToken = await GetAccessTokenAsync();
|
|
//_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
|
|
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "sup_sk_0rt9IFrMpE9qA6328vqMwCtiCntRXZxGR");
|
|
|
|
var requestContent = new StringContent(JsonSerializer.Serialize(paymentRequest), Encoding.UTF8, "application/json");
|
|
|
|
var response = await _httpClient.PostAsync("https://api.sumup.com/v0.1/checkouts", requestContent);
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
var responseContent = await response.Content.ReadAsStringAsync();
|
|
var paymentResponse = JsonSerializer.Deserialize<PaymentResponse>(responseContent);
|
|
|
|
return paymentResponse;
|
|
}
|
|
|
|
public async Task<PaymentResponse> CreateCheckout(SumupPaymentRequest request)
|
|
{
|
|
var options = new JsonSerializerOptions
|
|
{
|
|
PropertyNamingPolicy = new LowercaseNamingPolicy(),
|
|
PropertyNameCaseInsensitive = true // To ensure deserialization works regardless of case
|
|
};
|
|
|
|
var checkoutRequest = new HttpRequestMessage(HttpMethod.Post, "https://api.sumup.com/v0.1/checkouts");
|
|
checkoutRequest.Headers.Authorization = new AuthenticationHeaderValue("Bearer", "sup_sk_0rt9IFrMpE9qA6328vqMwCtiCntRXZxGR");
|
|
var kvaanyad = JsonSerializer.Serialize(request, options);
|
|
_logger.Detail($"Request json: {kvaanyad}");
|
|
checkoutRequest.Content = new StringContent(kvaanyad, Encoding.UTF8, "application/json");
|
|
_logger.Detail($"Request content: { await checkoutRequest.Content.ReadAsStringAsync()}");
|
|
var response = await _httpClient.SendAsync(checkoutRequest);
|
|
_logger.Detail(await response.Content.ReadAsStringAsync());
|
|
if (response.IsSuccessStatusCode)
|
|
{
|
|
var jsonResponse = await response.Content.ReadAsStringAsync();
|
|
var bleh = response.Content.ToString();
|
|
var valami = JsonSerializer.Deserialize<PaymentResponse>(jsonResponse);
|
|
return valami;
|
|
}
|
|
|
|
var errorResponse = await response.Content.ReadAsStringAsync();
|
|
throw new Exception($"Unable to create checkout. Response: {errorResponse}");
|
|
}
|
|
|
|
public async Task<string> GetPayment(string checkoutId)
|
|
{
|
|
string url = $"https://api.sumup.com/v0.1/checkouts/{checkoutId}";
|
|
|
|
// Replace with your actual access token
|
|
string accessToken = "sup_sk_0rt9IFrMpE9qA6328vqMwCtiCntRXZxGR";
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Get, url);
|
|
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
|
|
|
|
var response = await _httpClient.SendAsync(request);
|
|
|
|
if (!response.IsSuccessStatusCode)
|
|
{
|
|
return "error";
|
|
}
|
|
else
|
|
{
|
|
var jsonResponse = await response.Content.ReadAsStringAsync();
|
|
var checkoutResponse = JsonSerializer.Deserialize<GetPaymentResponse>(jsonResponse);
|
|
//if we have to do something with the payment, we can do it here
|
|
string paymentStatus = checkoutResponse.Status;
|
|
switch (paymentStatus)
|
|
{
|
|
case "pending":
|
|
//update in DB?
|
|
break;
|
|
case "failed":
|
|
//update in DB?
|
|
break;
|
|
case "payed":
|
|
//update in DB?
|
|
break;
|
|
}
|
|
return jsonResponse;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
public class SumUpTokenResponse
|
|
{
|
|
[JsonPropertyName("access_token")]
|
|
public string AccessToken { get; set; }
|
|
}
|
|
|
|
// SumUpService.cs (continued)
|
|
|
|
|
|
|
|
public class PaymentResponse
|
|
{
|
|
[JsonPropertyName("amount")]
|
|
public int Amount { get; set; }
|
|
|
|
[JsonPropertyName("checkout_reference")]
|
|
public string CheckoutReference { get; set; }
|
|
|
|
[JsonPropertyName("checkout_type")]
|
|
public string CheckoutType { get; set; }
|
|
|
|
[JsonPropertyName("currency")]
|
|
public string Currency { get; set; }
|
|
|
|
[JsonPropertyName("date")]
|
|
public DateTimeOffset Date { get; set; }
|
|
|
|
[JsonPropertyName("description")]
|
|
public string Description { get; set; }
|
|
|
|
[JsonPropertyName("id")]
|
|
public Guid Id { get; set; }
|
|
|
|
[JsonPropertyName("merchant_code")]
|
|
public string MerchantCode { get; set; }
|
|
|
|
[JsonPropertyName("merchant_country")]
|
|
public string MerchantCountry { get; set; }
|
|
|
|
[JsonPropertyName("merchant_name")]
|
|
public string MerchantName { get; set; }
|
|
|
|
[JsonPropertyName("pay_to_email")]
|
|
public string PayToEmail { get; set; }
|
|
|
|
[JsonPropertyName("product")]
|
|
public string Product { get; set; }
|
|
|
|
[JsonPropertyName("purpose")]
|
|
public string Purpose { get; set; }
|
|
|
|
[JsonPropertyName("status")]
|
|
public string Status { get; set; }
|
|
|
|
[JsonPropertyName("transactions")]
|
|
public object[] Transactions { get; set; }
|
|
}
|
|
|
|
public class GetPaymentResponse
|
|
{
|
|
[JsonPropertyName("amount")]
|
|
public double Amount { get; set; }
|
|
|
|
[JsonPropertyName("checkout_reference")]
|
|
public string CheckoutReference { get; set; }
|
|
|
|
[JsonPropertyName("currency")]
|
|
public string Currency { get; set; }
|
|
|
|
[JsonPropertyName("customer_id")]
|
|
public string CustomerId { get; set; }
|
|
|
|
[JsonPropertyName("date")]
|
|
public DateTimeOffset Date { get; set; }
|
|
|
|
[JsonPropertyName("description")]
|
|
public string Description { get; set; }
|
|
|
|
[JsonPropertyName("id")]
|
|
public Guid Id { get; set; }
|
|
|
|
[JsonPropertyName("mandate")]
|
|
public Mandate Mandate { get; set; }
|
|
|
|
[JsonPropertyName("merchant_code")]
|
|
public string MerchantCode { get; set; }
|
|
|
|
[JsonPropertyName("pay_to_email")]
|
|
public string PayToEmail { get; set; }
|
|
|
|
[JsonPropertyName("return_url")]
|
|
public string ReturnUrl { get; set; }
|
|
|
|
[JsonPropertyName("status")]
|
|
public string Status { get; set; }
|
|
|
|
[JsonPropertyName("transactions")]
|
|
public Transaction[] Transactions { get; set; }
|
|
|
|
[JsonPropertyName("valid_until")]
|
|
public DateTime ValidUntil { get; set; }
|
|
|
|
[JsonPropertyName("merchant_name")]
|
|
public string MerchantName { get; set; }
|
|
|
|
[JsonPropertyName("payment_instrument")]
|
|
public PaymentInstrument PaymentInstrument { get; set; }
|
|
|
|
[JsonPropertyName("redirect_url")]
|
|
public string RedirectUrl { get; set; }
|
|
|
|
[JsonPropertyName("transaction_code")]
|
|
public string TransactionCode { get; set; }
|
|
|
|
[JsonPropertyName("transaction_id")]
|
|
public Guid TransactionId { get; set; }
|
|
}
|
|
|
|
public partial class Mandate
|
|
{
|
|
[JsonPropertyName("merchant_code")]
|
|
public string MerchantCode { get; set; }
|
|
|
|
[JsonPropertyName("status")]
|
|
public string Status { get; set; }
|
|
|
|
[JsonPropertyName("type")]
|
|
public string Type { get; set; }
|
|
}
|
|
|
|
public partial class PaymentInstrument
|
|
{
|
|
[JsonPropertyName("token")]
|
|
public Guid Token { get; set; }
|
|
}
|
|
|
|
public partial class Transaction
|
|
{
|
|
[JsonPropertyName("amount")]
|
|
public double Amount { get; set; }
|
|
|
|
[JsonPropertyName("currency")]
|
|
public string Currency { get; set; }
|
|
|
|
[JsonPropertyName("id")]
|
|
public Guid Id { get; set; }
|
|
|
|
[JsonPropertyName("installments_count")]
|
|
public string InstallmentsCount { get; set; }
|
|
|
|
[JsonPropertyName("payment_type")]
|
|
public string PaymentType { get; set; }
|
|
|
|
[JsonPropertyName("status")]
|
|
public string Status { get; set; }
|
|
|
|
[JsonPropertyName("timestamp")]
|
|
public DateTimeOffset Timestamp { get; set; }
|
|
|
|
[JsonPropertyName("transaction_code")]
|
|
public string TransactionCode { get; set; }
|
|
|
|
[JsonPropertyName("auth_code")]
|
|
public string AuthCode { get; set; }
|
|
|
|
[JsonPropertyName("entry_mode")]
|
|
public string EntryMode { get; set; }
|
|
|
|
[JsonPropertyName("internal_id")]
|
|
public long InternalId { get; set; }
|
|
|
|
[JsonPropertyName("merchant_code")]
|
|
public string MerchantCode { get; set; }
|
|
|
|
[JsonPropertyName("tip_amount")]
|
|
public long TipAmount { get; set; }
|
|
|
|
[JsonPropertyName("vat_amount")]
|
|
public long VatAmount { get; set; }
|
|
}
|
|
|
|
public class LowercaseNamingPolicy : JsonNamingPolicy
|
|
{
|
|
public override string ConvertName(string name)
|
|
{
|
|
return name.ToLowerInvariant();
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
|