CustomerOrderPaymentServiceImpl extends PaymentModuleGenericServiceImpl<CustomerOrderPayment> implements CustomerOrderPaymentService { @Override public BigDecimal getOrderAmount(final String orderNumber) { final List<CustomerOrderPayment> payments = findPayments(orderNumber, null, new String[] { Payment.PAYMENT_STATUS_OK }, new String[] { PaymentGateway.AUTH_CAPTURE, PaymentGateway.CAPTURE, PaymentGateway.VOID_CAPTURE, PaymentGateway.REFUND }); BigDecimal credit = BigDecimal.ZERO; BigDecimal debit = BigDecimal.ZERO; BigDecimal voided = BigDecimal.ZERO; List<BigDecimal> credited = new ArrayList<>(); for (CustomerOrderPayment payment : payments) { if (PaymentGateway.AUTH_CAPTURE.equals(payment.getTransactionOperation()) || PaymentGateway.CAPTURE.equals(payment.getTransactionOperation())) { credit = credit.add(payment.getPaymentAmount()); credited.add(payment.getPaymentAmount()); } } for (CustomerOrderPayment payment : payments) { if (PaymentGateway.REFUND.equals(payment.getTransactionOperation())) { debit = debit.add(payment.getPaymentAmount()); } else if (PaymentGateway.VOID_CAPTURE.equals(payment.getTransactionOperation())) { final Iterator<BigDecimal> creditedIt = credited.iterator(); while (creditedIt.hasNext()) { if (creditedIt.next().compareTo(payment.getPaymentAmount()) == 0) { debit = debit.add(payment.getPaymentAmount()); creditedIt.remove(); } } } } return credit.subtract(debit).setScale(DEFAULT_SCALE, BigDecimal.ROUND_HALF_UP); } CustomerOrderPaymentServiceImpl(final PaymentModuleGenericDAO<CustomerOrderPayment, Long> genericDao); @Override BigDecimal getOrderAmount(final String orderNumber); @Override List<CustomerOrderPayment> findPayments(final int start, final int offset, final String sort, final boolean sortDescending, final Set<String> shops, final Map<String, List> filter); @Override int findCustomerOrderPaymentCount(final Set<String> shops, final Map<String, List> filter); @Override List<CustomerOrderPayment> findPayments(final String orderNumber,
                                                   final String shipmentNumber,
                                                   final String paymentProcessorResult,
                                                   final String transactionOperation); @Override List<CustomerOrderPayment> findPayments(final String orderNumber,
                                                   final String shipmentNumber,
                                                   final String[] paymentProcessorResult,
                                                   final String[] transactionOperation);  }
@Test public void testGetOrderAmountAuth() { service.create(createCustomerOrderPayment(new BigDecimal("223.00"), "A-OK-223-45", "A-OK-223-45-0", PaymentGateway.AUTH, Payment.PAYMENT_STATUS_OK)); service.create(createCustomerOrderPayment(new BigDecimal("0.45"), "A-OK-223-45", "A-OK-223-45-1", PaymentGateway.AUTH, Payment.PAYMENT_STATUS_OK)); assertEquals("0.00", service.getOrderAmount("A-OK-223-45").toPlainString()); }
@Test public void testGetOrderAmountAuthCaptureOk() { service.create(createCustomerOrderPayment(new BigDecimal("223.00"), "AC-OK-223-45", "AC-OK-223-45-0", PaymentGateway.AUTH_CAPTURE, Payment.PAYMENT_STATUS_OK)); service.create(createCustomerOrderPayment(new BigDecimal("0.45"), "AC-OK-223-45", "AC-OK-223-45-1", PaymentGateway.AUTH_CAPTURE, Payment.PAYMENT_STATUS_OK)); assertEquals("223.45", service.getOrderAmount("AC-OK-223-45").toPlainString()); }
@Test public void testGetOrderAmountAuthCaptureNotOk() { service.create(createCustomerOrderPayment(new BigDecimal("223.00"), "AC-PF-223-45", "AC-PF-223-45-0", PaymentGateway.AUTH_CAPTURE, Payment.PAYMENT_STATUS_FAILED)); service.create(createCustomerOrderPayment(new BigDecimal("0.45"), "AC-PF-223-45", "AC-PF-223-45-1", PaymentGateway.AUTH_CAPTURE, Payment.PAYMENT_STATUS_OK)); assertEquals("0.45", service.getOrderAmount("AC-PF-223-45").toPlainString()); }
@Test public void testGetOrderAmountCaptureOk() { service.create(createCustomerOrderPayment(new BigDecimal("223.00"), "C-OK-223-45", "C-OK-223-45-0", PaymentGateway.CAPTURE, Payment.PAYMENT_STATUS_OK)); service.create(createCustomerOrderPayment(new BigDecimal("0.45"), "C-OK-223-45", "C-OK-223-45-1", PaymentGateway.CAPTURE, Payment.PAYMENT_STATUS_OK)); assertEquals("223.45", service.getOrderAmount("C-OK-223-45").toPlainString()); }
@Test public void testGetOrderAmountCaptureNotOk() { service.create(createCustomerOrderPayment(new BigDecimal("223.00"), "C-PR-223-45", "C-PR-223-45-0", PaymentGateway.CAPTURE, Payment.PAYMENT_STATUS_PROCESSING)); service.create(createCustomerOrderPayment(new BigDecimal("0.45"), "C-PR-223-45", "C-PR-223-45-1", PaymentGateway.CAPTURE, Payment.PAYMENT_STATUS_OK)); assertEquals("0.45", service.getOrderAmount("C-PR-223-45").toPlainString()); }
@Test public void testGetOrderAmountCaptureRefundOk() { service.create(createCustomerOrderPayment(new BigDecimal("223.00"), "CR-OK-223-45", "CR-OK-223-45-0", PaymentGateway.CAPTURE, Payment.PAYMENT_STATUS_OK)); service.create(createCustomerOrderPayment(new BigDecimal("0.45"), "CR-OK-223-45", "CR-OK-223-45-1", PaymentGateway.REFUND, Payment.PAYMENT_STATUS_OK)); assertEquals("222.55", service.getOrderAmount("CR-OK-223-45").toPlainString()); }
@Test public void testGetOrderAmountCaptureRefundFailed() { service.create(createCustomerOrderPayment(new BigDecimal("223.00"), "CR-MP-223-45", "CR-MP-223-45-0", PaymentGateway.CAPTURE, Payment.PAYMENT_STATUS_OK)); service.create(createCustomerOrderPayment(new BigDecimal("0.45"), "CR-MP-223-45", "CR-MP-223-45-1", PaymentGateway.REFUND, Payment.PAYMENT_STATUS_MANUAL_PROCESSING_REQUIRED)); assertEquals("223.00", service.getOrderAmount("CR-MP-223-45").toPlainString()); }
@Test public void testGetOrderAmountCaptureVoidNotMatching() { service.create(createCustomerOrderPayment(new BigDecimal("223.00"), "CV-OK-223-45", "CV-OK-223-45-0", PaymentGateway.CAPTURE, Payment.PAYMENT_STATUS_OK)); service.create(createCustomerOrderPayment(new BigDecimal("0.45"), "CV-OK-223-45", "CV-OK-223-45-1", PaymentGateway.VOID_CAPTURE, Payment.PAYMENT_STATUS_OK)); assertEquals("223.00", service.getOrderAmount("CV-OK-223-45").toPlainString()); }
@Test public void testGetOrderAmountCaptureVoidOk() { service.create(createCustomerOrderPayment(new BigDecimal("223.00"), "CVF-OK-223-45", "CVF-OK-223-45-0", PaymentGateway.CAPTURE, Payment.PAYMENT_STATUS_OK)); service.create(createCustomerOrderPayment(new BigDecimal("223.00"), "CVF-OK-223-45", "CVF-OK-223-45-1", PaymentGateway.VOID_CAPTURE, Payment.PAYMENT_STATUS_OK)); assertEquals("0.00", service.getOrderAmount("CVF-OK-223-45").toPlainString()); }
@Test public void testGetOrderAmountCaptureVoidNotOk() { service.create(createCustomerOrderPayment(new BigDecimal("223.00"), "CV-PR-223-45", "CV-PR-223-45-0", PaymentGateway.CAPTURE, Payment.PAYMENT_STATUS_OK)); service.create(createCustomerOrderPayment(new BigDecimal("0.45"), "CV-PR-223-45", "CV-PR-223-45-1", PaymentGateway.VOID_CAPTURE, Payment.PAYMENT_STATUS_PROCESSING)); assertEquals("223.00", service.getOrderAmount("CV-PR-223-45").toPlainString()); }
@Test public void testGetOrderAmountCaptureProcessingVoid() { service.create(createCustomerOrderPayment(new BigDecimal("223.00"), "CV-PR2-223-45", "CV-PR2-223-45-0", PaymentGateway.CAPTURE, Payment.PAYMENT_STATUS_PROCESSING)); service.create(createCustomerOrderPayment(new BigDecimal("0.45"), "CV-PR2-223-45", "CV-PR2-223-45-1", PaymentGateway.VOID_CAPTURE, Payment.PAYMENT_STATUS_OK)); assertEquals("0.00", service.getOrderAmount("CV-PR2-223-45").toPlainString()); }