Example #1
0
  /** Test payInvoice(invoice) API call. */
  @Test
  public void testPayInvoice() {
    // setup
    UserWS user = buildUser(PRANCING_PONY_ACCOUNT_TYPE);
    user.setId(api.createUser(user));

    ItemTypeWS itemType = buildItemType();
    itemType.setId(api.createItemCategory(itemType));

    ItemDTOEx item = buildItem(itemType.getId(), api.getCallerCompanyId());
    item.setId(api.createItem(item));

    // testing
    System.out.println("Getting an invoice paid, and validating the payment.");
    OrderWS order = buildOrder(user.getId(), Arrays.asList(item.getId()), new BigDecimal("3.45"));
    Integer invoiceId =
        api.createOrderAndInvoice(
            order, OrderChangeBL.buildFromOrder(order, ORDER_CHANGE_STATUS_APPLY));
    Integer orderId = api.getInvoiceWS(invoiceId).getOrders()[0];
    PaymentAuthorizationDTOEx auth = api.payInvoice(invoiceId);
    assertNotNull("auth can not be null", auth);
    PaymentWS payment = api.getLatestPayment(user.getId());
    assertNotNull("payment can not be null", payment);
    assertNotNull("auth in payment can not be null", payment.getAuthorizationId());

    // cleanup
    api.deletePayment(auth.getPaymentId());
    api.deleteInvoice(invoiceId);
    api.deleteOrder(orderId);
    api.deleteItem(item.getId());
    api.deleteItemCategory(itemType.getId());
    api.deleteUser(user.getId());
  }
Example #2
0
  /**
   * Tries to create payment against review invoice. Here, instead of using the billing process to
   * generate a review invoice we are creating a review invoice with the help of saveLegacyInvoice
   * call.
   */
  @Test
  public void testPayReviewInvoice() {
    // creating new user
    UserWS user = buildUser(PRANCING_PONY_ACCOUNT_TYPE);
    user.setId(api.createUser(user));

    ItemTypeWS itemType = buildItemType();
    itemType.setId(api.createItemCategory(itemType));

    ItemDTOEx item = buildItem(itemType.getId(), api.getCallerCompanyId());
    item.setId(api.createItem(item));

    InvoiceWS invoice = buildInvoice(user.getId(), item.getId());
    invoice.setIsReview(Integer.valueOf(1));
    invoice.setId(api.saveLegacyInvoice(invoice));

    // check if invoice is a review invoice
    System.out.println("Invoice is review : " + invoice.getIsReview());
    assertEquals("Invoice is a review invoice", Integer.valueOf(1), invoice.getIsReview());

    try {
      // pay for a review invoice
      api.payInvoice(invoice.getId());
      fail("We should not be able to issue a payment against review invoice");
    } catch (SessionInternalError e) {
      System.out.println(e.getMessage());
    }

    // clean up
    api.deleteInvoice(invoice.getId());
    api.deleteItem(item.getId());
    api.deleteItemCategory(itemType.getId());
    api.deleteUser(user.getId());
  }
  public void testWrongPluginParameters() throws JbillingAPIException, IOException {
    long currentTimeMillis = System.currentTimeMillis();
    UserWS user = SureTaxCompositionTaskTest.createUser(currentTimeMillis, "");
    // Create an item with wrong Transaction Type code
    Integer itemId1 =
        CreateObjectUtil.createItem(
            "Long Distance Call intra-state", "1.5", currentTimeMillis + "", "2201", api);
    ItemDTOEx item1 = api.getItem(itemId1, null, null);
    MetaFieldValueWS[] metaFields = new MetaFieldValueWS[1];
    MetaFieldValueWS transTypeMetaField = new MetaFieldValueWS();
    transTypeMetaField.setStringValue("010101");
    transTypeMetaField.setFieldName("Transaction Type Code");
    metaFields[0] = transTypeMetaField;
    item1.setMetaFields(metaFields);
    api.updateItem(item1);
    // purchase order with taxable items
    Calendar cal = Calendar.getInstance();
    // I want to set the active since to 07 June 2012 , so the billing
    // process sees it and invoices it
    // set the calendar to 06/07
    cal.set(2010, 5, 7);
    OrderWS order =
        CreateObjectUtil.createOrderObject(
            user.getUserId(), 1, ServerConstants.ORDER_BILLING_POST_PAID, 1, cal.getTime());

    CreateObjectUtil.addLine(
        order,
        10,
        ServerConstants.ORDER_LINE_TYPE_ITEM,
        itemId1,
        new BigDecimal(1.5),
        "Long Distance Call-intra state");

    order.setDueDateUnitId(PeriodUnitDTO.DAY);
    order.setDueDateValue(0); // order due

    order.setId(api.createOrder(order)); // create order
    order = api.getOrder(order.getId());
    assertNotNull("order created", order.getId());

    Integer[] invoiceIds = null;
    try {
      invoiceIds =
          api.createInvoiceWithDate(
              user.getUserId(),
              new Date(),
              PeriodUnitDTO.DAY,
              45,
              false); // getAllInvoicesForUser(user.getUserId());
    } catch (Exception e) {
      if (e instanceof SessionInternalError && e.getMessage().contains("Invalid Validation Key")) {
        // do nothing
      } else {
        assertTrue("Wrong exception raised", false);
      }
    }
    assertNull("No invoices must be generated", invoiceIds);
  }
Example #4
0
  /** Removing pre-authorization when the CC number is changed. */
  @Test
  public void testRemoveOnCCChange() {
    UserWS user = buildUser(PRANCING_PONY_ACCOUNT_TYPE);
    user.setId(api.createUser(user));

    ItemTypeWS itemType = buildItemType();
    itemType.setId(api.createItemCategory(itemType));

    ItemDTOEx item = buildItem(itemType.getId(), api.getCallerCompanyId());
    item.setId(api.createItem(item));

    // put a pre-auth record on this user
    OrderWS order = buildOrder(user.getId(), Arrays.asList(item.getId()), new BigDecimal("3.45"));

    PaymentAuthorizationDTOEx auth =
        api.createOrderPreAuthorize(
            order, OrderChangeBL.buildFromOrder(order, ORDER_CHANGE_STATUS_APPLY));
    Integer orderId = api.getLatestOrder(user.getId()).getId();

    PaymentWS preAuthPayment = api.getPayment(auth.getPaymentId());
    assertThat(preAuthPayment, is(not(nullValue())));
    assertThat(preAuthPayment.getIsPreauth(), is(1));
    assertThat(preAuthPayment.getDeleted(), is(0)); // NOT deleted

    // update the user's credit card, this should remove the old card
    // and delete any associated pre-authorizations
    DateTimeFormatter format = DateTimeFormat.forPattern(Constants.CC_DATE_FORMAT);
    user = api.getUserWS(user.getId());
    com.sapienter.jbilling.server.user.WSTest.updateMetaField(
        user.getPaymentInstruments().iterator().next().getMetaFields(),
        PaymentMethodHelper.CC_MF_EXPIRY_DATE,
        format.print(new DateMidnight().plusYears(4).withDayOfMonth(1).toDate().getTime()));
    api.updateUser(user);
    System.out.println("User instruments are: " + user.getPaymentInstruments());
    // validate that the pre-auth payment is no longer available
    preAuthPayment = api.getPayment(auth.getPaymentId());
    assertThat(preAuthPayment, is(not(nullValue())));
    assertThat(preAuthPayment.getIsPreauth(), is(1));
    assertThat(preAuthPayment.getDeleted(), is(1)); // is now deleted

    // cleanup
    api.deleteOrder(orderId);
    api.deleteItem(item.getId());
    api.deleteItemCategory(itemType.getId());
    api.deleteUser(user.getId());
  }
Example #5
0
  /**
   * Test for: CreditCardFilter.For now it uses a value already in DB for the blacklisted cc number.
   * In prepare-test db the cc number 5555555555554444 is blacklisted.
   */
  @Test
  public void testBlacklistCreditCardFilter() {
    UserWS user = buildUser(PRANCING_PONY_ACCOUNT_TYPE, "5555555555554444");
    user.setId(api.createUser(user));

    ItemTypeWS itemType = buildItemType();
    itemType.setId(api.createItemCategory(itemType));

    ItemDTOEx item = buildItem(itemType.getId(), api.getCallerCompanyId());
    item.setId(api.createItem(item));

    InvoiceWS invoice = buildInvoice(user.getId(), item.getId());
    invoice.setId(api.saveLegacyInvoice(invoice));

    // get invoice id
    invoice = api.getLatestInvoice(user.getId());
    assertNotNull("Couldn't get last invoice", invoice);
    Integer invoiceId = invoice.getId();
    assertNotNull("Invoice id was null", invoiceId);

    // try paying the invoice
    System.out.println("Trying to pay invoice for blacklisted user ...");
    PaymentAuthorizationDTOEx authInfo = api.payInvoice(invoiceId);
    assertNotNull("Payment result empty", authInfo);

    // check that it was failed by the test blacklist filter
    assertFalse(
        "Payment wasn't failed for user: "******"Processor response", "Credit card number is blacklisted.", authInfo.getResponseMessage());

    // cleanup
    api.deleteInvoice(invoiceId);
    api.deleteItem(item.getId());
    api.deleteItemCategory(itemType.getId());
    api.deleteUser(user.getId());
  }
Example #6
0
 private ItemDTOEx buildItem(Integer itemTypeId, Integer priceModelCompanyId) {
   ItemDTOEx item = new ItemDTOEx();
   long millis = System.currentTimeMillis();
   String name = String.valueOf(millis) + new Random().nextInt(10000);
   item.setDescription("Payment, Product:" + name);
   item.setPriceModelCompanyId(priceModelCompanyId);
   item.setPrice(new BigDecimal("10"));
   item.setNumber("PYM-PROD-" + name);
   item.setAssetManagementEnabled(0);
   Integer typeIds[] = new Integer[] {itemTypeId};
   item.setTypes(typeIds);
   return item;
 }
 /**
  * To create a product without calling the createItem API.
  *
  * @param entityId
  * @param cost
  * @param currencyID
  * @param itemType
  * @return ItemDTOEx
  */
 public static ItemDTOEx createItem(
     Integer entityId,
     BigDecimal cost,
     Integer currencyID,
     Integer itemType,
     String description,
     Date startDate) {
   ItemDTOEx item = new ItemDTOEx();
   item.setNumber(String.valueOf(new Date().getTime()));
   item.setEntityId(entityId);
   item.setDescription(description + ServerConstants.SINGLE_SPACE + new Date().getTime());
   item.setCurrencyId(currencyID);
   item.setTypes(new Integer[] {itemType});
   item.setPrices(setItemPrice(cost, startDate, entityId, currencyID));
   return item;
 }
  public static ItemDTOEx createPercentageItem(
      Integer entityId,
      BigDecimal percentage,
      Integer currencyID,
      Integer itemType,
      String description) {
    ItemDTOEx item = new ItemDTOEx();
    item.setNumber(String.valueOf(new Date().getTime()));
    item.setEntityId(entityId);
    item.setDescription(
        "Percentage Item: " + description + ServerConstants.SINGLE_SPACE + new Date().getTime());
    item.setCurrencyId(currencyID);
    item.setTypes(new Integer[] {itemType});

    return item;
  }
Example #9
0
  /** Tests processPayment API call. */
  @Test
  public void testProcessPayment() {
    // setup
    UserWS user = buildUser(PRANCING_PONY_ACCOUNT_TYPE, "4111111111111111");
    user.setId(api.createUser(user));

    ItemTypeWS itemType = buildItemType();
    itemType.setId(api.createItemCategory(itemType));

    ItemDTOEx item = buildItem(itemType.getId(), api.getCallerCompanyId());
    item.setId(api.createItem(item));

    // first, create two unpaid invoices
    OrderWS order = buildOrder(user.getId(), Arrays.asList(item.getId()), new BigDecimal("10.00"));
    Integer invoiceId1 =
        api.createOrderAndInvoice(
            order, OrderChangeBL.buildFromOrder(order, ORDER_CHANGE_STATUS_APPLY));
    Integer invoiceId2 =
        api.createOrderAndInvoice(
            order, OrderChangeBL.buildFromOrder(order, ORDER_CHANGE_STATUS_APPLY));

    // create the payment
    PaymentWS payment = new PaymentWS();
    payment.setAmount(new BigDecimal("5.00"));
    payment.setIsRefund(new Integer(0));
    payment.setMethodId(Constants.PAYMENT_METHOD_VISA);
    payment.setPaymentDate(Calendar.getInstance().getTime());
    payment.setCurrencyId(CURRENCY_USD);
    payment.setUserId(user.getId());

    //  try a credit card number that fails
    // note that creating a payment with a NEW credit card will save it and associate
    // it with the user who made the payment.
    Calendar cal = Calendar.getInstance();
    cal.add(Calendar.YEAR, 5);

    PaymentInformationWS cc =
        PaymentMethodHelper.createCreditCard(
            CC_PAYMENT_TYPE, "Frodo Baggins", "4111111111111111", cal.getTime());
    cc.setPaymentMethodId(Constants.PAYMENT_METHOD_VISA);
    payment.getPaymentInstruments().add(cc);

    System.out.println("processing payment.");
    PaymentAuthorizationDTOEx authInfo = api.processPayment(payment, null);

    // check payment failed
    assertNotNull("Payment result not null", authInfo);
    assertFalse(
        "Payment Authorization result should be FAILED", authInfo.getResult().booleanValue());

    // check payment has zero balance
    PaymentWS lastPayment = api.getLatestPayment(user.getId());
    assertNotNull("payment can not be null", lastPayment);
    assertNotNull("auth in payment can not be null", lastPayment.getAuthorizationId());
    assertEquals("correct payment amount", new BigDecimal("5"), lastPayment.getAmountAsDecimal());
    assertEquals("correct payment balance", BigDecimal.ZERO, lastPayment.getBalanceAsDecimal());

    // check invoices still have balance
    InvoiceWS invoice1 = api.getInvoiceWS(invoiceId1);
    assertEquals("correct invoice balance", new BigDecimal("10.0"), invoice1.getBalanceAsDecimal());
    InvoiceWS invoice2 = api.getInvoiceWS(invoiceId1);
    assertEquals("correct invoice balance", new BigDecimal("10.0"), invoice2.getBalanceAsDecimal());

    // do it again, but using the credit card on file
    // which is also 4111111111111111
    payment.getPaymentInstruments().clear();
    System.out.println("processing payment.");
    authInfo = api.processPayment(payment, null);
    // check payment has zero balance
    PaymentWS lastPayment2 = api.getLatestPayment(user.getId());
    assertNotNull("payment can not be null", lastPayment2);
    assertNotNull("auth in payment can not be null", lastPayment2.getAuthorizationId());
    assertEquals("correct payment amount", new BigDecimal("5"), lastPayment2.getAmountAsDecimal());
    assertEquals("correct payment balance", BigDecimal.ZERO, lastPayment2.getBalanceAsDecimal());
    assertFalse("Payment is not the same as preiouvs", lastPayment2.getId() == lastPayment.getId());

    // check invoices still have balance
    invoice1 = api.getInvoiceWS(invoiceId1);
    assertEquals("correct invoice balance", new BigDecimal("10"), invoice1.getBalanceAsDecimal());
    invoice2 = api.getInvoiceWS(invoiceId1);
    assertEquals("correct invoice balance", new BigDecimal("10"), invoice2.getBalanceAsDecimal());

    //  do a successful payment of $5
    cc =
        PaymentMethodHelper.createCreditCard(
            CC_PAYMENT_TYPE, "Frodo Baggins", "4111111111111152", cal.getTime());
    cc.setPaymentMethodId(Constants.PAYMENT_METHOD_VISA);
    payment.getPaymentInstruments().add(cc);
    System.out.println("processing payment.");
    authInfo = api.processPayment(payment, null);

    // check payment successful
    assertNotNull("Payment result not null", authInfo);
    assertNotNull("Auth id not null", authInfo.getId());
    assertTrue("Payment Authorization result should be OK", authInfo.getResult().booleanValue());

    // check payment was made
    lastPayment = api.getLatestPayment(user.getId());
    assertNotNull("payment can not be null", lastPayment);
    assertNotNull("auth in payment can not be null", lastPayment.getAuthorizationId());
    assertEquals("payment ids match", lastPayment.getId(), authInfo.getPaymentId().intValue());
    assertEquals("correct payment amount", new BigDecimal("5"), lastPayment.getAmountAsDecimal());
    assertEquals("correct payment balance", BigDecimal.ZERO, lastPayment.getBalanceAsDecimal());

    // check invoice 1 was partially paid (balance 5)
    invoice1 = api.getInvoiceWS(invoiceId1);
    assertEquals("correct invoice balance", new BigDecimal("5.0"), invoice1.getBalanceAsDecimal());

    // check invoice 2 wan't paid at all
    invoice2 = api.getInvoiceWS(invoiceId2);
    assertEquals("correct invoice balance", new BigDecimal("10.0"), invoice2.getBalanceAsDecimal());

    //  another payment for $10, this time with the user's credit card

    // update the credit card to the one that is good
    user = api.getUserWS(user.getId());
    com.sapienter.jbilling.server.user.WSTest.updateMetaField(
        user.getPaymentInstruments().iterator().next().getMetaFields(),
        PaymentMethodHelper.CC_MF_NUMBER,
        "4111111111111152");
    api.updateUser(user);

    // process a payment without an attached credit card
    // should try and use the user's saved credit card
    payment.getPaymentInstruments().clear();
    payment.setAmount(new BigDecimal("10.00"));
    System.out.println("processing payment.");
    authInfo = api.processPayment(payment, null);

    // check payment successful
    assertNotNull("Payment result not null", authInfo);
    assertTrue("Payment Authorization result should be OK", authInfo.getResult().booleanValue());

    // check payment was made
    lastPayment = api.getLatestPayment(user.getId());
    assertNotNull("payment can not be null", lastPayment);
    assertNotNull("auth in payment can not be null", lastPayment.getAuthorizationId());
    assertEquals("correct payment amount", new BigDecimal("10"), lastPayment.getAmountAsDecimal());
    assertEquals("correct payment balance", BigDecimal.ZERO, lastPayment.getBalanceAsDecimal());

    // check invoice 1 is fully paid (balance 0)
    invoice1 = api.getInvoiceWS(invoiceId1);
    assertEquals("correct invoice balance", BigDecimal.ZERO, invoice1.getBalanceAsDecimal());

    // check invoice 2 was partially paid (balance 5)
    invoice2 = api.getInvoiceWS(invoiceId2);
    assertEquals("correct invoice balance", new BigDecimal("5"), invoice2.getBalanceAsDecimal());

    // another payment for $10

    payment.getPaymentInstruments().add(cc);
    payment.setAmount(new BigDecimal("10.00"));
    System.out.println("processing payment.");
    authInfo = api.processPayment(payment, null);

    // check payment successful
    assertNotNull("Payment result not null", authInfo);
    assertTrue("Payment Authorization result should be OK", authInfo.getResult().booleanValue());

    // check payment was made
    lastPayment = api.getLatestPayment(user.getId());
    assertNotNull("payment can not be null", lastPayment);
    assertNotNull("auth in payment can not be null", lastPayment.getAuthorizationId());
    assertEquals("correct  payment amount", new BigDecimal("10"), lastPayment.getAmountAsDecimal());
    assertEquals(
        "correct  payment balance", new BigDecimal("5"), lastPayment.getBalanceAsDecimal());

    // check invoice 1 balance is unchanged
    invoice1 = api.getInvoiceWS(invoiceId1);
    assertEquals("correct invoice balance", BigDecimal.ZERO, invoice1.getBalanceAsDecimal());

    // check invoice 2 is fully paid (balance 0)
    invoice2 = api.getInvoiceWS(invoiceId2);
    assertEquals("correct invoice balance", BigDecimal.ZERO, invoice2.getBalanceAsDecimal());

    // cleanup
    System.out.println("Deleting invoices and orders.");
    api.deleteInvoice(invoice1.getId());
    api.deleteInvoice(invoice2.getId());
    api.deleteOrder(invoice1.getOrders()[0]);
    api.deleteOrder(invoice2.getOrders()[0]);
    api.deleteItem(item.getId());
    api.deleteItemCategory(itemType.getId());
    api.deleteUser(user.getId());
  }
Example #10
0
  /** Tests the PaymentRouterCurrencyTask. */
  @Test
  public void testPaymentRouterCurrencyTask() {
    // prepare
    UserWS userUSD = buildUser(PRANCING_PONY_ACCOUNT_TYPE);
    userUSD.setCurrencyId(CURRENCY_USD);
    userUSD.setId(api.createUser(userUSD));

    UserWS userAUD = buildUser(PRANCING_PONY_ACCOUNT_TYPE);
    userAUD.setCurrencyId(CURRENCY_AUD);
    userAUD.setId(api.createUser(userAUD));

    ItemTypeWS itemType = buildItemType();
    itemType.setId(api.createItemCategory(itemType));

    ItemDTOEx item = buildItem(itemType.getId(), api.getCallerCompanyId());
    item.setId(api.createItem(item));

    // testing
    OrderWS order = buildOrder(userUSD.getId(), Arrays.asList(item.getId()), new BigDecimal("10"));
    order.setCurrencyId(userUSD.getCurrencyId());

    // create the order and invoice it
    System.out.println("Creating and invoicing order ...");
    Integer invoiceIdUSD =
        api.createOrderAndInvoice(
            order, OrderChangeBL.buildFromOrder(order, ORDER_CHANGE_STATUS_APPLY));
    Integer orderIdUSD = api.getLastOrders(userUSD.getId(), 1)[0];

    // try paying the invoice in USD
    System.out.println("Making payment in USD...");
    PaymentAuthorizationDTOEx authInfo = api.payInvoice(invoiceIdUSD);

    assertTrue("USD Payment should be successful", authInfo.getResult().booleanValue());
    assertEquals(
        "Should be processed by 'first_fake_processor'",
        authInfo.getProcessor(),
        "first_fake_processor");

    // create a new order in AUD and invoice it
    order.setUserId(userAUD.getId());
    order.setCurrencyId(userAUD.getCurrencyId());

    System.out.println("Creating and invoicing order ...");
    Integer invoiceIdAUD =
        api.createOrderAndInvoice(
            order, OrderChangeBL.buildFromOrder(order, ORDER_CHANGE_STATUS_APPLY));
    Integer orderIdAUD = api.getLastOrders(userAUD.getId(), 1)[0];

    // try paying the invoice in AUD
    System.out.println("Making payment in AUD...");
    authInfo = api.payInvoice(invoiceIdAUD);

    assertTrue("AUD Payment should be successful", authInfo.getResult().booleanValue());
    assertEquals(
        "Should be processed by 'second_fake_processor'",
        authInfo.getProcessor(),
        "second_fake_processor");

    // remove invoices and orders
    System.out.println("Deleting invoices and orders.");
    api.deleteInvoice(invoiceIdUSD);
    api.deleteInvoice(invoiceIdAUD);
    api.deleteOrder(orderIdUSD);
    api.deleteOrder(orderIdAUD);
    api.deleteUser(userUSD.getId());
    api.deleteUser(userAUD.getId());
    api.deleteItem(item.getId());
    api.deleteItemCategory(itemType.getId());
  }
Example #11
0
  /** Tests payment apply and retrieve. */
  @Test
  public void testApplyGet() {
    // setup
    UserWS mordorUser = buildUser(MORDOR_ACCOUNT_TYPE);
    mordorUser.setId(mordorApi.createUser(mordorUser));

    UserWS user = buildUser(PRANCING_PONY_ACCOUNT_TYPE);
    user.setId(api.createUser(user));

    ItemTypeWS itemType = buildItemType();
    itemType.setId(api.createItemCategory(itemType));

    ItemDTOEx item = buildItem(itemType.getId(), api.getCallerCompanyId());
    item.setId(api.createItem(item));

    InvoiceWS invoice = buildInvoice(user.getId(), item.getId());
    invoice.setId(api.saveLegacyInvoice(invoice));

    // testing
    PaymentWS payment = new PaymentWS();
    payment.setAmount(new BigDecimal("15.00"));
    payment.setIsRefund(new Integer(0));
    payment.setMethodId(Constants.PAYMENT_METHOD_CHEQUE);
    payment.setPaymentDate(Calendar.getInstance().getTime());
    payment.setResultId(Constants.RESULT_ENTERED);
    payment.setCurrencyId(CURRENCY_USD);
    payment.setUserId(user.getId());
    payment.setPaymentNotes("Notes");
    payment.setPaymentPeriod(PAYMENT_PERIOD);

    PaymentInformationWS cheque =
        PaymentMethodHelper.createCheque(
            CHEQUE_PAYMENT_TYPE, "ws bank", "2232-2323-2323", Calendar.getInstance().getTime());
    payment.getPaymentInstruments().add(cheque);

    System.out.println("Applying payment");
    Integer paymentId = api.applyPayment(payment, invoice.getId());
    System.out.println("Created payemnt " + paymentId);
    assertNotNull("Didn't get the payment id", paymentId);

    //  get

    // verify the created payment
    System.out.println("Getting created payment");
    PaymentWS retPayment = api.getPayment(paymentId);
    assertNotNull("didn't get payment ", retPayment);

    assertEquals("created payment result", retPayment.getResultId(), payment.getResultId());
    System.out.println("Instruments are: " + retPayment.getPaymentInstruments());

    assertEquals(
        "created payment cheque ",
        getMetaField(
                retPayment.getPaymentInstruments().iterator().next().getMetaFields(),
                PaymentMethodHelper.CHEQUE_MF_NUMBER)
            .getStringValue(),
        getMetaField(
                payment.getPaymentInstruments().iterator().next().getMetaFields(),
                PaymentMethodHelper.CHEQUE_MF_NUMBER)
            .getStringValue());

    assertEquals("created payment user ", retPayment.getUserId(), payment.getUserId());
    assertEquals("notes", retPayment.getPaymentNotes(), payment.getPaymentNotes());
    assertEquals("period", retPayment.getPaymentPeriod(), payment.getPaymentPeriod());

    System.out.println("Validated created payment and paid invoice");
    assertNotNull("payment not related to invoice", retPayment.getInvoiceIds());
    assertTrue("payment not related to invoice", retPayment.getInvoiceIds().length == 1);
    assertEquals("payment not related to invoice", retPayment.getInvoiceIds()[0], invoice.getId());

    InvoiceWS retInvoice = api.getInvoiceWS(retPayment.getInvoiceIds()[0]);
    assertNotNull("New invoice not present", retInvoice);
    assertEquals(
        "Balance of invoice should be total of order",
        BigDecimal.ZERO,
        retInvoice.getBalanceAsDecimal());
    assertEquals(
        "Total of invoice should be total of order",
        new BigDecimal("15"),
        retInvoice.getTotalAsDecimal());
    assertEquals("New invoice not paid", retInvoice.getToProcess(), new Integer(0));
    assertNotNull("invoice not related to payment", retInvoice.getPayments());
    assertTrue("invoice not related to payment", retInvoice.getPayments().length == 1);
    assertEquals(
        "invoice not related to payment",
        retInvoice.getPayments()[0].intValue(),
        retPayment.getId());

    //  get latest

    // verify the created payment
    System.out.println("Getting latest");
    retPayment = api.getLatestPayment(user.getId());
    assertNotNull("didn't get payment ", retPayment);
    assertEquals("latest id", paymentId.intValue(), retPayment.getId());
    assertEquals("created payment result", retPayment.getResultId(), payment.getResultId());

    assertEquals(
        "created payment cheque ",
        getMetaField(
                retPayment.getPaymentInstruments().iterator().next().getMetaFields(),
                PaymentMethodHelper.CHEQUE_MF_NUMBER)
            .getStringValue(),
        getMetaField(
                payment.getPaymentInstruments().iterator().next().getMetaFields(),
                PaymentMethodHelper.CHEQUE_MF_NUMBER)
            .getStringValue());

    assertEquals("created payment user ", retPayment.getUserId(), payment.getUserId());

    try {
      System.out.println("Getting latest - invalid");
      api.getLatestPayment(mordorUser.getId());
      fail("User belongs to entity Mordor");
    } catch (Exception e) {
    }

    //  get last

    System.out.println("Getting last");
    Integer retPayments[] = api.getLastPayments(user.getId(), Integer.valueOf(2));
    assertNotNull("didn't get payment ", retPayments);
    // fetch the payment

    retPayment = api.getPayment(retPayments[0]);

    assertEquals("created payment result", retPayment.getResultId(), payment.getResultId());

    assertEquals(
        "created payment cheque ",
        getMetaField(
                retPayment.getPaymentInstruments().iterator().next().getMetaFields(),
                PaymentMethodHelper.CHEQUE_MF_NUMBER)
            .getStringValue(),
        getMetaField(
                payment.getPaymentInstruments().iterator().next().getMetaFields(),
                PaymentMethodHelper.CHEQUE_MF_NUMBER)
            .getStringValue());

    assertEquals("created payment user ", retPayment.getUserId(), payment.getUserId());
    assertTrue("No more than two records", retPayments.length <= 2);

    try {
      System.out.println("Getting last - invalid");
      api.getLastPayments(mordorUser.getId(), Integer.valueOf(2));
      fail("User belongs to entity Mordor");
    } catch (Exception e) {
    }

    // cleanup
    api.deletePayment(paymentId);
    api.deleteInvoice(invoice.getId());
    api.deleteUser(user.getId());
    mordorApi.deleteUser(mordorUser.getId());
  }