Search in sources :

Example 36 with Invoice

use of org.killbill.billing.client.model.gen.Invoice in project killbill by killbill.

the class TestInvoicePayment method testPartialRefundWithFullInvoiceItemAdjustment.

@Test(groups = "slow", description = "Cannot create invoice item adjustments for more than the refund amount")
public void testPartialRefundWithFullInvoiceItemAdjustment() throws Exception {
    final InvoicePayment paymentJson = setupScenarioWithPayment(true);
    // Get the individual items for the invoice
    final Invoice invoice = invoiceApi.getInvoice(paymentJson.getTargetInvoiceId(), false, AuditLevel.NONE, requestOptions);
    final InvoiceItem itemToAdjust = invoice.getItems().get(0);
    // Issue a refund for a fraction of the amount
    final BigDecimal refundAmount = getFractionOfAmount(itemToAdjust.getAmount());
    final BigDecimal expectedInvoiceBalance = invoice.getBalance();
    // Post and verify the refund
    final InvoicePaymentTransaction refund = new InvoicePaymentTransaction();
    refund.setPaymentId(paymentJson.getPaymentId());
    refund.setAmount(refundAmount);
    refund.setIsAdjusted(true);
    final InvoiceItem adjustment = new InvoiceItem();
    adjustment.setInvoiceItemId(itemToAdjust.getInvoiceItemId());
    // Ask for an adjustment for the full amount (bigger than the refund amount)
    adjustment.setAmount(itemToAdjust.getAmount());
    refund.setAdjustments(ImmutableList.<InvoiceItem>of(adjustment));
    invoicePaymentApi.createRefundWithAdjustments(paymentJson.getPaymentId(), refund, paymentJson.getPaymentMethodId(), NULL_PLUGIN_PROPERTIES, requestOptions);
    final Payment paymentAfterRefundJson = paymentApi.getPayment(paymentJson.getPaymentId(), NULL_PLUGIN_PROPERTIES, requestOptions);
    // The refund did go through
    verifyRefund(paymentJson, paymentAfterRefundJson, refundAmount);
    // But not the adjustments
    verifyInvoice(paymentJson, expectedInvoiceBalance);
}
Also used : InvoicePayment(org.killbill.billing.client.model.gen.InvoicePayment) InvoicePayment(org.killbill.billing.client.model.gen.InvoicePayment) Payment(org.killbill.billing.client.model.gen.Payment) Invoice(org.killbill.billing.client.model.gen.Invoice) InvoiceItem(org.killbill.billing.client.model.gen.InvoiceItem) InvoicePaymentTransaction(org.killbill.billing.client.model.gen.InvoicePaymentTransaction) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 37 with Invoice

use of org.killbill.billing.client.model.gen.Invoice in project killbill by killbill.

the class TestInvoicePayment method verifyInvoice.

private void verifyInvoice(final InvoicePayment paymentJson, final BigDecimal expectedInvoiceBalance) throws KillBillClientException {
    final Invoice invoiceJson = invoiceApi.getInvoice(paymentJson.getTargetInvoiceId(), requestOptions);
    Assert.assertEquals(invoiceJson.getBalance().setScale(2, BigDecimal.ROUND_HALF_UP), expectedInvoiceBalance.setScale(2, BigDecimal.ROUND_HALF_UP));
}
Also used : Invoice(org.killbill.billing.client.model.gen.Invoice)

Example 38 with Invoice

use of org.killbill.billing.client.model.gen.Invoice in project killbill by killbill.

the class TestInvoicePayment method testRefundWithFullInvoiceItemAdjustment.

@Test(groups = "slow", description = "Can create a full refund with invoice item adjustment")
public void testRefundWithFullInvoiceItemAdjustment() throws Exception {
    final InvoicePayment paymentJson = setupScenarioWithPayment(true);
    // Get the individual items for the invoice
    final Invoice invoice = invoiceApi.getInvoice(paymentJson.getTargetInvoiceId(), false, AuditLevel.NONE, requestOptions);
    final InvoiceItem itemToAdjust = invoice.getItems().get(0);
    // Issue a refund for the full amount
    final BigDecimal refundAmount = itemToAdjust.getAmount();
    final BigDecimal expectedInvoiceBalance = BigDecimal.ZERO;
    // Post and verify the refund
    final InvoicePaymentTransaction refund = new InvoicePaymentTransaction();
    refund.setPaymentId(paymentJson.getPaymentId());
    refund.setAmount(refundAmount);
    refund.setIsAdjusted(true);
    final InvoiceItem adjustment = new InvoiceItem();
    adjustment.setInvoiceItemId(itemToAdjust.getInvoiceItemId());
    // null amount means full adjustment for that item
    refund.setAdjustments(ImmutableList.<InvoiceItem>of(adjustment));
    invoicePaymentApi.createRefundWithAdjustments(paymentJson.getPaymentId(), refund, paymentJson.getPaymentMethodId(), NULL_PLUGIN_PROPERTIES, requestOptions);
    final Payment paymentAfterRefundJson = paymentApi.getPayment(paymentJson.getPaymentId(), NULL_PLUGIN_PROPERTIES, requestOptions);
    verifyRefund(paymentJson, paymentAfterRefundJson, refundAmount);
    // Verify the invoice balance
    verifyInvoice(paymentJson, expectedInvoiceBalance);
}
Also used : InvoicePayment(org.killbill.billing.client.model.gen.InvoicePayment) InvoicePayment(org.killbill.billing.client.model.gen.InvoicePayment) Payment(org.killbill.billing.client.model.gen.Payment) Invoice(org.killbill.billing.client.model.gen.Invoice) InvoiceItem(org.killbill.billing.client.model.gen.InvoiceItem) InvoicePaymentTransaction(org.killbill.billing.client.model.gen.InvoicePaymentTransaction) BigDecimal(java.math.BigDecimal) Test(org.testng.annotations.Test)

Example 39 with Invoice

use of org.killbill.billing.client.model.gen.Invoice in project killbill by killbill.

the class TestOverdue method testExclusionControlTagOverdueConfig.

@Test(groups = "slow", description = "Allow overdue condition by exclusion control tag defined in overdue config xml file")
public void testExclusionControlTagOverdueConfig() throws Exception {
    uploadTenantOverdueConfig("org/killbill/billing/server/overdueWithExclusionControlTag.xml");
    // Create an account without a payment method and assign a TEST tag
    final Account accountJson = createAccountNoPMBundleAndSubscription();
    callbackServlet.pushExpectedEvent(ExtBusEventType.TAG_CREATION);
    final Tags accountTag = accountApi.createAccountTags(accountJson.getAccountId(), ImmutableList.<UUID>of(ControlTagType.TEST.getId()), requestOptions);
    callbackServlet.assertListenerStatus();
    assertEquals(accountTag.get(0).getTagDefinitionId(), ControlTagType.TEST.getId());
    // Create an account without a TEST tag
    final Account accountJsonNoTag = createAccountNoPMBundleAndSubscription();
    // move a month a wait for invoicing
    // No payment will be triggered as the account doesn't have a payment method
    callbackServlet.pushExpectedEvents(ExtBusEventType.SUBSCRIPTION_PHASE, ExtBusEventType.INVOICE_CREATION, ExtBusEventType.INVOICE_PAYMENT_FAILED, ExtBusEventType.SUBSCRIPTION_PHASE, ExtBusEventType.INVOICE_CREATION, ExtBusEventType.INVOICE_PAYMENT_FAILED);
    clock.addMonths(1);
    callbackServlet.assertListenerStatus();
    // Get the invoices
    final List<Invoice> invoices = accountApi.getInvoicesForAccount(accountJson.getAccountId(), null, null, null, requestOptions);
    // 2 invoices but look for the non zero dollar one
    assertEquals(invoices.size(), 2);
    final List<Invoice> invoicesNoTag = accountApi.getInvoicesForAccount(accountJsonNoTag.getAccountId(), null, null, null, requestOptions);
    // 2 invoices but look for the non zero dollar one
    assertEquals(invoicesNoTag.size(), 2);
    // We're still clear - see the configuration
    Assert.assertTrue(accountApi.getOverdueAccount(accountJson.getAccountId(), requestOptions).isClearState());
    Assert.assertTrue(accountApi.getOverdueAccount(accountJsonNoTag.getAccountId(), requestOptions).isClearState());
    callbackServlet.pushExpectedEvents(ExtBusEventType.INVOICE_CREATION, ExtBusEventType.INVOICE_CREATION, ExtBusEventType.INVOICE_PAYMENT_FAILED, ExtBusEventType.INVOICE_PAYMENT_FAILED, ExtBusEventType.BLOCKING_STATE, ExtBusEventType.OVERDUE_CHANGE);
    clock.addDays(30);
    callbackServlet.assertListenerStatus();
    // This account is not expected to move to OD1 state because it does not match with exclusion controlTag defined
    Assert.assertTrue(accountApi.getOverdueAccount(accountJson.getAccountId(), requestOptions).isClearState());
    // This account is expected to move to OD1 state because it matches with exclusion controlTag defined
    Assert.assertEquals(accountApi.getOverdueAccount(accountJsonNoTag.getAccountId(), requestOptions).getName(), "OD1");
}
Also used : Account(org.killbill.billing.client.model.gen.Account) Invoice(org.killbill.billing.client.model.gen.Invoice) Tags(org.killbill.billing.client.model.Tags) Test(org.testng.annotations.Test)

Example 40 with Invoice

use of org.killbill.billing.client.model.gen.Invoice in project killbill by killbill.

the class TestPayment method testWithAbortedInvoicePayment.

@Test(groups = "slow", description = "https://github.com/killbill/killbill/issues/1288")
public void testWithAbortedInvoicePayment() throws Exception {
    mockPaymentProviderPlugin.makeNextPaymentFailWithError();
    final Account account = createAccountWithPMBundleAndSubscriptionAndWaitForFirstInvoice(false);
    // Getting Invoice #2 (first after Trial period)
    final Invoice failedInvoice = accountApi.getInvoicesForAccount(account.getAccountId(), null, null, null, RequestOptions.empty()).get(1);
    // Verify initial state
    final Payments initialPayments = accountApi.getPaymentsForAccount(account.getAccountId(), true, false, ImmutableMap.<String, String>of(), AuditLevel.NONE, requestOptions);
    Assert.assertEquals(initialPayments.size(), 1);
    Assert.assertEquals(initialPayments.get(0).getTransactions().size(), 1);
    Assert.assertEquals(initialPayments.get(0).getTransactions().get(0).getStatus(), TransactionStatus.PAYMENT_FAILURE);
    Assert.assertEquals(initialPayments.get(0).getPaymentAttempts().size(), 2);
    Assert.assertEquals(initialPayments.get(0).getPaymentAttempts().get(0).getStateName(), "RETRIED");
    Assert.assertEquals(initialPayments.get(0).getPaymentAttempts().get(1).getStateName(), "SCHEDULED");
    final InvoicePayments initialInvoicePayments = invoiceApi.getPaymentsForInvoice(failedInvoice.getInvoiceId(), requestOptions);
    Assert.assertEquals(initialInvoicePayments.size(), 1);
    Assert.assertEquals(initialInvoicePayments.get(0).getPaymentId(), initialPayments.get(0).getPaymentId());
    // Trigger manually an invoice payment but make the control plugin abort it
    mockPaymentControlProviderPlugin.setAborted(true);
    final HashMultimap<String, String> queryParams = HashMultimap.create();
    queryParams.putAll("controlPluginName", Arrays.<String>asList(MockPaymentControlProviderPlugin.PLUGIN_NAME));
    final RequestOptions inputOptions = RequestOptions.builder().withCreatedBy(createdBy).withReason(reason).withComment(comment).withQueryParams(queryParams).build();
    final InvoicePayment invoicePayment = new InvoicePayment();
    invoicePayment.setPurchasedAmount(failedInvoice.getBalance());
    invoicePayment.setAccountId(failedInvoice.getAccountId());
    invoicePayment.setTargetInvoiceId(failedInvoice.getInvoiceId());
    final InvoicePayment invoicePaymentNull = invoiceApi.createInstantPayment(failedInvoice.getInvoiceId(), invoicePayment, ImmutableList.of(), null, inputOptions);
    Assert.assertNull(invoicePaymentNull);
    // Verify new state
    final Payments updatedPayments = accountApi.getPaymentsForAccount(account.getAccountId(), true, false, ImmutableMap.<String, String>of(), AuditLevel.NONE, requestOptions);
    Assert.assertEquals(updatedPayments.size(), 1);
    Assert.assertEquals(updatedPayments.get(0).getPaymentId(), initialPayments.get(0).getPaymentId());
    final InvoicePayments updatedInvoicePayments = invoiceApi.getPaymentsForInvoice(failedInvoice.getInvoiceId(), requestOptions);
    Assert.assertEquals(updatedInvoicePayments.size(), 1);
    Assert.assertEquals(updatedInvoicePayments.get(0).getPaymentId(), updatedPayments.get(0).getPaymentId());
}
Also used : Account(org.killbill.billing.client.model.gen.Account) InvoicePayment(org.killbill.billing.client.model.gen.InvoicePayment) Invoice(org.killbill.billing.client.model.gen.Invoice) RequestOptions(org.killbill.billing.client.RequestOptions) InvoicePayments(org.killbill.billing.client.model.InvoicePayments) Payments(org.killbill.billing.client.model.Payments) InvoicePayments(org.killbill.billing.client.model.InvoicePayments) Test(org.testng.annotations.Test)

Aggregations

Invoice (org.killbill.billing.client.model.gen.Invoice)42 Test (org.testng.annotations.Test)37 Account (org.killbill.billing.client.model.gen.Account)33 DateTime (org.joda.time.DateTime)17 InvoicePayment (org.killbill.billing.client.model.gen.InvoicePayment)15 InvoiceItem (org.killbill.billing.client.model.gen.InvoiceItem)14 BigDecimal (java.math.BigDecimal)12 Invoices (org.killbill.billing.client.model.Invoices)10 InvoiceItems (org.killbill.billing.client.model.InvoiceItems)7 InvoicePaymentTransaction (org.killbill.billing.client.model.gen.InvoicePaymentTransaction)7 LocalDate (org.joda.time.LocalDate)6 Subscription (org.killbill.billing.client.model.gen.Subscription)6 UUID (java.util.UUID)5 InvoicePayments (org.killbill.billing.client.model.InvoicePayments)5 Payment (org.killbill.billing.client.model.gen.Payment)4 ArrayList (java.util.ArrayList)3 KillBillClientException (org.killbill.billing.client.KillBillClientException)3 BulkSubscriptionsBundles (org.killbill.billing.client.model.BulkSubscriptionsBundles)3 Bundles (org.killbill.billing.client.model.Bundles)3 Payments (org.killbill.billing.client.model.Payments)3