Ejemplo n.º 1
0
  private UserWS buildUser(
      Integer accountTypeId, String firstName, String lastName, String ccNumber) {
    UserWS newUser = new UserWS();
    newUser.setUserName("payment-test-" + Calendar.getInstance().getTimeInMillis());
    newUser.setPassword("Admin123@");
    newUser.setLanguageId(LANGUAGE_ID);
    newUser.setMainRoleId(new Integer(5));
    newUser.setAccountTypeId(accountTypeId);
    newUser.setParentId(null);
    newUser.setStatusId(UserDTOEx.STATUS_ACTIVE);
    newUser.setCurrencyId(CURRENCY_USD);

    MetaFieldValueWS metaField1 = new MetaFieldValueWS();
    metaField1.setFieldName("contact.email");
    metaField1.setValue(newUser.getUserName() + "@shire.com");
    metaField1.setGroupId(accountTypeId);

    MetaFieldValueWS metaField2 = new MetaFieldValueWS();
    metaField2.setFieldName("contact.first.name");
    metaField2.setValue(firstName);
    metaField2.setGroupId(accountTypeId);

    MetaFieldValueWS metaField3 = new MetaFieldValueWS();
    metaField3.setFieldName("contact.last.name");
    metaField3.setValue(lastName);
    metaField3.setGroupId(accountTypeId);

    newUser.setMetaFields(new MetaFieldValueWS[] {metaField1, metaField2, metaField3});

    // add a credit card
    Calendar expiry = Calendar.getInstance();
    expiry.set(Calendar.YEAR, expiry.get(Calendar.YEAR) + 1);

    // add credit card
    newUser
        .getPaymentInstruments()
        .add(
            PaymentMethodHelper.createCreditCard(
                CC_PAYMENT_TYPE, "Frodo Baggins", ccNumber, expiry.getTime()));

    return newUser;
  }
Ejemplo n.º 2
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());
  }