DefaultWorldpayPaymentInfoService implements WorldpayPaymentInfoService { @Override public CreditCardPaymentInfoModel createCreditCardPaymentInfo(final AbstractOrderModel abstractOrderModel, final TokenReply tokenReply, final boolean saveCard, final String merchantId) { validateParameterNotNull(abstractOrderModel, CART_MODEL_CANNOT_BE_NULL); final Optional<CreditCardPaymentInfoModel> customerSavedCard = Optional.ofNullable(tokenReply) .map(existentTokenReply -> getCreditCardPaymentInfoModelBasedOnTokenReply(abstractOrderModel, existentTokenReply, saveCard)); if (customerSavedCard.isPresent()) { return customerSavedCard.get(); } final CreditCardPaymentInfoModel creditCardPaymentInfoModel = modelService.create(CreditCardPaymentInfoModel.class); creditCardPaymentInfoModel.setCode(generateCcPaymentInfoCode(abstractOrderModel)); creditCardPaymentInfoModel.setWorldpayOrderCode(abstractOrderModel.getWorldpayOrderCode()); creditCardPaymentInfoModel.setUser(abstractOrderModel.getUser()); creditCardPaymentInfoModel.setMerchantId(merchantId); Optional.ofNullable(tokenReply) .map(TokenReply::getPaymentInstrument) .map(Card::getBin) .ifPresent(creditCardPaymentInfoModel::setBin); setPaymentTypeAndCreditCardType(creditCardPaymentInfoModel, tokenReply.getPaymentInstrument()); updateCreditCardModel(creditCardPaymentInfoModel, tokenReply, saveCard); return creditCardPaymentInfoModel; } DefaultWorldpayPaymentInfoService(final ModelService modelService, final EnumerationService enumerationService, final APMConfigurationLookupService apmConfigurationLookupService, final ConfigurationService configurationService, final WorldpayMerchantInfoService worldpayMerchantInfoService); @Override void savePaymentType(final PaymentTransactionModel transactionModel, final String methodCode); @Override void setPaymentInfoModel(final PaymentTransactionModel paymentTransactionModel, final AbstractOrderModel orderModel, final OrderNotificationMessage orderNotificationMessage); @Override void updateAndAttachPaymentInfoModel(final PaymentTransactionModel paymentTransactionModel, final AbstractOrderModel orderModel, final PaymentInfoModel paymentInfoModel); @Override WorldpayAPMPaymentInfoModel createWorldpayApmPaymentInfo(final PaymentTransactionModel paymentTransactionModel); @Override PaymentInfoModel createPaymentInfo(final CartModel cartModel); @Override PaymentInfoModel createPaymentInfoGooglePay(final CartModel cartModel, final GooglePayAdditionalAuthInfo googleAuthInfo); @Override PaymentInfoModel createPaymentInfoApplePay(final CartModel cartModel, final ApplePayAdditionalAuthInfo applePayAdditionalAuthInfo); @Override CreditCardPaymentInfoModel createCreditCardPaymentInfo(final AbstractOrderModel abstractOrderModel, final TokenReply tokenReply, final boolean saveCard, final String merchantId); @Override void setCreditCardType(final CreditCardPaymentInfoModel creditCardPaymentInfoModel, final PaymentReply paymentReply); @Override Optional<CreditCardPaymentInfoModel> updateCreditCardPaymentInfo(final CartModel cartModel, final UpdateTokenServiceRequest updateTokenServiceRequest); @Override void setTransactionIdentifierOnPaymentInfo(final PaymentInfoModel paymentInfoModel, final String transactionIdentifier);  }
@Test public void shouldThrowIllegalArgumentExceptionWhenCartPassedToCreateCreditCardPaymentInfoIsNull() { thrown.expect(IllegalArgumentException.class); thrown.expectMessage("CartModel cannot be null"); final CartModel cartModel = null; testObj.createCreditCardPaymentInfo(cartModel, tokenReplyMock, false, MERCHANT_ID); }
@Test public void shouldUseExistingCreditCardForTokenInformationAndSaveItToOrderForCreateTokenResponse() { when(createTokenResponseMock.getToken()).thenReturn(tokenReplyMock); when(tokenReplyMock.getTokenDetails().getTokenEvent()).thenReturn(MATCH); when(savedPaymentInfoMock.isSaved()).thenReturn(false); testObj.createCreditCardPaymentInfo(cartModelMock, tokenReplyMock, true, MERCHANT_ID); verify(cartModelMock).setPaymentInfo(savedPaymentInfoMock); verify(savedPaymentInfoMock).setSaved(true); verify(modelServiceMock).save(cartModelMock); }
@Test public void shouldNotUpdateExistingCardIfAlreadySaved() { when(createTokenResponseMock.getToken()).thenReturn(tokenReplyMock); when(tokenReplyMock.getTokenDetails().getTokenEvent()).thenReturn(MATCH); when(savedPaymentInfoMock.isSaved()).thenReturn(true); testObj.createCreditCardPaymentInfo(cartModelMock, tokenReplyMock, false, MERCHANT_ID); verify(savedPaymentInfoMock, never()).setSaved(anyBoolean()); }