Search in sources :

Example 1 with FRStandingOrder

use of com.forgerock.openbanking.common.model.openbanking.persistence.account.FRStandingOrder in project openbanking-aspsp by OpenBankingToolkit.

the class StandingOrdersApiController method getAccountStandingOrders.

@Override
public ResponseEntity<OBReadStandingOrder1> getAccountStandingOrders(@PathVariable("AccountId") String accountId, @RequestParam(value = "page", defaultValue = "0") int page, @RequestHeader(value = "x-fapi-financial-id", required = true) String xFapiFinancialId, @RequestHeader(value = "Authorization", required = true) String authorization, @RequestHeader(value = "x-fapi-customer-last-logged-time", required = false) @DateTimeFormat(pattern = HTTP_DATE_FORMAT) DateTime xFapiCustomerLastLoggedTime, @RequestHeader(value = "x-fapi-customer-ip-address", required = false) String xFapiCustomerIpAddress, @RequestHeader(value = "x-fapi-interaction-id", required = false) String xFapiInteractionId, @RequestHeader(value = "x-customer-user-agent", required = false) String xCustomerUserAgent, @RequestHeader(value = "x-ob-permissions", required = true) List<OBExternalPermissions1Code> permissions, @RequestHeader(value = "x-ob-url", required = true) String httpUrl) {
    LOGGER.info("Read standing orders for account  {} with minimumPermissions {}", accountId, permissions);
    Page<FRStandingOrder> standingOrdersResponse = frStandingOrderRepository.byAccountIdWithPermissions(accountId, toFRExternalPermissionsCodeList(permissions), PageRequest.of(page, PAGE_LIMIT_STANDING_ORDERS));
    List<OBStandingOrder1> standingOrders = standingOrdersResponse.stream().map(so -> toOBStandingOrder1(so.getStandingOrder())).collect(Collectors.toList());
    int totalPages = standingOrdersResponse.getTotalPages();
    return ResponseEntity.ok(new OBReadStandingOrder1().data(new OBReadDataStandingOrder1().standingOrder(standingOrders)).links(PaginationUtil.generateLinks(httpUrl, page, totalPages)).meta(PaginationUtil.generateMetaData(totalPages)));
}
Also used : PathVariable(org.springframework.web.bind.annotation.PathVariable) FRStandingOrderConverter.toOBStandingOrder1(com.forgerock.openbanking.common.services.openbanking.converter.account.FRStandingOrderConverter.toOBStandingOrder1) RequestParam(org.springframework.web.bind.annotation.RequestParam) OBStandingOrder1(uk.org.openbanking.datamodel.account.OBStandingOrder1) LoggerFactory(org.slf4j.LoggerFactory) ApiParam(io.swagger.annotations.ApiParam) Autowired(org.springframework.beans.factory.annotation.Autowired) Controller(org.springframework.stereotype.Controller) OBReadDataStandingOrder1(uk.org.openbanking.datamodel.account.OBReadDataStandingOrder1) DateTimeFormat(org.springframework.format.annotation.DateTimeFormat) FRExternalPermissionsCodeConverter.toFRExternalPermissionsCodeList(com.forgerock.openbanking.common.services.openbanking.converter.account.FRExternalPermissionsCodeConverter.toFRExternalPermissionsCodeList) Value(org.springframework.beans.factory.annotation.Value) OBExternalPermissions1Code(uk.org.openbanking.datamodel.account.OBExternalPermissions1Code) HTTP_DATE_FORMAT(com.forgerock.openbanking.constants.OpenBankingConstants.HTTP_DATE_FORMAT) FRStandingOrderRepository(com.forgerock.openbanking.aspsp.rs.store.repository.accounts.standingorders.FRStandingOrderRepository) PaginationUtil(com.forgerock.openbanking.aspsp.rs.store.utils.PaginationUtil) Logger(org.slf4j.Logger) OBErrorResponseException(com.forgerock.openbanking.exceptions.OBErrorResponseException) DateTime(org.joda.time.DateTime) PageRequest(org.springframework.data.domain.PageRequest) Page(org.springframework.data.domain.Page) Collectors(java.util.stream.Collectors) List(java.util.List) FRStandingOrder(com.forgerock.openbanking.common.model.openbanking.persistence.account.FRStandingOrder) OBReadStandingOrder1(uk.org.openbanking.datamodel.account.OBReadStandingOrder1) ResponseEntity(org.springframework.http.ResponseEntity) RequestHeader(org.springframework.web.bind.annotation.RequestHeader) OBReadDataStandingOrder1(uk.org.openbanking.datamodel.account.OBReadDataStandingOrder1) OBReadStandingOrder1(uk.org.openbanking.datamodel.account.OBReadStandingOrder1) FRStandingOrder(com.forgerock.openbanking.common.model.openbanking.persistence.account.FRStandingOrder) FRStandingOrderConverter.toOBStandingOrder1(com.forgerock.openbanking.common.services.openbanking.converter.account.FRStandingOrderConverter.toOBStandingOrder1) OBStandingOrder1(uk.org.openbanking.datamodel.account.OBStandingOrder1)

Example 2 with FRStandingOrder

use of com.forgerock.openbanking.common.model.openbanking.persistence.account.FRStandingOrder in project openbanking-aspsp by OpenBankingToolkit.

the class StandingOrderService method createStandingOrder.

public FRStandingOrder createStandingOrder(FRStandingOrderData standingOrder, String pispId) {
    log.debug("Create a standing order in the store. {}", standingOrder);
    FRStandingOrder frStandingOrder = FRStandingOrder.builder().standingOrder(standingOrder).accountId(standingOrder.getAccountId()).id(standingOrder.getStandingOrderId()).status(StandingOrderStatus.PENDING).pispId(pispId).build();
    return restTemplate.postForObject(rsStoreRoot + BASE_RESOURCE_PATH, frStandingOrder, FRStandingOrder.class);
}
Also used : FRStandingOrder(com.forgerock.openbanking.common.model.openbanking.persistence.account.FRStandingOrder)

Example 3 with FRStandingOrder

use of com.forgerock.openbanking.common.model.openbanking.persistence.account.FRStandingOrder in project openbanking-aspsp by OpenBankingToolkit.

the class StandingOrderService method getActiveStandingOrders.

public Collection<FRStandingOrder> getActiveStandingOrders() {
    log.debug("Get active standing orders in the store. {}");
    ParameterizedTypeReference<List<FRStandingOrder>> ptr = new ParameterizedTypeReference<List<FRStandingOrder>>() {
    };
    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(rsStoreRoot + BASE_RESOURCE_PATH + "search/active");
    URI uri = builder.build().encode().toUri();
    ResponseEntity<List<FRStandingOrder>> entity = restTemplate.exchange(uri, HttpMethod.GET, null, ptr);
    return entity.getBody();
}
Also used : ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) FRStandingOrder(com.forgerock.openbanking.common.model.openbanking.persistence.account.FRStandingOrder) List(java.util.List) URI(java.net.URI)

Example 4 with FRStandingOrder

use of com.forgerock.openbanking.common.model.openbanking.persistence.account.FRStandingOrder in project openbanking-aspsp by OpenBankingToolkit.

the class AcceptDueStandingOrderTask method payDueStandingOrders.

@Scheduled(fixedRate = 60 * 1000)
@SchedulerLock(name = "payDueStandingOrders")
public void payDueStandingOrders() {
    log.info("Standing order payment task waking up. The time is now {}.", format.print(DateTime.now()));
    final DateTime now = DateTime.now();
    for (FRStandingOrder frStandingOrder : standingOrderService.getActiveStandingOrders()) {
        FRStandingOrderData obStandingOrder = frStandingOrder.getStandingOrder();
        // Check the OB status code has an ACTIVE status (because standing orders could be imported on /data endpoint with INACTIVE status).
        if (FRStandingOrderData.FRStandingOrderStatus.ACTIVE != obStandingOrder.getStandingOrderStatusCode()) {
            log.warn("Standing Order: '{}' has been given an OBExternalStandingOrderStatus1Code of {} and will not be processed.", frStandingOrder, obStandingOrder.getStandingOrderStatusCode());
            continue;
        }
        log.info("Processing standing order {}", obStandingOrder);
        try {
            boolean isFirstPaymentDue = obStandingOrder.getFirstPaymentDateTime().isBefore(now);
            boolean isFinalPaymentDue = obStandingOrder.getFinalPaymentDateTime().isBefore(now);
            boolean isNextRecurringPaymentDue = obStandingOrder.getNextPaymentDateTime().isBefore(now);
            boolean isBeforeFinalPayment = obStandingOrder.getNextPaymentDateTime().isBefore(obStandingOrder.getFinalPaymentDateTime());
            log.debug("Standing order '{}', status: '{}', isFirstPaymentDue {} , isFinalPaymentDue {} , isNextPaymentDue: {}, isBeforeFinalPayment: {}", frStandingOrder.getId(), frStandingOrder.getStatus(), isFirstPaymentDue, isFinalPaymentDue, isNextRecurringPaymentDue, isBeforeFinalPayment);
            if (StandingOrderStatus.ACTIVE == frStandingOrder.getStatus() && isNextRecurringPaymentDue && isBeforeFinalPayment) {
                log.info("Active standing order '{}' has passed recurring payment date but not reached final payment date - make payment and calculate next recurring payment", frStandingOrder.getId());
                doCreditAndDebitPayment(frStandingOrder, obStandingOrder.getNextPaymentAmount());
                frStandingOrder.getStandingOrder().setNextPaymentDateTime(frequencyService.getNextDateTime(obStandingOrder.getNextPaymentDateTime(), obStandingOrder.getFrequency()));
            } else if (StandingOrderStatus.ACTIVE == frStandingOrder.getStatus() && isFinalPaymentDue) {
                log.info("Active standing order '{}' has passed final payment date - make final payment and set to COMPLETE", frStandingOrder.getId());
                doCreditAndDebitPayment(frStandingOrder, obStandingOrder.getFinalPaymentAmount());
                frStandingOrder.setStatus(StandingOrderStatus.COMPLETED);
            } else if (StandingOrderStatus.PENDING == frStandingOrder.getStatus() && isFirstPaymentDue) {
                log.info("Pending standing order '{}' has passed start payment date - make first payment and set to active", frStandingOrder.getId());
                doCreditAndDebitPayment(frStandingOrder, obStandingOrder.getFirstPaymentAmount());
                frStandingOrder.setStatus(StandingOrderStatus.ACTIVE);
            } else {
                log.debug("Active standing order '{}' is not due for payment", frStandingOrder.getId());
            }
        } catch (CurrencyConverterException e) {
            log.error("Can't convert amount in the right currency", e);
            log.error("Update payment status to rejected");
            frStandingOrder.setRejectionReason("Can't convert amount in the right currency: " + e.getMessage());
            frStandingOrder.setStatus(StandingOrderStatus.REJECTED);
            log.info("Rejected payment {}", obStandingOrder);
        } catch (Exception e) {
            log.error("Couldn't pay standing order payment.", e);
            log.error("Update payment status to rejected");
            frStandingOrder.setRejectionReason("Failed to execute payment: " + e.getMessage());
            frStandingOrder.setStatus(StandingOrderStatus.REJECTED);
            log.info("Rejected payment {}", obStandingOrder);
        } finally {
            standingOrderService.updateStandingOrder(frStandingOrder);
            paymentNotificationService.paymentStatusChanged(frStandingOrder);
        }
    }
    log.info("All due standing order payments are now completed. The time is now {}.", format.print(DateTime.now()));
}
Also used : CurrencyConverterException(com.tunyk.currencyconverter.api.CurrencyConverterException) FRStandingOrder(com.forgerock.openbanking.common.model.openbanking.persistence.account.FRStandingOrder) FRStandingOrderData(com.forgerock.openbanking.common.model.openbanking.domain.account.FRStandingOrderData) DateTime(org.joda.time.DateTime) CurrencyConverterException(com.tunyk.currencyconverter.api.CurrencyConverterException) Scheduled(org.springframework.scheduling.annotation.Scheduled) SchedulerLock(net.javacrumbs.shedlock.core.SchedulerLock)

Example 5 with FRStandingOrder

use of com.forgerock.openbanking.common.model.openbanking.persistence.account.FRStandingOrder in project openbanking-aspsp by OpenBankingToolkit.

the class AcceptDomesticStandingOrderTaskTest method shouldRejectPaymentWhenAnyException.

@Test
public void shouldRejectPaymentWhenAnyException() throws CurrencyConverterException {
    // Given
    FRStandingOrder payment = defaultPayment(StandingOrderStatus.PENDING);
    given(paymentsService.getActiveStandingOrders()).willReturn(Collections.singletonList(payment));
    FRAccount account = defaultAccount(DEBIT_ACCOUNT);
    given(account2StoreService.getAccount(DEBIT_ACCOUNT)).willReturn(account);
    doThrow(new RuntimeException("Simulated failure")).when(moneyService).moveMoney(any(), any(), any(), any(), any());
    // When
    acceptDueStandingOrderTask.payDueStandingOrders();
    // Then
    verify(moneyService).moveMoney(eq(account), any(), eq(FRCreditDebitIndicator.DEBIT), eq(payment), any());
    verify(paymentsService).updateStandingOrder(argThat(p -> p.getStatus().equals(StandingOrderStatus.REJECTED)));
    assertThat(payment.getRejectionReason()).isEqualTo("Failed to execute payment: Simulated failure");
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Mock(org.mockito.Mock) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) FrequencyService(com.forgerock.openbanking.common.services.openbanking.frequency.FrequencyService) StandingOrderStatus(com.forgerock.openbanking.common.model.openbanking.status.StandingOrderStatus) Mockito.verifyZeroInteractions(org.mockito.Mockito.verifyZeroInteractions) AccountStoreService(com.forgerock.openbanking.common.services.store.account.AccountStoreService) Mockito.doThrow(org.mockito.Mockito.doThrow) BDDMockito.given(org.mockito.BDDMockito.given) FRCreditDebitIndicator(com.forgerock.openbanking.common.model.openbanking.domain.account.common.FRCreditDebitIndicator) MoneyService(com.forgerock.openbanking.aspsp.rs.simulator.service.MoneyService) FRAccount(com.forgerock.openbanking.common.model.openbanking.persistence.account.FRAccount) InjectMocks(org.mockito.InjectMocks) FRAccountIdentifier(com.forgerock.openbanking.common.model.openbanking.domain.common.FRAccountIdentifier) DateTime(org.joda.time.DateTime) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) FRAmount(com.forgerock.openbanking.common.model.openbanking.domain.common.FRAmount) Mockito.verify(org.mockito.Mockito.verify) FRStandingOrderStatus(com.forgerock.openbanking.common.model.openbanking.domain.account.FRStandingOrderData.FRStandingOrderStatus) FRStandingOrder(com.forgerock.openbanking.common.model.openbanking.persistence.account.FRStandingOrder) CurrencyConverterException(com.tunyk.currencyconverter.api.CurrencyConverterException) StandingOrderService(com.forgerock.openbanking.common.services.store.account.standingorder.StandingOrderService) Optional(java.util.Optional) FRStandingOrderData(com.forgerock.openbanking.common.model.openbanking.domain.account.FRStandingOrderData) PaymentNotificationFacade(com.forgerock.openbanking.aspsp.rs.simulator.service.PaymentNotificationFacade) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Collections(java.util.Collections) FRAccount(com.forgerock.openbanking.common.model.openbanking.persistence.account.FRAccount) FRStandingOrder(com.forgerock.openbanking.common.model.openbanking.persistence.account.FRStandingOrder) Test(org.junit.Test)

Aggregations

FRStandingOrder (com.forgerock.openbanking.common.model.openbanking.persistence.account.FRStandingOrder)22 DateTime (org.joda.time.DateTime)13 FRStandingOrderData (com.forgerock.openbanking.common.model.openbanking.domain.account.FRStandingOrderData)7 List (java.util.List)7 Test (org.junit.Test)7 MoneyService (com.forgerock.openbanking.aspsp.rs.simulator.service.MoneyService)6 PaymentNotificationFacade (com.forgerock.openbanking.aspsp.rs.simulator.service.PaymentNotificationFacade)6 FRStandingOrderRepository (com.forgerock.openbanking.aspsp.rs.store.repository.accounts.standingorders.FRStandingOrderRepository)6 PaginationUtil (com.forgerock.openbanking.aspsp.rs.store.utils.PaginationUtil)6 FRStandingOrderStatus (com.forgerock.openbanking.common.model.openbanking.domain.account.FRStandingOrderData.FRStandingOrderStatus)6 FRCreditDebitIndicator (com.forgerock.openbanking.common.model.openbanking.domain.account.common.FRCreditDebitIndicator)6 FRAccountIdentifier (com.forgerock.openbanking.common.model.openbanking.domain.common.FRAccountIdentifier)6 FRAmount (com.forgerock.openbanking.common.model.openbanking.domain.common.FRAmount)6 FRAccount (com.forgerock.openbanking.common.model.openbanking.persistence.account.FRAccount)6 StandingOrderStatus (com.forgerock.openbanking.common.model.openbanking.status.StandingOrderStatus)6 FRExternalPermissionsCodeConverter.toFRExternalPermissionsCodeList (com.forgerock.openbanking.common.services.openbanking.converter.account.FRExternalPermissionsCodeConverter.toFRExternalPermissionsCodeList)6 FrequencyService (com.forgerock.openbanking.common.services.openbanking.frequency.FrequencyService)6 AccountStoreService (com.forgerock.openbanking.common.services.store.account.AccountStoreService)6 HTTP_DATE_FORMAT (com.forgerock.openbanking.constants.OpenBankingConstants.HTTP_DATE_FORMAT)6 OBErrorResponseException (com.forgerock.openbanking.exceptions.OBErrorResponseException)6