Skip to content

Commit

Permalink
Merge pull request #109 from TransbankDevelopers/feat/oneclick-fullTr…
Browse files Browse the repository at this point in the history
…ansaction

Add Oneclick Mall and Full Transaction REST
  • Loading branch information
a1b4 authored Dec 26, 2019
2 parents 1f29e93 + 78dfb7a commit cd93e62
Show file tree
Hide file tree
Showing 69 changed files with 2,041 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ indent_style = space
[*.{cs,csx,vb,vbx}]
indent_size = 4
insert_final_newline = true
charset = utf-8-bom
charset = utf-8

###############################
# .NET Coding Conventions #
Expand Down
30 changes: 30 additions & 0 deletions Transbank/Common/BaseRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Net.Http;
using Newtonsoft.Json;

namespace Transbank.Common
{
public abstract class BaseRequest
{
[JsonIgnore]
private readonly string _endpoint;
[JsonIgnore]
public string Endpoint { get { return _endpoint; } }
[JsonIgnore]
private readonly HttpMethod _method;
[JsonIgnore]
public HttpMethod Method { get { return _method; } }

protected BaseRequest(string endpoint, HttpMethod method)
{
if (String.IsNullOrEmpty(endpoint))
throw new ArgumentNullException(
nameof(endpoint), "Endpoint can't be null.");

_endpoint = endpoint;

_method = method ?? throw new ArgumentNullException(
nameof(method), "Method can't be null.");
}
}
}
47 changes: 47 additions & 0 deletions Transbank/Common/ExceptionHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
using System;
using Transbank.Exceptions;

namespace Transbank.Common
{
internal static class ExceptionHandler
{
// These two methods should act the same, the difference is the return type.
// This is needed because at the moment Inscription.delete returns void.

internal static ReturnType Perform<ReturnType, ExceptionType>(Func<ReturnType> block)
where ExceptionType : TransbankException
{
try
{
return block();
}
catch (Exception e)
{
throw RiseTransbankException<ExceptionType>(e);
}
}

internal static void Perform<ExceptionType>(Action block)
where ExceptionType : TransbankException
{
try
{
block();
}
catch (Exception e)
{
throw RiseTransbankException<ExceptionType>(e);
}
}

private static ExceptionType RiseTransbankException<ExceptionType>(Exception e)
where ExceptionType : TransbankException
{
int code = e.GetType().Equals(typeof(TransbankException)) ?
((TransbankException)e).Code : -1;

return (ExceptionType)Activator.CreateInstance(
typeof(ExceptionType), new object[] { code, e.Message, e });
}
}
}
10 changes: 10 additions & 0 deletions Transbank/Common/IIntegrationType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;

namespace Transbank.Common
{
public interface IIntegrationType
{
string Key { get; }
string ApiBase { get; }
}
}
62 changes: 62 additions & 0 deletions Transbank/Common/Options.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using Newtonsoft.Json;

namespace Transbank.Common
{
public class Options
{
private string _commerceCode;
private string _apiKey;
private IIntegrationType _integrationType;

private static string _commerceCodeHeaderName = "Tbk-Api-Key-Id";
private static string _apiKeyHeaderName = "Tbk-Api-Key-Secret";

private static RequestServiceHeaders _headers = new RequestServiceHeaders(_apiKeyHeaderName, _commerceCodeHeaderName);

public RequestServiceHeaders Headers
{
get => _headers;
set => _headers = value ?? throw new ArgumentNullException(
nameof(value), "headers can't be null."
);
}

public string CommerceCode
{
get => _commerceCode;
set => _commerceCode = value ?? throw new ArgumentNullException(
nameof(value), "Commerce code can't be null."
);
}

public string ApiKey
{
get => _apiKey;
set => _apiKey = value ?? throw new ArgumentNullException(
nameof(value), "Api Key can't be null."
);
}

public IIntegrationType IntegrationType
{
get => _integrationType;
set => _integrationType = value ?? throw new ArgumentNullException(
nameof(value), "IntegrationType can't be null."
);
}

public Options(string commerceCode, string apiKey, IIntegrationType integrationType, RequestServiceHeaders headers)
{
CommerceCode = commerceCode;
ApiKey = apiKey;
IntegrationType = integrationType;
Headers = headers;
}

public override string ToString()
{
return $"{nameof(CommerceCode)}: {CommerceCode}, {nameof(ApiKey)}: {ApiKey}, {nameof(IntegrationType)}: {IntegrationType}";
}
}
}
48 changes: 48 additions & 0 deletions Transbank/Common/RequestService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Transbank.Exceptions;

namespace Transbank.Common
{
public static class RequestService
{
private static readonly string CONTENT_TYPE = "application/json";

private static void AddRequiredHeaders(HttpClient client, string commerceCode, string apiKey, RequestServiceHeaders headers)
{
var header = new MediaTypeWithQualityHeaderValue(CONTENT_TYPE);
client.DefaultRequestHeaders.Accept.Add(header);
client.DefaultRequestHeaders.Add(headers.CommerceCodeHeaderName, commerceCode);
client.DefaultRequestHeaders.Add(headers.ApiKeyHeaderName, apiKey);
}

public static string Perform<T>(BaseRequest request, Options options) where T : TransbankException
{
var message = new HttpRequestMessage(request.Method, new Uri(options.IntegrationType.ApiBase + request.Endpoint));
if (request.Method != HttpMethod.Get)
message.Content = new StringContent(JsonConvert.SerializeObject(request),
Encoding.UTF8, CONTENT_TYPE);

using (var client = new HttpClient())
{
AddRequiredHeaders(client, options.CommerceCode, options.ApiKey, options.Headers);
var response = client.SendAsync(message)
.ConfigureAwait(false).GetAwaiter().GetResult();
var jsonResponse = response.Content.ReadAsStringAsync()
.ConfigureAwait(false).GetAwaiter().GetResult();
if (!response.IsSuccessStatusCode)
{
var jsonObject = (JObject)JsonConvert.DeserializeObject(jsonResponse);
throw (T)Activator.CreateInstance(typeof(T), new object[] {
(int)response.StatusCode, $"Error message: {jsonObject.Value<string>("error_message")}"
});
}
return jsonResponse;
}
}
}
}
22 changes: 22 additions & 0 deletions Transbank/Common/RequestServiceHeaders.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System.ComponentModel;
using Newtonsoft.Json;

namespace Transbank.Common
{
public class RequestServiceHeaders
{
[DefaultValue("Tbk-Api-Key-Id")]
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
public string ApiKeyHeaderName { get; set; }

[DefaultValue("Tbk-Api-Key-Secret")]
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
public string CommerceCodeHeaderName { get; set; }

public RequestServiceHeaders(string apiKeyHeaderName, string commerceCodeHeaderName)
{
ApiKeyHeaderName = apiKeyHeaderName;
CommerceCodeHeaderName = commerceCodeHeaderName;
}
}
}
26 changes: 26 additions & 0 deletions Transbank/Exceptions/InscriptionDeleteException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;

namespace Transbank.Exceptions
{
public class InscriptionDeleteException : TransbankException
{
public InscriptionDeleteException() : base()
{
}

public InscriptionDeleteException(string message)
: base(-1, message)
{
}

public InscriptionDeleteException(int code, string message)
: base(code, message)
{
}

public InscriptionDeleteException(int code, string message,
Exception innerException) : base(code, message, innerException)
{
}
}
}
26 changes: 26 additions & 0 deletions Transbank/Exceptions/InscriptionFinishException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;

namespace Transbank.Exceptions
{
public class InscriptionFinishException : TransbankException
{
public InscriptionFinishException() : base()
{
}

public InscriptionFinishException(string message)
: base(-1, message)
{
}

public InscriptionFinishException(int code, string message)
: base(code, message)
{
}

public InscriptionFinishException(int code, string message,
Exception innerException) : base(code, message, innerException)
{
}
}
}
13 changes: 13 additions & 0 deletions Transbank/Exceptions/InscriptionStartException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace Transbank.Exceptions
{
public class InscriptionStartException : TransbankException
{
public InscriptionStartException(string message) : base(-1, message) { }

public InscriptionStartException(int code, string message) : base(code, message) { }

public InscriptionStartException(int code, string message, Exception innerException) : base(code, message, innerException) { }
}
}
14 changes: 14 additions & 0 deletions Transbank/Exceptions/InscriptionStatusException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace Transbank.Exceptions
{
public class InscriptionStatusException : TransbankException
{
public InscriptionStatusException(string message) : base(-1, message){ }

public InscriptionStatusException(int code, string message) : base(code, message){}

public InscriptionStatusException(int code, string message, Exception innerException) : base(code, message,
innerException){}
}
}
15 changes: 15 additions & 0 deletions Transbank/Exceptions/MallTransactionAuthorizeException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;

namespace Transbank.Exceptions
{
public class MallTransactionAuthorizeException : TransbankException
{
public MallTransactionAuthorizeException(string message) : base(-1, message) { }

public MallTransactionAuthorizeException(int code, string message) : base(code, message) { }

public MallTransactionAuthorizeException(int code, string message, Exception innerException)
: base(code, message, innerException) { }

}
}
14 changes: 14 additions & 0 deletions Transbank/Exceptions/MallTransactionCommitException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace Transbank.Exceptions
{
public class MallTransactionCommitException : TransbankException
{
public MallTransactionCommitException(string message) : base(-1, message) { }

public MallTransactionCommitException(int code, string message) : base(code, message) { }

public MallTransactionCommitException(int code, string message, Exception innerException)
: base(code, message, innerException) { }
}
}
14 changes: 14 additions & 0 deletions Transbank/Exceptions/MallTransactionCreateException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace Transbank.Exceptions
{
public class MallTransactionCreateException : TransbankException
{
public MallTransactionCreateException(string message) : base(-1, message) { }

public MallTransactionCreateException(int code, string message) : base(code, message) { }

public MallTransactionCreateException(int code, string message, Exception innerException)
: base(code, message, innerException) { }
}
}
14 changes: 14 additions & 0 deletions Transbank/Exceptions/MallTransactionInstallmentsExceptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace Transbank.Exceptions
{
public class MallTransactionInstallmentsExceptions : TransbankException
{
public MallTransactionInstallmentsExceptions(string message) : base(-1, message) { }

public MallTransactionInstallmentsExceptions(int code, string message) : base(code, message) { }

public MallTransactionInstallmentsExceptions(int code, string message, Exception innerException)
: base(code, message, innerException) { }
}
}
14 changes: 14 additions & 0 deletions Transbank/Exceptions/MallTransactionRefundException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace Transbank.Exceptions
{
public class MallTransactionRefundException : TransbankException
{
public MallTransactionRefundException(string message) : base(-1, message) { }

public MallTransactionRefundException(int code, string message) : base(code, message) { }

public MallTransactionRefundException(int code, string message, Exception innerException)
: base(code, message, innerException) { }
}
}
Loading

0 comments on commit cd93e62

Please sign in to comment.