Search in sources :

Example 31 with AccountData

use of org.killbill.billing.account.api.AccountData in project killbill by killbill.

the class TestWithTimeZones method testCancellationFrom_PST_to_PDT.

// Same test as previous test but this time going from PST -> PDT (somehow not too interesting in that direction because we start with
// an offset of 8 hours and then go through 7 hours so anyway we would stay in the same day.
@Test(groups = "slow")
public void testCancellationFrom_PST_to_PDT() throws Exception {
    clock.setTime(new DateTime("2015-02-01T08:01:01.000Z"));
    final DateTimeZone tz = DateTimeZone.forID("America/Los_Angeles");
    final AccountData accountData = new MockAccountBuilder().name(UUID.randomUUID().toString().substring(1, 8)).firstNameLength(6).email(UUID.randomUUID().toString().substring(1, 8)).phone(UUID.randomUUID().toString().substring(1, 8)).migrated(false).isNotifiedForInvoices(false).externalKey(UUID.randomUUID().toString().substring(1, 8)).billingCycleDayLocal(1).currency(Currency.USD).paymentMethodId(UUID.randomUUID()).timeZone(tz).build();
    final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
    accountChecker.checkAccount(account.getId(), accountData, callContext);
    busHandler.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Blowdart", BillingPeriod.MONTHLY, "notrial", null);
    Entitlement entitlement = entitlementApi.createBaseEntitlement(account.getId(), spec, "Something", ImmutableList.<PlanPhasePriceOverride>of(), null, null, false, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    // Cancel the next month specifying just a LocalDate
    final LocalDate cancellationDate = new LocalDate("2015-03-01", tz);
    entitlement = entitlement.cancelEntitlementWithDate(cancellationDate, true, ImmutableList.<PluginProperty>of(), callContext);
    // Verify first entitlement is correctly cancelled on the right date
    Assert.assertEquals(entitlement.getEffectiveEndDate(), cancellationDate);
    // We now move the clock to the date of the cancellation  which match the cancellation day from the client point of view
    busHandler.pushExpectedEvents(NextEvent.CANCEL, NextEvent.BLOCK, NextEvent.NULL_INVOICE, NextEvent.NULL_INVOICE);
    clock.setTime(new DateTime("2015-03-01T08:01:02"));
    assertListenerStatus();
    // Verify second that there was no repair (so the cancellation did correctly happen on the "2015-12-01"
    final List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, callContext);
    Assert.assertEquals(invoices.size(), 1);
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) Account(org.killbill.billing.account.api.Account) PluginProperty(org.killbill.billing.payment.api.PluginProperty) MockAccountBuilder(org.killbill.billing.mock.MockAccountBuilder) Invoice(org.killbill.billing.invoice.api.Invoice) AccountData(org.killbill.billing.account.api.AccountData) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) Entitlement(org.killbill.billing.entitlement.api.Entitlement) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) DateTimeZone(org.joda.time.DateTimeZone) Test(org.testng.annotations.Test)

Example 32 with AccountData

use of org.killbill.billing.account.api.AccountData in project killbill by killbill.

the class TestWithTimeZones method testCancellationFrom_PDT_to_PST.

// Verify cancellation logic when we exit daylight saving period
@Test(groups = "slow")
public void testCancellationFrom_PDT_to_PST() throws Exception {
    // Start with a date in daylight saving period (PDT) and make sure we use a time of 7 hour so that we we reach standard time (PST)
    // the next month where the difference is 8 hours, a transformation from DateTime to LocalDate with the account time zone would bring us a day earlier
    // (e.g new LocalDate("2015-12-01T07:01:01.000Z", tz) -> "2015-11-30.
    clock.setTime(new DateTime("2015-11-01T07:01:01.000Z"));
    final DateTimeZone tz = DateTimeZone.forID("America/Los_Angeles");
    final AccountData accountData = new MockAccountBuilder().name(UUID.randomUUID().toString().substring(1, 8)).firstNameLength(6).email(UUID.randomUUID().toString().substring(1, 8)).phone(UUID.randomUUID().toString().substring(1, 8)).migrated(false).isNotifiedForInvoices(false).externalKey(UUID.randomUUID().toString().substring(1, 8)).billingCycleDayLocal(1).currency(Currency.USD).paymentMethodId(UUID.randomUUID()).timeZone(tz).build();
    final Account account = createAccountWithNonOsgiPaymentMethod(accountData);
    accountChecker.checkAccount(account.getId(), accountData, callContext);
    busHandler.pushExpectedEvents(NextEvent.CREATE, NextEvent.BLOCK, NextEvent.INVOICE, NextEvent.PAYMENT, NextEvent.INVOICE_PAYMENT);
    final PlanPhaseSpecifier spec = new PlanPhaseSpecifier("Blowdart", BillingPeriod.MONTHLY, "notrial", null);
    Entitlement entitlement = entitlementApi.createBaseEntitlement(account.getId(), spec, "Something", ImmutableList.<PlanPhasePriceOverride>of(), null, null, false, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    // Cancel the next month specifying just a LocalDate
    final LocalDate cancellationDate = new LocalDate("2015-12-01", tz);
    entitlement = entitlement.cancelEntitlementWithDate(cancellationDate, true, ImmutableList.<PluginProperty>of(), callContext);
    assertListenerStatus();
    // Verify first entitlement is correctly cancelled on the right date
    Assert.assertEquals(entitlement.getEffectiveEndDate(), cancellationDate);
    // We now move the clock to the date of the cancellation, which match the cancellation day from the client point of view
    busHandler.pushExpectedEvents(NextEvent.NULL_INVOICE, NextEvent.CANCEL, NextEvent.BLOCK, NextEvent.NULL_INVOICE);
    clock.setTime(new DateTime("2015-12-01T07:01:02Z"));
    assertListenerStatus();
    // Verify second that there was no repair (so the cancellation did correctly happen on the "2015-12-01")
    final List<Invoice> invoices = invoiceUserApi.getInvoicesByAccount(account.getId(), false, callContext);
    Assert.assertEquals(invoices.size(), 1);
}
Also used : PlanPhaseSpecifier(org.killbill.billing.catalog.api.PlanPhaseSpecifier) Account(org.killbill.billing.account.api.Account) PluginProperty(org.killbill.billing.payment.api.PluginProperty) MockAccountBuilder(org.killbill.billing.mock.MockAccountBuilder) Invoice(org.killbill.billing.invoice.api.Invoice) AccountData(org.killbill.billing.account.api.AccountData) DefaultEntitlement(org.killbill.billing.entitlement.api.DefaultEntitlement) Entitlement(org.killbill.billing.entitlement.api.Entitlement) LocalDate(org.joda.time.LocalDate) DateTime(org.joda.time.DateTime) DateTimeZone(org.joda.time.DateTimeZone) Test(org.testng.annotations.Test)

Example 33 with AccountData

use of org.killbill.billing.account.api.AccountData in project killbill by killbill.

the class TestTransfer method beforeMethod.

@Override
@BeforeMethod(groups = "slow")
public void beforeMethod() throws Exception {
    // Note: this will cleanup all tables
    super.beforeMethod();
    final AccountData accountData2 = subscriptionTestInitializer.initAccountData();
    final Account account2 = createAccount(accountData2);
    finalNewAccountId = account2.getId();
    // internal context will be configured for newAccountId
    final AccountData accountData = subscriptionTestInitializer.initAccountData();
    final Account account = createAccount(accountData);
    newAccountId = account.getId();
}
Also used : Account(org.killbill.billing.account.api.Account) AccountData(org.killbill.billing.account.api.AccountData) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 34 with AccountData

use of org.killbill.billing.account.api.AccountData in project killbill by killbill.

the class InvoicePaymentControlPluginApi method getPluginPurchaseResult.

private PriorPaymentControlResult getPluginPurchaseResult(final PaymentControlContext paymentControlPluginContext, final Iterable<PluginProperty> pluginProperties, final InternalCallContext internalContext) throws PaymentControlApiException {
    try {
        final UUID invoiceId = getInvoiceId(pluginProperties);
        final Invoice invoice = getAndSanitizeInvoice(invoiceId, internalContext);
        if (!InvoiceStatus.COMMITTED.equals(invoice.getStatus())) {
            // abort payment if the invoice status is not COMMITTED
            return new DefaultPriorPaymentControlResult(true);
        }
        // Get account and check if it is child and payment is delegated to parent => abort
        final AccountData accountData = accountApi.getAccountById(invoice.getAccountId(), internalContext);
        if ((accountData != null) && (accountData.getParentAccountId() != null) && accountData.isPaymentDelegatedToParent()) {
            return new DefaultPriorPaymentControlResult(true);
        }
        final BigDecimal requestedAmount = validateAndComputePaymentAmount(invoice, paymentControlPluginContext.getAmount(), paymentControlPluginContext.isApiPayment());
        final boolean isAborted = requestedAmount.compareTo(BigDecimal.ZERO) == 0;
        if (!isAborted && paymentControlPluginContext.getPaymentMethodId() == null) {
            log.warn("Payment for invoiceId='{}' was not triggered, accountId='{}' doesn't have a default payment method", getInvoiceId(pluginProperties), paymentControlPluginContext.getAccountId());
            invoiceApi.recordPaymentAttemptCompletion(invoiceId, paymentControlPluginContext.getAmount(), paymentControlPluginContext.getCurrency(), paymentControlPluginContext.getProcessedCurrency(), paymentControlPluginContext.getPaymentId(), paymentControlPluginContext.getTransactionExternalKey(), paymentControlPluginContext.getCreatedDate(), false, internalContext);
            return new DefaultPriorPaymentControlResult(true);
        }
        if (!isAborted && insert_AUTO_PAY_OFF_ifRequired(paymentControlPluginContext, requestedAmount)) {
            return new DefaultPriorPaymentControlResult(true);
        }
        if (paymentControlPluginContext.isApiPayment() && isAborted) {
            throw new PaymentControlApiException("Abort purchase call: ", new PaymentApiException(ErrorCode.PAYMENT_PLUGIN_EXCEPTION, String.format("Aborted Payment for invoice %s : invoice balance is = %s, requested payment amount is = %s", invoice.getId(), invoice.getBalance(), paymentControlPluginContext.getAmount())));
        } else {
            //
            // Insert attempt row with a success = false status to implement a two-phase commit strategy and guard against scenario where payment would go through
            // but onSuccessCall callback never gets called (leaving the place for a double payment if user retries the operation)
            //
            invoiceApi.recordPaymentAttemptInit(invoice.getId(), MoreObjects.firstNonNull(paymentControlPluginContext.getAmount(), BigDecimal.ZERO), paymentControlPluginContext.getCurrency(), paymentControlPluginContext.getCurrency(), // to match the operation in the checkForIncompleteInvoicePaymentAndRepair logic below
            paymentControlPluginContext.getPaymentId(), paymentControlPluginContext.getTransactionExternalKey(), paymentControlPluginContext.getCreatedDate(), internalContext);
            return new DefaultPriorPaymentControlResult(isAborted, requestedAmount);
        }
    } catch (final InvoiceApiException e) {
        throw new PaymentControlApiException(e);
    } catch (final IllegalArgumentException e) {
        throw new PaymentControlApiException(e);
    } catch (AccountApiException e) {
        throw new PaymentControlApiException(e);
    }
}
Also used : InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) Invoice(org.killbill.billing.invoice.api.Invoice) AccountData(org.killbill.billing.account.api.AccountData) AccountApiException(org.killbill.billing.account.api.AccountApiException) PaymentApiException(org.killbill.billing.payment.api.PaymentApiException) UUID(java.util.UUID) DefaultPriorPaymentControlResult(org.killbill.billing.payment.retry.DefaultPriorPaymentControlResult) BigDecimal(java.math.BigDecimal) PaymentControlApiException(org.killbill.billing.control.plugin.api.PaymentControlApiException)

Example 35 with AccountData

use of org.killbill.billing.account.api.AccountData in project killbill by killbill.

the class TestDefaultAccountUserApiWithMocks method testCreateAccount.

@Test(groups = "fast", description = "Test Account create API")
public void testCreateAccount() throws Exception {
    final UUID id = UUID.randomUUID();
    final String externalKey = UUID.randomUUID().toString();
    final String email = UUID.randomUUID().toString();
    final String name = UUID.randomUUID().toString();
    final Integer firstNameLength = Integer.MAX_VALUE;
    final Currency currency = Currency.BRL;
    final Integer billCycleDay = Integer.MAX_VALUE;
    final UUID paymentMethodId = UUID.randomUUID();
    final DateTimeZone timeZone = DateTimeZone.UTC;
    final String locale = UUID.randomUUID().toString();
    final String address1 = UUID.randomUUID().toString();
    final String address2 = UUID.randomUUID().toString();
    final String companyName = UUID.randomUUID().toString();
    final String city = UUID.randomUUID().toString();
    final String stateOrProvince = UUID.randomUUID().toString();
    final String country = UUID.randomUUID().toString();
    final String postalCode = UUID.randomUUID().toString();
    final String phone = UUID.randomUUID().toString();
    final String notes = UUID.randomUUID().toString();
    final Boolean isMigrated = true;
    final Boolean isNotifiedForInvoices = false;
    final AccountData data = new DefaultAccount(id, externalKey, email, name, firstNameLength, currency, null, false, billCycleDay, paymentMethodId, timeZone, locale, address1, address2, companyName, city, stateOrProvince, country, postalCode, phone, notes, isMigrated, isNotifiedForInvoices);
    accountUserApi.createAccount(data, callContext);
    final AccountModelDao account = accountDao.getAccountByKey(externalKey, tenantContext);
    Assert.assertEquals(account.getExternalKey(), externalKey);
    Assert.assertEquals(account.getEmail(), email);
    Assert.assertEquals(account.getName(), name);
    Assert.assertEquals(account.getFirstNameLength(), firstNameLength);
    Assert.assertEquals(account.getCurrency(), currency);
    Assert.assertEquals(account.getBillingCycleDayLocal(), (Integer) billCycleDay);
    Assert.assertEquals(account.getPaymentMethodId(), paymentMethodId);
    Assert.assertEquals(account.getTimeZone(), timeZone);
    Assert.assertEquals(account.getLocale(), locale);
    Assert.assertEquals(account.getAddress1(), address1);
    Assert.assertEquals(account.getAddress2(), address2);
    Assert.assertEquals(account.getCompanyName(), companyName);
    Assert.assertEquals(account.getCity(), city);
    Assert.assertEquals(account.getStateOrProvince(), stateOrProvince);
    Assert.assertEquals(account.getCountry(), country);
    Assert.assertEquals(account.getPostalCode(), postalCode);
    Assert.assertEquals(account.getPhone(), phone);
    Assert.assertEquals(account.getMigrated(), isMigrated);
    Assert.assertEquals(account.getIsNotifiedForInvoices(), isNotifiedForInvoices);
}
Also used : AccountModelDao(org.killbill.billing.account.dao.AccountModelDao) DefaultAccount(org.killbill.billing.account.api.DefaultAccount) AccountData(org.killbill.billing.account.api.AccountData) Currency(org.killbill.billing.catalog.api.Currency) UUID(java.util.UUID) DateTimeZone(org.joda.time.DateTimeZone) Test(org.testng.annotations.Test)

Aggregations

AccountData (org.killbill.billing.account.api.AccountData)54 Test (org.testng.annotations.Test)49 Account (org.killbill.billing.account.api.Account)47 LocalDate (org.joda.time.LocalDate)38 BigDecimal (java.math.BigDecimal)33 DefaultEntitlement (org.killbill.billing.entitlement.api.DefaultEntitlement)30 ExpectedInvoiceItemCheck (org.killbill.billing.beatrix.util.InvoiceChecker.ExpectedInvoiceItemCheck)26 Invoice (org.killbill.billing.invoice.api.Invoice)19 ArrayList (java.util.ArrayList)17 Payment (org.killbill.billing.payment.api.Payment)14 DateTime (org.joda.time.DateTime)11 MutableAccountData (org.killbill.billing.account.api.MutableAccountData)9 DefaultAccount (org.killbill.billing.account.api.DefaultAccount)8 DefaultMutableAccountData (org.killbill.billing.account.api.DefaultMutableAccountData)8 PlanPhaseSpecifier (org.killbill.billing.catalog.api.PlanPhaseSpecifier)8 Entitlement (org.killbill.billing.entitlement.api.Entitlement)8 ExpectedPaymentCheck (org.killbill.billing.beatrix.util.PaymentChecker.ExpectedPaymentCheck)7 MockAccountBuilder (org.killbill.billing.mock.MockAccountBuilder)7 DateTimeZone (org.joda.time.DateTimeZone)6 AccountTestUtils.createAccountData (org.killbill.billing.account.AccountTestUtils.createAccountData)6