WorldpayTakePaymentAction extends AbstractAction<OrderProcessModel> { @Override public String execute(final OrderProcessModel process) { final OrderModel order = process.getOrder(); for (final PaymentTransactionModel paymentTransaction : order.getPaymentTransactions()) { final List<PaymentTransactionEntryModel> entries = worldpayPaymentTransactionService.filterPaymentTransactionEntriesOfType(paymentTransaction, CAPTURE); if (entries.isEmpty()) { return handleEmptyTransactionEntries(paymentTransaction); } else if (entries.size() != 1) { return handleMultipleTransactionEntries(process, order, paymentTransaction, entries); } if (checkForNotAccepted(order, paymentTransaction)) { return NOK; } if (checkForPendingStatus(order, paymentTransaction)) { return WAIT; } } setOrderStatus(order, PAYMENT_CAPTURED); return OK; }  @Override Set<String> getTransitions(); @Override String execute(final OrderProcessModel process); @Required void setPaymentService(final PaymentService paymentService); @Required void setWorldpayPaymentTransactionService(final WorldpayPaymentTransactionService worldpayPaymentTransactionService);  }
@Test public void execute_WhenExistingPendingCaptureEntry_ShouldReturnWAIT(){ when(worldpayPaymentTransactionServiceMock.areAllPaymentTransactionsAcceptedForType(orderMock, CAPTURE)).thenReturn(true); when(worldpayPaymentTransactionServiceMock.isPaymentTransactionPending(paymentTransactionMock, CAPTURE)).thenReturn(true); final String result = testObj.execute(processMock); assertEquals(WAIT, result); }
@Test public void execute_WhenCaptureIsNotAccepted_ShouldReturnNOK() { when(worldpayPaymentTransactionServiceMock.areAllPaymentTransactionsAcceptedForType(orderMock, CAPTURE)).thenReturn(false); final String result = testObj.execute(processMock); assertEquals(NOK, result); verify(orderMock).setStatus(PAYMENT_NOT_CAPTURED); verify(modelServiceMock).save(orderMock); }
@Test public void execute_WhenCaptureNotPendingAndAccepted_ShouldReturnOK() { when(worldpayPaymentTransactionServiceMock.areAllPaymentTransactionsAcceptedForType(orderMock, CAPTURE)).thenReturn(true); when(worldpayPaymentTransactionServiceMock.isPaymentTransactionPending(paymentTransactionMock, CAPTURE)).thenReturn(false); when(paymentServiceMock.capture(paymentTransactionMock)).thenReturn(paymentTransactionEntryModelMock); when(paymentTransactionEntryModelMock.getTransactionStatus()).thenReturn(TransactionStatus.ACCEPTED.name()); final String result = testObj.execute(processMock); assertEquals(OK, result); verify(orderMock).setStatus(PAYMENT_CAPTURED); verify(modelServiceMock).save(orderMock); }
@Test public void execute_WhenThereIsNotCapture_ShouldReturnWAITAndUpdatesOrderStatus() { when(worldpayPaymentTransactionServiceMock.filterPaymentTransactionEntriesOfType(paymentTransactionMock, CAPTURE)).thenReturn(Collections.emptyList()); when(paymentServiceMock.capture(paymentTransactionMock)).thenReturn(paymentTransactionEntryModelMock); when(paymentTransactionEntryModelMock.getTransactionStatus()).thenReturn(TransactionStatus.ACCEPTED.name()); final String result = testObj.execute(processMock); assertEquals(WAIT, result); verify(paymentServiceMock).capture(paymentTransactionMock); }
@Test public void execute_WhenThereIsMoreThanOneTransactionEntry_ShouldReturnNOK() { when(worldpayPaymentTransactionServiceMock.filterPaymentTransactionEntriesOfType(paymentTransactionMock, CAPTURE)). thenReturn(Arrays.asList(paymentTransactionEntryModelMock, paymentTransactionEntryModelMock2)); when(paymentServiceMock.capture(paymentTransactionMock)).thenReturn(paymentTransactionEntryModelMock); when(paymentTransactionEntryModelMock.getTransactionStatus()).thenReturn(TransactionStatus.ACCEPTED.name()); final String result = testObj.execute(processMock); assertEquals(NOK, result); verify(orderMock).setStatus(PAYMENT_NOT_CAPTURED); verify(modelServiceMock).save(orderMock); }
@Test public void execute_WhenNoCaptureEntriesAndPaymentInfoIsAPM_ShouldNotCaptureAndShouldWait() { when(worldpayPaymentTransactionServiceMock.filterPaymentTransactionEntriesOfType(paymentTransactionMock, CAPTURE)).thenReturn(Collections.emptyList()); when(paymentTransactionMock.getInfo()).thenReturn(paymentInfoMock); when(paymentInfoMock.getIsApm()).thenReturn(true); final String result = testObj.execute(processMock); assertEquals(WAIT, result); verify(paymentServiceMock, never()).capture(anyObject()); }
@Test public void execute_WhenPaymentInfoIsKlarna_ShouldIssueACapture() { when(worldpayPaymentTransactionServiceMock.filterPaymentTransactionEntriesOfType(paymentTransactionMock, CAPTURE)).thenReturn(Collections.emptyList()); when(paymentTransactionMock.getInfo()).thenReturn(worldpayAPMPaymentInfoModelMock); when(worldpayAPMPaymentInfoModelMock.getApmConfiguration().getCode()).thenReturn(PaymentType.KLARNASSL.getMethodCode()); final String result = testObj.execute(processMock); assertEquals(WAIT, result); verify(paymentServiceMock).capture(anyObject()); }