diff --git a/src/main/java/com/unicenta/pos/payment/PaymentGatewayMediaPayments.java b/src/main/java/com/unicenta/pos/payment/PaymentGatewayMediaPayments.java index a54fe0f..1bb5245 100644 --- a/src/main/java/com/unicenta/pos/payment/PaymentGatewayMediaPayments.java +++ b/src/main/java/com/unicenta/pos/payment/PaymentGatewayMediaPayments.java @@ -30,69 +30,68 @@ import java.util.ArrayList; @Slf4j public class PaymentGatewayMediaPayments implements PaymentGateway { - public PaymentGatewayMediaPayments() { + public PaymentGatewayMediaPayments() { + } + + Window getPaymentWindow(double amount) { + if (amount > 0) { + ArrayList paymentWindows = new ArrayList<>(); + for (Window window : Window.getWindows()) { + if (window instanceof JPaymentSelectReceipt) { + paymentWindows.add((JPaymentSelectReceipt) window); + } + } + return paymentWindows.get(paymentWindows.size() - 1); + } else { + ArrayList paymentWindows = new ArrayList<>(); + for (Window window : Window.getWindows()) { + if (window instanceof JPaymentSelectRefund) { + paymentWindows.add((JPaymentSelectRefund) window); + } + } + return paymentWindows.get(paymentWindows.size() - 1); + } + } + + @Override + public void execute(PaymentInfoMagcard payinfo) { + + int timer = 0; + int timeout = 180; + + double roundedValue = RoundUtils.round(payinfo.getTotal()); + new Application().mediaPaymentsTransaction(roundedValue, getPaymentWindow(roundedValue), + payinfo.getTransactionID()); + + while (AppContext.getIsProcessing() == null || AppContext.getIsProcessing()) { + try { + log.info("uniCenta-oPos: waiting for payment to complete ...."); + Thread.sleep(1000); + timer += 1; + if (timer > timeout) + break; + } catch (InterruptedException e) { + log.error(e.getMessage()); + } } - Window getPaymentWindow(double amount) { - if (amount > 0) { - ArrayList paymentWindows = new ArrayList<>(); - for (Window window : Window.getWindows()) { - if (window instanceof JPaymentSelectReceipt) { - paymentWindows.add((JPaymentSelectReceipt) window); - } - } - return paymentWindows.get(paymentWindows.size()-1); - } - else { - ArrayList paymentWindows = new ArrayList<>(); - for (Window window : Window.getWindows()) { - if (window instanceof JPaymentSelectRefund) { - paymentWindows.add((JPaymentSelectRefund) window); - } - } - return paymentWindows.get(paymentWindows.size()-1); - } - } - - @Override - public void execute(PaymentInfoMagcard payinfo) { - - int timer = 0; - int timeout = 180; - - double roundedValue = RoundUtils.round(payinfo.getTotal()); - new Application().mediaPaymentsTransaction(roundedValue, getPaymentWindow(roundedValue), roundedValue > 0 ? payinfo.getTransactionID() : null); - - while (AppContext.getIsProcessing() == null || AppContext.getIsProcessing()) { - try { - log.info("uniCenta-oPos: waiting for payment to complete ...."); - Thread.sleep(1000); - timer += 1; - if (timer > timeout) break; - } catch (InterruptedException e) { - log.error(e.getMessage()); - } - } - - if (AppContext.getMediaPaymentsPaymentResult() == null) { - payinfo.paymentError("Transaction Error! Please try again", "No Response"); - } - else if ("APPROVED".equals(AppContext.getMediaPaymentsPaymentResult().getProcessingDetails().getStatus())) { - payinfo.setCardName(AppContext.getMediaPaymentsPaymentResult().getProcessingDetails().getCard().getType()); - payinfo.setVerification(AppContext.getMediaPaymentsPaymentResult().getProcessingDetails().getVerificationMethod()); - payinfo.setChipAndPin(true); - payinfo.paymentOK( - AppContext.getMediaPaymentsPaymentResult().getAdditionalInformation().getRequestId(), - AppContext.getMediaPaymentsPaymentResult().getTransactionDetails().getId(), - AppContext.getMediaPaymentsPaymentResult().getProcessingDetails().getStatus() - ); - } - else { - String errorMsg = AppContext.getMediaPaymentsPaymentResult().getErrorMessage(); - if (errorMsg == null) { - errorMsg = AppContext.getMediaPaymentsPaymentResult().getMessage(); - } - payinfo.paymentError("Transaction Error! Please try again", errorMsg); - } + if (AppContext.getMediaPaymentsPaymentResult() == null) { + payinfo.paymentError("Transaction Error! Please try again", "No Response"); + } else if ("APPROVED".equals(AppContext.getMediaPaymentsPaymentResult().getProcessingDetails().getStatus())) { + payinfo.setCardName(AppContext.getMediaPaymentsPaymentResult().getProcessingDetails().getCard().getType()); + payinfo + .setVerification(AppContext.getMediaPaymentsPaymentResult().getProcessingDetails().getVerificationMethod()); + payinfo.setChipAndPin(true); + payinfo.paymentOK( + AppContext.getMediaPaymentsPaymentResult().getAdditionalInformation().getRequestId(), + AppContext.getMediaPaymentsPaymentResult().getTransactionDetails().getId(), + AppContext.getMediaPaymentsPaymentResult().getProcessingDetails().getStatus()); + } else { + String errorMsg = AppContext.getMediaPaymentsPaymentResult().getErrorMessage(); + if (errorMsg == null) { + errorMsg = AppContext.getMediaPaymentsPaymentResult().getMessage(); + } + payinfo.paymentError("Transaction Error! Please try again", errorMsg); } + } }