Skip to content

Commit

Permalink
Merge pull request #119 from TransbankDevelopers/feat/add-webpay-rest…
Browse files Browse the repository at this point in the history
…-support

Add REST support
  • Loading branch information
Alfredo Fiebig authored Oct 20, 2020
2 parents 98b3cfe + f701a29 commit 76260c6
Show file tree
Hide file tree
Showing 71 changed files with 2,600 additions and 19 deletions.
53 changes: 53 additions & 0 deletions Transbank/PatpassRest/Common/Detail.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using Newtonsoft.Json;

namespace Transbank.Patpass.Common
{
internal class Detail
{
[JsonProperty("service_id")]
internal string ServiceId { get; }

[JsonProperty("card_holder_id")]
internal string CardHolderId { get; }

[JsonProperty("card_holder_name")]
internal string CardHolderName { get; }

[JsonProperty("card_holder_last_name1")]
internal string CardHolderLastName1 { get; }

[JsonProperty("card_holder_last_name2")]
internal string CardHolderLastName2 { get; }

[JsonProperty("card_holder_mail")]
internal string CardHolderMail { get; }

[JsonProperty("cellphone_number")]
internal string CellphoneNumber { get; }

[JsonProperty("expiration_date")]
internal string ExpirationDate { get; }

[JsonProperty("commerce_mail")]
internal string CommerceMail { get; }

[JsonProperty("uf_flag")]
internal bool UfFlag { get; }

public Detail(string serviceId, string cardHolderId, string cardHolderName,
string cardHolderLastName1, string cardHolderLastName2, string cardHolderMail,
string cellphoneNumber, string expirationDate, string commerceMail, bool ufFlag)
{
ServiceId = serviceId;
CardHolderId = cardHolderId;
CardHolderName = cardHolderName;
CardHolderLastName1 = cardHolderLastName1;
CardHolderLastName2 = cardHolderLastName2;
CardHolderMail = cardHolderMail;
CellphoneNumber = cellphoneNumber;
ExpirationDate = expirationDate;
CommerceMail = commerceMail;
UfFlag = ufFlag;
}
}
}
22 changes: 22 additions & 0 deletions Transbank/PatpassRest/Common/PatpassByWebpayIntegrationType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using Transbank.Common;

namespace Transbank.Patpass.Common
{
public class PatpassByWebpayIntegrationType : IIntegrationType
{
public string Key { get; private set; }
public string ApiBase { get; private set; }

private PatpassByWebpayIntegrationType(string key, string apiBase)
{
Key = key;
ApiBase = apiBase;
}

public static readonly PatpassByWebpayIntegrationType Live =
new PatpassByWebpayIntegrationType("LIVE", "https://webpay3g.transbank.cl");
public static readonly PatpassByWebpayIntegrationType Test =
new PatpassByWebpayIntegrationType("TEST", "https://webpay3gint.transbank.cl");
}
}
22 changes: 22 additions & 0 deletions Transbank/PatpassRest/Common/PatpassComercioIntegrationType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using Transbank.Common;

namespace Transbank.Patpass.Common
{
public class PatpassComercioIntegrationType : IIntegrationType
{
public string Key { get; private set; }
public string ApiBase { get; private set; }

private PatpassComercioIntegrationType(string key, string apiBase)
{
Key = key;
ApiBase = apiBase;
}

public static readonly PatpassComercioIntegrationType Live =
new PatpassComercioIntegrationType("LIVE", "https://www.pagoautomaticocontarjetas.cl/");
public static readonly PatpassComercioIntegrationType Test =
new PatpassComercioIntegrationType("TEST", "https://pagoautomaticocontarjetasint.transbank.cl/");
}
}
12 changes: 12 additions & 0 deletions Transbank/PatpassRest/PatpassByWebpay/Requests/CommitRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Net.Http;
using Transbank.Common;

namespace Transbank.Patpass.PatpassByWebpay.Requests
{
internal class CommitRequest : BaseRequest
{
internal CommitRequest(string token)
: base($"/rswebpaytransaction/api/webpay/v1.0/transactions/{token}", HttpMethod.Put) {}
}
}
40 changes: 40 additions & 0 deletions Transbank/PatpassRest/PatpassByWebpay/Requests/CreateRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Newtonsoft.Json;
using Transbank.Common;
using System.Net.Http;
using Transbank.Patpass.Common;

namespace Transbank.Patpass.PatpassByWebpay.Requests
{
internal class CreateRequest : BaseRequest
{
[JsonProperty("buy_order")]
public string BuyOrder { get; set; }

[JsonProperty("session_id")]
public string SessionId { get; set; }

[JsonProperty("amount")]
public decimal Amount { get; set; }

[JsonProperty("return_url")]
public string ReturnUrl { get; set; }

[JsonProperty("wpm_detail")]
public Detail Details { get; set; }

internal CreateRequest(string buyOrder, string sessionId, decimal amount, string returnUrl, string serviceId, string cardHolderId,
string cardHolderName, string cardHolderLastName1, string cardHolderLastName2, string cardHolderMail, string cellphoneNumber,
string expirationDate, string commerceMail, bool ufFlag)
: base("/rswebpaytransaction/api/webpay/v1.0/transactions", HttpMethod.Post)
{
BuyOrder = buyOrder;
SessionId = sessionId;
Amount = amount;
ReturnUrl = returnUrl;
Details = new Detail(serviceId, cardHolderId, cardHolderName,
cardHolderLastName1, cardHolderLastName2, cardHolderMail,
cellphoneNumber, expirationDate, commerceMail, ufFlag
);
}
}
}
12 changes: 12 additions & 0 deletions Transbank/PatpassRest/PatpassByWebpay/Requests/StatusRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using Transbank.Common;
using System.Net.Http;

namespace Transbank.Patpass.PatpassByWebpay.Requests
{
public class StatusRequest : BaseRequest
{
internal StatusRequest(string token)
: base($"/rswebpaytransaction/api/webpay/v1.0/transactions/{token}", HttpMethod.Get) { }
}
}
52 changes: 52 additions & 0 deletions Transbank/PatpassRest/PatpassByWebpay/Responses/CommitResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Newtonsoft.Json;
using Transbank.Webpay.Common;

namespace Transbank.Patpass.PatpassByWebpay.Responses
{
public class CommitResponse
{
[JsonProperty("vci")]
public string Vci { get; set; }
[JsonProperty("amount")]
public decimal Amount { get; set; }
[JsonProperty("status")]
public string Status { get; set; }
[JsonProperty("buy_order")]
public string BuyOrder { get; set; }
[JsonProperty("session_id")]
public string SessionId { get; set; }
[JsonProperty("card_detail")]
public CardDetail CardDetail { get; set; }
[JsonProperty("accounting_date")]
public string AccountingDate { get; set; }
[JsonProperty("transaction_date")]
public DateTime TransactionDate { get; set; }
[JsonProperty("authorization_code")]
public string AuthorizationCode { get; set; }
[JsonProperty("payment_type_code")]
public string PaymentTypeCode { get; set; }
[JsonProperty("response_code")]
public int ResponseCode { get; set; }
[JsonProperty("installments_amount")]
public int InstallmentsAmount { get; set; }
[JsonProperty("installments_number")]
public int InstallmentsNumber { get; set; }
[JsonProperty("balance")]
public decimal Balance { get; set; }

public override string ToString()
{
var properties = new List<string>();
foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(this))
{
string name = descriptor.Name;
object value = descriptor.GetValue(this);
properties.Add($"{name}={value}");
}
return String.Join(", ", properties);
}
}
}
18 changes: 18 additions & 0 deletions Transbank/PatpassRest/PatpassByWebpay/Responses/CreateResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Newtonsoft.Json;

namespace Transbank.Patpass.PatpassByWebpay.Responses
{
public class CreateResponse
{
[JsonProperty("token")]
public string Token { get; private set; }

[JsonProperty("url")]
public string Url { get; private set; }

public override string ToString()
{
return $"Token={Token}, Url={Url}";
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using System;
namespace Transbank.Patpass.PatpassByWebpay.Responses
{
public class StatusResponse : CommitResponse { }
}
116 changes: 116 additions & 0 deletions Transbank/PatpassRest/PatpassByWebpay/Transaction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
using System;
using Newtonsoft.Json;
using Transbank.Common;
using Transbank.Exceptions;
using Transbank.Patpass.Common;
using Transbank.Patpass.PatpassByWebpay.Requests;
using Transbank.Patpass.PatpassByWebpay.Responses;
using RequestService = Transbank.Common.RequestService;

namespace Transbank.Patpass.PatpassByWebpay
{
public static class Transaction
{
private static string _commerceCode = "597055555550";
private static string _apiKey = "579B532A7440BB0C9079DED94D31EA1615BACEB56610332264630D42D0A36B1C";
private static PatpassByWebpayIntegrationType _integrationType = PatpassByWebpayIntegrationType.Test;

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 static RequestServiceHeaders Headers
{
get => _headers;
set => _headers = value ?? throw new ArgumentNullException(
nameof(value), "Integration type can't be null."
);
}
public static string CommerceCode
{
get => _commerceCode;
set => _commerceCode = value ?? throw new ArgumentNullException(
nameof(value), "Commerce code can't be null."
);
}

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

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



public static Options DefaultOptions()
{
return new Options(CommerceCode, ApiKey, IntegrationType, Headers);
}

public static CreateResponse Create(string buyOrder, string sessionId, decimal amount, string returnUrl, string serviceId, string cardHolderId,
string cardHolderName, string cardHolderLastName1, string cardHolderLastName2, string cardHolderMail, string cellphoneNumber,
string expirationDate, string commerceMail, bool ufFlag)
{
return Create(buyOrder, sessionId, amount, returnUrl, serviceId, cardHolderId,
cardHolderName, cardHolderLastName1, cardHolderLastName2, cardHolderMail, cellphoneNumber,
expirationDate, commerceMail, ufFlag, DefaultOptions());
}

public static CreateResponse Create(string buyOrder, string sessionId, decimal amount, string returnUrl, string serviceId, string cardHolderId,
string cardHolderName, string cardHolderLastName1, string cardHolderLastName2, string cardHolderMail, string cellphoneNumber,
string expirationDate, string commerceMail, bool ufFlag, Options options)
{
var createRequest = new CreateRequest(buyOrder, sessionId, amount, returnUrl, serviceId, cardHolderId,
cardHolderName, cardHolderLastName1, cardHolderLastName2, cardHolderMail, cellphoneNumber,
expirationDate, commerceMail, ufFlag);
var response = RequestService.Perform<TransactionCreateException>(createRequest, options);

return JsonConvert.DeserializeObject<CreateResponse>(response);
}

public static CommitResponse Commit(string token)
{
return Commit(token, DefaultOptions());
}

public static CommitResponse Commit(string token, Options options)
{
return ExceptionHandler.Perform<CommitResponse, TransactionCommitException>(() =>
{
var commitRequest = new CommitRequest(token);
var response = RequestService.Perform<TransactionCommitException>(
commitRequest, options);

return JsonConvert.DeserializeObject<CommitResponse>(response);
});
}

public static StatusResponse Status(string token)
{
return Status(token, DefaultOptions());
}

public static StatusResponse Status(string token, Options options)
{
return ExceptionHandler.Perform<StatusResponse, TransactionStatusException>(() =>
{
var statusRequest = new StatusRequest(token);
var response = RequestService.Perform<TransactionStatusException>(
statusRequest, options);

return JsonConvert.DeserializeObject<StatusResponse>(response);
});
}
}
}
Loading

0 comments on commit 76260c6

Please sign in to comment.