Ao criar a requisição /create-order na api, recebo o erro:
"message": "[403 Forbidden] during [POST] to [https://sandbox.api.pagseguro.com/create-order/pay] [PagBankClient#payOrder(String,PaymentRequestDto)]: [{\"message\":\"Forbidden\"}\n]",
"path": "/create-order"
Estou usando o feignClient com a configuração:
@FeignClient(name = "pagseguro-api", url = "https://sandbox.api.pagseguro.com", configuration = FeignConfig.class)
public interface PagBankClient {
@PostMapping("/orders")
OrderResponseDto createOrder(@RequestBody OrderRequestDto dto);
@PostMapping("/{order_id}/pay")
PaymentResponseDto payOrder(@PathVariable("order_id") String orderId, @RequestBody PaymentRequestDto dto);
@PostMapping("/charges/{charge_id}/cancel")
PaymentResponse cancelPayment(@PathVariable("charge_id") String chargeId,
@RequestBody CancelPaymentRequest cancelRequest);
@PostMapping("/certificates")
CertificateResponse createCertificate();
}
o token está correto, passando o Bearer, conforme o código acima e por isso estou na dúvida. Veja o meu service:
@Service
public class PaymentService {
@Autowired
private PagBankClient client;
@Autowired
private ModelMapper modelMapper;
@Transactional
public PaymentResponseDto pay(String orderId, PaymentRequestDto dto) {
PaymentResponse paymentResponse = client.pagarPedido(orderId, dto);
return modelMapper.map(paymentResponse, PaymentResponseDto.class);
}
}
@Service
public class OrderService {
@Autowired
private PagBankClient client;
@Autowired
private ModelMapper modelMapper;
@Transactional
public OrderResponseDto createOrder(OrderRequestDto dto) {
OrderResponse orderResponse = client.createOrder(dto);
return modelMapper.map(orderResponse, OrderResponseDto.class);
}
}
@Configuration
public class FeignConfig {
@Value("${pagseguro-token-sandbox}")
private String token;
@Bean
public RequestInterceptor pagSeguroAuthInterceptor() {
return new ApiRequestInterceptor(token);
}
}
public class ApiRequestInterceptor implements RequestInterceptor {
private final String token;
public ApiRequestInterceptor(String token) {
this.token = token;
}
@Override
public void apply(RequestTemplate template) {
template.header("accept", "application/json");
template.header("Authorization", token);
template.header("content-type", "application/json");
}
}
Estou passando a requisição /create-order e com erro no create-order/pay. Não estou entendendo este motivo do erro e não é explícito na documentação.
Criei um controller e service para o caminho /orders:
@RestController
@RequestMapping(name = "/create-order")
public class OrderController {
@Autowired
private OrderService service;
@PostMapping
public ResponseEntity<OrderResponseDto> createOrder(@RequestBody OrderRequestDto dto) throws IOException {
OrderResponseDto response = service.createOrder(dto);
return ResponseEntity.status(HttpStatus.CREATED).body(response);
}
}
@Service
public class OrderService {
@Autowired
private PagBankClient client;
@Autowired
private ModelMapper modelMapper;
@Transactional
public OrderResponseDto createOrder(OrderRequestDto dto) {
OrderResponseDto orderResponse = client.createOrder(dto);
return modelMapper.map(orderResponse, OrderResponseDto.class);
}
}
@RestController
@RequestMapping(name = "/create-pay")
public class PaymentController {
@Autowired
private PaymentService service;
@PostMapping("/{order_id}")
public ResponseEntity<PaymentRequestDto> pagarPedido(@PathVariable("order_id") String orderId,
@RequestBody PaymentRequestDto dto) {
PaymentResponseDto responseDto = service.pay(orderId, dto);
URI uri = ServletUriComponentsBuilder.fromCurrentRequest().path("/{id}")
.buildAndExpand(responseDto .getId()).toUri();
return ResponseEntity.created(uri).body(dto);
}
}
@Service
public class PaymentService {
@Autowired
private PagBankClient client;
@Autowired
private ModelMapper modelMapper;
@Transactional
public PaymentResponseDto pay(String orderId, PaymentRequestDto dto) {
PaymentResponseDto paymentResponse = client.payOrder(orderId, dto);
return modelMapper.map(paymentResponse, PaymentResponseDto.class);
}
}
Agradeço se responderem.