|
package br.com.caelum.payfast.rest; |
|
|
|
import java.math.BigDecimal; |
|
import java.net.URI; |
|
import java.net.URISyntaxException; |
|
import java.util.Collection; |
|
import java.util.HashMap; |
|
import java.util.Map; |
|
|
|
import javax.ejb.Singleton; |
|
import javax.ws.rs.Consumes; |
|
import javax.ws.rs.GET; |
|
import javax.ws.rs.HeaderParam; |
|
import javax.ws.rs.NotAuthorizedException; |
|
import javax.ws.rs.POST; |
|
import javax.ws.rs.PUT; |
|
import javax.ws.rs.Path; |
|
import javax.ws.rs.PathParam; |
|
import javax.ws.rs.Produces; |
|
import javax.ws.rs.core.MediaType; |
|
import javax.ws.rs.core.Response; |
|
|
|
import br.com.caelum.payfast.modelo.Pagamento; |
|
import br.com.caelum.payfast.modelo.Transacao; |
|
|
|
@Path("/pagamentos") |
|
@Singleton |
|
public class PagamentoResource { |
|
|
|
private Map<Integer, Pagamento> repositorio = new HashMap<>(); |
|
private Integer idPagamento = 1; |
|
|
|
public PagamentoResource() { |
|
Pagamento pagamento = new Pagamento(); |
|
pagamento.setId(idPagamento++); |
|
pagamento.setValor(BigDecimal.TEN); |
|
pagamento.comStatusCriado(); |
|
repositorio.put(pagamento.getId(), pagamento); |
|
} |
|
|
|
@GET |
|
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) |
|
public Collection<Pagamento> todos() { |
|
return repositorio.values(); |
|
} |
|
|
|
@GET |
|
@Path("/{id}") |
|
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) |
|
public Pagamento buscaPagamento(@PathParam("id") Integer id) { |
|
return repositorio.get(id); |
|
} |
|
|
|
@POST |
|
@Consumes(MediaType.APPLICATION_JSON) |
|
public Response criaPagamento(Transacao transacao, @HeaderParam("token") String token) throws URISyntaxException { |
|
if(!"TOKEN123".equals(token)){ |
|
throw new NotAuthorizedException("Token inválido."); |
|
} |
|
|
|
Pagamento pagamento = new Pagamento(); |
|
pagamento.setId(idPagamento++); |
|
pagamento.setValor(transacao.getValor()); |
|
repositorio.put(pagamento.getId(), pagamento); |
|
pagamento.comStatusCriado(); |
|
System.out.println("PAGAMENTO CRIADO: " + pagamento); |
|
return Response.created(new URI("/pagamentos/"+pagamento.getId())).type(MediaType.APPLICATION_JSON).entity(pagamento).build(); |
|
} |
|
|
|
@PUT |
|
@Path("/{id}") |
|
@Produces(MediaType.APPLICATION_JSON) |
|
public Pagamento confirmaPagamento(@PathParam("id") Integer id) { |
|
Pagamento pagamento = repositorio.get(id); |
|
pagamento.comStatusConfirmado(); |
|
System.out.println("PAGAMENTO CONFIRMADO: " + pagamento); |
|
return pagamento; |
|
} |
|
} |