WorldpayPaymentInfoRemoveInterceptor implements RemoveInterceptor<PaymentInfoModel> { @Override public void onRemove(final PaymentInfoModel paymentInfoModel, final InterceptorContext interceptorContext) { if (!(paymentInfoModel instanceof CreditCardPaymentInfoModel)) { return; } final CreditCardPaymentInfoModel creditCardPaymentInfoModel = (CreditCardPaymentInfoModel) paymentInfoModel; if (shouldNotDeleteToken(creditCardPaymentInfoModel)) { return; } final String merchantId = creditCardPaymentInfoModel.getMerchantId(); try { final MerchantInfo merchantInfo = worldpayMerchantInfoService.getMerchantInfoByCode(merchantId); worldpayDirectOrderService.deleteToken(merchantInfo, creditCardPaymentInfoModel); LOG.info(MessageFormat.format("Deleting worldpay token for user with authenticated shopper id {0}", creditCardPaymentInfoModel.getAuthenticatedShopperID())); } catch (final WorldpayConfigurationException e) { LOG.error(MessageFormat.format( "Could not find merchant configuration for merchant id [{0}]. The token was not deleted in Worldpay - subscription id [{1}]", merchantId, creditCardPaymentInfoModel.getSubscriptionId()), e); } catch (final WorldpayException e) { LOG.error(MessageFormat.format("Failed to delete token in worldpay, subscription id [{0}]", creditCardPaymentInfoModel.getSubscriptionId()), e); } }  @Override void onRemove(final PaymentInfoModel paymentInfoModel, final InterceptorContext interceptorContext); @Required void setWorldpayMerchantInfoService(final WorldpayMerchantInfoService worldpayMerchantInfoService); @Required void setWorldpayDirectOrderService(final WorldpayDirectOrderService worldpayDirectOrderService);  }
@Test public void shouldDeleteTokenInWorldpay() throws Exception { when(creditCardPaymentInfoModelMock.getMerchantId()).thenReturn(MERCHANT_ID); when(worldpayMerchantInfoServiceMock.getMerchantInfoByCode(MERCHANT_ID)).thenReturn(merchantInfoMock); testObj.onRemove(creditCardPaymentInfoModelMock, interceptorContextMock); verify(worldpayDirectOrderServiceMock).deleteToken(merchantInfoMock, creditCardPaymentInfoModelMock); }
@Test public void shouldNotDeleteTokenNotAttachedToUser() throws Exception { when(creditCardPaymentInfoModelMock.getMerchantId()).thenReturn(MERCHANT_ID); when(creditCardPaymentInfoModelMock.getUser()).thenReturn(null); when(worldpayMerchantInfoServiceMock.getMerchantInfoByCode(MERCHANT_ID)).thenReturn(merchantInfoMock); testObj.onRemove(creditCardPaymentInfoModelMock, interceptorContextMock); verifyZeroInteractions(worldpayDirectOrderServiceMock); }
@Test public void shouldNotDeleteTokenForDuplicatePaymentInfo() throws Exception { when(creditCardPaymentInfoModelMock.getMerchantId()).thenReturn(MERCHANT_ID); when(creditCardPaymentInfoModelMock.getDuplicate()).thenReturn(true); when(worldpayMerchantInfoServiceMock.getMerchantInfoByCode(MERCHANT_ID)).thenReturn(merchantInfoMock); testObj.onRemove(creditCardPaymentInfoModelMock, interceptorContextMock); verifyZeroInteractions(worldpayDirectOrderServiceMock); }
@Test public void shouldNotDeleteTokenIfNormalPaymentInfo() throws Exception { testObj.onRemove(paymentInfoModelMock, interceptorContextMock); verifyZeroInteractions(worldpayDirectOrderServiceMock); verifyZeroInteractions(worldpayMerchantInfoServiceMock); }
@Test public void shouldNotDeleteTokenIfNoSubscriptionId() throws Exception { when(creditCardPaymentInfoModelMock.getSubscriptionId()).thenReturn(null); testObj.onRemove(creditCardPaymentInfoModelMock, interceptorContextMock); verifyZeroInteractions(worldpayDirectOrderServiceMock); verifyZeroInteractions(worldpayMerchantInfoServiceMock); }