PaymentInfoInquiryJobPerformable extends AbstractJobPerformable { @Override public PerformResult perform(final CronJobModel cronJobModel) { LOG.info("Executing timeout preparation cronjob for pending payment transaction"); final int waitTime = configurationService.getConfiguration().getInt(WORLDPAY_APM_MINUTES_BEFORE_INQUIRING_TIMEOUT, DEFAULT_WAIT_IN_MINUTES); final int blanketTime = configurationService.getConfiguration().getInt(WORLDPAY_APM_DAYS_BEFORE_STOP_INQUIRING_TIMEOUT, DEFAULT_BLANKET_TIME_IN_DAYS); final List<PaymentTransactionModel> pendingPaymentTransactions = worldpayPaymentTransactionDao.findPendingPaymentTransactions(waitTime); for (final PaymentTransactionModel paymentTransactionModel : pendingPaymentTransactions) { if (paymentTransactionIsOverBlanketTime(paymentTransactionModel, blanketTime)) { paymentTransactionRejectionStrategy.executeRejection(paymentTransactionModel); } else { final String requestId = paymentTransactionModel.getRequestId(); try { final MerchantInfo merchantConfig = worldpayMerchantInfoService.getMerchantInfoFromTransaction(paymentTransactionModel); final OrderInquiryServiceResponse orderInquiryServiceResponse = orderInquiryService.inquirePaymentTransaction(merchantConfig, paymentTransactionModel); LOG.info(format("Processing order Inquiry Service Response for pending payment transaction with worldpay order code [{0}]", requestId)); orderInquiryService.processOrderInquiryServiceResponse(paymentTransactionModel, orderInquiryServiceResponse); } catch (final WorldpayException e) { LOG.error(format("Error receiving response from Worldpay for orderInquiry with worldpayOrderCode [{0}]. " + "Probably the service is down, or there is a problem with the merchant configuration", requestId), e); return new PerformResult(FAILURE, FINISHED); } } } return new PerformResult(SUCCESS, FINISHED); }  @Override PerformResult perform(final CronJobModel cronJobModel); @Required void setOrderInquiryService(final OrderInquiryService orderInquiryService); @Required void setWorldpayPaymentTransactionDao(final WorldpayPaymentTransactionDao worldpayPaymentTransactionDao); @Required void setWorldpayMerchantInfoService(final WorldpayMerchantInfoService worldpayMerchantInfoService); @Required void setConfigurationService(final ConfigurationService configurationService); @Required void setPaymentTransactionRejectionStrategy(final PaymentTransactionRejectionStrategy paymentTransactionRejectionStrategy);  }
@Test public void performShouldProcessOrderInquiryServiceResponse() throws Exception { final PerformResult result = testObj.perform(cronjobModelMock); assertEquals(SUCCESS, result.getResult()); assertEquals(FINISHED, result.getStatus()); verify(worldpayPaymentTransactionDaoMock).findPendingPaymentTransactions(WAIT_TIME); verify(worldpayMerchantInfoServiceMock).getMerchantInfoFromTransaction(paymentTransactionModelMock); verify(orderInquiryServiceMock).inquirePaymentTransaction(merchantInfoMock, paymentTransactionModelMock); verify(orderInquiryServiceMock).processOrderInquiryServiceResponse(paymentTransactionModelMock, orderInquiryServiceResponseMock); verify(configurationMock).getInt(WORLDPAY_APM_MINUTES_BEFORE_INQUIRING_TIMEOUT, WAIT_TIME); verify(configurationMock).getInt(WORLDPAY_APM_DAYS_BEFORE_STOP_INQUIRING_TIMEOUT, BLANKET_TIMEOUT_DAYS); verify(paymentTransactionRejectionStrategyMock, never()).executeRejection(paymentTransactionModelMock); }
@Test public void performShouldBreakWhenWorldpayExceptionThrown() throws Exception { when(orderInquiryServiceMock.inquirePaymentTransaction(merchantInfoMock, paymentTransactionModelMock)).thenThrow(new WorldpayException(EXCEPTION_MESSAGE)); final PerformResult result = testObj.perform(cronjobModelMock); assertEquals(FAILURE, result.getResult()); assertEquals(FINISHED, result.getStatus()); verify(worldpayPaymentTransactionDaoMock).findPendingPaymentTransactions(WAIT_TIME); verify(worldpayMerchantInfoServiceMock).getMerchantInfoFromTransaction(paymentTransactionModelMock); verify(orderInquiryServiceMock).inquirePaymentTransaction(merchantInfoMock, paymentTransactionModelMock); verify(orderInquiryServiceMock, never()).processOrderInquiryServiceResponse(paymentTransactionModelMock, orderInquiryServiceResponseMock); verify(configurationMock).getInt(WORLDPAY_APM_MINUTES_BEFORE_INQUIRING_TIMEOUT, WAIT_TIME); verify(configurationMock).getInt(WORLDPAY_APM_DAYS_BEFORE_STOP_INQUIRING_TIMEOUT, BLANKET_TIMEOUT_DAYS); verify(paymentTransactionRejectionStrategyMock, never()).executeRejection(paymentTransactionModelMock); }
@Test public void performShouldNotPollWhenPaymentTransactionModelIsOlderThanConfiguredBlanketTimeout() throws WorldpayException { final LocalDate expectedLocalDate = LocalDate.now().minus(BLANKET_TIMEOUT_DAYS + 1, ChronoUnit.DAYS); final Date expectedDate = Date.from(expectedLocalDate.atStartOfDay().toInstant(ZoneOffset.UTC)); when(paymentTransactionModelMock.getCreationtime()).thenReturn(expectedDate); final PerformResult result = testObj.perform(cronjobModelMock); assertEquals(SUCCESS, result.getResult()); assertEquals(FINISHED, result.getStatus()); verify(worldpayPaymentTransactionDaoMock).findPendingPaymentTransactions(WAIT_TIME); verify(paymentTransactionModelMock).getCreationtime(); verify(configurationMock).getInt(WORLDPAY_APM_MINUTES_BEFORE_INQUIRING_TIMEOUT, WAIT_TIME); verify(configurationMock).getInt(WORLDPAY_APM_DAYS_BEFORE_STOP_INQUIRING_TIMEOUT, BLANKET_TIMEOUT_DAYS); verify(orderInquiryServiceMock, never()).inquirePaymentTransaction(merchantInfoMock, paymentTransactionModelMock); verify(orderInquiryServiceMock, never()).processOrderInquiryServiceResponse(paymentTransactionModelMock, orderInquiryServiceResponseMock); verify(paymentTransactionRejectionStrategyMock).executeRejection(paymentTransactionModelMock); }