Search in sources :

Example 1 with Payment

use of org.killbill.billing.payment.api.Payment in project killbill by killbill.

the class PaymentChecker method checkPayment.

public Payment checkPayment(final UUID accountId, final int paymentOrderingNumber, final CallContext context, final ExpectedPaymentCheck expected) throws PaymentApiException {
    final List<Payment> payments = paymentApi.getAccountPayments(accountId, false, false, ImmutableList.<PluginProperty>of(), context);
    Assert.assertEquals(payments.size(), paymentOrderingNumber);
    final Payment payment = payments.get(paymentOrderingNumber - 1);
    final PaymentTransaction transaction = getPurchaseTransaction(payment);
    if (transaction.getTransactionStatus() == TransactionStatus.UNKNOWN) {
        checkPaymentNoAuditForRuntimeException(accountId, payment, expected);
    } else {
        checkPayment(accountId, payment, context, expected);
    }
    return payment;
}
Also used : PaymentTransaction(org.killbill.billing.payment.api.PaymentTransaction) InvoicePayment(org.killbill.billing.invoice.api.InvoicePayment) Payment(org.killbill.billing.payment.api.Payment)

Example 2 with Payment

use of org.killbill.billing.payment.api.Payment in project killbill by killbill.

the class TestRetryService method testAbortedPlugin.

// PLUGIN_EXCEPTION will lead to UNKNOWN row that will not be retried by the plugin
@Test(groups = "fast")
public void testAbortedPlugin() 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.PLUGIN_EXCEPTION);
    boolean failed = false;
    final String paymentExternalKey = UUID.randomUUID().toString();
    final String transactionExternalKey = UUID.randomUUID().toString();
    try {
        pluginControlPaymentProcessor.createPurchase(false, account, account.getPaymentMethodId(), null, amount, Currency.USD, paymentExternalKey, transactionExternalKey, createPropertiesForInvoice(invoice), ImmutableList.<String>of(InvoicePaymentControlPluginApi.PLUGIN_NAME), callContext, internalCallContext);
    } catch (final PaymentApiException e) {
        failed = true;
    }
    assertTrue(failed);
    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);
    attempts = paymentDao.getPaymentAttempts(payment.getExternalKey(), internalCallContext);
    final int expectedAttempts = 1;
    assertEquals(attempts.size(), expectedAttempts);
    assertEquals(attempts.get(0).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) PaymentApiException(org.killbill.billing.payment.api.PaymentApiException) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) Payment(org.killbill.billing.payment.api.Payment) PaymentTransactionModelDao(org.killbill.billing.payment.dao.PaymentTransactionModelDao) UUID(java.util.UUID) Test(org.testng.annotations.Test)

Example 3 with Payment

use of org.killbill.billing.payment.api.Payment in project killbill by killbill.

the class TestJanitor method testUnknownEntriesWithFailures.

@Test(groups = "slow")
public void testUnknownEntriesWithFailures() throws PaymentApiException, EventBusException {
    final BigDecimal requestedAmount = BigDecimal.TEN;
    final String paymentExternalKey = "minus";
    final String transactionExternalKey = "plus";
    // Make sure the state as seen by the plugin will be in PaymentPluginStatus.ERROR, which will be returned later to Janitor
    mockPaymentProviderPlugin.makeNextPaymentFailWithError();
    testListener.pushExpectedEvent(NextEvent.PAYMENT_ERROR);
    final Payment payment = paymentApi.createAuthorization(account, account.getPaymentMethodId(), null, requestedAmount, account.getCurrency(), paymentExternalKey, transactionExternalKey, ImmutableList.<PluginProperty>of(), callContext);
    testListener.assertListenerStatus();
    // Artificially move the transaction status to UNKNOWN
    final String paymentStateName = paymentSMHelper.getErroredStateForTransaction(TransactionType.AUTHORIZE).toString();
    testListener.pushExpectedEvent(NextEvent.PAYMENT_PLUGIN_ERROR);
    paymentDao.updatePaymentAndTransactionOnCompletion(account.getId(), null, payment.getId(), TransactionType.AUTHORIZE, paymentStateName, paymentStateName, payment.getTransactions().get(0).getId(), TransactionStatus.UNKNOWN, requestedAmount, account.getCurrency(), "foo", "bar", internalCallContext);
    testListener.assertListenerStatus();
    final List<PaymentTransactionModelDao> paymentTransactionHistoryBeforeJanitor = getPaymentTransactionHistory(transactionExternalKey);
    Assert.assertEquals(paymentTransactionHistoryBeforeJanitor.size(), 3);
    // Move clock for notification to be processed
    testListener.pushExpectedEvent(NextEvent.PAYMENT_ERROR);
    clock.addDeltaFromReality(5 * 60 * 1000);
    assertNotificationsCompleted(internalCallContext, 5);
    testListener.assertListenerStatus();
    // Proves the Janitor ran (and updated the transaction)
    final List<PaymentTransactionModelDao> paymentTransactionHistoryAfterJanitor = getPaymentTransactionHistory(transactionExternalKey);
    Assert.assertEquals(paymentTransactionHistoryAfterJanitor.size(), 4);
    Assert.assertEquals(paymentTransactionHistoryAfterJanitor.get(3).getTransactionStatus(), TransactionStatus.PAYMENT_FAILURE);
    final Payment updatedPayment = paymentApi.getPayment(payment.getId(), false, false, ImmutableList.<PluginProperty>of(), callContext);
    // Janitor should have moved us to PAYMENT_FAILURE
    assertEquals(updatedPayment.getTransactions().get(0).getTransactionStatus(), TransactionStatus.PAYMENT_FAILURE);
}
Also used : Payment(org.killbill.billing.payment.api.Payment) PaymentTransactionModelDao(org.killbill.billing.payment.dao.PaymentTransactionModelDao) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 4 with Payment

use of org.killbill.billing.payment.api.Payment in project killbill by killbill.

the class TestJanitor method testCreateSuccessPurchaseWithPaymentControl.

@Test(groups = "slow")
public void testCreateSuccessPurchaseWithPaymentControl() throws PaymentApiException, InvoiceApiException, EventBusException {
    final BigDecimal requestedAmount = BigDecimal.TEN;
    final UUID subscriptionId = UUID.randomUUID();
    final UUID bundleId = UUID.randomUUID();
    final LocalDate now = clock.getUTCToday();
    testListener.pushExpectedEvent(NextEvent.INVOICE);
    final Invoice invoice = testHelper.createTestInvoice(account, now, Currency.USD);
    testListener.assertListenerStatus();
    final String paymentExternalKey = invoice.getId().toString();
    final String transactionExternalKey = "wouf wouf";
    invoice.addInvoiceItem(new MockRecurringInvoiceItem(invoice.getId(), account.getId(), subscriptionId, bundleId, "test plan", "test phase", null, now, now.plusMonths(1), requestedAmount, new BigDecimal("1.0"), Currency.USD));
    testListener.pushExpectedEvent(NextEvent.PAYMENT);
    final Payment payment = paymentApi.createPurchaseWithPaymentControl(account, account.getPaymentMethodId(), null, requestedAmount, Currency.USD, paymentExternalKey, transactionExternalKey, createPropertiesForInvoice(invoice), INVOICE_PAYMENT, callContext);
    testListener.assertListenerStatus();
    assertEquals(payment.getTransactions().size(), 1);
    assertEquals(payment.getTransactions().get(0).getTransactionStatus(), TransactionStatus.SUCCESS);
    assertEquals(payment.getTransactions().get(0).getTransactionType(), TransactionType.PURCHASE);
    final List<PaymentAttemptModelDao> attempts = paymentDao.getPaymentAttempts(paymentExternalKey, internalCallContext);
    assertEquals(attempts.size(), 1);
    final PaymentAttemptModelDao attempt = attempts.get(0);
    assertEquals(attempt.getStateName(), "SUCCESS");
    // Ok now the fun part starts... we modify the attempt state to be 'INIT' and wait the the Janitor to do its job.
    paymentDao.updatePaymentAttempt(attempt.getId(), attempt.getTransactionId(), "INIT", internalCallContext);
    final PaymentAttemptModelDao attempt2 = paymentDao.getPaymentAttempt(attempt.getId(), internalCallContext);
    assertEquals(attempt2.getStateName(), "INIT");
    clock.addDays(1);
    try {
        Thread.sleep(1500);
    } catch (InterruptedException e) {
    }
    final PaymentAttemptModelDao attempt3 = paymentDao.getPaymentAttempt(attempt.getId(), internalCallContext);
    assertEquals(attempt3.getStateName(), "SUCCESS");
}
Also used : PaymentAttemptModelDao(org.killbill.billing.payment.dao.PaymentAttemptModelDao) Payment(org.killbill.billing.payment.api.Payment) Invoice(org.killbill.billing.invoice.api.Invoice) UUID(java.util.UUID) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 5 with Payment

use of org.killbill.billing.payment.api.Payment in project killbill by killbill.

the class TestJanitor method testUnknownEntriesWithExceptions.

@Test(groups = "slow")
public void testUnknownEntriesWithExceptions() throws PaymentApiException, EventBusException {
    final BigDecimal requestedAmount = BigDecimal.TEN;
    final String paymentExternalKey = "minus";
    final String transactionExternalKey = "plus";
    // Make sure the state as seen by the plugin will be in PaymentPluginStatus.ERROR, which will be returned later to Janitor
    mockPaymentProviderPlugin.makeNextPaymentFailWithException();
    try {
        testListener.pushExpectedEvent(NextEvent.PAYMENT_PLUGIN_ERROR);
        paymentApi.createAuthorization(account, account.getPaymentMethodId(), null, requestedAmount, account.getCurrency(), paymentExternalKey, transactionExternalKey, ImmutableList.<PluginProperty>of(), callContext);
    } catch (PaymentApiException ignore) {
        testListener.assertListenerStatus();
    }
    final Payment payment = paymentApi.getPaymentByExternalKey(paymentExternalKey, false, false, ImmutableList.<PluginProperty>of(), callContext);
    // Artificially move the transaction status to UNKNOWN
    final String paymentStateName = paymentSMHelper.getErroredStateForTransaction(TransactionType.AUTHORIZE).toString();
    testListener.pushExpectedEvent(NextEvent.PAYMENT_PLUGIN_ERROR);
    paymentDao.updatePaymentAndTransactionOnCompletion(account.getId(), null, payment.getId(), TransactionType.AUTHORIZE, paymentStateName, paymentStateName, payment.getTransactions().get(0).getId(), TransactionStatus.UNKNOWN, requestedAmount, account.getCurrency(), "foo", "bar", internalCallContext);
    testListener.assertListenerStatus();
    // Move clock for notification to be processed
    clock.addDeltaFromReality(5 * 60 * 1000);
    // NO because we will keep retrying as we can't fix it...
    //assertNotificationsCompleted(internalCallContext, 5);
    final List<PaymentTransactionModelDao> paymentTransactionHistoryBeforeJanitor = getPaymentTransactionHistory(transactionExternalKey);
    Assert.assertEquals(paymentTransactionHistoryBeforeJanitor.size(), 3);
    // Nothing new happened
    final List<PaymentTransactionModelDao> paymentTransactionHistoryAfterJanitor = getPaymentTransactionHistory(transactionExternalKey);
    Assert.assertEquals(paymentTransactionHistoryAfterJanitor.size(), 3);
}
Also used : Payment(org.killbill.billing.payment.api.Payment) PaymentTransactionModelDao(org.killbill.billing.payment.dao.PaymentTransactionModelDao) PaymentApiException(org.killbill.billing.payment.api.PaymentApiException) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Aggregations

Payment (org.killbill.billing.payment.api.Payment)75 Test (org.testng.annotations.Test)42 Account (org.killbill.billing.account.api.Account)40 UUID (java.util.UUID)39 BigDecimal (java.math.BigDecimal)32 PluginProperty (org.killbill.billing.payment.api.PluginProperty)30 Invoice (org.killbill.billing.invoice.api.Invoice)27 LocalDate (org.joda.time.LocalDate)24 ApiOperation (io.swagger.annotations.ApiOperation)20 ApiResponses (io.swagger.annotations.ApiResponses)20 Produces (javax.ws.rs.Produces)20 Path (javax.ws.rs.Path)19 TimedResource (org.killbill.commons.metrics.TimedResource)19 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)17 InvoicePayment (org.killbill.billing.invoice.api.InvoicePayment)15 CallContext (org.killbill.billing.util.callcontext.CallContext)15 AccountData (org.killbill.billing.account.api.AccountData)14 GET (javax.ws.rs.GET)13 DateTime (org.joda.time.DateTime)13 PaymentApiException (org.killbill.billing.payment.api.PaymentApiException)13