Skip to content

Commit

Permalink
Entender o Gateway de pagamento
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffreysSharp committed Nov 14, 2024
1 parent 8a0d188 commit 6405647
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 46 deletions.
7 changes: 7 additions & 0 deletions JS.Enterprise.sln
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JSE.Pedidos.Infra", "src\se
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JSE.Clientes.API", "src\services\JSE.Clientes.API\JSE.Clientes.API.csproj", "{AC75EECB-1BC5-4653-AB42-E8148D5B05D9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JSE.Pagamentos.NerdsPag", "src\services\JSE.Pagamentos.NerdsPag\JSE.Pagamentos.NerdsPag.csproj", "{3D83A7B3-B140-43CF-885C-660C043B498A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -119,6 +121,10 @@ Global
{AC75EECB-1BC5-4653-AB42-E8148D5B05D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AC75EECB-1BC5-4653-AB42-E8148D5B05D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AC75EECB-1BC5-4653-AB42-E8148D5B05D9}.Release|Any CPU.Build.0 = Release|Any CPU
{3D83A7B3-B140-43CF-885C-660C043B498A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3D83A7B3-B140-43CF-885C-660C043B498A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3D83A7B3-B140-43CF-885C-660C043B498A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3D83A7B3-B140-43CF-885C-660C043B498A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -152,6 +158,7 @@ Global
{42556C73-9ABF-45D0-911F-16E74D2919EB} = {856468DF-3B1E-4BAB-AD97-2D72FB744435}
{2BB79FC1-2E45-486B-AC1E-F680B3C54BCD} = {856468DF-3B1E-4BAB-AD97-2D72FB744435}
{AC75EECB-1BC5-4653-AB42-E8148D5B05D9} = {B89AEBFA-BA86-4AC4-A0B4-117C29036158}
{3D83A7B3-B140-43CF-885C-660C043B498A} = {68C69BFC-835D-4BD4-899B-56E0349A1895}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {F19279BD-86B0-48AF-9FA4-26C84BD715EB}
Expand Down

This file was deleted.

11 changes: 11 additions & 0 deletions src/services/JSE.Pagamento.API/JSE.Pagamento.API.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,15 @@
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="Controllers\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\building blocks\JSE.Core\JSE.Core.csproj" />
<ProjectReference Include="..\..\building blocks\JSE.MessageBus\JSE.MessageBus.csproj" />
<ProjectReference Include="..\..\building blocks\JSE.WebAPI.Core\JSE.WebAPI.Core.csproj" />
<ProjectReference Include="..\JSE.Pagamentos.NerdsPag\JSE.Pagamentos.NerdsPag.csproj" />
</ItemGroup>

</Project>
13 changes: 0 additions & 13 deletions src/services/JSE.Pagamento.API/WeatherForecast.cs

This file was deleted.

40 changes: 40 additions & 0 deletions src/services/JSE.Pagamentos.NerdsPag/Card.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.Security.Cryptography;
using System.Text;

namespace JSE.Pagamentos.NerdsPag
{
public class CardHash
{
public CardHash(NerdsPagService nerdsPagService)
{
NerdsPagService = nerdsPagService;
}

private readonly NerdsPagService NerdsPagService;

public string CardHolderName { get; set; }
public string CardNumber { get; set; }
public string CardExpirationDate { get; set; }
public string CardCvv { get; set; }

public string Generate()
{
using var aesAlg = Aes.Create();

aesAlg.IV = Encoding.Default.GetBytes(NerdsPagService.EncryptionKey);
aesAlg.Key = Encoding.Default.GetBytes(NerdsPagService.ApiKey);

var encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

using var msEncrypt = new MemoryStream();
using var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);

using (var swEncrypt = new StreamWriter(csEncrypt))
{
swEncrypt.Write(CardHolderName + CardNumber + CardExpirationDate + CardCvv);
}

return Encoding.ASCII.GetString(msEncrypt.ToArray());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>
14 changes: 14 additions & 0 deletions src/services/JSE.Pagamentos.NerdsPag/NerdsPagService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace JSE.Pagamentos.NerdsPag
{
public class NerdsPagService
{
public readonly string ApiKey;
public readonly string EncryptionKey;

public NerdsPagService(string apiKey, string encryptionKey)
{
ApiKey = apiKey;
EncryptionKey = encryptionKey;
}
}
}
8 changes: 8 additions & 0 deletions src/services/JSE.Pagamentos.NerdsPag/PaymentMethod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace JSE.Pagamentos.NerdsPag
{
public enum PaymentMethod
{
CreditCard = 1,
Billet
}
}
169 changes: 169 additions & 0 deletions src/services/JSE.Pagamentos.NerdsPag/Transaction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
namespace JSE.Pagamentos.NerdsPag
{
public class Transaction
{
public Transaction(NerdsPagService nerdsPagService)
{
NerdsPagService = nerdsPagService;
}

protected Transaction(){}

private readonly NerdsPagService NerdsPagService;

protected string Endpoint { get; set; }

public int SubscriptionId { get; set; }

public TransactionStatus Status { get; set; }

public int AuthorizationAmount { get; set; }

public int PaidAmount { get; set; }

public int RefundedAmount { get; set; }

public string CardHash { get; set; }

public string CardNumber { get; set; }

public string CardExpirationDate { get; set; }

public string StatusReason { get; set; }

public string AcquirerResponseCode { get; set; }

public string AcquirerName { get; set; }

public string AuthorizationCode { get; set; }

public string SoftDescriptor { get; set; }

public string RefuseReason { get; set; }

public string Tid { get; set; }

public string Nsu { get; set; }

public decimal Amount { get; set; }

public int? Installments { get; set; }

public decimal Cost { get; set; }

public string CardHolderName { get; set; }

public string CardCvv { get; set; }

public string CardLastDigits { get; set; }

public string CardFirstDigits { get; set; }

public string CardBrand { get; set; }

public string CardEmvResponse { get; set; }

public string PostbackUrl { get; set; }

public PaymentMethod PaymentMethod { get; set; }

public float? AntifraudScore { get; set; }

public string BilletUrl { get; set; }

public string BilletInstructions { get; set; }

public DateTime? BilletExpirationDate { get; set; }

public string BilletBarcode { get; set; }

public string Referer { get; set; }

public string IP { get; set; }

public bool? ShouldCapture { get; set; }

public bool? Async { get; set; }

public string LocalTime { get; set; }

public DateTime TransactionDate { get; set; }

public Task<Transaction> AuthorizeCardTransaction()
{
var success = new Random().Next(2) == 0;
Transaction transaction;

if (success)
{
transaction = new Transaction
{
AuthorizationCode = GetGenericCode(),
CardBrand = "MasterCard",
TransactionDate = DateTime.Now,
Cost = Amount * (decimal)0.03,
Amount = Amount,
Status = TransactionStatus.Authorized,
Tid = GetGenericCode(),
Nsu = GetGenericCode()
};

return Task.FromResult(transaction);
}

transaction = new Transaction
{
AuthorizationCode = "",
CardBrand = "",
TransactionDate = DateTime.Now,
Cost = 0,
Amount = 0,
Status = TransactionStatus.Refused,
Tid = "",
Nsu = ""
};

return Task.FromResult(transaction);
}

public Task<Transaction> CaptureCardTransaction()
{
var transaction = new Transaction
{
AuthorizationCode = GetGenericCode(),
CardBrand = CardBrand,
TransactionDate = DateTime.Now,
Cost = 0,
Amount = Amount,
Status = TransactionStatus.Paid,
Tid = Tid,
Nsu = Nsu
};

return Task.FromResult(transaction);
}

public Task<Transaction> CancelAuthorization()
{
var transaction = new Transaction
{
AuthorizationCode = "",
CardBrand = CardBrand,
TransactionDate = DateTime.Now,
Cost = 0,
Amount = Amount,
Status = TransactionStatus.Cancelled,
Tid = Tid,
Nsu = Nsu
};

return Task.FromResult(transaction);
}

private string GetGenericCode()
{
return new string(Enumerable.Repeat("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 10)
.Select(s => s[new Random().Next(s.Length)]).ToArray());
}
}
}
11 changes: 11 additions & 0 deletions src/services/JSE.Pagamentos.NerdsPag/TransactionStatus.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace JSE.Pagamentos.NerdsPag
{
public enum TransactionStatus
{
Authorized = 1,
Paid,
Refused,
Chargedback,
Cancelled
}
}

0 comments on commit 6405647

Please sign in to comment.