Search in sources :

Example 11 with CurrencyConverterException

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

the class AcceptInternationalPaymentTaskTest method shouldCreditAccount.

@Test
public void shouldCreditAccount() throws CurrencyConverterException {
    // Given
    FRInternationalConsent 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));
    // When
    acceptDomesticPaymentTask.autoAcceptPayment();
    // Then
    FRAmount amount = payment.getInitiation().getInstructedAmount();
    verify(moneyService).moveMoney(eq(account), eq(amount), 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) FRInternationalConsent(com.forgerock.openbanking.common.model.openbanking.persistence.payment.FRInternationalConsent) 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) FRWriteInternationalConsentData(com.forgerock.openbanking.common.model.openbanking.domain.payment.FRWriteInternationalConsentData) 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) FRWriteInternationalDataInitiation(com.forgerock.openbanking.common.model.openbanking.domain.payment.FRWriteInternationalDataInitiation) Test(org.junit.Test) InternationalPaymentService(com.forgerock.openbanking.common.services.store.payment.InternationalPaymentService) 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) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Collections(java.util.Collections) FRWriteInternationalConsent(com.forgerock.openbanking.common.model.openbanking.domain.payment.FRWriteInternationalConsent) FRAccount(com.forgerock.openbanking.common.model.openbanking.persistence.account.FRAccount) FRAmount(com.forgerock.openbanking.common.model.openbanking.domain.common.FRAmount) FRInternationalConsent(com.forgerock.openbanking.common.model.openbanking.persistence.payment.FRInternationalConsent) Test(org.junit.Test)

Example 12 with CurrencyConverterException

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

the class AcceptSinglePaymentTaskTest method shouldRejectPaymentWhenAnyException.

@Test
public void shouldRejectPaymentWhenAnyException() throws CurrencyConverterException {
    // Given
    FRPaymentSetup payment = defaultPayment();
    given(paymentsService.getAllPaymentsInProcess()).willReturn(Collections.singleton(payment));
    FRAccount account = defaultAccount(DEBIT_ACCOUNT);
    given(account2StoreService.getAccount(DEBIT_ACCOUNT)).willReturn(account);
    doThrow(new RuntimeException()).when(moneyService).moveMoney(any(), any(), any(), any(), any());
    // When
    autoAcceptPaymentTask.autoAcceptPayment();
    // Then
    FRAmount instructedAmount = payment.getInitiation().getInstructedAmount();
    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) SinglePaymentService(com.forgerock.openbanking.common.services.store.payment.SinglePaymentService) 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) FRPaymentSetup(com.forgerock.openbanking.common.model.openbanking.persistence.payment.FRPaymentSetup) 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) 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) 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) FRPaymentSetup(com.forgerock.openbanking.common.model.openbanking.persistence.payment.FRPaymentSetup) Test(org.junit.Test)

Example 13 with CurrencyConverterException

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

the class AcceptSinglePaymentTaskTest method shouldRejectPaymentWhenCurrencyConversionException.

@Test
public void shouldRejectPaymentWhenCurrencyConversionException() throws CurrencyConverterException {
    // Given
    FRPaymentSetup 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());
    // When
    autoAcceptPaymentTask.autoAcceptPayment();
    // Then
    FRAmount instructedAmount = payment.getInitiation().getInstructedAmount();
    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) SinglePaymentService(com.forgerock.openbanking.common.services.store.payment.SinglePaymentService) 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) FRPaymentSetup(com.forgerock.openbanking.common.model.openbanking.persistence.payment.FRPaymentSetup) 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) 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) 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) FRPaymentSetup(com.forgerock.openbanking.common.model.openbanking.persistence.payment.FRPaymentSetup) Test(org.junit.Test)

Example 14 with CurrencyConverterException

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

the class AcceptDomesticPaymentTask method autoAcceptPayment.

@Scheduled(fixedRate = 60 * 1000)
@SchedulerLock(name = "domesticPayment")
public void autoAcceptPayment() {
    log.info("Auto-accept payment task waking up. The time is now {}.", format.print(DateTime.now()));
    Collection<FRDomesticConsent> allPaymentsInProcess = domesticPaymentsService.getAllPaymentsInProcess();
    for (FRDomesticConsent 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 {
            domesticPaymentsService.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) FRDomesticConsent(com.forgerock.openbanking.common.model.openbanking.persistence.payment.FRDomesticConsent) CurrencyConverterException(com.tunyk.currencyconverter.api.CurrencyConverterException) Scheduled(org.springframework.scheduling.annotation.Scheduled) SchedulerLock(net.javacrumbs.shedlock.core.SchedulerLock)

Example 15 with CurrencyConverterException

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

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