From 02984fccd98a43d6b11970d860d51f9ef29def61 Mon Sep 17 00:00:00 2001 From: adamg-hmcts Date: Tue, 24 Dec 2024 13:39:07 +0000 Subject: [PATCH] Create service to process payment callbacks --- .../CaseworkerGenerateNotifications.java | 89 ------------------- .../controller/PaymentCallbackController.java | 34 ++----- .../dto => payment/callback}/FeeDto.java | 2 +- .../callback/PaymentCallbackDto.java} | 4 +- .../callback/PaymentCallbackService.java | 71 +++++++++++++++ .../event/page/SolFinalOrderPayment.java | 1 - 6 files changed, 80 insertions(+), 121 deletions(-) delete mode 100644 src/main/java/uk/gov/hmcts/divorce/caseworker/event/CaseworkerGenerateNotifications.java rename src/main/java/uk/gov/hmcts/divorce/{controller/dto => payment/callback}/FeeDto.java (94%) rename src/main/java/uk/gov/hmcts/divorce/{controller/dto/PaymentDto.java => payment/callback/PaymentCallbackDto.java} (96%) create mode 100644 src/main/java/uk/gov/hmcts/divorce/payment/callback/PaymentCallbackService.java diff --git a/src/main/java/uk/gov/hmcts/divorce/caseworker/event/CaseworkerGenerateNotifications.java b/src/main/java/uk/gov/hmcts/divorce/caseworker/event/CaseworkerGenerateNotifications.java deleted file mode 100644 index 5fbaef6db3b..00000000000 --- a/src/main/java/uk/gov/hmcts/divorce/caseworker/event/CaseworkerGenerateNotifications.java +++ /dev/null @@ -1,89 +0,0 @@ -package uk.gov.hmcts.divorce.caseworker.event; - -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Component; -import uk.gov.hmcts.ccd.sdk.api.CCDConfig; -import uk.gov.hmcts.ccd.sdk.api.CaseDetails; -import uk.gov.hmcts.ccd.sdk.api.ConfigBuilder; -import uk.gov.hmcts.ccd.sdk.api.callback.AboutToStartOrSubmitResponse; -import uk.gov.hmcts.ccd.sdk.type.YesOrNo; -import uk.gov.hmcts.divorce.citizen.notification.Applicant1IntendToSwitchToSoleFoNotification; -import uk.gov.hmcts.divorce.common.ccd.PageBuilder; -import uk.gov.hmcts.divorce.common.notification.Applicant2SolicitorAppliedForFinalOrderNotification; -import uk.gov.hmcts.divorce.common.notification.ApplicantSwitchToSoleAfterIntentionFONotification; -import uk.gov.hmcts.divorce.common.notification.PartnerNotAppliedForFinalOrderNotification; -import uk.gov.hmcts.divorce.divorcecase.model.CaseData; -import uk.gov.hmcts.divorce.divorcecase.model.State; -import uk.gov.hmcts.divorce.divorcecase.model.UserRole; -import uk.gov.hmcts.divorce.solicitor.notification.SolicitorIntendsToSwitchToSoleFoNotification; - -import static uk.gov.hmcts.divorce.divorcecase.model.State.POST_SUBMISSION_STATES_WITH_WITHDRAWN_AND_REJECTED; -import static uk.gov.hmcts.divorce.divorcecase.model.UserRole.CASE_WORKER; -import static uk.gov.hmcts.divorce.divorcecase.model.UserRole.CITIZEN; -import static uk.gov.hmcts.divorce.divorcecase.model.UserRole.JUDGE; -import static uk.gov.hmcts.divorce.divorcecase.model.UserRole.LEGAL_ADVISOR; -import static uk.gov.hmcts.divorce.divorcecase.model.UserRole.SOLICITOR; -import static uk.gov.hmcts.divorce.divorcecase.model.UserRole.SUPER_USER; -import static uk.gov.hmcts.divorce.divorcecase.model.access.Permissions.CREATE_READ_UPDATE; - -@Component -@Slf4j -public class CaseworkerGenerateNotifications implements CCDConfig { - - @Autowired - private PartnerNotAppliedForFinalOrderNotification partnerNotAppliedForFinalOrderNotification; - - @Autowired - private Applicant1IntendToSwitchToSoleFoNotification applicant1IntendToSwitchToSoleFoNotification; - - @Autowired - private SolicitorIntendsToSwitchToSoleFoNotification solicitorIntendsToSwitchToSoleFoNotification; - - @Autowired - private ApplicantSwitchToSoleAfterIntentionFONotification applicantSwitchToSoleAfterIntentionFONotification; - - @Autowired - private Applicant2SolicitorAppliedForFinalOrderNotification applicant2SolicitorAppliedForFinalOrderNotification; - - @Override - public void configure(final ConfigBuilder configBuilder) { - new PageBuilder(configBuilder - .event("caseworker-create-notifications") - .forStates(POST_SUBMISSION_STATES_WITH_WITHDRAWN_AND_REJECTED) - .name("Create Notifications (TEST)") - .description("Create Notifications (TEST)") - .showSummary() - .showEventNotes() - .aboutToSubmitCallback(this::aboutToSubmit) - .grant(CREATE_READ_UPDATE, CASE_WORKER) - .grantHistoryOnly(SUPER_USER, LEGAL_ADVISOR, JUDGE, SOLICITOR, CITIZEN, JUDGE)); - } - - public AboutToStartOrSubmitResponse aboutToSubmit(final CaseDetails details, - final CaseDetails beforeDetails) { - - details.getData().getApplicant1().setLanguagePreferenceWelsh(YesOrNo.NO); - details.getData().getApplicant2().setLanguagePreferenceWelsh(YesOrNo.NO); - sendNotifications(details); - - details.getData().getApplicant1().setLanguagePreferenceWelsh(YesOrNo.YES); - details.getData().getApplicant2().setLanguagePreferenceWelsh(YesOrNo.YES); - sendNotifications(details); - - return AboutToStartOrSubmitResponse.builder() - .data(details.getData()) - .build(); - } - - private void sendNotifications(final CaseDetails details) { -// partnerNotAppliedForFinalOrderNotification.sendToApplicant1(details.getData(), details.getId()); -// applicant1IntendToSwitchToSoleFoNotification.sendToApplicant1(details.getData(), details.getId()); -// applicant1IntendToSwitchToSoleFoNotification.sendToApplicant2(details.getData(), details.getId()); -// solicitorIntendsToSwitchToSoleFoNotification.sendToApplicant2(details.getData(), details.getId()); -// applicantSwitchToSoleAfterIntentionFONotification.sendToApplicant1(details.getData(), details.getId()); - applicant1IntendToSwitchToSoleFoNotification.sendToApplicant2Solicitor(details.getData(), details.getId()); - applicantSwitchToSoleAfterIntentionFONotification.sendToApplicant1Solicitor(details.getData(), details.getId()); - applicant2SolicitorAppliedForFinalOrderNotification.sendToApplicant2Solicitor(details.getData(), details.getId()); - } -} diff --git a/src/main/java/uk/gov/hmcts/divorce/controller/PaymentCallbackController.java b/src/main/java/uk/gov/hmcts/divorce/controller/PaymentCallbackController.java index bb951aa1451..ede6be9d0d8 100644 --- a/src/main/java/uk/gov/hmcts/divorce/controller/PaymentCallbackController.java +++ b/src/main/java/uk/gov/hmcts/divorce/controller/PaymentCallbackController.java @@ -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 @@ -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 = { @@ -47,26 +39,12 @@ public class PaymentCallbackController { produces = MediaType.APPLICATION_JSON_VALUE ) public ResponseEntity 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 updatePayment() { return new ResponseEntity<>(HttpStatus.OK); } } diff --git a/src/main/java/uk/gov/hmcts/divorce/controller/dto/FeeDto.java b/src/main/java/uk/gov/hmcts/divorce/payment/callback/FeeDto.java similarity index 94% rename from src/main/java/uk/gov/hmcts/divorce/controller/dto/FeeDto.java rename to src/main/java/uk/gov/hmcts/divorce/payment/callback/FeeDto.java index 81363707d36..cadd2d549f8 100644 --- a/src/main/java/uk/gov/hmcts/divorce/controller/dto/FeeDto.java +++ b/src/main/java/uk/gov/hmcts/divorce/payment/callback/FeeDto.java @@ -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; diff --git a/src/main/java/uk/gov/hmcts/divorce/controller/dto/PaymentDto.java b/src/main/java/uk/gov/hmcts/divorce/payment/callback/PaymentCallbackDto.java similarity index 96% rename from src/main/java/uk/gov/hmcts/divorce/controller/dto/PaymentDto.java rename to src/main/java/uk/gov/hmcts/divorce/payment/callback/PaymentCallbackDto.java index 30368452dec..89126b7eb8f 100644 --- a/src/main/java/uk/gov/hmcts/divorce/controller/dto/PaymentDto.java +++ b/src/main/java/uk/gov/hmcts/divorce/payment/callback/PaymentCallbackDto.java @@ -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; @@ -25,7 +25,7 @@ @AllArgsConstructor @NoArgsConstructor @Data -public class PaymentDto { +public class PaymentCallbackDto { private String id; diff --git a/src/main/java/uk/gov/hmcts/divorce/payment/callback/PaymentCallbackService.java b/src/main/java/uk/gov/hmcts/divorce/payment/callback/PaymentCallbackService.java new file mode 100644 index 00000000000..10f9d8fd60b --- /dev/null +++ b/src/main/java/uk/gov/hmcts/divorce/payment/callback/PaymentCallbackService.java @@ -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; + } + } +} diff --git a/src/main/java/uk/gov/hmcts/divorce/solicitor/event/page/SolFinalOrderPayment.java b/src/main/java/uk/gov/hmcts/divorce/solicitor/event/page/SolFinalOrderPayment.java index 027ee15d7b4..664abd37137 100644 --- a/src/main/java/uk/gov/hmcts/divorce/solicitor/event/page/SolFinalOrderPayment.java +++ b/src/main/java/uk/gov/hmcts/divorce/solicitor/event/page/SolFinalOrderPayment.java @@ -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;