CaptureRequestTransformer implements ServiceRequestTransformer { @Override public PaymentService transform(final ServiceRequest request) throws WorldpayModelTransformationException { if (request == null || request.getMerchantInfo() == null || request.getOrderCode() == null) { throw new WorldpayModelTransformationException("Request provided to do the capture is invalid."); } final CaptureServiceRequest captureRequest = (CaptureServiceRequest) request; final PaymentService paymentService = new PaymentService(); paymentService.setMerchantCode(request.getMerchantInfo().getMerchantCode()); paymentService.setVersion(configurationService.getConfiguration().getString(WORLDPAY_CONFIG_VERSION)); if (captureRequest.getAmount() == null) { throw new WorldpayModelTransformationException("No amount object to transform on the capture request"); } final Modify modify = new Modify(); final OrderModification orderModification = new OrderModification(); orderModification.setOrderCode(request.getOrderCode()); final Capture capture = new Capture(); capture.setAmount((Amount) captureRequest.getAmount().transformToInternalModel()); if (captureRequest.getDate() != null) { capture.setDate((Date) captureRequest.getDate().transformToInternalModel()); } setShippingInfo(captureRequest.getTrackingIds(), capture); orderModification.getCancelOrCaptureOrRefundOrRevokeOrAddBackOfficeCodeOrAuthoriseOrIncreaseAuthorisationOrCancelOrRefundOrDefendOrShopperWebformRefundDetailsOrExtendExpiryDateOrCancelRefundOrCancelRetryOrVoidSale().add(capture); modify.getOrderModificationOrBatchModificationOrAccountBatchModificationOrFuturePayAgreementModificationOrPaymentTokenUpdateOrPaymentTokenDelete().add(orderModification); paymentService.getSubmitOrModifyOrInquiryOrReplyOrNotifyOrVerify().add(modify); return paymentService; } CaptureRequestTransformer(final ConfigurationService configurationService); @Override PaymentService transform(final ServiceRequest request);  }
@Test public void transform_WhenACaptureRequestIsReceived_ShouldGenerateACaptureWithShippingInfosContainingTrackingIds() throws WorldpayModelTransformationException { final PaymentService result = testObj.transform(captureRequestMock); final Capture captureResult = getCaptureResult(result); final List<ShippingInfo> shippingInfoResults = captureResult.getShipping().getShippingInfo(); assertThat(shippingInfoResults.size()).isEqualTo(2); final List<String> trackingIdsFromShippingInfoResults = shippingInfoResults.stream().map(ShippingInfo::getTrackingId).collect(Collectors.toList()); assertThat(trackingIdsFromShippingInfoResults).contains(TRACKING_ID_1, TRACKING_ID_2); }
@Test public void transform_WhenCaptureRequestIsReceived_ShouldGenerateACaptureWithOutShippingIfThereIsNoTrackingInfoOnCaptureRequest() throws WorldpayModelTransformationException { when(captureRequestMock.getTrackingIds()).thenReturn(Collections.emptyList()); final PaymentService result = testObj.transform(captureRequestMock); final Capture captureResult = getCaptureResult(result); final Shipping shippingResult = captureResult.getShipping(); assertThat(shippingResult).isNull(); }
@Test public void transform_WhenCaptureRequestIsReceived_ShouldGenerateACaptureWithRequestedDateAndRequestedAmount() throws WorldpayModelTransformationException { final PaymentService result = testObj.transform(captureRequestMock); final Capture captureResult = getCaptureResult(result); assertThat(captureResult.getDate()).isEqualTo(internalDateMock); assertThat(captureResult.getAmount()).isEqualTo(internalAmountMock); }
@Test public void transform_WhenCaptureRequestIsReceived_ShouldGenerateAnOrderModificationWithRequestedOrderCode() throws WorldpayModelTransformationException { final PaymentService result = testObj.transform(captureRequestMock); final OrderModification orderModificationResult = getOrderModificationResult(result); assertThat(orderModificationResult.getOrderCode()).isEqualTo(ORDER_CODE); }
@Test public void transform_WhenCaptureRequestIsReceived_ShouldGenerateAPaymentServiceWithRequestedMerchantCodeAndWorldpayConfiguredVersion() throws WorldpayModelTransformationException { final PaymentService result = testObj.transform(captureRequestMock); assertThat(result.getMerchantCode()).isEqualTo(MERCHANT_CODE); assertThat(result.getVersion()).isEqualTo(VERSION); }
@Test(expected = WorldpayModelTransformationException.class) public void transform_WhenNullCaptureRequestIsReceived_ShouldThrownWorldpayModelTransformationExceptionWhenTheRequestIsNull() throws WorldpayModelTransformationException { testObj.transform(null); }
@Test(expected = WorldpayModelTransformationException.class) public void transform_WhenMerchantInfoIsNullOnCaptureRequest_ShouldThrownWorldpayModelTransformationExceptionWhenMerchantInfoOnRequestIsNull() throws WorldpayModelTransformationException { when(captureRequestMock.getMerchantInfo()).thenReturn(null); testObj.transform(captureRequestMock); }
@Test(expected = WorldpayModelTransformationException.class) public void transform_WhenOrderCodeIsNullOnCaptureRequest_ShouldThrownWorldpayModelTransformationExceptionWhenOrderCodeOnRequestIsNull() throws WorldpayModelTransformationException { when(captureRequestMock.getOrderCode()).thenReturn(null); testObj.transform(captureRequestMock); }
@Test(expected = WorldpayModelTransformationException.class) public void transform_WhenAmountIsNullOnCaptureRequest_ShouldThrownWorldpayModelTransformationExceptionWhenAmountOnRequestIsNull() throws WorldpayModelTransformationException { when(captureRequestMock.getAmount()).thenReturn(null); testObj.transform(captureRequestMock); }