-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from TransbankDevelopers/feat/oneclick-fullTr…
…ansaction Add Oneclick Mall and Full Transaction REST
- Loading branch information
Showing
69 changed files
with
2,041 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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."); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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){} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { } | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
Transbank/Exceptions/MallTransactionInstallmentsExceptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { } | ||
} | ||
} |
Oops, something went wrong.