public void testCreateInvoiceSecurity() { try { JbillingAPI api = JbillingAPIFactory.getAPI(); try { api.createInvoice(13, false); fail("User 13 belongs to entity 2"); } catch (Exception e) { } } catch (Exception e) { e.printStackTrace(); fail("Exception caught:" + e); } }
public void testGetTotalAsDecimal() { List<Integer> invoiceIds = new ArrayList<Integer>(); List<Integer> orderIds = new ArrayList<Integer>(); JbillingAPI api = null; try { final Integer USER_ID = 10730; // user has no orders api = JbillingAPIFactory.getAPI(); // test BigDecimal behavior assertFalse(new BigDecimal("1.1").equals(new BigDecimal("1.10"))); assertTrue(new BigDecimal("1.1").compareTo(new BigDecimal("1.10")) == 0); // with items 2 and 3 10% discount should apply orderIds.add( api.createOrder( com.sapienter.jbilling.server.order.WSTest.createMockOrder( USER_ID, 3, new BigDecimal("0.32")))); orderIds.add( api.createOrder( com.sapienter.jbilling.server.order.WSTest.createMockOrder( USER_ID, 3, new BigDecimal("0.32")))); invoiceIds.addAll(Arrays.asList(api.createInvoice(USER_ID, false))); assertEquals(1, invoiceIds.size()); InvoiceWS invoice = api.getInvoiceWS(invoiceIds.get(0)); assertEquals("1.7300000000", invoice.getTotal()); assertEquals(new BigDecimal("1.730"), invoice.getTotalAsDecimal()); } catch (Exception e) { e.printStackTrace(); fail("Exception caught:" + e); } finally { try { for (Integer integer : invoiceIds) { api.deleteInvoice(integer); } System.out.println("Successfully deleted invoices: " + invoiceIds.size()); for (Integer integer : orderIds) { api.deleteOrder(integer); } System.out.println("Successfully deleted orders: " + orderIds.size()); } catch (Exception ignore) { } } }
/** * Tests that when a past due invoice is processed it will generate a new invoice for the current * period that contains all previously un-paid balances as the carried balance. * * <p>Invoices that have been carried still show the original balance for reporting/paper-trail * purposes, but will not be re-processed by the system as part of the normal billing process. * * @throws Exception */ public void testCreateWithCarryOver() throws Exception { final Integer USER_ID = 10743; // user has one past-due invoice to be carried forward final Integer OVERDUE_INVOICE_ID = 70; // holds a $20 balance JbillingAPI api = JbillingAPIFactory.getAPI(); // new order witha single line item OrderWS order = new OrderWS(); order.setUserId(USER_ID); order.setBillingTypeId(Constants.ORDER_BILLING_PRE_PAID); order.setPeriod(1); // once order.setCurrencyId(1); order.setActiveSince(new Date()); OrderLineWS line = new OrderLineWS(); line.setTypeId(Constants.ORDER_LINE_TYPE_ITEM); line.setDescription("Order line"); line.setItemId(1); line.setQuantity(1); line.setPrice(new BigDecimal("10.00")); line.setAmount(new BigDecimal("10.00")); order.setOrderLines(new OrderLineWS[] {line}); // create order Integer orderId = api.createOrder(order); // create invoice Integer invoiceId = api.createInvoice(USER_ID, false)[0]; // validate that the overdue invoice has been carried forward to the newly created invoice InvoiceWS overdue = api.getInvoiceWS(OVERDUE_INVOICE_ID); assertEquals( "Status updated to 'unpaid and carried'", Constants.INVOICE_STATUS_UNPAID_AND_CARRIED, overdue.getStatusId()); assertEquals("Carried invoice will not be re-processed", 0, overdue.getToProcess().intValue()); assertEquals( "Overdue invoice holds original balance", new BigDecimal("20.00"), overdue.getBalanceAsDecimal()); assertEquals( "Overdue invoice delegated to the newly created invoice", invoiceId, overdue.getDelegatedInvoiceId()); // validate that the newly created invoice contains the carried balance InvoiceWS invoice = api.getInvoiceWS(invoiceId); assertEquals( "New invoice balance is equal to the current period charges", new BigDecimal("10.00"), invoice.getBalanceAsDecimal()); assertEquals( "New invoice holds the carried balance equal to the old invoice balance", overdue.getBalanceAsDecimal(), invoice.getCarriedBalanceAsDecimal()); assertEquals( "New invoice total is equal to the current charges plus the carried total", new BigDecimal("30.00"), invoice.getTotalAsDecimal()); }
public void testCreateInvoice() { try { final Integer USER_ID = 10730; // user has no orders JbillingAPI api = JbillingAPIFactory.getAPI(); // setup order OrderWS order = new OrderWS(); order.setUserId(USER_ID); order.setBillingTypeId(Constants.ORDER_BILLING_PRE_PAID); order.setPeriod(1); // once order.setCurrencyId(1); order.setActiveSince(new Date()); OrderLineWS line = new OrderLineWS(); line.setTypeId(Constants.ORDER_LINE_TYPE_ITEM); line.setDescription("Order line"); line.setItemId(1); line.setQuantity(1); line.setPrice(new BigDecimal("10.00")); line.setAmount(new BigDecimal("10.00")); order.setOrderLines(new OrderLineWS[] {line}); /* * Test invoicing of one-time and recurring orders */ // create 1st order Integer orderId1 = api.createOrder(order); // create 2nd order line.setPrice(new BigDecimal("20.00")); line.setAmount(new BigDecimal("20.00")); Integer orderId2 = api.createOrder(order); // create invoice Integer[] invoices = api.createInvoice(USER_ID, false); assertEquals("Number of invoices returned", 1, invoices.length); InvoiceWS invoice = api.getInvoiceWS(invoices[0]); assertNull("Invoice is not delegated.", invoice.getDelegatedInvoiceId()); assertEquals( "Invoice does not have a carried balance.", BigDecimal.ZERO, invoice.getCarriedBalanceAsDecimal()); Integer[] invoicedOrderIds = invoice.getOrders(); assertEquals("Number of orders invoiced", 2, invoicedOrderIds.length); Arrays.sort(invoicedOrderIds); assertEquals("Order 1 invoiced", orderId1, invoicedOrderIds[0]); assertEquals("Order 2 invoiced", orderId2, invoicedOrderIds[1]); assertEquals("Total is 30.0", new BigDecimal("30.00"), invoice.getTotalAsDecimal()); // clean up api.deleteInvoice(invoices[0]); api.deleteOrder(orderId1); api.deleteOrder(orderId2); /* * Test only recurring order can generate invoice. */ // one-time order line.setPrice(new BigDecimal("2.00")); line.setAmount(new BigDecimal("2.00")); orderId1 = api.createOrder(order); // try to create invoice, but none should be returned invoices = api.createInvoice(USER_ID, true); // Note: CXF returns null for empty array if (invoices != null) { assertEquals("Number of invoices returned", 0, invoices.length); } // recurring order order.setPeriod(2); // monthly line.setPrice(new BigDecimal("3.00")); line.setAmount(new BigDecimal("3.00")); orderId2 = api.createOrder(order); // create invoice invoices = api.createInvoice(USER_ID, true); assertEquals("Number of invoices returned", 1, invoices.length); invoice = api.getInvoiceWS(invoices[0]); invoicedOrderIds = invoice.getOrders(); assertEquals("Number of orders invoiced", 2, invoicedOrderIds.length); Arrays.sort(invoicedOrderIds); assertEquals("Order 1 invoiced", orderId1, invoicedOrderIds[0]); assertEquals("Order 2 invoiced", orderId2, invoicedOrderIds[1]); assertEquals("Total is 5.0", new BigDecimal("5.00"), invoice.getTotalAsDecimal()); // clean up api.deleteInvoice(invoices[0]); api.deleteOrder(orderId1); api.deleteOrder(orderId2); } catch (Exception e) { e.printStackTrace(); fail("Exception caught:" + e); } }