Skip to content

Commit

Permalink
ROC-6905 Add cancel payment operation
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewwa-kainos authored Feb 21, 2020
2 parents 1f3e1ea + 1f9ec81 commit 3eaddb4
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 39 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class PaymentsService {
public PaymentDTO retrievePayment(String authorisation, String paymentReference) {
return paymentsClient.retrievePayment(authorisation, paymentReference);
}

public void cancelPayment(String authorisation, String paymentReference) {
paymentsClient.cancelPayment(authorisation, paymentReference);
}
}

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class CreatePaymentTest extends BaseTest {
private static final String FEE_VERSION = "version";
private static final Integer FEE_VOLUME = 1;
private static final String INITIATED_STATUS = "Initiated";
private static final String FAILED_STATUS = "Failed";
private static final String JURISDICTION_1 = "jurisdiction 1";
private static final String JURISDICTION_2 = "jurisdiction 2";
private static final String MEMO_LINE = "Memo line";
Expand All @@ -45,44 +46,63 @@ class CreatePaymentTest extends BaseTest {
private static final String SERVICE_NAME = "Civil Money Claims";
private static final String SITE_ID = "AA00";

private static final CardPaymentRequest CARD_PAYMENT_REQUEST = CardPaymentRequest.builder()
.caseReference(UUID.randomUUID().toString())
.ccdCaseNumber(CCD_CASE_NUMBER)
.description(PAYMENT_DESCRIPTION)
.service(SERVICE)
.currency(CURRENCY)
.siteId(SITE_ID)
.fees(new FeeDto[]{
FeeDto.builder()
.id(1)
.calculatedAmount(FEE_AMOUNT)
.ccdCaseNumber(CCD_CASE_NUMBER)
.code(FEE_CODE)
.description(FEE_DESCRIPTION)
.jurisdiction1(JURISDICTION_1)
.jurisdiction2(JURISDICTION_2)
.memoLine(MEMO_LINE)
.naturalAccountCode(NATURAL_ACCOUNT_CODE)
.netAmount(NET_FEE_AMOUNT)
.reference(FEE_REFERENCE)
.version(FEE_VERSION)
.volume(FEE_VOLUME)
.build()
})
.amount(PAYMENT_AMOUNT)
.build();

@Test
void canCreateAndRetrievePayments() {
void canCreateRetrieveAndCancelPayments() {
User citizen = createCitizen();
PaymentDto createdPayment = paymentsClient.createPayment(
citizen.getAuthToken(),
CardPaymentRequest.builder()
.caseReference(UUID.randomUUID().toString())
.ccdCaseNumber(CCD_CASE_NUMBER)
.description(PAYMENT_DESCRIPTION)
.service(SERVICE)
.currency(CURRENCY)
.siteId(SITE_ID)
.fees(new FeeDto[]{
FeeDto.builder()
.id(1)
.calculatedAmount(FEE_AMOUNT)
.ccdCaseNumber(CCD_CASE_NUMBER)
.code(FEE_CODE)
.description(FEE_DESCRIPTION)
.jurisdiction1(JURISDICTION_1)
.jurisdiction2(JURISDICTION_2)
.memoLine(MEMO_LINE)
.naturalAccountCode(NATURAL_ACCOUNT_CODE)
.netAmount(NET_FEE_AMOUNT)
.reference(FEE_REFERENCE)
.version(FEE_VERSION)
.volume(FEE_VOLUME)
.build()
})
.amount(PAYMENT_AMOUNT)
.build(),
"https://www.google.com"
citizen.getAuthToken(),
CARD_PAYMENT_REQUEST,
"https://www.google.com"
);
verifyCreatedPayment(createdPayment);

assertNotNull(createdPayment);
final String paymentGroupReference = createdPayment.getPaymentGroupReference();
final String reference = createdPayment.getReference();

PaymentDto retrievedPayment = paymentsClient.retrievePayment(
citizen.getAuthToken(),
reference
);
verifyRetrievedPayment(retrievedPayment, paymentGroupReference, reference);

paymentsClient.cancelPayment(citizen.getAuthToken(), reference);

PaymentDto cancelledPayment = paymentsClient.retrievePayment(
citizen.getAuthToken(),
reference
);
verifyCancelledPayment(cancelledPayment, paymentGroupReference, reference);
}

private PaymentDto verifyCreatedPayment(PaymentDto createdPayment) {
assertNotNull(createdPayment);
assertAll("created payment",
() -> assertAll("links",
() -> assertNotNull(createdPayment.getLinks()),
Expand Down Expand Up @@ -122,16 +142,19 @@ void canCreateAndRetrievePayments() {
() -> assertNotNull(createdPayment.getExternalReference())
),
() -> assertAll("expected matching values",
() -> assertNotNull(paymentGroupReference),
() -> assertNotNull(reference)
() -> assertNotNull(createdPayment.getPaymentGroupReference()),
() -> assertNotNull(createdPayment.getReference())
)
);

PaymentDto retrievedPayment = paymentsClient.retrievePayment(
citizen.getAuthToken(),
createdPayment.getReference()
);
return createdPayment;
}

private void verifyRetrievedPayment(
PaymentDto retrievedPayment,
String expectedPaymentGroupReference,
String expectedReference
) {
assertNotNull(retrievedPayment);

assertAll("retrieved payment",
Expand Down Expand Up @@ -168,8 +191,72 @@ void canCreateAndRetrievePayments() {
() -> assertEquals(INITIATED_STATUS, retrievedPayment.getStatus())
),
() -> assertAll("expected matching values",
() -> assertEquals(paymentGroupReference, retrievedPayment.getPaymentGroupReference()),
() -> assertEquals(reference, retrievedPayment.getReference())
() -> assertEquals(expectedPaymentGroupReference, retrievedPayment.getPaymentGroupReference()),
() -> assertEquals(expectedReference, retrievedPayment.getReference())
),
() -> assertAll("fees",
() -> assertNotNull(retrievedPayment.getFees()),
() -> assertEquals(1, retrievedPayment.getFees().length),
() -> assertEquals(FEE_AMOUNT, retrievedPayment.getFees()[0].getCalculatedAmount()),
() -> assertEquals(CCD_CASE_NUMBER, retrievedPayment.getFees()[0].getCcdCaseNumber()),
() -> assertEquals(FEE_CODE, retrievedPayment.getFees()[0].getCode()),
() -> assertNull(retrievedPayment.getFees()[0].getDescription()),
() -> assertNotNull(retrievedPayment.getFees()[0].getId()),
() -> assertNull(retrievedPayment.getFees()[0].getJurisdiction1()),
() -> assertNull(retrievedPayment.getFees()[0].getJurisdiction2()),
() -> assertNull(retrievedPayment.getFees()[0].getMemoLine()),
() -> assertNull(retrievedPayment.getFees()[0].getNaturalAccountCode()),
() -> assertNull(retrievedPayment.getFees()[0].getNetAmount()),
() -> assertEquals(FEE_REFERENCE, retrievedPayment.getFees()[0].getReference()),
() -> assertEquals(FEE_VERSION, retrievedPayment.getFees()[0].getVersion()),
() -> assertEquals(FEE_VOLUME, retrievedPayment.getFees()[0].getVolume())
)
);
}

private void verifyCancelledPayment(
PaymentDto retrievedPayment,
String expectedPaymentGroupReference,
String expectedReference
) {
assertNotNull(retrievedPayment);

assertAll("retrieved payment",
() -> assertAll("links",
() -> assertNotNull(retrievedPayment.getLinks()),
() -> assertNull(retrievedPayment.getLinks().getNextUrl()),
() -> assertNotNull(retrievedPayment.getLinks().getSelf()),
() -> assertNotNull(retrievedPayment.getLinks().getSelf().getHref()),
() -> assertEquals(RequestMethod.GET, retrievedPayment.getLinks().getSelf().getMethod()),
() -> assertNull(retrievedPayment.getLinks().getCancel())
),
() -> assertAll("expected to be null",
() -> assertNull(retrievedPayment.getAccountNumber()),
() -> assertNull(retrievedPayment.getCustomerReference()),
() -> assertNull(retrievedPayment.getDateCreated()),
() -> assertNull(retrievedPayment.getDateUpdated()),
() -> assertNull(retrievedPayment.getGiroSlipNo()),
() -> assertNull(retrievedPayment.getId()),
() -> assertNull(retrievedPayment.getOrganisationName()),
() -> assertNull(retrievedPayment.getPaymentReference()),
() -> assertNull(retrievedPayment.getReportedDateOffline()),
() -> assertNull(retrievedPayment.getStatusHistories())
),
() -> assertAll("expected known values",
() -> assertEquals(BigDecimal.TEN, retrievedPayment.getAmount()),
() -> assertEquals(CCD_CASE_NUMBER, retrievedPayment.getCcdCaseNumber()),
() -> assertEquals(ONLINE_CHANNEL, retrievedPayment.getChannel()),
() -> assertEquals(CURRENCY, retrievedPayment.getCurrency()),
() -> assertEquals(PAYMENT_DESCRIPTION, retrievedPayment.getDescription()),
() -> assertEquals(EXTERNAL_PROVIDER, retrievedPayment.getExternalProvider()),
() -> assertEquals(PAYMENT_METHOD, retrievedPayment.getMethod()),
() -> assertEquals(SERVICE_NAME, retrievedPayment.getServiceName()),
() -> assertEquals(SITE_ID, retrievedPayment.getSiteId()),
() -> assertEquals(FAILED_STATUS, retrievedPayment.getStatus())
),
() -> assertAll("expected matching values",
() -> assertEquals(expectedPaymentGroupReference, retrievedPayment.getPaymentGroupReference()),
() -> assertEquals(expectedReference, retrievedPayment.getReference())
),
() -> assertAll("fees",
() -> assertNotNull(retrievedPayment.getFees()),
Expand All @@ -183,7 +270,7 @@ void canCreateAndRetrievePayments() {
() -> assertNull(retrievedPayment.getFees()[0].getJurisdiction2()),
() -> assertNull(retrievedPayment.getFees()[0].getMemoLine()),
() -> assertNull(retrievedPayment.getFees()[0].getNaturalAccountCode()),
() -> assertEquals(NET_FEE_AMOUNT, retrievedPayment.getFees()[0].getNetAmount()),
() -> assertNull(retrievedPayment.getFees()[0].getNetAmount()),
() -> assertEquals(FEE_REFERENCE, retrievedPayment.getFees()[0].getReference()),
() -> assertEquals(FEE_VERSION, retrievedPayment.getFees()[0].getVersion()),
() -> assertEquals(FEE_VOLUME, retrievedPayment.getFees()[0].getVolume())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,11 @@ PaymentDto retrieve(
@RequestHeader("Authorization") String authorization,
@RequestHeader("ServiceAuthorization") String serviceAuthorization
);

@PostMapping(value = "/card-payments/{paymentReference}/cancel")
void cancel(
@PathVariable("paymentReference") String paymentReference,
@RequestHeader("Authorization") String authorization,
@RequestHeader("ServiceAuthorization") String serviceAuthorization
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,12 @@ public PaymentDto retrievePayment(String authorisation, String paymentReference)
authTokenGenerator.generate()
);
}

public void cancelPayment(String authorisation, String paymentReference) {
paymentsApi.cancel(
paymentReference,
authorisation,
authTokenGenerator.generate()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpStatus;
import org.springframework.web.client.HttpClientErrorException;
import uk.gov.hmcts.reform.authorisation.generators.AuthTokenGenerator;

import java.math.BigDecimal;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand Down Expand Up @@ -55,6 +59,13 @@ void retrievePaymentShouldInvokePaymentsApi() {
verify(paymentsApi).retrieve("payment reference", "authorisation", "auth token");
}

@Test
void cancelPaymentShouldInvokePaymentsApi() {
client.cancelPayment("authorisation", "payment reference");

verify(paymentsApi).cancel("payment reference", "authorisation", "auth token");
}

@Test
void createPaymentShouldPropagateExceptions() {
when(authTokenGenerator.generate())
Expand All @@ -74,4 +85,14 @@ void retrievePaymentShouldPropagateExceptions() {
.isInstanceOf(RuntimeException.class)
.hasMessage("expected exception for retrieve payment");
}

@Test
void cancelPaymentShouldPropagateExceptions() {
doThrow(new HttpClientErrorException(HttpStatus.NOT_FOUND, "Payment Not found"))
.when(paymentsApi).cancel(anyString(), anyString(), anyString());

assertThatThrownBy(() -> client.cancelPayment("authorisation", "payment reference"))
.isInstanceOf(HttpClientErrorException.class)
.hasMessage("404 Payment Not found");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ void testRetrievePayment() {
);
}

@Test
void testCancelPayment() {
// test passes if no exceptions are thrown
paymentsClient.cancelPayment("Authorisation", "RC-7238-3245-0193-7732");
}

@Service
static class ServiceTestSupportAuthTokenGenerator implements AuthTokenGenerator {
@Override
Expand Down
24 changes: 24 additions & 0 deletions src/test/resources/mappings/cancel-payment-successful.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"id": "21e5df7c-6afa-404c-a44f-8354e18297c3",
"name": "card-payments_rc-7238-3245-0193-7732",
"request": {
"url": "/card-payments/RC-7238-3245-0193-7732/cancel",
"method": "POST"
},
"response": {
"status": 204,
"body": "{}",
"headers": {
"X-Content-Type-Options": "nosniff",
"X-XSS-Protection": "1; mode=block",
"Cache-Control": "no-cache, no-store, max-age=0, must-revalidate",
"Pragma": "no-cache",
"Expires": "0",
"X-Frame-Options": "DENY",
"Content-Type": "application/json;charset=UTF-8",
"Date": "Mon, 19 Aug 2019 09:46:54 GMT"
}
},
"uuid": "21e5df7c-6afa-404c-a44f-8354e18297c3",
"persistent": true
}

0 comments on commit 3eaddb4

Please sign in to comment.