Search in sources :

Example 1 with CurrencyConverterException

use of com.tunyk.currencyconverter.api.CurrencyConverterException 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 2 with CurrencyConverterException

use of com.tunyk.currencyconverter.api.CurrencyConverterException in project openbanking-aspsp by OpenBankingToolkit.

the class AcceptInternationalPaymentTask method autoAcceptPayment.

@Scheduled(fixedRate = 60 * 1000)
@SchedulerLock(name = "internationalPayment")
public void autoAcceptPayment() {
    log.info("Auto-accept payment task waking up. The time is now {}.", format.print(DateTime.now()));
    Collection<FRInternationalConsent> allPaymentsInProcess = internationalPaymentService.getAllPaymentsInProcess();
    for (FRInternationalConsent payment : allPaymentsInProcess) {
        log.info("Processing payment {}", payment);
        try {
            String identificationTo = moveDebitPayment(payment);
            Optional<Account> isAccountToFromOurs = accountStoreService.findAccountByIdentification(identificationTo);
            if (isAccountToFromOurs.isPresent()) {
                moveCreditPayment(payment, identificationTo, isAccountToFromOurs.get());
            } else {
                log.info("Account '{}' not ours", identificationTo);
            }
            log.info("Update payment status to completed");
            payment.setStatus(ConsentStatusCode.ACCEPTEDSETTLEMENTCOMPLETED);
            log.info("Payment {}", payment);
        } catch (CurrencyConverterException e) {
            log.info("Can't convert amount in the right currency", e);
            log.info("Update payment status to rejected");
            payment.setStatus(ConsentStatusCode.REJECTED);
            log.info("Payment {}", payment);
        } catch (Exception e) {
            log.error("Couldn't auto-pay payment.", e);
            log.info("Update payment status to rejected");
            payment.setStatus(ConsentStatusCode.REJECTED);
            log.info("Payment {}", payment);
        } finally {
            internationalPaymentService.updatePayment(payment);
            paymentNotificationService.paymentStatusChanged(payment);
        }
    }
    log.info("All payments in process are now accepted. See you in 1 minute! The time is now {}.", format.print(DateTime.now()));
}
Also used : Account(com.forgerock.openbanking.common.model.openbanking.persistence.account.Account) CurrencyConverterException(com.tunyk.currencyconverter.api.CurrencyConverterException) FRInternationalConsent(com.forgerock.openbanking.common.model.openbanking.persistence.payment.FRInternationalConsent) CurrencyConverterException(com.tunyk.currencyconverter.api.CurrencyConverterException) Scheduled(org.springframework.scheduling.annotation.Scheduled) SchedulerLock(net.javacrumbs.shedlock.core.SchedulerLock)

Example 3 with CurrencyConverterException

use of com.tunyk.currencyconverter.api.CurrencyConverterException in project openbanking-aspsp by OpenBankingToolkit.

the class AcceptDomesticPaymentTaskTest method shouldRejectPaymentWhenCurrencyConversionException.

@Test
public void shouldRejectPaymentWhenCurrencyConversionException() throws CurrencyConverterException {
    // Given
    FRDomesticConsent payment = defaultPayment();
    given(paymentsService.getAllPaymentsInProcess()).willReturn(Collections.singleton(payment));
    FRAccount account = defaultAccount(DEBIT_ACCOUNT);
    given(account2StoreService.getAccount(DEBIT_ACCOUNT)).willReturn(account);
    doThrow(CurrencyConverterException.class).when(moneyService).moveMoney(any(), any(), any(), any(), any());
    FRAmount instructedAmount = payment.getInitiation().getInstructedAmount();
    // When
    acceptDomesticPaymentTask.autoAcceptPayment();
    // Then
    verify(moneyService).moveMoney(eq(account), eq(instructedAmount), eq(FRCreditDebitIndicator.DEBIT), eq(payment), any());
    verify(paymentsService).updatePayment(argThat(p -> p.getStatus().equals(ConsentStatusCode.REJECTED)));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Mock(org.mockito.Mock) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) RunWith(org.junit.runner.RunWith) FRWriteDomesticDataInitiation(com.forgerock.openbanking.common.model.openbanking.domain.payment.FRWriteDomesticDataInitiation) AccountStoreService(com.forgerock.openbanking.common.services.store.account.AccountStoreService) Mockito.doThrow(org.mockito.Mockito.doThrow) ConsentStatusCode(com.forgerock.openbanking.common.model.openbanking.persistence.payment.ConsentStatusCode) 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) DomesticPaymentService(com.forgerock.openbanking.common.services.store.payment.DomesticPaymentService) FRAccountIdentifier(com.forgerock.openbanking.common.model.openbanking.domain.common.FRAccountIdentifier) FRWriteDomesticConsentData(com.forgerock.openbanking.common.model.openbanking.domain.payment.FRWriteDomesticConsentData) FRWriteDomesticConsent(com.forgerock.openbanking.common.model.openbanking.domain.payment.FRWriteDomesticConsent) Test(org.junit.Test) FRAmount(com.forgerock.openbanking.common.model.openbanking.domain.common.FRAmount) Mockito.verify(org.mockito.Mockito.verify) CurrencyConverterException(com.tunyk.currencyconverter.api.CurrencyConverterException) Optional(java.util.Optional) PaymentNotificationFacade(com.forgerock.openbanking.aspsp.rs.simulator.service.PaymentNotificationFacade) FRDomesticConsent(com.forgerock.openbanking.common.model.openbanking.persistence.payment.FRDomesticConsent) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Collections(java.util.Collections) FRAccount(com.forgerock.openbanking.common.model.openbanking.persistence.account.FRAccount) FRAmount(com.forgerock.openbanking.common.model.openbanking.domain.common.FRAmount) FRDomesticConsent(com.forgerock.openbanking.common.model.openbanking.persistence.payment.FRDomesticConsent) Test(org.junit.Test)

Example 4 with CurrencyConverterException

use of com.tunyk.currencyconverter.api.CurrencyConverterException in project openbanking-aspsp by OpenBankingToolkit.

the class AcceptDomesticPaymentTaskTest method shouldCreditAccount.

@Test
public void shouldCreditAccount() throws CurrencyConverterException {
    // Given
    FRDomesticConsent payment = defaultPayment();
    given(paymentsService.getAllPaymentsInProcess()).willReturn(Collections.singleton(payment));
    given(account2StoreService.getAccount(DEBIT_ACCOUNT)).willReturn(defaultAccount(DEBIT_ACCOUNT));
    FRAccount account = defaultAccount(CREDIT_ACCOUNT);
    given(account2StoreService.findAccountByIdentification(CREDIT_ACCOUNT)).willReturn(Optional.of(account));
    FRAmount instructedAmount = payment.getInitiation().getInstructedAmount();
    // When
    acceptDomesticPaymentTask.autoAcceptPayment();
    // Then
    verify(moneyService).moveMoney(eq(account), eq(instructedAmount), eq(FRCreditDebitIndicator.CREDIT), eq(payment), any());
    verify(paymentsService).updatePayment(argThat(p -> p.getStatus().equals(ConsentStatusCode.ACCEPTEDSETTLEMENTCOMPLETED)));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) Mock(org.mockito.Mock) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) RunWith(org.junit.runner.RunWith) FRWriteDomesticDataInitiation(com.forgerock.openbanking.common.model.openbanking.domain.payment.FRWriteDomesticDataInitiation) AccountStoreService(com.forgerock.openbanking.common.services.store.account.AccountStoreService) Mockito.doThrow(org.mockito.Mockito.doThrow) ConsentStatusCode(com.forgerock.openbanking.common.model.openbanking.persistence.payment.ConsentStatusCode) 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) DomesticPaymentService(com.forgerock.openbanking.common.services.store.payment.DomesticPaymentService) FRAccountIdentifier(com.forgerock.openbanking.common.model.openbanking.domain.common.FRAccountIdentifier) FRWriteDomesticConsentData(com.forgerock.openbanking.common.model.openbanking.domain.payment.FRWriteDomesticConsentData) FRWriteDomesticConsent(com.forgerock.openbanking.common.model.openbanking.domain.payment.FRWriteDomesticConsent) Test(org.junit.Test) FRAmount(com.forgerock.openbanking.common.model.openbanking.domain.common.FRAmount) Mockito.verify(org.mockito.Mockito.verify) CurrencyConverterException(com.tunyk.currencyconverter.api.CurrencyConverterException) Optional(java.util.Optional) PaymentNotificationFacade(com.forgerock.openbanking.aspsp.rs.simulator.service.PaymentNotificationFacade) FRDomesticConsent(com.forgerock.openbanking.common.model.openbanking.persistence.payment.FRDomesticConsent) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Collections(java.util.Collections) FRAccount(com.forgerock.openbanking.common.model.openbanking.persistence.account.FRAccount) FRAmount(com.forgerock.openbanking.common.model.openbanking.domain.common.FRAmount) FRDomesticConsent(com.forgerock.openbanking.common.model.openbanking.persistence.payment.FRDomesticConsent) Test(org.junit.Test)

Example 5 with CurrencyConverterException

use of com.tunyk.currencyconverter.api.CurrencyConverterException in project openbanking-aspsp by OpenBankingToolkit.

the class AcceptDomesticScheduledPaymentTaskTest method scheduledPaymentDue_shouldDebitAccount.

@Test
public void scheduledPaymentDue_shouldDebitAccount() throws CurrencyConverterException {
    // Given
    FRScheduledPayment payment = defaultPayment(DateTime.now().minusDays(1), ScheduledPaymentStatus.PENDING);
    given(paymentsService.getPendingAndDueScheduledPayments()).willReturn(Collections.singletonList(payment));
    FRAccount account = defaultAccount(DEBIT_ACCOUNT);
    given(account2StoreService.getAccount(DEBIT_ACCOUNT)).willReturn(account);
    // When
    acceptDueScheduledPaymentTask.payDueScheduledPayments();
    // Then
    verify(moneyService).moveMoney(eq(account), eq(payment.getScheduledPayment().getInstructedAmount()), eq(FRCreditDebitIndicator.DEBIT), 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

CurrencyConverterException (com.tunyk.currencyconverter.api.CurrencyConverterException)28 MoneyService (com.forgerock.openbanking.aspsp.rs.simulator.service.MoneyService)22 PaymentNotificationFacade (com.forgerock.openbanking.aspsp.rs.simulator.service.PaymentNotificationFacade)22 FRCreditDebitIndicator (com.forgerock.openbanking.common.model.openbanking.domain.account.common.FRCreditDebitIndicator)22 FRAccountIdentifier (com.forgerock.openbanking.common.model.openbanking.domain.common.FRAccountIdentifier)22 FRAmount (com.forgerock.openbanking.common.model.openbanking.domain.common.FRAmount)22 FRAccount (com.forgerock.openbanking.common.model.openbanking.persistence.account.FRAccount)22 AccountStoreService (com.forgerock.openbanking.common.services.store.account.AccountStoreService)22 Collections (java.util.Collections)22 Optional (java.util.Optional)22 Test (org.junit.Test)22 RunWith (org.junit.runner.RunWith)22 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)22 ArgumentMatchers.argThat (org.mockito.ArgumentMatchers.argThat)22 ArgumentMatchers.eq (org.mockito.ArgumentMatchers.eq)22 BDDMockito.given (org.mockito.BDDMockito.given)22 InjectMocks (org.mockito.InjectMocks)22 Mock (org.mockito.Mock)22 Mockito.doThrow (org.mockito.Mockito.doThrow)22 Mockito.verify (org.mockito.Mockito.verify)22