Skip to content

Commit 6405647

Browse files
committed
Entender o Gateway de pagamento
1 parent 8a0d188 commit 6405647

File tree

10 files changed

+269
-46
lines changed

10 files changed

+269
-46
lines changed

JS.Enterprise.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JSE.Pedidos.Infra", "src\se
6161
EndProject
6262
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "JSE.Clientes.API", "src\services\JSE.Clientes.API\JSE.Clientes.API.csproj", "{AC75EECB-1BC5-4653-AB42-E8148D5B05D9}"
6363
EndProject
64+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "JSE.Pagamentos.NerdsPag", "src\services\JSE.Pagamentos.NerdsPag\JSE.Pagamentos.NerdsPag.csproj", "{3D83A7B3-B140-43CF-885C-660C043B498A}"
65+
EndProject
6466
Global
6567
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6668
Debug|Any CPU = Debug|Any CPU
@@ -119,6 +121,10 @@ Global
119121
{AC75EECB-1BC5-4653-AB42-E8148D5B05D9}.Debug|Any CPU.Build.0 = Debug|Any CPU
120122
{AC75EECB-1BC5-4653-AB42-E8148D5B05D9}.Release|Any CPU.ActiveCfg = Release|Any CPU
121123
{AC75EECB-1BC5-4653-AB42-E8148D5B05D9}.Release|Any CPU.Build.0 = Release|Any CPU
124+
{3D83A7B3-B140-43CF-885C-660C043B498A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
125+
{3D83A7B3-B140-43CF-885C-660C043B498A}.Debug|Any CPU.Build.0 = Debug|Any CPU
126+
{3D83A7B3-B140-43CF-885C-660C043B498A}.Release|Any CPU.ActiveCfg = Release|Any CPU
127+
{3D83A7B3-B140-43CF-885C-660C043B498A}.Release|Any CPU.Build.0 = Release|Any CPU
122128
EndGlobalSection
123129
GlobalSection(SolutionProperties) = preSolution
124130
HideSolutionNode = FALSE
@@ -152,6 +158,7 @@ Global
152158
{42556C73-9ABF-45D0-911F-16E74D2919EB} = {856468DF-3B1E-4BAB-AD97-2D72FB744435}
153159
{2BB79FC1-2E45-486B-AC1E-F680B3C54BCD} = {856468DF-3B1E-4BAB-AD97-2D72FB744435}
154160
{AC75EECB-1BC5-4653-AB42-E8148D5B05D9} = {B89AEBFA-BA86-4AC4-A0B4-117C29036158}
161+
{3D83A7B3-B140-43CF-885C-660C043B498A} = {68C69BFC-835D-4BD4-899B-56E0349A1895}
155162
EndGlobalSection
156163
GlobalSection(ExtensibilityGlobals) = postSolution
157164
SolutionGuid = {F19279BD-86B0-48AF-9FA4-26C84BD715EB}

src/services/JSE.Pagamento.API/Controllers/WeatherForecastController.cs

Lines changed: 0 additions & 33 deletions
This file was deleted.

src/services/JSE.Pagamento.API/JSE.Pagamento.API.csproj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,15 @@
1313
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
1414
</ItemGroup>
1515

16+
<ItemGroup>
17+
<Folder Include="Controllers\" />
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<ProjectReference Include="..\..\building blocks\JSE.Core\JSE.Core.csproj" />
22+
<ProjectReference Include="..\..\building blocks\JSE.MessageBus\JSE.MessageBus.csproj" />
23+
<ProjectReference Include="..\..\building blocks\JSE.WebAPI.Core\JSE.WebAPI.Core.csproj" />
24+
<ProjectReference Include="..\JSE.Pagamentos.NerdsPag\JSE.Pagamentos.NerdsPag.csproj" />
25+
</ItemGroup>
26+
1627
</Project>

src/services/JSE.Pagamento.API/WeatherForecast.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Security.Cryptography;
2+
using System.Text;
3+
4+
namespace JSE.Pagamentos.NerdsPag
5+
{
6+
public class CardHash
7+
{
8+
public CardHash(NerdsPagService nerdsPagService)
9+
{
10+
NerdsPagService = nerdsPagService;
11+
}
12+
13+
private readonly NerdsPagService NerdsPagService;
14+
15+
public string CardHolderName { get; set; }
16+
public string CardNumber { get; set; }
17+
public string CardExpirationDate { get; set; }
18+
public string CardCvv { get; set; }
19+
20+
public string Generate()
21+
{
22+
using var aesAlg = Aes.Create();
23+
24+
aesAlg.IV = Encoding.Default.GetBytes(NerdsPagService.EncryptionKey);
25+
aesAlg.Key = Encoding.Default.GetBytes(NerdsPagService.ApiKey);
26+
27+
var encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);
28+
29+
using var msEncrypt = new MemoryStream();
30+
using var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
31+
32+
using (var swEncrypt = new StreamWriter(csEncrypt))
33+
{
34+
swEncrypt.Write(CardHolderName + CardNumber + CardExpirationDate + CardCvv);
35+
}
36+
37+
return Encoding.ASCII.GetString(msEncrypt.ToArray());
38+
}
39+
}
40+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace JSE.Pagamentos.NerdsPag
2+
{
3+
public class NerdsPagService
4+
{
5+
public readonly string ApiKey;
6+
public readonly string EncryptionKey;
7+
8+
public NerdsPagService(string apiKey, string encryptionKey)
9+
{
10+
ApiKey = apiKey;
11+
EncryptionKey = encryptionKey;
12+
}
13+
}
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace JSE.Pagamentos.NerdsPag
2+
{
3+
public enum PaymentMethod
4+
{
5+
CreditCard = 1,
6+
Billet
7+
}
8+
}
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
namespace JSE.Pagamentos.NerdsPag
2+
{
3+
public class Transaction
4+
{
5+
public Transaction(NerdsPagService nerdsPagService)
6+
{
7+
NerdsPagService = nerdsPagService;
8+
}
9+
10+
protected Transaction(){}
11+
12+
private readonly NerdsPagService NerdsPagService;
13+
14+
protected string Endpoint { get; set; }
15+
16+
public int SubscriptionId { get; set; }
17+
18+
public TransactionStatus Status { get; set; }
19+
20+
public int AuthorizationAmount { get; set; }
21+
22+
public int PaidAmount { get; set; }
23+
24+
public int RefundedAmount { get; set; }
25+
26+
public string CardHash { get; set; }
27+
28+
public string CardNumber { get; set; }
29+
30+
public string CardExpirationDate { get; set; }
31+
32+
public string StatusReason { get; set; }
33+
34+
public string AcquirerResponseCode { get; set; }
35+
36+
public string AcquirerName { get; set; }
37+
38+
public string AuthorizationCode { get; set; }
39+
40+
public string SoftDescriptor { get; set; }
41+
42+
public string RefuseReason { get; set; }
43+
44+
public string Tid { get; set; }
45+
46+
public string Nsu { get; set; }
47+
48+
public decimal Amount { get; set; }
49+
50+
public int? Installments { get; set; }
51+
52+
public decimal Cost { get; set; }
53+
54+
public string CardHolderName { get; set; }
55+
56+
public string CardCvv { get; set; }
57+
58+
public string CardLastDigits { get; set; }
59+
60+
public string CardFirstDigits { get; set; }
61+
62+
public string CardBrand { get; set; }
63+
64+
public string CardEmvResponse { get; set; }
65+
66+
public string PostbackUrl { get; set; }
67+
68+
public PaymentMethod PaymentMethod { get; set; }
69+
70+
public float? AntifraudScore { get; set; }
71+
72+
public string BilletUrl { get; set; }
73+
74+
public string BilletInstructions { get; set; }
75+
76+
public DateTime? BilletExpirationDate { get; set; }
77+
78+
public string BilletBarcode { get; set; }
79+
80+
public string Referer { get; set; }
81+
82+
public string IP { get; set; }
83+
84+
public bool? ShouldCapture { get; set; }
85+
86+
public bool? Async { get; set; }
87+
88+
public string LocalTime { get; set; }
89+
90+
public DateTime TransactionDate { get; set; }
91+
92+
public Task<Transaction> AuthorizeCardTransaction()
93+
{
94+
var success = new Random().Next(2) == 0;
95+
Transaction transaction;
96+
97+
if (success)
98+
{
99+
transaction = new Transaction
100+
{
101+
AuthorizationCode = GetGenericCode(),
102+
CardBrand = "MasterCard",
103+
TransactionDate = DateTime.Now,
104+
Cost = Amount * (decimal)0.03,
105+
Amount = Amount,
106+
Status = TransactionStatus.Authorized,
107+
Tid = GetGenericCode(),
108+
Nsu = GetGenericCode()
109+
};
110+
111+
return Task.FromResult(transaction);
112+
}
113+
114+
transaction = new Transaction
115+
{
116+
AuthorizationCode = "",
117+
CardBrand = "",
118+
TransactionDate = DateTime.Now,
119+
Cost = 0,
120+
Amount = 0,
121+
Status = TransactionStatus.Refused,
122+
Tid = "",
123+
Nsu = ""
124+
};
125+
126+
return Task.FromResult(transaction);
127+
}
128+
129+
public Task<Transaction> CaptureCardTransaction()
130+
{
131+
var transaction = new Transaction
132+
{
133+
AuthorizationCode = GetGenericCode(),
134+
CardBrand = CardBrand,
135+
TransactionDate = DateTime.Now,
136+
Cost = 0,
137+
Amount = Amount,
138+
Status = TransactionStatus.Paid,
139+
Tid = Tid,
140+
Nsu = Nsu
141+
};
142+
143+
return Task.FromResult(transaction);
144+
}
145+
146+
public Task<Transaction> CancelAuthorization()
147+
{
148+
var transaction = new Transaction
149+
{
150+
AuthorizationCode = "",
151+
CardBrand = CardBrand,
152+
TransactionDate = DateTime.Now,
153+
Cost = 0,
154+
Amount = Amount,
155+
Status = TransactionStatus.Cancelled,
156+
Tid = Tid,
157+
Nsu = Nsu
158+
};
159+
160+
return Task.FromResult(transaction);
161+
}
162+
163+
private string GetGenericCode()
164+
{
165+
return new string(Enumerable.Repeat("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 10)
166+
.Select(s => s[new Random().Next(s.Length)]).ToArray());
167+
}
168+
}
169+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace JSE.Pagamentos.NerdsPag
2+
{
3+
public enum TransactionStatus
4+
{
5+
Authorized = 1,
6+
Paid,
7+
Refused,
8+
Chargedback,
9+
Cancelled
10+
}
11+
}

0 commit comments

Comments
 (0)