Search in sources :

Example 1 with MockRecurringInvoiceItem

use of org.killbill.billing.payment.MockRecurringInvoiceItem in project killbill by killbill.

the class TestPaymentApi method testCreateSuccessRefundWithPaymentControl.

@Test(groups = "slow")
public void testCreateSuccessRefundWithPaymentControl() throws PaymentApiException, InvoiceApiException, EventBusException {
    final BigDecimal requestedAmount = BigDecimal.TEN;
    final UUID subscriptionId = UUID.randomUUID();
    final UUID bundleId = UUID.randomUUID();
    final LocalDate now = clock.getUTCToday();
    final Invoice invoice = testHelper.createTestInvoice(account, now, Currency.USD);
    final String paymentExternalKey = invoice.getId().toString();
    final String transactionExternalKey = "sacrebleu";
    final String transactionExternalKey2 = "maisenfin";
    final InvoiceItem invoiceItem = new MockRecurringInvoiceItem(invoice.getId(), account.getId(), subscriptionId, bundleId, "test plan", "test phase", null, now, now.plusMonths(1), requestedAmount, new BigDecimal("1.0"), Currency.USD);
    invoice.addInvoiceItem(invoiceItem);
    invoicePaymentApi.createPurchaseForInvoicePayment(account, invoice.getId(), account.getPaymentMethodId(), null, requestedAmount, Currency.USD, null, paymentExternalKey, transactionExternalKey, ImmutableList.<PluginProperty>of(), INVOICE_PAYMENT, callContext);
    final Payment payment = paymentApi.getPaymentByExternalKey(paymentExternalKey, false, false, ImmutableList.<PluginProperty>of(), callContext);
    final List<PluginProperty> refundProperties = ImmutableList.<PluginProperty>of();
    final Payment payment2 = paymentApi.createRefundWithPaymentControl(account, payment.getId(), requestedAmount, Currency.USD, null, transactionExternalKey2, refundProperties, INVOICE_PAYMENT, callContext);
    assertEquals(payment2.getTransactions().size(), 2);
    assertEquals(payment2.getExternalKey(), paymentExternalKey);
    assertEquals(payment2.getPaymentMethodId(), account.getPaymentMethodId());
    assertEquals(payment2.getAccountId(), account.getId());
    assertEquals(payment2.getAuthAmount().compareTo(BigDecimal.ZERO), 0);
    assertEquals(payment2.getCapturedAmount().compareTo(BigDecimal.ZERO), 0);
    assertEquals(payment2.getPurchasedAmount().compareTo(requestedAmount), 0);
    assertEquals(payment2.getRefundedAmount().compareTo(requestedAmount), 0);
    assertEquals(payment2.getCurrency(), Currency.USD);
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) MockRecurringInvoiceItem(org.killbill.billing.payment.MockRecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) MockRecurringInvoiceItem(org.killbill.billing.payment.MockRecurringInvoiceItem) UUID(java.util.UUID) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 2 with MockRecurringInvoiceItem

use of org.killbill.billing.payment.MockRecurringInvoiceItem in project killbill by killbill.

the class TestPaymentApiNoDB method testSimplePayment.

private void testSimplePayment(final BigDecimal invoiceAmount, final BigDecimal requestedAmount, final BigDecimal expectedAmount) throws Exception {
    final LocalDate now = clock.getUTCToday();
    final Invoice invoice = testHelper.createTestInvoice(account, now, Currency.USD);
    final UUID subscriptionId = UUID.randomUUID();
    final UUID bundleId = UUID.randomUUID();
    invoice.addInvoiceItem(new MockRecurringInvoiceItem(invoice.getId(), account.getId(), subscriptionId, bundleId, "test plan", "test phase", null, now, now.plusMonths(1), invoiceAmount, new BigDecimal("1.0"), Currency.USD));
    try {
        invoicePaymentApi.createPurchaseForInvoicePayment(account, invoice.getId(), account.getPaymentMethodId(), null, requestedAmount, account.getCurrency(), null, invoice.getId().toString(), UUID.randomUUID().toString(), ImmutableList.<PluginProperty>of(), PAYMENT_OPTIONS, callContext);
        final Payment paymentInfo = paymentApi.getPaymentByExternalKey(invoice.getId().toString(), false, false, ImmutableList.<PluginProperty>of(), callContext);
        if (requestedAmount != null && expectedAmount == null) {
            fail("Expected to fail because requested amount > invoice amount");
        }
        assertNotNull(paymentInfo.getId());
        assertTrue(paymentInfo.getPurchasedAmount().compareTo(MoreObjects.firstNonNull(expectedAmount, invoiceAmount)) == 0);
        assertNotNull(paymentInfo.getPaymentNumber());
        assertEquals(paymentInfo.getExternalKey(), invoice.getId().toString());
        assertEquals(paymentInfo.getCurrency(), Currency.USD);
        assertTrue(paymentInfo.getTransactions().get(0).getAmount().compareTo(MoreObjects.firstNonNull(expectedAmount, invoiceAmount)) == 0);
        assertEquals(paymentInfo.getTransactions().get(0).getCurrency(), Currency.USD);
        assertEquals(paymentInfo.getTransactions().get(0).getPaymentId(), paymentInfo.getId());
        assertEquals(paymentInfo.getTransactions().get(0).getTransactionType(), TransactionType.PURCHASE);
        assertEquals(paymentInfo.getTransactions().get(0).getTransactionStatus(), TransactionStatus.SUCCESS);
    } catch (final PaymentApiException e) {
        if (expectedAmount != null) {
            fail("Failed to create payment", e);
        } else {
            log.info(e.getMessage());
        }
    }
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) MockRecurringInvoiceItem(org.killbill.billing.payment.MockRecurringInvoiceItem) UUID(java.util.UUID) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal)

Example 3 with MockRecurringInvoiceItem

use of org.killbill.billing.payment.MockRecurringInvoiceItem in project killbill by killbill.

the class TestPaymentApi method testCreateFailedPurchaseWithPaymentControl.

@Test(groups = "slow")
public void testCreateFailedPurchaseWithPaymentControl() throws PaymentApiException, InvoiceApiException, EventBusException {
    final BigDecimal requestedAmount = BigDecimal.TEN;
    final UUID subscriptionId = UUID.randomUUID();
    final UUID bundleId = UUID.randomUUID();
    final LocalDate now = clock.getUTCToday();
    final Invoice invoice = testHelper.createTestInvoice(account, now, Currency.USD);
    final String paymentExternalKey = invoice.getId().toString();
    final String transactionExternalKey = "brrrrrr";
    mockPaymentProviderPlugin.makeNextPaymentFailWithError();
    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));
    try {
        invoicePaymentApi.createPurchaseForInvoicePayment(account, invoice.getId(), account.getPaymentMethodId(), null, requestedAmount, Currency.USD, null, paymentExternalKey, transactionExternalKey, ImmutableList.<PluginProperty>of(), INVOICE_PAYMENT, callContext);
    } catch (final PaymentApiException expected) {
        assertTrue(true);
    }
    final List<Payment> accountPayments = paymentApi.getAccountPayments(account.getId(), false, false, ImmutableList.<PluginProperty>of(), callContext);
    assertEquals(accountPayments.size(), 1);
    final Payment payment = accountPayments.get(0);
    assertEquals(payment.getExternalKey(), paymentExternalKey);
    assertEquals(payment.getPaymentMethodId(), account.getPaymentMethodId());
    assertEquals(payment.getAccountId(), account.getId());
    assertEquals(payment.getAuthAmount().compareTo(BigDecimal.ZERO), 0);
    assertEquals(payment.getCapturedAmount().compareTo(BigDecimal.ZERO), 0);
    assertEquals(payment.getPurchasedAmount().compareTo(BigDecimal.ZERO), 0);
    assertEquals(payment.getRefundedAmount().compareTo(BigDecimal.ZERO), 0);
    assertEquals(payment.getCurrency(), Currency.USD);
    assertEquals(payment.getTransactions().size(), 1);
    assertEquals(payment.getTransactions().get(0).getExternalKey(), transactionExternalKey);
    assertEquals(payment.getTransactions().get(0).getPaymentId(), payment.getId());
    assertEquals(payment.getTransactions().get(0).getAmount().compareTo(requestedAmount), 0);
    assertEquals(payment.getTransactions().get(0).getCurrency(), Currency.USD);
    assertEquals(payment.getTransactions().get(0).getProcessedAmount().compareTo(BigDecimal.ZERO), 0);
    assertEquals(payment.getTransactions().get(0).getProcessedCurrency(), Currency.USD);
    assertEquals(payment.getTransactions().get(0).getTransactionStatus(), TransactionStatus.PAYMENT_FAILURE);
    assertEquals(payment.getTransactions().get(0).getTransactionType(), TransactionType.PURCHASE);
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) MockRecurringInvoiceItem(org.killbill.billing.payment.MockRecurringInvoiceItem) UUID(java.util.UUID) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 4 with MockRecurringInvoiceItem

use of org.killbill.billing.payment.MockRecurringInvoiceItem in project killbill by killbill.

the class TestPaymentApi method testCreateAbortedRefundWithPaymentControl.

@Test(groups = "slow")
public void testCreateAbortedRefundWithPaymentControl() throws PaymentApiException, InvoiceApiException, EventBusException {
    final BigDecimal requestedAmount = BigDecimal.ONE;
    final UUID subscriptionId = UUID.randomUUID();
    final UUID bundleId = UUID.randomUUID();
    final LocalDate now = clock.getUTCToday();
    final Invoice invoice = testHelper.createTestInvoice(account, now, Currency.USD);
    final String paymentExternalKey = invoice.getId().toString();
    final String transactionExternalKey = "payment";
    final String transactionExternalKey2 = "refund";
    final InvoiceItem invoiceItem = new MockRecurringInvoiceItem(invoice.getId(), account.getId(), subscriptionId, bundleId, "test plan", "test phase", null, now, now.plusMonths(1), requestedAmount, new BigDecimal("1.0"), Currency.USD);
    invoice.addInvoiceItem(invoiceItem);
    invoicePaymentApi.createPurchaseForInvoicePayment(account, invoice.getId(), account.getPaymentMethodId(), null, requestedAmount, Currency.USD, null, paymentExternalKey, transactionExternalKey, ImmutableList.<PluginProperty>of(), INVOICE_PAYMENT, callContext);
    final Payment payment = paymentApi.getPaymentByExternalKey(paymentExternalKey, false, false, ImmutableList.<PluginProperty>of(), callContext);
    final List<PluginProperty> refundProperties = ImmutableList.<PluginProperty>of();
    try {
        paymentApi.createRefundWithPaymentControl(account, payment.getId(), BigDecimal.TEN, Currency.USD, null, transactionExternalKey2, refundProperties, INVOICE_PAYMENT, callContext);
    } catch (final PaymentApiException e) {
        assertTrue(e.getCause() instanceof PaymentControlApiException);
    }
}
Also used : Invoice(org.killbill.billing.invoice.api.Invoice) MockRecurringInvoiceItem(org.killbill.billing.payment.MockRecurringInvoiceItem) InvoiceItem(org.killbill.billing.invoice.api.InvoiceItem) MockRecurringInvoiceItem(org.killbill.billing.payment.MockRecurringInvoiceItem) UUID(java.util.UUID) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) PaymentControlApiException(org.killbill.billing.control.plugin.api.PaymentControlApiException) Test(org.testng.annotations.Test)

Example 5 with MockRecurringInvoiceItem

use of org.killbill.billing.payment.MockRecurringInvoiceItem in project killbill by killbill.

the class TestPaymentApi 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();
    final Invoice invoice = testHelper.createTestInvoice(account, now, Currency.USD);
    final String paymentExternalKey = invoice.getId().toString();
    final String transactionExternalKey = "brrrrrr";
    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));
    invoicePaymentApi.createPurchaseForInvoicePayment(account, invoice.getId(), account.getPaymentMethodId(), null, null, Currency.USD, null, paymentExternalKey, transactionExternalKey, ImmutableList.<PluginProperty>of(), /*
                                                           * We explicitly don't pass InvoicePaymentControlPluginApi.PLUGIN_NAME
                                                             to verify it it automatically added
                                                          */
    CONTROL_PLUGIN_OPTIONS, callContext);
    final Payment payment = paymentApi.getPaymentByExternalKey(paymentExternalKey, false, false, ImmutableList.<PluginProperty>of(), callContext);
    assertEquals(payment.getExternalKey(), paymentExternalKey);
    assertEquals(payment.getPaymentMethodId(), account.getPaymentMethodId());
    assertEquals(payment.getAccountId(), account.getId());
    assertEquals(payment.getAuthAmount().compareTo(BigDecimal.ZERO), 0);
    assertEquals(payment.getCapturedAmount().compareTo(BigDecimal.ZERO), 0);
    assertEquals(payment.getPurchasedAmount().compareTo(requestedAmount), 0);
    assertEquals(payment.getRefundedAmount().compareTo(BigDecimal.ZERO), 0);
    assertEquals(payment.getCurrency(), Currency.USD);
    assertEquals(payment.getTransactions().size(), 1);
    assertEquals(payment.getTransactions().get(0).getExternalKey(), transactionExternalKey);
    assertEquals(payment.getTransactions().get(0).getPaymentId(), payment.getId());
    assertEquals(payment.getTransactions().get(0).getAmount().compareTo(requestedAmount), 0);
    assertEquals(payment.getTransactions().get(0).getCurrency(), Currency.USD);
    assertEquals(payment.getTransactions().get(0).getProcessedAmount().compareTo(requestedAmount), 0);
    assertEquals(payment.getTransactions().get(0).getProcessedCurrency(), Currency.USD);
    assertEquals(payment.getTransactions().get(0).getTransactionStatus(), TransactionStatus.SUCCESS);
    assertEquals(payment.getTransactions().get(0).getTransactionType(), TransactionType.PURCHASE);
    assertNotNull(payment.getTransactions().get(0).getGatewayErrorMsg());
    assertNotNull(payment.getTransactions().get(0).getGatewayErrorCode());
    // Not stricly an API test but interesting to verify that we indeed went through the attempt logic
    final List<PaymentAttemptModelDao> attempts = paymentDao.getPaymentAttempts(payment.getExternalKey(), internalCallContext);
    assertEquals(attempts.size(), 1);
}
Also used : PaymentAttemptModelDao(org.killbill.billing.payment.dao.PaymentAttemptModelDao) Invoice(org.killbill.billing.invoice.api.Invoice) MockRecurringInvoiceItem(org.killbill.billing.payment.MockRecurringInvoiceItem) UUID(java.util.UUID) LocalDate(org.joda.time.LocalDate) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Aggregations

BigDecimal (java.math.BigDecimal)8 UUID (java.util.UUID)8 LocalDate (org.joda.time.LocalDate)8 Invoice (org.killbill.billing.invoice.api.Invoice)8 MockRecurringInvoiceItem (org.killbill.billing.payment.MockRecurringInvoiceItem)8 Test (org.testng.annotations.Test)7 InvoiceItem (org.killbill.billing.invoice.api.InvoiceItem)3 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 PaymentControlApiException (org.killbill.billing.control.plugin.api.PaymentControlApiException)1 PaymentAttemptModelDao (org.killbill.billing.payment.dao.PaymentAttemptModelDao)1