forked from magnoazneto/orange-talents-04-template-ecommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: realizar venda - segundo passo (pagamento)
- Loading branch information
1 parent
c4d45ec
commit 0577589
Showing
13 changed files
with
395 additions
and
1 deletion.
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
12 changes: 12 additions & 0 deletions
12
src/main/java/br/com/zupacademy/william/ecommerce/exception/ErrorBody.java
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
35 changes: 35 additions & 0 deletions
35
src/main/java/br/com/zupacademy/william/ecommerce/notafiscal/NotaFiscalController.java
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,35 @@ | ||
package br.com.zupacademy.william.ecommerce.notafiscal; | ||
|
||
import br.com.zupacademy.william.ecommerce.usuario.Usuario; | ||
import br.com.zupacademy.william.ecommerce.venda.Venda; | ||
import br.com.zupacademy.william.ecommerce.venda.VendaRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
@RestController | ||
@RequestMapping("/venda/{id}/nota-fiscal") | ||
public class NotaFiscalController { | ||
|
||
@Autowired | ||
private VendaRepository vendaRepository; | ||
|
||
@PostMapping | ||
public ResponseEntity emitirNotaFiscal(@PathVariable Long id) { | ||
Venda venda = vendaRepository.findById(id) | ||
.orElseThrow(() -> | ||
new ResponseStatusException( | ||
HttpStatus.NOT_FOUND, | ||
String.format("Não foi encontrada uma venda com id %d", id)) | ||
); | ||
|
||
Usuario comprador = venda.getComprador(); | ||
|
||
return ResponseEntity.ok().build(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/br/com/zupacademy/william/ecommerce/pagamento/EmailPagamentoComFalha.java
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,35 @@ | ||
package br.com.zupacademy.william.ecommerce.pagamento; | ||
|
||
import br.com.zupacademy.william.ecommerce.email.Email; | ||
import br.com.zupacademy.william.ecommerce.venda.Venda; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class EmailPagamentoComFalha implements Email { | ||
|
||
private static final String remetente = "ecommerce@ecomerce.com"; | ||
|
||
private String titulo = "%s, seu pagamento não pode ser realizado com sucesso.\nProduto: %s\nQuantidade: %s\nValor: %s\nData da recusa: %s"; | ||
private String destinatario; | ||
|
||
public EmailPagamentoComFalha(Venda venda) { | ||
this.titulo = String.format(titulo, venda.getEmailDoComprador(), venda.getProduto().getNome(), venda.getQuantidade(), venda.getValor(), LocalDateTime.now().format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss"))); | ||
this.destinatario = venda.getEmailDoComprador(); | ||
} | ||
|
||
@Override | ||
public String getTitulo() { | ||
return this.titulo; | ||
} | ||
|
||
@Override | ||
public String getRemetente() { | ||
return this.remetente; | ||
} | ||
|
||
@Override | ||
public String getDestinatario() { | ||
return this.destinatario; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...java/br/com/zupacademy/william/ecommerce/pagamento/EmailPagamentoRealizadoComSucesso.java
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,35 @@ | ||
package br.com.zupacademy.william.ecommerce.pagamento; | ||
|
||
import br.com.zupacademy.william.ecommerce.email.Email; | ||
import br.com.zupacademy.william.ecommerce.venda.Venda; | ||
|
||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class EmailPagamentoRealizadoComSucesso implements Email { | ||
|
||
private static final String remetente = "ecommerce@ecomerce.com"; | ||
|
||
private String titulo = "%s, sua compra foi realizada com sucesso\nProduto: %s\nQuantidade: %s\nValor: %s\nData da aprovação: %s"; | ||
private String destinatario; | ||
|
||
public EmailPagamentoRealizadoComSucesso(Venda venda) { | ||
this.titulo = String.format(titulo, venda.getEmailDoComprador(), venda.getProduto().getNome(), venda.getQuantidade(), venda.getValor(), LocalDateTime.now().format(DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm:ss"))); | ||
this.destinatario = venda.getEmailDoComprador(); | ||
} | ||
|
||
@Override | ||
public String getTitulo() { | ||
return this.titulo; | ||
} | ||
|
||
@Override | ||
public String getRemetente() { | ||
return this.remetente; | ||
} | ||
|
||
@Override | ||
public String getDestinatario() { | ||
return this.destinatario; | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
src/main/java/br/com/zupacademy/william/ecommerce/pagamento/Pagamento.java
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,51 @@ | ||
package br.com.zupacademy.william.ecommerce.pagamento; | ||
|
||
import br.com.zupacademy.william.ecommerce.venda.Venda; | ||
import org.hibernate.annotations.CreationTimestamp; | ||
|
||
import javax.persistence.*; | ||
import java.time.LocalDateTime; | ||
import java.util.Objects; | ||
|
||
@Entity | ||
public class Pagamento { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
|
||
private String identificador; | ||
|
||
@ManyToOne | ||
@JoinColumn(name = "id_venda") | ||
private Venda venda; | ||
|
||
@Enumerated(EnumType.STRING) | ||
private StatusPagamento statusPagamento; | ||
|
||
@CreationTimestamp | ||
private LocalDateTime instanteCriacao = LocalDateTime.now(); | ||
|
||
public Pagamento(String identificador, Venda venda, StatusPagamento statusPagamento) { | ||
this.identificador = identificador; | ||
this.venda = venda; | ||
this.statusPagamento = statusPagamento; | ||
} | ||
|
||
public boolean sucessoNoPagamento() { | ||
return statusPagamento.isSucesso(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
Pagamento pagamento = (Pagamento) o; | ||
return identificador.equals(pagamento.identificador) && venda.equals(pagamento.venda); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hash(identificador, venda); | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
src/main/java/br/com/zupacademy/william/ecommerce/pagamento/PagamentoController.java
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,76 @@ | ||
package br.com.zupacademy.william.ecommerce.pagamento; | ||
|
||
import br.com.zupacademy.william.ecommerce.venda.Venda; | ||
import br.com.zupacademy.william.ecommerce.venda.VendaRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
import javax.transaction.Transactional; | ||
import javax.validation.Valid; | ||
|
||
@RestController | ||
public class PagamentoController { | ||
|
||
@Autowired | ||
private ApplicationEventPublisher publisher; | ||
|
||
@Autowired | ||
private VendaRepository vendaRepository; | ||
|
||
@PostMapping("/retorno-pagseguro/{id}") | ||
@Transactional | ||
public void retornoPagSeguro(@PathVariable(name = "id") Long idVenda, | ||
@RequestBody @Valid PagamentoRequest pagamentoRequest) { | ||
Venda venda = vendaRepository.findById(idVenda) | ||
.orElseThrow(() -> new ResponseStatusException( | ||
HttpStatus.NOT_FOUND, | ||
String.format("Não foi encontrada uma venda com id %d", idVenda)) | ||
); | ||
|
||
Pagamento novoPagamento = pagamentoRequest.toModel(venda); | ||
venda.adicionarTentativaPagamento(novoPagamento); | ||
|
||
if (venda.taPaga()) { | ||
enviarEmailPagamento(venda); | ||
} else { | ||
enviarEmailFalha(venda); | ||
} | ||
} | ||
|
||
@PostMapping("/retorno-paypal/{id}") | ||
@Transactional | ||
public void retornoPaypal(@PathVariable(name = "id") Long idVenda, | ||
@RequestBody @Valid PagamentoRequest pagamentoRequest) { | ||
Venda venda = vendaRepository.findById(idVenda) | ||
.orElseThrow(() -> new ResponseStatusException( | ||
HttpStatus.NOT_FOUND, | ||
String.format("Não foi encontrada uma venda com id %d", idVenda)) | ||
); | ||
|
||
Pagamento novoPagamento = pagamentoRequest.toModel(venda); | ||
venda.adicionarTentativaPagamento(novoPagamento); | ||
|
||
if (venda.taPaga()) { | ||
enviarEmailPagamento(venda); | ||
} else { | ||
enviarEmailFalha(venda); | ||
} | ||
} | ||
|
||
|
||
private void enviarEmailPagamento(Venda venda) { | ||
EmailPagamentoRealizadoComSucesso email = new EmailPagamentoRealizadoComSucesso(venda); | ||
publisher.publishEvent(email); | ||
} | ||
|
||
private void enviarEmailFalha(Venda venda) { | ||
EmailPagamentoComFalha email = new EmailPagamentoComFalha(venda); | ||
publisher.publishEvent(email); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/br/com/zupacademy/william/ecommerce/pagamento/PagamentoRequest.java
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,36 @@ | ||
package br.com.zupacademy.william.ecommerce.pagamento; | ||
|
||
import br.com.zupacademy.william.ecommerce.venda.Venda; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
import javax.validation.constraints.NotBlank; | ||
import java.util.Objects; | ||
|
||
public class PagamentoRequest { | ||
|
||
@NotBlank | ||
@JsonProperty("identificador_pagamento") | ||
private String identificadorPagamento; | ||
|
||
@NotBlank | ||
private String status; | ||
|
||
public PagamentoRequest(String identificadorPagamento, String status) { | ||
this.identificadorPagamento = identificadorPagamento; | ||
this.status = status; | ||
} | ||
|
||
public Pagamento toModel(Venda venda) { | ||
StatusPagamento statusPagamentoEValido = StatusPagamento.getEnumByString(this.status); | ||
|
||
if (Objects.isNull(statusPagamentoEValido)) { | ||
throw new ResponseStatusException(HttpStatus.CONFLICT, "A resposta do gateway não é válida para os gateways mapeados"); | ||
} | ||
|
||
StatusPagamento statusPagamento = StatusPagamento.getEnumByString(this.status); | ||
|
||
return new Pagamento(this.identificadorPagamento, venda, statusPagamento); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
src/main/java/br/com/zupacademy/william/ecommerce/pagamento/StatusPagamento.java
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 @@ | ||
package br.com.zupacademy.william.ecommerce.pagamento; | ||
|
||
public enum StatusPagamento { | ||
SUCESSO(true, "SUCESSO", "1"), | ||
FALHA(false, "FALHA", "0"); | ||
|
||
private final boolean sucesso; | ||
private final String pagSeguro; | ||
private final String paypal; | ||
|
||
StatusPagamento(boolean sucesso, String pagSeguro, String paypal) { | ||
this.sucesso = sucesso; | ||
this.pagSeguro = pagSeguro; | ||
this.paypal = paypal; | ||
} | ||
|
||
public boolean isSucesso() { | ||
return this.sucesso; | ||
} | ||
|
||
public static StatusPagamento getEnumByString(String response) { | ||
for (StatusPagamento statusPagamento : StatusPagamento.values()) { | ||
if (statusPagamento.pagSeguro.equals(response) || statusPagamento.paypal.equals(response)) { | ||
return statusPagamento; | ||
} | ||
} | ||
return 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
37 changes: 37 additions & 0 deletions
37
.../com/zupacademy/william/ecommerce/usuario/vendedor/ranking/VendedorRankingController.java
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,37 @@ | ||
package br.com.zupacademy.william.ecommerce.usuario.vendedor.ranking; | ||
|
||
import br.com.zupacademy.william.ecommerce.usuario.Usuario; | ||
import br.com.zupacademy.william.ecommerce.venda.Venda; | ||
import br.com.zupacademy.william.ecommerce.venda.VendaRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.server.ResponseStatusException; | ||
|
||
import javax.validation.Valid; | ||
|
||
@RestController | ||
@RequestMapping("/usuarios/ranking-vendedor") | ||
public class VendedorRankingController { | ||
|
||
@Autowired | ||
private VendaRepository vendaRepository; | ||
|
||
@PostMapping | ||
public ResponseEntity pontuarVendedor(@RequestBody @Valid VendedorRankingRequest vendedorRankingRequest) { | ||
Venda venda = vendaRepository.findById(vendedorRankingRequest.getIdVenda()) | ||
.orElseThrow(() -> | ||
new ResponseStatusException( | ||
HttpStatus.NOT_FOUND, | ||
String.format("Não foi encontrada uma venda com id %d", vendedorRankingRequest.getIdVenda())) | ||
); | ||
|
||
Usuario vendedor = venda.getVendedor(); | ||
|
||
return ResponseEntity.ok().build(); | ||
} | ||
} |
Oops, something went wrong.