Skip to content

Commit

Permalink
Create service to process payment callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
adamg-hmcts committed Dec 24, 2024
1 parent 9f9a95c commit 02984fc
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 121 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RestController;
import uk.gov.hmcts.divorce.idam.IdamService;
import uk.gov.hmcts.divorce.idam.User;
import uk.gov.hmcts.divorce.systemupdate.service.CcdUpdateService;
import uk.gov.hmcts.reform.authorisation.generators.AuthTokenGenerator;
import uk.gov.hmcts.divorce.payment.callback.PaymentCallbackDto;
import uk.gov.hmcts.divorce.payment.callback.PaymentCallbackService;

import static uk.gov.hmcts.divorce.common.config.ControllerConstants.SERVICE_AUTHORIZATION;
import static uk.gov.hmcts.divorce.systemupdate.event.SystemUpdatePaymentStatus.SYSTEM_UPDATE_PAYMENT_STATUS;

@RestController
@Slf4j
Expand All @@ -28,11 +24,7 @@ public class PaymentCallbackController {

public static final String PAYMENT_UPDATE_PATH = "/payment-update";

private final CcdUpdateService ccdUpdateService;

private final IdamService idamService;

private final AuthTokenGenerator authTokenGenerator;
private final PaymentCallbackService paymentCallbackService;

@Operation(summary = "Update payment", description = "Update payment")
@ApiResponses(value = {
Expand All @@ -47,26 +39,12 @@ public class PaymentCallbackController {
produces = MediaType.APPLICATION_JSON_VALUE
) public ResponseEntity<HttpStatus> updatePayment(
@RequestHeader(value = SERVICE_AUTHORIZATION) String s2sAuthToken,
@RequestBody uk.gov.hmcts.divorce.controller.dto.PaymentDto paymentDto
@RequestBody PaymentCallbackDto paymentCallbackDto
) {
log.info("Payment Callback Received For Case: {}", paymentDto.getCcdCaseNumber());

User systemUpdateUser = idamService.retrieveSystemUpdateUserDetails();

final String serviceAuthorization = authTokenGenerator.generate();
log.info("Payment Callback Received For Case: {}", paymentCallbackDto.getCcdCaseNumber());

ccdUpdateService.submitEvent(
Long.parseLong(paymentDto.getCcdCaseNumber()),
SYSTEM_UPDATE_PAYMENT_STATUS,
systemUpdateUser,
serviceAuthorization
);

return new ResponseEntity<>(HttpStatus.OK);
}
paymentCallbackService.handleCallback(paymentCallbackDto);

@GetMapping("/payment-test")
public ResponseEntity<HttpStatus> updatePayment() {
return new ResponseEntity<>(HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uk.gov.hmcts.divorce.controller.dto;
package uk.gov.hmcts.divorce.payment.callback;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uk.gov.hmcts.divorce.controller.dto;
package uk.gov.hmcts.divorce.payment.callback;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
Expand All @@ -25,7 +25,7 @@
@AllArgsConstructor
@NoArgsConstructor
@Data
public class PaymentDto {
public class PaymentCallbackDto {

private String id;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package uk.gov.hmcts.divorce.payment.callback;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import uk.gov.hmcts.divorce.common.service.task.UpdateSuccessfulPaymentStatus;
import uk.gov.hmcts.divorce.divorcecase.model.PaymentStatus;
import uk.gov.hmcts.divorce.divorcecase.model.State;
import uk.gov.hmcts.divorce.idam.IdamService;
import uk.gov.hmcts.divorce.idam.User;
import uk.gov.hmcts.divorce.systemupdate.service.CcdUpdateService;
import uk.gov.hmcts.reform.authorisation.generators.AuthTokenGenerator;
import uk.gov.hmcts.reform.ccd.client.CoreCaseDataApi;
import uk.gov.hmcts.reform.ccd.client.model.CaseDetails;

import static uk.gov.hmcts.divorce.citizen.event.CitizenPaymentMade.CITIZEN_PAYMENT_MADE;
import static uk.gov.hmcts.divorce.citizen.event.RespondentFinalOrderPaymentMade.RESPONDENT_FINAL_ORDER_PAYMENT_MADE;

@Service
@RequiredArgsConstructor
@Slf4j
public class PaymentCallbackService {

private final CcdUpdateService ccdUpdateService;

private final IdamService idamService;

private final AuthTokenGenerator authTokenGenerator;

private final CoreCaseDataApi coreCaseDataApi;

private final UpdateSuccessfulPaymentStatus updateSuccessfulPaymentStatus;

public void handleCallback(PaymentCallbackDto paymentCallback) {
User systemUpdateUser = idamService.retrieveSystemUpdateUserDetails();
final String serviceAuthorization = authTokenGenerator.generate();

if (!PaymentStatus.SUCCESS.getLabel().equals(paymentCallback.getStatus())) {
log.info("Payment unsuccessful for case: {}, not processing callback", paymentCallback.getCcdCaseNumber());
}

String caseReference = paymentCallback.getCcdCaseNumber();
CaseDetails details = coreCaseDataApi.getCase(systemUpdateUser.getAuthToken(), serviceAuthorization, caseReference);
State state = State.valueOf(details.getState());

String paymentMadeEvent = paymentMadeEvent(state);

if (paymentMadeEvent != null) {
ccdUpdateService.submitEventWithRetry(
caseReference,
paymentMadeEvent(state),
updateSuccessfulPaymentStatus,
systemUpdateUser,
serviceAuthorization
);
} else {
log.info("Case not in awaiting payment state: {}, not processing callback", paymentCallback.getCcdCaseNumber());
}
}

private String paymentMadeEvent(State state) {
switch(state) {
case AwaitingPayment:
return CITIZEN_PAYMENT_MADE;
case AwaitingFinalOrderPayment:
return RESPONDENT_FINAL_ORDER_PAYMENT_MADE;
default:
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import feign.FeignException;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import uk.gov.hmcts.ccd.sdk.api.CaseDetails;
import uk.gov.hmcts.ccd.sdk.api.callback.AboutToStartOrSubmitResponse;
Expand Down

0 comments on commit 02984fc

Please sign in to comment.