diff --git a/LiqPayIntegration/LiqPay.cs b/LiqPayIntegration/LiqPay.cs new file mode 100644 index 0000000..6517416 --- /dev/null +++ b/LiqPayIntegration/LiqPay.cs @@ -0,0 +1,55 @@ +using System.Buffers.Text; +using System.Net.Http.Json; +using System.Security.Cryptography; +using System.Text; +using System.Text.Json; +using System.Text.Json.Nodes; +using System.Text.Json.Serialization; +using static System.Net.WebRequestMethods; + +namespace LiqPayIntegration +{ + public class LiqPay + { + private readonly HttpClient _http; + + public const string API_URL = "https://www.liqpay.ua/api/request"; + public string PublicKey { get; init; } + public string PrivateKey { get; init; } + public LiqPay(string publicKey, string privateKey) + { + _http = new HttpClient(); + PublicKey = publicKey; + PrivateKey = privateKey; + } + + public async ValueTask PaymentArchive(DateTime from, DateTime to) + { + var json = JsonContent.Create(new + { + action = "reports", + version = 3, + public_key = PublicKey, + date_from = (long)from.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds, + date_to = (long)to.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds + }); + var data = Convert.ToBase64String(await json.ReadAsByteArrayAsync()); + var signString = PrivateKey + data + PrivateKey; + var signature = Convert.ToBase64String(SHA1.HashData(Encoding.UTF8.GetBytes(signString))); + var requestContent = new FormUrlEncodedContent(new[] { + new KeyValuePair("data", data), + new KeyValuePair("signature", signature) + }); + var response = await _http.PostAsync(API_URL, requestContent); + var rawResponse = await response.Content.ReadAsStringAsync(); + var jsonResponse = JsonNode.Parse(rawResponse); + if (jsonResponse["result"] == null) + throw new NullReferenceException("jsonResponse[\"result\"]"); + var result = jsonResponse["result"].Deserialize(); + if (result != "success") + throw new Exception("result isn't success"); + var paymentResponses = jsonResponse["data"].Deserialize(); + return paymentResponses; + } + } +} \ No newline at end of file diff --git a/LiqPayIntegration/LiqPayIntegration.csproj b/LiqPayIntegration/LiqPayIntegration.csproj new file mode 100644 index 0000000..132c02c --- /dev/null +++ b/LiqPayIntegration/LiqPayIntegration.csproj @@ -0,0 +1,9 @@ + + + + net6.0 + enable + enable + + + diff --git a/LiqPayIntegration/PaymentResponse.cs b/LiqPayIntegration/PaymentResponse.cs new file mode 100644 index 0000000..42782dc --- /dev/null +++ b/LiqPayIntegration/PaymentResponse.cs @@ -0,0 +1,199 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json.Serialization; +using System.Threading.Tasks; + +namespace LiqPayIntegration +{ + public class PaymentResponse + { + /// + /// Id платежу в системі LiqPay + /// + [JsonPropertyName("payment_id")] + public long PaymentId { get; set; } + /// + /// Тип операції. + /// Можливі значення: pay - платіж, + /// hold - блокування коштів на рахунку відправника, + /// paysplit - розщеплення платежу, + /// subscribe - створення регулярного платежу, + /// paydonate - пожертвування, + /// auth - предавторізація картки, + /// regular - регулярний платіж + /// + [JsonPropertyName("action")] + public string Action { get; set; } + /// + /// Статус платежу. + /// Можливі значення: + /// + [JsonPropertyName("status")] + public string Status { get; set; } + /// + /// Версія API. Поточне значення - 3 + /// + [JsonPropertyName("version")] + public int Version { get; set; } + /// + /// Тип платежу + /// + [JsonPropertyName("type")] + public string Type { get; set; } + /// + /// Спосіб оплати. + /// Можливі значення: + /// + [JsonPropertyName("paytype")] + public string PayType { get; set; } + /// + /// Публічний ключ магазину + /// + [JsonPropertyName("public_key")] + public string PublicKey { get; set; } + /// + /// ID еквайера + /// + [JsonPropertyName("acq_id")] + public int AcqId { get; set; } + /// + /// Order_id платежу + /// + [JsonPropertyName("order_id")] + public string OrderId { get; set; } + /// + /// Order_id платежу в системі LiqPay + /// + [JsonPropertyName("liqpay_order_id")] + public string LiqPayOrderId { get; set; } + /// + /// Коментар до платежу + /// + [JsonPropertyName("description")] + public string Description { get; set; } + /// + /// Карта відправника + /// + [JsonPropertyName("sender_card_mask2")] + public string SenderCardMask2 { get; set; } + /// + /// Банк відправника + /// + [JsonPropertyName("sender_card_bank")] + public string SenderCardBank { get; set; } + /// + /// Тип картки відправника MC/Visa + /// + [JsonPropertyName("sender_card_type")] + public string SenderCardType { get; set; } + /// + /// Країна картки відправника. Цифровий ISO 3166-1 код + /// + [JsonPropertyName("sender_card_country")] + public int SenderCardCountry { get; set; } + /// + /// + /// + [JsonPropertyName("ip")] + public string Ip { get; set; } + /// + /// Сума платежу + /// + [JsonPropertyName("amount")] + public double Amount { get; set; } + /// + /// Валюта платежу + /// + [JsonPropertyName("currency")] + public string Currency { get; set; } + /// + /// Комісія з відправника у валюті платежу + /// + [JsonPropertyName("sender_commission")] + public double SenderCommission { get; set; } + /// + /// Комісія з одержувача у валюті платежу + /// + [JsonPropertyName("receiver_commission")] + public double ReceiverCommission { get; set; } + /// + /// Комісія агента в валюті платежу + /// + [JsonPropertyName("agent_commission")] + public double AgentCommission { get; set; } + /// + /// Сума транзакції debit у валюті currency_debit + /// + [JsonPropertyName("amount_debit")] + public double AmountDebit { get; set; } + /// + /// Сума транзакції credit в валюті amount_credit + /// + [JsonPropertyName("amount_credit")] + public double AmountCredit { get; set; } + /// + /// Комісія з відправника у валюті currency_debit + /// + [JsonPropertyName("commission_debit")] + public double CommissionDebit { get; set; } + /// + /// Комісія з одержувача у валюті currency_credit + /// + [JsonPropertyName("commission_credit")] + public double CommissionCredit { get; set; } + /// + /// Валюта транзакції debit + /// + [JsonPropertyName("currency_debit")] + public string CurrencyDebit { get; set; } + /// + /// Валюта транзакції credit + /// + [JsonPropertyName("currency_credit")] + public string CurrencyCredit { get; set; } + /// + /// Бонус відправника у валюті платежу + /// + [JsonPropertyName("sender_bonus")] + public double SenderBonus { get; set; } + /// + /// Бонус відправника у валюті платежу debit + /// + [JsonPropertyName("amount_bonus")] + public double AmountBonus { get; set; } + /// + /// Можливі значення: 5 - транзакція пройшла з 3DS (емітент і еквайєр підтримують технологію 3D-Secure), 6 - емітент картки платника не підтримує технологію 3D-Secure, 7 - операція пройшла без 3D-Secure + /// + [JsonPropertyName("mpi_eci")] + public string MpiEci { get; set; } + /// + /// Можливі значення: + /// true - транзакція пройшла з 3DS перевіркою, + /// false - транзакція пройшла без 3DS перевірки + /// + [JsonPropertyName("is_3ds")] + public bool Is3ds { get; set; } + /// + /// + /// + [JsonPropertyName("language")] + public string Language { get; set; } + /// + /// Дата створення платежу + /// + [JsonPropertyName("create_date")] + public long CreateDate { get; set; } + /// + /// Дата завершення/зміни платежу + /// + [JsonPropertyName("end_date")] + public long EndDate { get; set; } + /// + /// Id транзакції в системі LiqPay + /// + [JsonPropertyName("transaction_id")] + public long TransactionId { get; set; } + } +} \ No newline at end of file