Skip to main content

Enbank - .NET example docs

Read on how the payment flow works first and the OpenAPI docs. Then look at the example code bellow.

caution

Instead of using the code below we suggest you to use our OpenAPI docs with NSwag to generate a client.

using System.Net.Http;
using System.Threading.Tasks;

namespace Enbank
{
public class NewTransactionRequest
{
[System.Text.Json.Serialization.JsonPropertyName("amount")]

[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.Never)]
public string Amount { get; set; }

[System.Text.Json.Serialization.JsonPropertyName("token")]

[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.Never)]
[System.ComponentModel.DataAnnotations.Required(AllowEmptyStrings = true)]
public string Token { get; set; }

[System.Text.Json.Serialization.JsonPropertyName("description")]

[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.Never)]
[System.ComponentModel.DataAnnotations.Required]
public string Description { get; set; }

[System.Text.Json.Serialization.JsonPropertyName("currency")]

[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)]
public string Currency { get; set; } = "BGN";

[System.Text.Json.Serialization.JsonPropertyName("iban")]

[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)]
public string Iban { get; set; }

[System.Text.Json.Serialization.JsonPropertyName("successCallback")]

[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)]
public string SuccessCallback { get; set; }

[System.Text.Json.Serialization.JsonPropertyName("errorCallback")]

[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)]
public string ErrorCallback { get; set; }

[System.Text.Json.Serialization.JsonPropertyName("flags")]

[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)]
public System.Collections.Generic.IDictionary<string, object> Flags { get; set; }
}

public partial class NewTransactionResponse
{
[System.Text.Json.Serialization.JsonPropertyName("transactionId")]
[System.Text.Json.Serialization.JsonIgnore(Condition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingDefault)]
public string TransactionId { get; set; }
}

public partial class ApiException : System.Exception
{
public int StatusCode { get; private set; }

public string Response { get; private set; }

public System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> Headers { get; private set; }

public ApiException(string message, int statusCode, string response, System.Collections.Generic.IReadOnlyDictionary<string, System.Collections.Generic.IEnumerable<string>> headers, System.Exception innerException)
: base(message + "\n\nStatus: " + statusCode + "\nResponse: \n" + ((response == null) ? "(null)" : response.Substring(0, response.Length >= 512 ? 512 : response.Length)), innerException)
{
StatusCode = statusCode;
Response = response;
Headers = headers;
}

public override string ToString()
{
return string.Format("HTTP Response: \n\n{0}\n\n{1}", Response, base.ToString());
}
}

/// <summary>
/// Represents the plugin service manager
/// </summary>
public class EnbankService
{
private readonly HttpClient _httpClient;

public EnbankServic(HttpClient httpClient, EnbankPaymentSettings enbankPaymentSettings)
{
_httpClient = httpClient;
}

public virtual async Task<NewTransactionResponse> NewTransactionAsync(NewTransactionRequest body, System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken))
{
var urlBuilder_ = new System.Text.StringBuilder();
urlBuilder_.Append("https://enbank-server-local.fly.dev").Append("/transactions/new");

var client_ = _httpClient;
var disposeClient_ = false;
try
{
using (var request_ = new System.Net.Http.HttpRequestMessage())
{
var json_ = System.Text.Json.JsonSerializer.Serialize(body);
var content_ = new System.Net.Http.StringContent(json_);
content_.Headers.ContentType = System.Net.Http.Headers.MediaTypeHeaderValue.Parse("application/json");
request_.Content = content_;
request_.Method = new System.Net.Http.HttpMethod("POST");
request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json"));

var url_ = urlBuilder_.ToString();
request_.RequestUri = new System.Uri(url_, System.UriKind.RelativeOrAbsolute);

var response_ = await client_.SendAsync(request_, System.Net.Http.HttpCompletionOption.ResponseHeadersRead, cancellationToken).ConfigureAwait(false);
var disposeResponse_ = true;
try
{
var headers_ = System.Linq.Enumerable.ToDictionary(response_.Headers, h_ => h_.Key, h_ => h_.Value);
if (response_.Content != null && response_.Content.Headers != null)
{
foreach (var item_ in response_.Content.Headers)
headers_[item_.Key] = item_.Value;
}

var status_ = (int)response_.StatusCode;
if (status_ == 200)
{
using (var responseStream = await response_.Content.ReadAsStreamAsync().ConfigureAwait(false))
{
var typedBody = await System.Text.Json.JsonSerializer.DeserializeAsync<NewTransactionResponse>(responseStream, new System.Text.Json.JsonSerializerOptions(), cancellationToken).ConfigureAwait(false);
if (typedBody == null)
{
throw new ApiException("Response was null which was not expected.", status_, "", headers_, null);
}
return typedBody;
}
}
else
{
var responseData_ = response_.Content == null ? null : await response_.Content.ReadAsStringAsync().ConfigureAwait(false);
throw new ApiException("The HTTP status code of the response was not expected (" + status_ + ").", status_, responseData_, headers_, null);
}
}
finally
{
if (disposeResponse_)
response_.Dispose();
}
}
}
finally
{
if (disposeClient_)
client_.Dispose();
}
}
}
}