CardTokenRequest implements InternalModelTransformer, Serializable { @Override public InternalModelObject transformToInternalModel() throws WorldpayModelTransformationException { final PaymentTokenCreate paymentTokenCreate = new PaymentTokenCreate(); Optional.ofNullable(authenticatedShopperId) .ifPresent(paymentTokenCreate::setAuthenticatedShopperID); Optional.ofNullable(tokenRequest) .map(TokenRequest::transformToInternalModel) .map(CreateToken.class::cast) .ifPresent(paymentTokenCreate::setCreateToken); Optional.ofNullable(storedCredentials) .map(StoredCredentials::transformToInternalModel) .ifPresent(paymentTokenCreate::setStoredCredentials); paymentTokenCreate.getPaymentInstrumentOrCSEDATA().add(payment.transformToInternalModel()); return paymentTokenCreate; } CardTokenRequest(final TokenRequest tokenRequest, final String authenticatedShopperId, final Payment payment, final StoredCredentials storedCredentials);  CardTokenRequest(final TokenRequest tokenRequest, final String authenticatedShopperId, final Payment payment);  CardTokenRequest(final TokenRequest tokenRequest, final Payment payment); @Override InternalModelObject transformToInternalModel(); TokenRequest getTokenRequest(); String getAuthenticatedShopperId(); Payment getPayment(); void setTokenRequest(final TokenRequest tokenRequest); void setAuthenticatedShopperId(final String authenticatedShopperId); void setPayment(final Payment payment); StoredCredentials getStoredCredentials(); void setStoredCredentials(final StoredCredentials storedCredentials); @Override String toString();  }
@Test public void paymentTokenCreateShouldContainTokenRequestIfProvided() throws WorldpayModelTransformationException { final CardTokenRequest cardTokenRequest = new CardTokenRequest(TOKEN_REQUEST, AUTHENTICATED_SHOPPER_ID, PAYMENT); final PaymentTokenCreate result = (PaymentTokenCreate) cardTokenRequest.transformToInternalModel(); assertEquals(TOKEN_REASON, result.getCreateToken().getTokenReason().getvalue()); assertEquals(TOKEN_EVENT_REFERENCE, result.getCreateToken().getTokenEventReference()); }
@Test public void paymentTokenCreateShouldNotContainTokenRequestIfNotProvided() throws WorldpayModelTransformationException { final CardTokenRequest cardTokenRequest = new CardTokenRequest(null, AUTHENTICATED_SHOPPER_ID, PAYMENT); final PaymentTokenCreate result = (PaymentTokenCreate) cardTokenRequest.transformToInternalModel(); assertNull(result.getCreateToken()); }
@Test public void paymentTokenCreateShouldContainPayment() throws WorldpayModelTransformationException { final CardTokenRequest cardTokenRequest = new CardTokenRequest(TOKEN_REQUEST, AUTHENTICATED_SHOPPER_ID, PAYMENT); final PaymentTokenCreate result = (PaymentTokenCreate) cardTokenRequest.transformToInternalModel(); final CSEDATA csePayment = (CSEDATA) result.getPaymentInstrumentOrCSEDATA().get(0); final com.worldpay.internal.model.Address address = csePayment.getCardAddress().getAddress(); assertEquals(PAYMENT.getEncryptedData(), csePayment.getEncryptedData()); assertEquals(BILLING_ADDRESS.getFirstName(), address.getFirstName()); assertEquals(BILLING_ADDRESS.getLastName(), address.getLastName()); for (final Object addressLine : address.getStreetOrHouseNameOrHouseNumberOrHouseNumberExtensionOrAddress1OrAddress2OrAddress3()) { if (addressLine instanceof Address1) { assertEquals(BILLING_ADDRESS.getAddress1(), ((Address1) addressLine).getvalue()); } else if (addressLine instanceof Address2) { assertEquals(BILLING_ADDRESS.getAddress2(), ((Address2) addressLine).getvalue()); } else { assertEquals(BILLING_ADDRESS.getAddress3(), ((Address3) addressLine).getvalue()); } } assertEquals(BILLING_ADDRESS.getPostalCode(), address.getPostalCode()); assertEquals(BILLING_ADDRESS.getCity(), address.getCity()); assertEquals(BILLING_ADDRESS.getCountryCode(), address.getCountryCode()); }