DefaultWorldpayTokenisedAuthorizationCommand extends WorldpayCommand implements SubscriptionAuthorizationCommand { @Override public AuthorizationResult perform(final SubscriptionAuthorizationRequest subscriptionAuthorizationRequest) { final WorldpayAdditionalInfoData additionalInfo = getWorldpayAdditionalInfoData(subscriptionAuthorizationRequest.getCv2()); final MerchantInfo merchantInfo = getMerchant(); if (merchantInfo != null) { final String merchantCode = merchantInfo.getMerchantCode(); try { final DirectAuthoriseServiceRequest request = buildWorldpayRequest(subscriptionAuthorizationRequest, additionalInfo, merchantInfo); final DirectAuthoriseServiceResponse response = getWorldpayServiceGateway().directAuthorise(request); return getAuthorizationResult(merchantCode, response, subscriptionAuthorizationRequest); } catch (final WorldpayException e) { LOG.error("Worldpay Exception for transaction: " + subscriptionAuthorizationRequest.getMerchantTransactionCode(), e); return createErrorAuthorizeResult(merchantCode, subscriptionAuthorizationRequest); } } return createErrorAuthorizeResult(UNKNOWN_MERCHANT_CODE, subscriptionAuthorizationRequest); } DefaultWorldpayTokenisedAuthorizationCommand(final Converter<BillingInfo, Address> worldpayBillingInfoAddressConverter, final Converter<DirectAuthoriseServiceResponse, AuthorizationResult> worldpayAuthorizationResultConverter, final WorldpayDynamicInteractionResolverService worldpayDynamicInteractionResolverService, final WorldpayMerchantInfoService worldpayMerchantInfoService, final WorldpayPaymentTransactionService worldpayPaymentTransactionService, final WorldpayOrderService worldpayOrderService, final WorldpayServiceGateway worldpayServiceGateway); @Override AuthorizationResult perform(final SubscriptionAuthorizationRequest subscriptionAuthorizationRequest);  }
@Test public void perform_shouldReturnAuthorizationResult_whenResponseIsAuthorisedUsingCurrentSiteMerchant() throws WorldpayException { when(worldpayGatewayMock.directAuthorise(directAuthoriseServiceRequestCaptor.capture())).thenReturn(directAuthoriseServiceResponseMock); when(authorizationResultMock.getTransactionStatus()).thenReturn(TransactionStatus.ACCEPTED); when(worldpayOrderServiceMock.convertAmount(amountMock)).thenReturn(BigDecimal.valueOf(3.30)); testObj.perform(subscriptionAuthorizationRequestMock); verify(worldpayGatewayMock).directAuthorise(directAuthoriseServiceRequestCaptor.capture()); verify(worldpayAuthorizationResultConverterMock).convert(directAuthoriseServiceResponseMock); verify(authorizationResultMock).setMerchantTransactionCode(WORLDPAY_ORDER_CODE); verify(authorizationResultMock).setRequestId(WORLDPAY_ORDER_CODE); verify(authorizationResultMock).setRequestToken(MERCHANT_CODE); verify(authorizationResultMock).setCurrency(subscriptionAuthorizationRequestMock.getCurrency()); verify(authorizationResultMock).setAuthorizationTime(any(Date.class)); verify(authorizationResultMock).setTotalAmount(BigDecimal.valueOf(3.30)); verify(worldpayMerchantInfoServiceMock).getCurrentSiteMerchant(); checkDirectAuthoriseServiceRequestValues(); }
@Test public void performShouldReturnErrorAuthorizationDueToMisConfigurationAndShouldNeverTryToDirectAuthorise() throws WorldpayException { when(worldpayGatewayMock.directAuthorise(directAuthoriseServiceRequestCaptor.capture())).thenReturn(directAuthoriseServiceResponseMock); when(authorizationResultMock.getCurrency()).thenReturn(CURRENCY); when(authorizationResultMock.getTransactionStatus()).thenReturn(TransactionStatus.ACCEPTED); when(worldpayMerchantInfoServiceMock.getCurrentSiteMerchant()).thenReturn(null); final AuthorizationResult result = testObj.perform(subscriptionAuthorizationRequestMock); verify(worldpayGatewayMock, never()).directAuthorise(directAuthoriseServiceRequestCaptor.capture()); verify(worldpayAuthorizationResultConverterMock, never()).convert(directAuthoriseServiceResponseMock); assertEquals(ERROR, result.getTransactionStatus()); assertEquals(GENERAL_SYSTEM_ERROR, result.getTransactionStatusDetails()); assertEquals(WORLDPAY_ORDER_CODE, result.getMerchantTransactionCode()); assertEquals(WORLDPAY_ORDER_CODE, result.getRequestId()); assertEquals(UNKNOWN_MERCHANT_CODE, result.getRequestToken()); assertEquals(new BigDecimal(REQUEST_AMOUNT), result.getTotalAmount()); assertEquals(Currency.getInstance(Locale.UK), result.getCurrency()); }
@Test public void performShouldReturnAuthorizationResultWhenResponseIsNotErrorAndNotAuthorised() throws WorldpayException { when(worldpayGatewayMock.directAuthorise(directAuthoriseServiceRequestCaptor.capture())).thenReturn(directAuthoriseServiceResponseMock); when(directAuthoriseServiceResponseMock.getPaymentReply().getAuthStatus()).thenReturn(AuthorisedStatus.REFUSED); when(authorizationResultMock.getCurrency()).thenReturn(CURRENCY); testObj.perform(subscriptionAuthorizationRequestMock); verify(worldpayGatewayMock).directAuthorise(directAuthoriseServiceRequestCaptor.capture()); verify(worldpayAuthorizationResultConverterMock).convert(directAuthoriseServiceResponseMock); verify(authorizationResultMock).setMerchantTransactionCode(WORLDPAY_ORDER_CODE); verify(authorizationResultMock).setRequestId(WORLDPAY_ORDER_CODE); verify(authorizationResultMock).setRequestToken(MERCHANT_CODE); verify(authorizationResultMock).setCurrency(subscriptionAuthorizationRequestMock.getCurrency()); verify(authorizationResultMock).setAuthorizationTime(any(Date.class)); verify(authorizationResultMock).setTotalAmount((new BigDecimal(REQUEST_AMOUNT))); checkDirectAuthoriseServiceRequestValues(); }
@Test public void performShouldReturnErrorAuthorizationResultWhenResponseIsNull() throws WorldpayException { when(worldpayGatewayMock.directAuthorise(directAuthoriseServiceRequestCaptor.capture())).thenReturn(null); final AuthorizationResult result = testObj.perform(subscriptionAuthorizationRequestMock); verify(worldpayGatewayMock).directAuthorise(directAuthoriseServiceRequestCaptor.capture()); checkDirectAuthoriseServiceRequestValues(); assertEquals(ERROR, result.getTransactionStatus()); assertEquals(GENERAL_SYSTEM_ERROR, result.getTransactionStatusDetails()); assertEquals(WORLDPAY_ORDER_CODE, result.getMerchantTransactionCode()); assertEquals(WORLDPAY_ORDER_CODE, result.getRequestId()); assertEquals(MERCHANT_CODE, result.getRequestToken()); assertEquals(new BigDecimal(REQUEST_AMOUNT), result.getTotalAmount()); assertEquals(Currency.getInstance(Locale.UK), result.getCurrency()); }