AuthoriseRequestTransformer implements ServiceRequestTransformer { @Override public PaymentService transform(ServiceRequest request) throws WorldpayModelTransformationException { if (request == null || request.getMerchantInfo() == null || request.getOrderCode() == null) { throw new WorldpayModelTransformationException("Request provided to do the authorise is invalid."); } final AuthoriseServiceRequest authRequest = (AuthoriseServiceRequest) request; final PaymentService paymentService = new PaymentService(); paymentService.setMerchantCode(request.getMerchantInfo().getMerchantCode()); paymentService.setVersion(configurationService.getConfiguration().getString(WORLDPAY_CONFIG_VERSION)); if (authRequest.getOrder() == null) { throw new WorldpayModelTransformationException("No order object to transform on the authorise request"); } final Submit submit = new Submit(); final Order order = (Order) authRequest.getOrder().transformToInternalModel(); submit.getOrderOrOrderBatchOrShopperOrFuturePayAgreementOrMakeFuturePayPaymentOrIdentifyMeRequestOrPaymentTokenCreateOrChallenge().add(order); paymentService.getSubmitOrModifyOrInquiryOrReplyOrNotifyOrVerify().add(submit); return paymentService; }  @Override PaymentService transform(ServiceRequest request); @Required void setConfigurationService(final ConfigurationService configurationService);  }
@Test public void testTransformAuthoriseRequestToPaymentServiceWithTokenRequest() throws WorldpayModelTransformationException { final MerchantInfo merchantInfo = new MerchantInfo("MERCHANT_CODE", "MERCHANT_PASSWORD"); final BasicOrderInfo orderInfo = new BasicOrderInfo("DS1347889928107_3", "Your Order & Order desc", new Amount("100", "EUR", "2")); final Address shippingAddress = new Address(FIRST_NAME, LAST_NAME, SHOPPER_ADDRESS_1, SHOPPER_ADDRESS_2, SHOPPER_ADDRESS_3, POSTAL_CODE, CITY, GB); final Address billingAddress = new Address(FIRST_NAME, LAST_NAME, SHOPPER_ADDRESS_1, SHOPPER_ADDRESS_2, SHOPPER_ADDRESS_3, POSTAL_CODE, CITY, GB); final List<PaymentType> includedPTs = singletonList(ONLINE); final TokenRequest tokenRequest = new TokenRequest(TOKEN_REFERENCE, TOKEN_REASON); final Shopper shopper = new Shopper(SHOPPER_EMAIL_ADDRESS, AUTH_SHOPPER_ID, null, null); final AuthoriseRequestParameters authoriseRequestParameters = AuthoriseRequestParameters.AuthoriseRequestParametersBuilder.getInstance() .withMerchantInfo(merchantInfo) .withOrderInfo(orderInfo) .withOrderContent(ORDER_CONTENT) .withIncludedPTs(includedPTs) .withShopper(shopper) .withShippingAddress(shippingAddress) .withBillingAddress(billingAddress) .withTokenRequest(tokenRequest) .withStatementNarrative(STATEMENT_NARRATIVE_TEXT) .build(); final AuthoriseServiceRequest request = RedirectAuthoriseServiceRequest.createRedirectAuthoriseRequest(authoriseRequestParameters); final PaymentService result = testObj.transform(request); final List<Object> submitList = result.getSubmitOrModifyOrInquiryOrReplyOrNotifyOrVerify(); final Submit submit = (Submit) submitList.get(0); final List<Object> orderList = submit.getOrderOrOrderBatchOrShopperOrFuturePayAgreementOrMakeFuturePayPaymentOrIdentifyMeRequestOrPaymentTokenCreateOrChallenge(); final com.worldpay.internal.model.Order intOrder = (com.worldpay.internal.model.Order) orderList.get(0); final List<Object> orderElements = intOrder.getDescriptionOrAmountOrRiskOrOrderContentOrPaymentMethodMaskOrPaymentDetailsOrPayAsOrderOrShopperOrShippingAddressOrBillingAddressOrBranchSpecificExtensionOrRedirectPageAttributeOrPaymentMethodAttributeOrEchoDataOrStatementNarrativeOrHcgAdditionalDataOrThirdPartyDataOrResultURLOrShopperAdditionalDataOrApprovedAmountOrMandateOrAuthorisationAmountStatusOrDynamic3DSOrCreateTokenOrCreateTokenApprovalOrOrderLinesOrSubMerchantDataOrDynamicMCCOrDynamicInteractionTypeOrPrimeRoutingRequestOrRiskDataOrAdditional3DSDataOrExemptionOrShippingMethodOrProductSkuOrDeviceSessionOrInfo3DSecureOrSession(); final String description = ((Description) orderElements.stream().filter(Description.class::isInstance).findFirst().get()).getvalue(); assertEquals("Incorrect description", "Your Order & Order desc", description); final String orderCode = intOrder.getOrderCode(); assertTrue(orderElements.stream().anyMatch(CreateToken.class::isInstance)); final CreateToken createToken = ((CreateToken) orderElements.stream().filter(CreateToken.class::isInstance).findFirst().get()); assertEquals(TOKEN_REFERENCE, createToken.getTokenEventReference()); assertEquals(TOKEN_REASON, createToken.getTokenReason().getvalue()); final com.worldpay.internal.model.Shopper intShopper = ((com.worldpay.internal.model.Shopper) orderElements.stream().filter(com.worldpay.internal.model.Shopper.class::isInstance).findFirst().get()); assertEquals(AUTH_SHOPPER_ID, intShopper.getAuthenticatedShopperID()); assertEquals("Incorrect orderCode", "DS1347889928107_3", orderCode); assertEquals(merchantInfo.getMerchantCode(), result.getMerchantCode()); }
@Test public void testTransformAuthoriseRequestToPaymentService() throws WorldpayModelTransformationException { final MerchantInfo merchantInfo = new MerchantInfo("MERCHANT_CODE", "MERCHANT_PASSWORD"); final BasicOrderInfo orderInfo = new BasicOrderInfo("DS1347889928107_3", "Your Order & Order desc", new Amount("100", "EUR", "2")); final Address shippingAddress = new Address(FIRST_NAME, LAST_NAME, SHOPPER_ADDRESS_1, SHOPPER_ADDRESS_2, SHOPPER_ADDRESS_3, POSTAL_CODE, CITY, GB); final Address billingAddress = new Address(FIRST_NAME, LAST_NAME, SHOPPER_ADDRESS_1, SHOPPER_ADDRESS_2, SHOPPER_ADDRESS_3, POSTAL_CODE, CITY, GB); final List<PaymentType> includedPTs = singletonList(ONLINE); final Shopper shopper = new Shopper(SHOPPER_EMAIL_ADDRESS, AUTH_SHOPPER_ID, null, null); final AuthoriseRequestParameters authoriseRequestParameters = AuthoriseRequestParameters.AuthoriseRequestParametersBuilder.getInstance() .withMerchantInfo(merchantInfo) .withOrderInfo(orderInfo) .withOrderContent(ORDER_CONTENT) .withIncludedPTs(includedPTs) .withShopper(shopper) .withShippingAddress(shippingAddress) .withBillingAddress(billingAddress) .withStatementNarrative(STATEMENT_NARRATIVE_TEXT) .build(); final AuthoriseServiceRequest request = RedirectAuthoriseServiceRequest.createRedirectAuthoriseRequest(authoriseRequestParameters); final PaymentService result = testObj.transform(request); final List<Object> submitList = result.getSubmitOrModifyOrInquiryOrReplyOrNotifyOrVerify(); final Submit submit = (Submit) submitList.get(0); final List<Object> orderList = submit.getOrderOrOrderBatchOrShopperOrFuturePayAgreementOrMakeFuturePayPaymentOrIdentifyMeRequestOrPaymentTokenCreateOrChallenge(); final com.worldpay.internal.model.Order intOrder = (com.worldpay.internal.model.Order) orderList.get(0); final List<Object> orderElements = intOrder.getDescriptionOrAmountOrRiskOrOrderContentOrPaymentMethodMaskOrPaymentDetailsOrPayAsOrderOrShopperOrShippingAddressOrBillingAddressOrBranchSpecificExtensionOrRedirectPageAttributeOrPaymentMethodAttributeOrEchoDataOrStatementNarrativeOrHcgAdditionalDataOrThirdPartyDataOrResultURLOrShopperAdditionalDataOrApprovedAmountOrMandateOrAuthorisationAmountStatusOrDynamic3DSOrCreateTokenOrCreateTokenApprovalOrOrderLinesOrSubMerchantDataOrDynamicMCCOrDynamicInteractionTypeOrPrimeRoutingRequestOrRiskDataOrAdditional3DSDataOrExemptionOrShippingMethodOrProductSkuOrDeviceSessionOrInfo3DSecureOrSession(); final String description = ((Description) orderElements.stream().filter(Description.class::isInstance).findFirst().get()).getvalue(); assertEquals("Incorrect description", "Your Order & Order desc", description); final String orderCode = intOrder.getOrderCode(); assertFalse(orderElements.stream().anyMatch(CreateToken.class::isInstance)); assertEquals("Incorrect orderCode", "DS1347889928107_3", orderCode); assertEquals(merchantInfo.getMerchantCode(), result.getMerchantCode()); }