Skip to content

Commit

Permalink
Merge pull request #65 from Brunobento1990/develop
Browse files Browse the repository at this point in the history
adicionado configuração webhook mercado pago
  • Loading branch information
Brunobento1990 authored Feb 7, 2025
2 parents d831e03 + 0120715 commit 3f072ee
Show file tree
Hide file tree
Showing 10 changed files with 1,498 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ public class ConfiguracaoDePagamentoCreateOrUpdate
{
public string PublicKey { get; set; } = string.Empty;
public string AccessToken { get; set; } = string.Empty;
public string? UrlWebHook { get; set; }
public bool CobrarCpf { get; set; }
public bool CobrarCnpj { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public class ConfiguracaoDePagamentoViewModel
{
public string PublicKey { get; set; } = string.Empty;
public string AccessToken { get; set; } = string.Empty;
public string? UrlWebHook { get; set; }
public bool CobrarCpf { get; set; }
public bool CobrarCnpj { get; set; }

Expand All @@ -17,7 +18,8 @@ public static explicit operator ConfiguracaoDePagamentoViewModel(ConfiguracaoDeP
AccessToken = Criptografia.Decrypt(configuracaoDePagamento.AccessToken),
CobrarCnpj = configuracaoDePagamento.CobrarCnpj,
CobrarCpf = configuracaoDePagamento.CobrarCpf,
PublicKey = Criptografia.Decrypt(configuracaoDePagamento.PublicKey)
PublicKey = Criptografia.Decrypt(configuracaoDePagamento.PublicKey),
UrlWebHook = configuracaoDePagamento.UrlWebHook
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public async Task<ConfiguracaoDePagamentoViewModel> CreateOrUpdateAsync(Configur
publicKey: Criptografia.Encrypt(configuracaoDePagamentoCreateOrUpdate.PublicKey),
accessToken: Criptografia.Encrypt(configuracaoDePagamentoCreateOrUpdate.AccessToken),
cobrarCpf: configuracaoDePagamentoCreateOrUpdate.CobrarCpf,
cobrarCnpj: configuracaoDePagamentoCreateOrUpdate.CobrarCnpj);
cobrarCnpj: configuracaoDePagamentoCreateOrUpdate.CobrarCnpj,
urlWebHook: configuracaoDePagamentoCreateOrUpdate.UrlWebHook);

await _configuracaoDePagamentoRepository.AddAsync(configuracao);
return (ConfiguracaoDePagamentoViewModel)configuracao;
Expand All @@ -71,7 +72,8 @@ public async Task<ConfiguracaoDePagamentoViewModel> CreateOrUpdateAsync(Configur
publicKey: Criptografia.Encrypt(configuracaoDePagamentoCreateOrUpdate.PublicKey),
accessToken: Criptografia.Encrypt(configuracaoDePagamentoCreateOrUpdate.AccessToken),
cobrarCpf: configuracaoDePagamentoCreateOrUpdate.CobrarCpf,
cobrarCnpj: configuracaoDePagamentoCreateOrUpdate.CobrarCnpj);
cobrarCnpj: configuracaoDePagamentoCreateOrUpdate.CobrarCnpj,
urlWebHook: configuracaoDePagamentoCreateOrUpdate.UrlWebHook);

await _configuracaoDePagamentoRepository.UpdateAsync(configuracao);

Expand Down
15 changes: 10 additions & 5 deletions OpenAdm.Application/Services/GerarPixPedidoService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,17 @@ public async Task<PagamentoViewModel> GerarPixAsync(GerarPixParcelaDto gerarPixP
var configuracaoPagamento = await _configuracaoDePagamentoService.GetAsync()
?? throw new ExceptionApi("Não há configuração de pagamento para o mercado pago!", enviarErroDiscord: true);

if (string.IsNullOrWhiteSpace(configuracaoPagamento.UrlWebHook))
{
throw new ExceptionApi("Não há uma url de web hook configurada, verifique as configurações de pagamento");
}

if (fatura == null || fatura.Pedido == null)
{
var pedido = await _pedidoRepository.GetPedidoByIdAsync(gerarPixParcelaDto.PedidoId)
?? throw new ExceptionApi("Nãi foi possível localizar o pedido");
?? throw new ExceptionApi("Não foi possível localizar o pedido");

var mercadoPagoRequest = GerarBodyMercadoPado(pedido, gerarPixParcelaDto.Valor);
var mercadoPagoRequest = GerarBodyMercadoPado(pedido, gerarPixParcelaDto.Valor, configuracaoPagamento.UrlWebHook);
var idempontencyKey = Guid.NewGuid();
var resultPix = await _httpClientMercadoPago
.PostAsync(mercadoPagoRequest, configuracaoPagamento.AccessToken, idempontencyKey.ToString());
Expand Down Expand Up @@ -113,7 +118,7 @@ public async Task<PagamentoViewModel> GerarPixAsync(GerarPixParcelaDto gerarPixP
_faturaRepository.ExcluirParcelasAsync(parcelasExcluir);
}

var mercadoPagoRequest1 = GerarBodyMercadoPado(fatura.Pedido, gerarPixParcelaDto.Valor);
var mercadoPagoRequest1 = GerarBodyMercadoPado(fatura.Pedido, gerarPixParcelaDto.Valor, configuracaoPagamento.UrlWebHook);
var idempontencyKey1 = Guid.NewGuid();
var resultPix1 = await _httpClientMercadoPago
.PostAsync(mercadoPagoRequest1, configuracaoPagamento.AccessToken, idempontencyKey1.ToString());
Expand Down Expand Up @@ -157,14 +162,14 @@ await _parcelaRepository.AddAsync(new Parcela(
};
}

private static MercadoPagoRequest GerarBodyMercadoPado(Pedido pedido, decimal valor)
private static MercadoPagoRequest GerarBodyMercadoPado(Pedido pedido, decimal valor, string urlWebHook)
{
return new MercadoPagoRequest()
{
Description = $"Pedido {pedido.Numero}",
Transaction_amount = valor,
External_reference = pedido.Id.ToString(),
Notification_url = $"https://api.hml.iscaslune.com.br/api/pagamento/notificar",
Notification_url = urlWebHook,
Payer = new()
{
Email = pedido.Usuario.Email,
Expand Down
8 changes: 6 additions & 2 deletions OpenAdm.Domain/Entities/ConfiguracaoDePagamento.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,29 @@ public ConfiguracaoDePagamento(
string publicKey,
string accessToken,
bool cobrarCpf,
bool cobrarCnpj)
bool cobrarCnpj,
string? urlWebHook)
: base(id, dataDeCriacao, dataDeAtualizacao, numero)
{
PublicKey = publicKey;
AccessToken = accessToken;
CobrarCpf = cobrarCpf;
CobrarCnpj = cobrarCnpj;
UrlWebHook = urlWebHook;
}

public void Update(string publicKey, string accessToken, bool cobrarCpf, bool cobrarCnpj)
public void Update(string publicKey, string accessToken, bool cobrarCpf, bool cobrarCnpj, string? urlWebHook)
{
CobrarCnpj = cobrarCnpj;
CobrarCpf = cobrarCpf;
AccessToken = accessToken;
PublicKey = publicKey;
UrlWebHook = urlWebHook;
}

public string PublicKey { get; private set; }
public string AccessToken { get; private set; }
public string? UrlWebHook { get; private set; }
public bool CobrarCpf { get; private set; }
public bool CobrarCnpj { get; private set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ public void Configure(EntityTypeBuilder<ConfiguracaoDePagamento> builder)
builder.Property(x => x.AccessToken)
.IsRequired()
.HasMaxLength(2000);
builder.Property(x => x.UrlWebHook)
.HasMaxLength(2000);
}
}
5 changes: 2 additions & 3 deletions OpenAdm.Infra/HttpService/Services/MercadoPagoHttpService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,15 @@ public async Task<MercadoPagoResponse> PostAsync(MercadoPagoRequest mercadoPagoR
httpClient.DefaultRequestHeaders.Add("X-Idempotency-Key", idempotencyKey);
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var response = await httpClient.PostAsync("payments", JsonSerializerOptionsApi.ToJson(mercadoPagoRequest));

var body = await response.Content.ReadAsStreamAsync();
if (!response.IsSuccessStatusCode)
{
var error = await response.Content.ReadAsStringAsync();
Console.WriteLine(error);
throw new ExceptionApi($"Não foi possível gerar o pagamento.", enviarErroDiscord: true);
}
var content = await response.Content.ReadAsStringAsync();

return JsonSerializer.Deserialize<MercadoPagoResponse>(content, JsonSerializerOptionsApi.Options())
return JsonSerializer.Deserialize<MercadoPagoResponse>(body, JsonSerializerOptionsApi.Options())
?? throw new Exception("Não foi possível desserealizar o objeto response do mercado pago!");
}
}
Loading

0 comments on commit 3f072ee

Please sign in to comment.