Exemple #1
0
  public void testCreateInvoiceFromOrder() throws Exception {
    JbillingAPI api = JbillingAPIFactory.getAPI();

    final Integer USER_ID = 10730; // user has no orders

    // setup orders
    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 orders
    Integer orderId1 = api.createOrder(order);
    Integer orderId2 = api.createOrder(order);

    // generate invoice using first order
    Integer invoiceId = api.createInvoiceFromOrder(orderId1, null);

    assertNotNull("Order 1 created", orderId1);
    assertNotNull("Order 2 created", orderId2);
    assertNotNull("Invoice created", invoiceId);

    Integer[] invoiceIds = api.getLastInvoices(USER_ID, 2);
    assertEquals("Only 1 invoice was generated", 1, invoiceIds.length);

    InvoiceWS invoice = api.getInvoiceWS(invoiceId);
    assertEquals("Invoice total is $10.00", new BigDecimal("10.00"), invoice.getTotalAsDecimal());
    assertEquals("Only 1 order invoiced", 1, invoice.getOrders().length);
    assertEquals("Invoice generated from 1st order", orderId1, invoice.getOrders()[0]);

    // add second order to invoice
    Integer invoiceId2 = api.createInvoiceFromOrder(orderId2, invoiceId);
    assertEquals("Order added to the same invoice", invoiceId, invoiceId2);

    invoiceIds = api.getLastInvoices(USER_ID, 2);
    assertEquals("Still only 1 invoice generated", 1, invoiceIds.length);

    invoice = api.getInvoiceWS(invoiceId);
    assertEquals("Invoice total is $20.00", new BigDecimal("20.00"), invoice.getTotalAsDecimal());
    assertEquals("2 orders invoiced", 2, invoice.getOrders().length);

    // cleanup
    api.deleteInvoice(invoiceId);
    api.deleteOrder(orderId1);
    api.deleteOrder(orderId2);
  }
Exemple #2
0
  public void testGetPaperInvoicePDF() throws Exception {
    final Integer USER_ID = 2; // user has invoices

    JbillingAPI api = JbillingAPIFactory.getAPI();

    Integer[] invoiceIds = api.getLastInvoices(USER_ID, 1);
    assertEquals("Invoice found for user", 1, invoiceIds.length);

    byte[] pdf = api.getPaperInvoicePDF(invoiceIds[0]);
    assertTrue("PDF invoice bytes returned", pdf.length > 0);
  }
Exemple #3
0
  public void testGet() {
    try {
      JbillingAPI api = JbillingAPIFactory.getAPI();

      // get
      // try getting one that doesn't belong to us
      try {
        System.out.println("Getting invalid invoice");
        api.getInvoiceWS(75);
        fail("Invoice 75 belongs to entity 2");
      } catch (Exception e) {
      }

      System.out.println("Getting invoice");
      InvoiceWS retInvoice = api.getInvoiceWS(15);
      assertNotNull("invoice not returned", retInvoice);
      assertEquals("invoice id", retInvoice.getId(), new Integer(15));
      System.out.println("Got = " + retInvoice);

      // latest
      // first, from a guy that is not mine
      try {
        api.getLatestInvoice(13);
        fail("User 13 belongs to entity 2");
      } catch (Exception e) {
      }
      System.out.println("Getting latest invoice");
      retInvoice = api.getLatestInvoice(2);
      assertNotNull("invoice not returned", retInvoice);
      assertEquals("invoice's user id", retInvoice.getUserId(), new Integer(2));
      System.out.println("Got = " + retInvoice);
      Integer lastInvoice = retInvoice.getId();

      // List of last
      // first, from a guy that is not mine
      try {
        api.getLastInvoices(13, 5);
        fail("User 13 belongs to entity 2");
      } catch (Exception e) {
      }
      System.out.println("Getting last 5 invoices");
      Integer invoices[] = api.getLastInvoices(2, 5);
      assertNotNull("invoice not returned", invoices);

      retInvoice = api.getInvoiceWS(invoices[0]);
      assertEquals("invoice's user id", new Integer(2), retInvoice.getUserId());
      System.out.println("Got = " + invoices.length + " invoices");
      for (int f = 0; f < invoices.length; f++) {
        System.out.println(" Invoice " + (f + 1) + invoices[f]);
      }

      // now I want just the two latest
      System.out.println("Getting last 2 invoices");
      invoices = api.getLastInvoices(2, 2);
      assertNotNull("invoice not returned", invoices);
      retInvoice = api.getInvoiceWS(invoices[0]);
      assertEquals("invoice's user id", new Integer(2), retInvoice.getUserId());
      assertEquals("invoice's has to be latest", lastInvoice, retInvoice.getId());
      assertEquals("there should be only 2", 2, invoices.length);

      // get some by date
      System.out.println("Getting by date (empty)");
      Integer invoices2[] = api.getInvoicesByDate("2000-01-01", "2005-01-01");
      // CXF returns null instead of empty arrays
      // assertNotNull("invoice not returned", invoices2);
      if (invoices2 != null) {
        assertTrue("array not empty", invoices2.length == 0);
      }

      System.out.println("Getting by date");
      invoices2 = api.getInvoicesByDate("2006-01-01", "2007-01-01");
      assertNotNull("invoice not returned", invoices2);
      assertFalse("array not empty", invoices2.length == 0);
      System.out.println("Got array " + invoices2.length + " getting " + invoices2[0]);
      retInvoice = api.getInvoiceWS(invoices2[0]);
      assertNotNull("invoice not there", retInvoice);
      System.out.println("Got invoice " + retInvoice);

      System.out.println("Done!");

    } catch (Exception e) {
      e.printStackTrace();
      fail("Exception caught:" + e);
    }
  }