Search in sources :

Example 6 with FRScheduledPayment

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

the class ScheduledPaymentApiControllerIT method testInternalGetScheduledPayments.

@Test
public void testInternalGetScheduledPayments() throws UnirestException {
    // Given
    springSecForTest.mockAuthCollector.mockAuthorities(OBRIRole.ROLE_AISP);
    FRScheduledPayment payment = JMockData.mock(FRScheduledPayment.class);
    payment.setStatus(ScheduledPaymentStatus.PENDING);
    payment.setCreated(new java.util.Date(0));
    scheduledPaymentRepository.save(payment);
    // When
    HttpResponse<List> response = Unirest.get("https://rs-store:" + port + "/api/accounts/scheduled-payments/search/find").queryString("status", ScheduledPaymentStatus.PENDING).queryString("toDateTime", DateTime.now().toString(ISODateTimeFormat.dateTimeNoMillis())).asObject(List.class);
    log.debug("Response: {}", response.getBody());
    // Then
    assertThat(response.getStatus()).isEqualTo(200);
    assertThat(response.getBody().isEmpty()).isEqualTo(false);
}
Also used : FRScheduledPayment(com.forgerock.openbanking.common.model.openbanking.persistence.account.FRScheduledPayment) List(java.util.List) SpringSecForTest(com.forgerock.openbanking.integration.test.support.SpringSecForTest) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 7 with FRScheduledPayment

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

the class AcceptDueScheduledPaymentTask method payDueScheduledPayments.

@Scheduled(fixedRate = 60 * 1000)
@SchedulerLock(name = "payDueScheduledPayments")
public void payDueScheduledPayments() {
    log.info("Scheduled payments payment task waking up. The time is now {}.", format.print(DateTime.now()));
    for (FRScheduledPayment scheduledPayment : scheduledPaymentService.getPendingAndDueScheduledPayments()) {
        FRScheduledPaymentData payment = scheduledPayment.getScheduledPayment();
        if (!payment.getScheduledPaymentDateTime().isBefore(DateTime.now())) {
            log.warn("Scheduled payment: {} with scheduledPaymentDateTime: {} is not due yet and should not have been loaded", scheduledPayment.getId(), payment.getScheduledPaymentDateTime());
            continue;
        }
        log.info("Processing scheduled payment {}", payment);
        try {
            String identificationFrom = moveDebitPayment(scheduledPayment);
            Optional<Account> isAccountFromOurs = accountStoreService.findAccountByIdentification(identificationFrom);
            if (isAccountFromOurs.isPresent()) {
                moveCreditPayment(scheduledPayment, identificationFrom, isAccountFromOurs.get());
            } else {
                log.info("Account '{}' not ours", identificationFrom);
            }
            log.info("Update payment status to completed");
            scheduledPayment.setStatus(ScheduledPaymentStatus.COMPLETED);
            log.debug("Payment {}", payment);
        } catch (CurrencyConverterException e) {
            log.info("Can't convert amount in the right currency", e);
            log.info("Update payment status to rejected");
            scheduledPayment.setRejectionReason("Can't convert amount in the right currency: " + e.getMessage());
            scheduledPayment.setStatus(ScheduledPaymentStatus.REJECTED);
            log.info("Payment {}", payment);
        } catch (Exception e) {
            log.error("Couldn't pay scheduled payment.", e);
            log.info("Update payment status to rejected");
            scheduledPayment.setRejectionReason("Failed to execute payment: " + e.getMessage());
            scheduledPayment.setStatus(ScheduledPaymentStatus.REJECTED);
            log.info("Payment {}", payment);
        } finally {
            scheduledPaymentService.updateSchedulePayment(scheduledPayment);
            paymentNotificationService.paymentStatusChanged(scheduledPayment);
        }
    }
    log.info("All due scheduled payments are now completed. The time is now {}.", format.print(DateTime.now()));
}
Also used : FRScheduledPaymentData(com.forgerock.openbanking.common.model.openbanking.domain.account.FRScheduledPaymentData) Account(com.forgerock.openbanking.common.model.openbanking.persistence.account.Account) CurrencyConverterException(com.tunyk.currencyconverter.api.CurrencyConverterException) FRScheduledPayment(com.forgerock.openbanking.common.model.openbanking.persistence.account.FRScheduledPayment) CurrencyConverterException(com.tunyk.currencyconverter.api.CurrencyConverterException) Scheduled(org.springframework.scheduling.annotation.Scheduled) SchedulerLock(net.javacrumbs.shedlock.core.SchedulerLock)

Example 8 with FRScheduledPayment

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

the class ScheduledPaymentsApiController method getScheduledPayments.

@Override
public ResponseEntity<OBReadScheduledPayment2> getScheduledPayments(@ApiParam(value = "The unique id of the ASPSP to which the request is issued. The unique id will be issued by OB.", required = true) @RequestHeader(value = "x-fapi-financial-id", required = true) String xFapiFinancialId, @ApiParam(value = "Page number.", required = false, defaultValue = "0") @RequestParam(value = "page", defaultValue = "0") int page, @ApiParam(value = "An Authorisation Token as per https://tools.ietf.org/html/rfc6750", required = true) @RequestHeader(value = "Authorization", required = true) String authorization, @ApiParam(value = "The time when the PSU last logged in with the TPP.  " + "All dates in the HTTP headers are represented as RFC 7231 Full Dates. An example is below:  " + "Sun, 10 Sep 2017 19:43:31 UTC") @RequestHeader(value = "x-fapi-customer-last-logged-time", required = false) @DateTimeFormat(pattern = HTTP_DATE_FORMAT) DateTime xFapiCustomerLastLoggedTime, @ApiParam(value = "The PSU's IP address if the PSU is currently logged in with the TPP.") @RequestHeader(value = "x-fapi-customer-ip-address", required = false) String xFapiCustomerIpAddress, @ApiParam(value = "An RFC4122 UID used as a correlation id.") @RequestHeader(value = "x-fapi-interaction-id", required = false) String xFapiInteractionId, @ApiParam(value = "Indicates the user-agent that the PSU is using.") @RequestHeader(value = "x-customer-user-agent", required = false) String xCustomerUserAgent, @ApiParam(value = "The OB account IDs") @RequestHeader(value = "x-ob-account-ids", required = true) List<String> accountIds, @ApiParam(value = "The OB permissions") @RequestHeader(value = "x-ob-permissions", required = true) List<OBExternalPermissions1Code> permissions, @ApiParam(value = "The origin http url") @RequestHeader(value = "x-ob-url", required = true) String httpUrl) throws OBErrorResponseException {
    log.info("Reading schedule payment from account ids {}", accountIds);
    Page<FRScheduledPayment> scheduledPayments = frScheduledPaymentRepository.byAccountIdInWithPermissions(accountIds, toFRExternalPermissionsCodeList(permissions), PageRequest.of(page, PAGE_LIMIT_SCHEDULE_PAYMENTS));
    int totalPages = scheduledPayments.getTotalPages();
    return ResponseEntity.ok(new OBReadScheduledPayment2().data(new OBReadScheduledPayment2Data().scheduledPayment(scheduledPayments.getContent().stream().map(FRScheduledPayment::getScheduledPayment).map(FRScheduledPaymentConverter::toOBScheduledPayment2).map(dd -> accountDataInternalIdFilter.apply(dd)).collect(Collectors.toList()))).links(PaginationUtil.generateLinks(httpUrl, page, totalPages)).meta(PaginationUtil.generateMetaData(totalPages)));
}
Also used : OBReadScheduledPayment2Data(uk.org.openbanking.datamodel.account.OBReadScheduledPayment2Data) FRScheduledPayment(com.forgerock.openbanking.common.model.openbanking.persistence.account.FRScheduledPayment) OBReadScheduledPayment2(uk.org.openbanking.datamodel.account.OBReadScheduledPayment2)

Example 9 with FRScheduledPayment

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

the class FRScheduledPaymentRepositoryImpl method filter.

private Page<FRScheduledPayment> filter(Page<FRScheduledPayment> scheduledPayments, List<FRExternalPermissionsCode> permissions) {
    for (FRScheduledPayment scheduledPayment : scheduledPayments) {
        for (FRExternalPermissionsCode permission : permissions) {
            switch(permission) {
                case READSCHEDULEDPAYMENTSBASIC:
                    scheduledPayment.getScheduledPayment().setCreditorAccount(null);
                    scheduledPayment.getScheduledPayment().setCreditorAgent(null);
                    break;
            }
        }
    }
    return scheduledPayments;
}
Also used : FRScheduledPayment(com.forgerock.openbanking.common.model.openbanking.persistence.account.FRScheduledPayment) FRExternalPermissionsCode(com.forgerock.openbanking.common.model.openbanking.domain.account.common.FRExternalPermissionsCode)

Example 10 with FRScheduledPayment

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

the class AcceptDomesticScheduledPaymentTaskTest method scheduledPaymentDue_shouldCreditAccount.

@Test
public void scheduledPaymentDue_shouldCreditAccount() throws CurrencyConverterException {
    // Given
    FRScheduledPayment payment = defaultPayment(DateTime.now().minusDays(1), ScheduledPaymentStatus.PENDING);
    given(paymentsService.getPendingAndDueScheduledPayments()).willReturn(Collections.singletonList(payment));
    given(account2StoreService.getAccount(DEBIT_ACCOUNT)).willReturn(defaultAccount(DEBIT_ACCOUNT));
    FRAccount account = defaultAccount(CREDIT_ACCOUNT);
    given(account2StoreService.findAccountByIdentification(CREDIT_ACCOUNT)).willReturn(Optional.of(account));
    // When
    acceptDueScheduledPaymentTask.payDueScheduledPayments();
    // Then
    verify(moneyService).moveMoney(eq(account), eq(payment.getScheduledPayment().getInstructedAmount()), eq(FRCreditDebitIndicator.CREDIT), eq(payment), any());
    verify(paymentsService).updateSchedulePayment(argThat(p -> p.getStatus().equals(ScheduledPaymentStatus.COMPLETED)));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) FRScheduledPaymentData(com.forgerock.openbanking.common.model.openbanking.domain.account.FRScheduledPaymentData) 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) ScheduledPaymentStatus(com.forgerock.openbanking.common.model.openbanking.status.ScheduledPaymentStatus) 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) FRScheduledPayment(com.forgerock.openbanking.common.model.openbanking.persistence.account.FRScheduledPayment) 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) CurrencyConverterException(com.tunyk.currencyconverter.api.CurrencyConverterException) ScheduledPaymentService(com.forgerock.openbanking.common.services.store.account.scheduledpayment.ScheduledPaymentService) Optional(java.util.Optional) 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) FRScheduledPayment(com.forgerock.openbanking.common.model.openbanking.persistence.account.FRScheduledPayment) Test(org.junit.Test)

Aggregations

FRScheduledPayment (com.forgerock.openbanking.common.model.openbanking.persistence.account.FRScheduledPayment)14 Test (org.junit.Test)6 FRScheduledPaymentData (com.forgerock.openbanking.common.model.openbanking.domain.account.FRScheduledPaymentData)5 CurrencyConverterException (com.tunyk.currencyconverter.api.CurrencyConverterException)5 MoneyService (com.forgerock.openbanking.aspsp.rs.simulator.service.MoneyService)4 PaymentNotificationFacade (com.forgerock.openbanking.aspsp.rs.simulator.service.PaymentNotificationFacade)4 FRCreditDebitIndicator (com.forgerock.openbanking.common.model.openbanking.domain.account.common.FRCreditDebitIndicator)4 FRAccountIdentifier (com.forgerock.openbanking.common.model.openbanking.domain.common.FRAccountIdentifier)4 FRAmount (com.forgerock.openbanking.common.model.openbanking.domain.common.FRAmount)4 FRAccount (com.forgerock.openbanking.common.model.openbanking.persistence.account.FRAccount)4 ScheduledPaymentStatus (com.forgerock.openbanking.common.model.openbanking.status.ScheduledPaymentStatus)4 AccountStoreService (com.forgerock.openbanking.common.services.store.account.AccountStoreService)4 ScheduledPaymentService (com.forgerock.openbanking.common.services.store.account.scheduledpayment.ScheduledPaymentService)4 Collections (java.util.Collections)4 Optional (java.util.Optional)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 DateTime (org.joda.time.DateTime)4 RunWith (org.junit.runner.RunWith)4 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)4 ArgumentMatchers.argThat (org.mockito.ArgumentMatchers.argThat)4