Search in sources :

Example 31 with PaymentTransactionModelDao

use of org.killbill.billing.payment.dao.PaymentTransactionModelDao in project killbill by killbill.

the class TestRetryService method testAbortedPayment.

@Test(groups = "fast")
public void testAbortedPayment() throws Exception {
    final Account account = testHelper.createTestAccount("yiyi.gmail.com", true);
    final Invoice invoice = testHelper.createTestInvoice(account, clock.getUTCToday(), Currency.USD);
    final BigDecimal amount = new BigDecimal("10.00");
    final UUID subscriptionId = UUID.randomUUID();
    final UUID bundleId = UUID.randomUUID();
    final LocalDate startDate = clock.getUTCToday();
    final LocalDate endDate = startDate.plusMonths(1);
    invoice.addInvoiceItem(new MockRecurringInvoiceItem(invoice.getId(), account.getId(), subscriptionId, bundleId, "test plan", "test phase", null, startDate, endDate, amount, new BigDecimal("1.0"), Currency.USD));
    setPaymentFailure(FailureType.PAYMENT_FAILURE);
    final String paymentExternalKey = UUID.randomUUID().toString();
    final String transactionExternalKey = UUID.randomUUID().toString();
    pluginControlPaymentProcessor.createPurchase(false, account, account.getPaymentMethodId(), null, amount, Currency.USD, paymentExternalKey, transactionExternalKey, createPropertiesForInvoice(invoice), ImmutableList.<String>of(InvoicePaymentControlPluginApi.PLUGIN_NAME), callContext, internalCallContext);
    Payment payment = getPaymentForExternalKey(paymentExternalKey);
    List<PaymentAttemptModelDao> attempts = paymentDao.getPaymentAttempts(paymentExternalKey, internalCallContext);
    assertEquals(attempts.size(), 1);
    final List<PaymentTransactionModelDao> transactions = paymentDao.getTransactionsForPayment(payment.getId(), internalCallContext);
    assertEquals(transactions.size(), 1);
    int maxTries = paymentConfig.getPaymentFailureRetryDays(internalCallContext).size();
    for (int curFailure = 0; curFailure < maxTries; curFailure++) {
        // Set plugin to fail with specific type unless this is the last attempt and we want a success
        setPaymentFailure(FailureType.PAYMENT_FAILURE);
        moveClockForFailureType(FailureType.PAYMENT_FAILURE, curFailure);
        final int curFailureCondition = curFailure;
        try {
            await().atMost(TIMEOUT, SECONDS).until(new Callable<Boolean>() {

                @Override
                public Boolean call() throws Exception {
                    final List<PaymentAttemptModelDao> attempts = paymentDao.getPaymentAttempts(paymentExternalKey, internalCallContext);
                    final List<PaymentAttemptModelDao> filteredAttempts = ImmutableList.copyOf(Iterables.filter(attempts, new Predicate<PaymentAttemptModelDao>() {

                        @Override
                        public boolean apply(final PaymentAttemptModelDao input) {
                            return input.getStateName().equals("SUCCESS") || input.getStateName().equals("RETRIED") || input.getStateName().equals("ABORTED");
                        }
                    }));
                    return filteredAttempts.size() == curFailureCondition + 2;
                }
            });
        } catch (final TimeoutException e) {
            fail("Timeout curFailure = " + curFailureCondition);
        }
    }
    attempts = paymentDao.getPaymentAttempts(payment.getExternalKey(), internalCallContext);
    final int expectedAttempts = maxTries + 1;
    assertEquals(attempts.size(), expectedAttempts);
    Collections.sort(attempts, new Comparator<PaymentAttemptModelDao>() {

        @Override
        public int compare(final PaymentAttemptModelDao o1, final PaymentAttemptModelDao o2) {
            return o1.getCreatedDate().compareTo(o2.getCreatedDate());
        }
    });
    for (int i = 0; i < attempts.size(); i++) {
        final PaymentAttemptModelDao cur = attempts.get(i);
        if (i < attempts.size() - 1) {
            assertEquals(cur.getStateName(), "RETRIED");
        } else {
            assertEquals(cur.getStateName(), "ABORTED");
        }
    }
}
Also used : PaymentAttemptModelDao(org.killbill.billing.payment.dao.PaymentAttemptModelDao) Account(org.killbill.billing.account.api.Account) Invoice(org.killbill.billing.invoice.api.Invoice) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) TimeoutException(java.util.concurrent.TimeoutException) PaymentApiException(org.killbill.billing.payment.api.PaymentApiException) Payment(org.killbill.billing.payment.api.Payment) PaymentTransactionModelDao(org.killbill.billing.payment.dao.PaymentTransactionModelDao) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) UUID(java.util.UUID) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test)

Example 32 with PaymentTransactionModelDao

use of org.killbill.billing.payment.dao.PaymentTransactionModelDao in project killbill by killbill.

the class TestRetryService method testFailedPaymentWithOneSuccessfulRetry.

@Test(groups = "fast")
public void testFailedPaymentWithOneSuccessfulRetry() throws Exception {
    final Account account = testHelper.createTestAccount("yiyi.gmail.com", true);
    final Invoice invoice = testHelper.createTestInvoice(account, clock.getUTCToday(), Currency.USD);
    final BigDecimal amount = new BigDecimal("10.00");
    final UUID subscriptionId = UUID.randomUUID();
    final UUID bundleId = UUID.randomUUID();
    final LocalDate startDate = clock.getUTCToday();
    final LocalDate endDate = startDate.plusMonths(1);
    invoice.addInvoiceItem(new MockRecurringInvoiceItem(invoice.getId(), account.getId(), subscriptionId, bundleId, "test plan", "test phase", null, startDate, endDate, amount, new BigDecimal("1.0"), Currency.USD));
    setPaymentFailure(FailureType.PAYMENT_FAILURE);
    final String paymentExternalKey = UUID.randomUUID().toString();
    final String transactionExternalKey = UUID.randomUUID().toString();
    pluginControlPaymentProcessor.createPurchase(false, account, account.getPaymentMethodId(), null, amount, Currency.USD, paymentExternalKey, transactionExternalKey, createPropertiesForInvoice(invoice), ImmutableList.<String>of(InvoicePaymentControlPluginApi.PLUGIN_NAME), callContext, internalCallContext);
    Payment payment = getPaymentForExternalKey(paymentExternalKey);
    List<PaymentAttemptModelDao> attempts = paymentDao.getPaymentAttempts(paymentExternalKey, internalCallContext);
    assertEquals(attempts.size(), 1);
    final List<PaymentTransactionModelDao> transactions = paymentDao.getTransactionsForPayment(payment.getId(), internalCallContext);
    assertEquals(transactions.size(), 1);
    moveClockForFailureType(FailureType.PAYMENT_FAILURE, 0);
    try {
        await().atMost(TIMEOUT, SECONDS).until(new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                final List<PaymentAttemptModelDao> attempts = paymentDao.getPaymentAttempts(paymentExternalKey, internalCallContext);
                final List<PaymentAttemptModelDao> filteredAttempts = ImmutableList.copyOf(Iterables.filter(attempts, new Predicate<PaymentAttemptModelDao>() {

                    @Override
                    public boolean apply(final PaymentAttemptModelDao input) {
                        return input.getStateName().equals("SUCCESS") || input.getStateName().equals("RETRIED") || input.getStateName().equals("ABORTED");
                    }
                }));
                return filteredAttempts.size() == 2;
            }
        });
    } catch (final TimeoutException e) {
        fail("Timeout ");
    }
    attempts = paymentDao.getPaymentAttempts(payment.getExternalKey(), internalCallContext);
    final int expectedAttempts = 2;
    assertEquals(attempts.size(), expectedAttempts);
    Collections.sort(attempts, new Comparator<PaymentAttemptModelDao>() {

        @Override
        public int compare(final PaymentAttemptModelDao o1, final PaymentAttemptModelDao o2) {
            return o1.getCreatedDate().compareTo(o2.getCreatedDate());
        }
    });
    for (int i = 0; i < attempts.size(); i++) {
        final PaymentAttemptModelDao cur = attempts.get(i);
        if (i < attempts.size() - 1) {
            assertEquals(cur.getStateName(), "RETRIED");
        } else {
            assertEquals(cur.getStateName(), "SUCCESS");
        }
    }
}
Also used : PaymentAttemptModelDao(org.killbill.billing.payment.dao.PaymentAttemptModelDao) Account(org.killbill.billing.account.api.Account) Invoice(org.killbill.billing.invoice.api.Invoice) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) TimeoutException(java.util.concurrent.TimeoutException) PaymentApiException(org.killbill.billing.payment.api.PaymentApiException) Payment(org.killbill.billing.payment.api.Payment) PaymentTransactionModelDao(org.killbill.billing.payment.dao.PaymentTransactionModelDao) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) UUID(java.util.UUID) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test)

Example 33 with PaymentTransactionModelDao

use of org.killbill.billing.payment.dao.PaymentTransactionModelDao in project killbill by killbill.

the class TestRetryService method testFailedPaymentWithLastRetrySuccess.

@Test(groups = "fast")
public void testFailedPaymentWithLastRetrySuccess() throws Exception {
    final Account account = testHelper.createTestAccount("yiyi.gmail.com", true);
    final Invoice invoice = testHelper.createTestInvoice(account, clock.getUTCToday(), Currency.USD);
    final BigDecimal amount = new BigDecimal("10.00");
    final UUID subscriptionId = UUID.randomUUID();
    final UUID bundleId = UUID.randomUUID();
    final LocalDate startDate = clock.getUTCToday();
    final LocalDate endDate = startDate.plusMonths(1);
    invoice.addInvoiceItem(new MockRecurringInvoiceItem(invoice.getId(), account.getId(), subscriptionId, bundleId, "test plan", "test phase", null, startDate, endDate, amount, new BigDecimal("1.0"), Currency.USD));
    setPaymentFailure(FailureType.PAYMENT_FAILURE);
    final String paymentExternalKey = UUID.randomUUID().toString();
    final String transactionExternalKey = UUID.randomUUID().toString();
    pluginControlPaymentProcessor.createPurchase(false, account, account.getPaymentMethodId(), null, amount, Currency.USD, paymentExternalKey, transactionExternalKey, createPropertiesForInvoice(invoice), ImmutableList.<String>of(InvoicePaymentControlPluginApi.PLUGIN_NAME), callContext, internalCallContext);
    Payment payment = getPaymentForExternalKey(paymentExternalKey);
    List<PaymentAttemptModelDao> attempts = paymentDao.getPaymentAttempts(paymentExternalKey, internalCallContext);
    assertEquals(attempts.size(), 1);
    final List<PaymentTransactionModelDao> transactions = paymentDao.getTransactionsForPayment(payment.getId(), internalCallContext);
    assertEquals(transactions.size(), 1);
    int maxTries = paymentConfig.getPaymentFailureRetryDays(internalCallContext).size();
    for (int curFailure = 0; curFailure < maxTries; curFailure++) {
        // Set plugin to fail with specific type unless this is the last attempt and we want a success
        if (curFailure < (maxTries - 1)) {
            setPaymentFailure(FailureType.PAYMENT_FAILURE);
        }
        moveClockForFailureType(FailureType.PAYMENT_FAILURE, curFailure);
        final int curFailureCondition = curFailure;
        try {
            await().atMost(TIMEOUT, SECONDS).until(new Callable<Boolean>() {

                @Override
                public Boolean call() throws Exception {
                    final List<PaymentAttemptModelDao> attempts = paymentDao.getPaymentAttempts(paymentExternalKey, internalCallContext);
                    final List<PaymentAttemptModelDao> filteredAttempts = ImmutableList.copyOf(Iterables.filter(attempts, new Predicate<PaymentAttemptModelDao>() {

                        @Override
                        public boolean apply(final PaymentAttemptModelDao input) {
                            return input.getStateName().equals("SUCCESS") || input.getStateName().equals("RETRIED") || input.getStateName().equals("ABORTED");
                        }
                    }));
                    return filteredAttempts.size() == curFailureCondition + 2;
                }
            });
        } catch (final TimeoutException e) {
            fail("Timeout curFailure = " + curFailureCondition);
        }
    }
    attempts = paymentDao.getPaymentAttempts(payment.getExternalKey(), internalCallContext);
    final int expectedAttempts = maxTries + 1;
    assertEquals(attempts.size(), expectedAttempts);
    Collections.sort(attempts, new Comparator<PaymentAttemptModelDao>() {

        @Override
        public int compare(final PaymentAttemptModelDao o1, final PaymentAttemptModelDao o2) {
            return o1.getCreatedDate().compareTo(o2.getCreatedDate());
        }
    });
    for (int i = 0; i < attempts.size(); i++) {
        final PaymentAttemptModelDao cur = attempts.get(i);
        if (i < attempts.size() - 1) {
            assertEquals(cur.getStateName(), "RETRIED");
        } else {
            assertEquals(cur.getStateName(), "SUCCESS");
        }
    }
}
Also used : PaymentAttemptModelDao(org.killbill.billing.payment.dao.PaymentAttemptModelDao) Account(org.killbill.billing.account.api.Account) Invoice(org.killbill.billing.invoice.api.Invoice) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) TimeoutException(java.util.concurrent.TimeoutException) PaymentApiException(org.killbill.billing.payment.api.PaymentApiException) Payment(org.killbill.billing.payment.api.Payment) PaymentTransactionModelDao(org.killbill.billing.payment.dao.PaymentTransactionModelDao) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) UUID(java.util.UUID) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test)

Example 34 with PaymentTransactionModelDao

use of org.killbill.billing.payment.dao.PaymentTransactionModelDao in project killbill by killbill.

the class TestDefaultAdminPaymentApi method testFixPaymentTransactionStateFromPaymentTransactionInfoPlugin.

@Test(groups = "slow", description = "https://github.com/killbill/killbill/issues/551")
public void testFixPaymentTransactionStateFromPaymentTransactionInfoPlugin() throws PaymentApiException {
    final Payment payment = paymentApi.createAuthorization(account, account.getPaymentMethodId(), null, BigDecimal.TEN, Currency.EUR, UUID.randomUUID().toString(), UUID.randomUUID().toString(), ImmutableList.<PluginProperty>of(), callContext);
    final PaymentModelDao paymentModelDao = paymentDao.getPayment(payment.getId(), internalCallContext);
    final PaymentTransactionModelDao paymentTransactionModelDao = paymentDao.getPaymentTransaction(payment.getTransactions().get(0).getId(), internalCallContext);
    Assert.assertEquals(paymentModelDao.getStateName(), "AUTH_SUCCESS");
    Assert.assertEquals(paymentModelDao.getLastSuccessStateName(), "AUTH_SUCCESS");
    Assert.assertEquals(paymentTransactionModelDao.getTransactionStatus(), TransactionStatus.SUCCESS);
    Assert.assertEquals(paymentTransactionModelDao.getProcessedAmount().compareTo(BigDecimal.TEN), 0);
    Assert.assertEquals(paymentTransactionModelDao.getProcessedCurrency(), Currency.EUR);
    Assert.assertEquals(paymentTransactionModelDao.getGatewayErrorCode(), "");
    Assert.assertEquals(paymentTransactionModelDao.getGatewayErrorMsg(), "");
    final PaymentTransactionInfoPlugin infoPlugin = new DefaultNoOpPaymentInfoPlugin(paymentTransactionModelDao.getPaymentId(), paymentTransactionModelDao.getId(), paymentTransactionModelDao.getTransactionType(), paymentTransactionModelDao.getAmount(), paymentTransactionModelDao.getCurrency(), paymentTransactionModelDao.getEffectiveDate(), paymentTransactionModelDao.getCreatedDate(), PaymentPluginStatus.ERROR, "error-code", "error-msg");
    final PaymentTransaction newPaymentTransaction = new DefaultPaymentTransaction(paymentTransactionModelDao.getId(), paymentTransactionModelDao.getAttemptId(), paymentTransactionModelDao.getTransactionExternalKey(), paymentTransactionModelDao.getCreatedDate(), paymentTransactionModelDao.getUpdatedDate(), paymentTransactionModelDao.getPaymentId(), paymentTransactionModelDao.getTransactionType(), paymentTransactionModelDao.getEffectiveDate(), TransactionStatus.PAYMENT_FAILURE, paymentTransactionModelDao.getAmount(), paymentTransactionModelDao.getCurrency(), paymentTransactionModelDao.getProcessedAmount(), paymentTransactionModelDao.getProcessedCurrency(), infoPlugin.getGatewayErrorCode(), infoPlugin.getGatewayError(), infoPlugin);
    adminPaymentApi.fixPaymentTransactionState(payment, newPaymentTransaction, null, null, "AUTH_ERRORED", ImmutableList.<PluginProperty>of(), callContext);
    final PaymentModelDao refreshedPaymentModelDao = paymentDao.getPayment(payment.getId(), internalCallContext);
    final PaymentTransactionModelDao refreshedPaymentTransactionModelDao = paymentDao.getPaymentTransaction(payment.getTransactions().get(0).getId(), internalCallContext);
    Assert.assertEquals(refreshedPaymentModelDao.getStateName(), "AUTH_ERRORED");
    // TODO Shouldn't we allow the user to override this too?
    Assert.assertEquals(refreshedPaymentModelDao.getLastSuccessStateName(), "AUTH_SUCCESS");
    Assert.assertEquals(refreshedPaymentTransactionModelDao.getTransactionStatus(), TransactionStatus.PAYMENT_FAILURE);
    Assert.assertEquals(refreshedPaymentTransactionModelDao.getProcessedAmount().compareTo(BigDecimal.TEN), 0);
    Assert.assertEquals(refreshedPaymentTransactionModelDao.getProcessedCurrency(), Currency.EUR);
    Assert.assertEquals(refreshedPaymentTransactionModelDao.getGatewayErrorCode(), "error-code");
    Assert.assertEquals(refreshedPaymentTransactionModelDao.getGatewayErrorMsg(), "error-msg");
}
Also used : DefaultNoOpPaymentInfoPlugin(org.killbill.billing.payment.provider.DefaultNoOpPaymentInfoPlugin) PaymentTransactionModelDao(org.killbill.billing.payment.dao.PaymentTransactionModelDao) PaymentModelDao(org.killbill.billing.payment.dao.PaymentModelDao) PaymentTransactionInfoPlugin(org.killbill.billing.payment.plugin.api.PaymentTransactionInfoPlugin) Test(org.testng.annotations.Test)

Example 35 with PaymentTransactionModelDao

use of org.killbill.billing.payment.dao.PaymentTransactionModelDao in project killbill by killbill.

the class TestDefaultAdminPaymentApi method testFixPaymentTransactionStateNoPaymentTransactionInfoPlugin.

@Test(groups = "slow", description = "https://github.com/killbill/killbill/issues/551")
public void testFixPaymentTransactionStateNoPaymentTransactionInfoPlugin() throws PaymentApiException {
    final Payment payment = paymentApi.createAuthorization(account, account.getPaymentMethodId(), null, BigDecimal.TEN, Currency.EUR, UUID.randomUUID().toString(), UUID.randomUUID().toString(), ImmutableList.<PluginProperty>of(), callContext);
    final PaymentModelDao paymentModelDao = paymentDao.getPayment(payment.getId(), internalCallContext);
    final PaymentTransactionModelDao paymentTransactionModelDao = paymentDao.getPaymentTransaction(payment.getTransactions().get(0).getId(), internalCallContext);
    Assert.assertEquals(paymentModelDao.getStateName(), "AUTH_SUCCESS");
    Assert.assertEquals(paymentModelDao.getLastSuccessStateName(), "AUTH_SUCCESS");
    Assert.assertEquals(paymentTransactionModelDao.getTransactionStatus(), TransactionStatus.SUCCESS);
    Assert.assertEquals(paymentTransactionModelDao.getProcessedAmount().compareTo(BigDecimal.TEN), 0);
    Assert.assertEquals(paymentTransactionModelDao.getProcessedCurrency(), Currency.EUR);
    Assert.assertEquals(paymentTransactionModelDao.getGatewayErrorCode(), "");
    Assert.assertEquals(paymentTransactionModelDao.getGatewayErrorMsg(), "");
    try {
        // Since no transaction status is passed, PaymentTransactionInfoPlugin should be set
        adminPaymentApi.fixPaymentTransactionState(payment, Mockito.mock(DefaultPaymentTransaction.class), null, null, "AUTH_ERRORED", ImmutableList.<PluginProperty>of(), callContext);
    } catch (final PaymentApiException e) {
        Assert.assertEquals(e.getCode(), ErrorCode.PAYMENT_INVALID_PARAMETER.getCode());
    }
}
Also used : PaymentTransactionModelDao(org.killbill.billing.payment.dao.PaymentTransactionModelDao) PaymentModelDao(org.killbill.billing.payment.dao.PaymentModelDao) Test(org.testng.annotations.Test)

Aggregations

PaymentTransactionModelDao (org.killbill.billing.payment.dao.PaymentTransactionModelDao)39 PaymentModelDao (org.killbill.billing.payment.dao.PaymentModelDao)17 PaymentApiException (org.killbill.billing.payment.api.PaymentApiException)16 Test (org.testng.annotations.Test)16 UUID (java.util.UUID)12 BigDecimal (java.math.BigDecimal)10 PaymentAttemptModelDao (org.killbill.billing.payment.dao.PaymentAttemptModelDao)10 Predicate (com.google.common.base.Predicate)8 Payment (org.killbill.billing.payment.api.Payment)7 Account (org.killbill.billing.account.api.Account)6 PaymentTransactionInfoPlugin (org.killbill.billing.payment.plugin.api.PaymentTransactionInfoPlugin)6 ImmutableList (com.google.common.collect.ImmutableList)5 List (java.util.List)5 LinkedList (java.util.LinkedList)4 LocalDate (org.joda.time.LocalDate)4 Invoice (org.killbill.billing.invoice.api.Invoice)4 PluginProperty (org.killbill.billing.payment.api.PluginProperty)4 ArrayList (java.util.ArrayList)3 TimeoutException (java.util.concurrent.TimeoutException)3 OperationException (org.killbill.automaton.OperationException)3