@Override public String registerOrder(Order order) { Map<String, Object> metadata = new HashMap<>(); metadata.put("A5LId", order.getId()); Optional<String> method = Optional.of("ideal"); CreatePayment payment = new CreatePayment( method, (double) order.getAmount(), "Area FiftyLAN Ticket", returnUrl + "?order=" + order.getId(), Optional.empty(), metadata); // First try is for IOExceptions coming from the Mollie Client. try { // Create the payment over at Mollie ResponseOrError<Payment> molliePayment = mollie.payments().create(payment); if (molliePayment.getSuccess()) { // All good, update the order updateOrder(order, molliePayment); return molliePayment.getData().getLinks().getPaymentUrl(); } else { // Mollie returned an error. handleMollieError(molliePayment); return null; } } catch (IOException e) { // This indicates the HttpClient encountered some error throw new PaymentException("Could not connect to the Paymentprovider"); } }
private void updateOrder(Order order, ResponseOrError<Payment> molliePayment) { // Insert the Mollie ID for future reference order.setReference(molliePayment.getData().getId()); order.setStatus(OrderStatus.WAITING); // Save the changes to the order orderRepository.save(order); }
private void handleMollieError(ResponseOrError<?> mollieResponseWithError) { // Some error occured, but connection to Mollie succeeded, which means they have something to // say. Map molliePaymentError = mollieResponseWithError.getError(); // Make the compiler shut up, this is something stupid in the Mollie API Client Map error = (Map) molliePaymentError.get("error"); throw new PaymentException((String) error.get("message")); }
@Override public Order updateStatus(String orderReference) { Order order = orderRepository .findByReference(orderReference) .orElseThrow( () -> new OrderNotFoundException( "Order with reference " + orderReference + " not found")); // This try is for the Mollie API internal HttpClient try { // Request a payment from Mollie ResponseOrError<Payment> molliePaymentStatus = mollie.payments().get(orderReference); // ResponseOrError<PaymentStatus> molliePaymentStatus = // mollie.getPaymentStatus(apiKey, // orderReference); // If the request was a success, we can update the order if (molliePaymentStatus.getSuccess()) { // There are a couple of possible statuses. Enum would have been nice. We select a couple of // relevant // statuses to translate to our own status. switch (molliePaymentStatus.getData().getStatus()) { case "pending": { order.setStatus(OrderStatus.WAITING); break; } case "cancelled": { order.setStatus(OrderStatus.CANCELLED); break; } case "expired": { order.setStatus(OrderStatus.EXPIRED); break; } case "paid": { order.setStatus(OrderStatus.PAID); break; } case "paidout": { order.setStatus(OrderStatus.PAID); break; } } return orderRepository.save(order); } else { // Order status could not be updated for some reason. Return the original order handleMollieError(molliePaymentStatus); return order; } } catch (IOException e) { // This indicates the HttpClient encountered some error throw new PaymentServiceConnectionException(e.getMessage()); } }