private Account createAccount(final int billCycleDay) throws AccountApiException { final Account account = Mockito.mock(Account.class); Mockito.when(account.getBillCycleDayLocal()).thenReturn(billCycleDay); Mockito.when(account.getCurrency()).thenReturn(Currency.USD); Mockito.when(account.getId()).thenReturn(UUID.randomUUID()); Mockito.when(account.getTimeZone()).thenReturn(DateTimeZone.UTC); Mockito.when(accountInternalApi.getAccountById(Mockito.<UUID>any(), Mockito.<InternalTenantContext>any())).thenReturn(account); return account; }
public Account createTestAccount(final String email, final boolean addPaymentMethod) throws Exception { final String name = "First" + UUID.randomUUID().toString() + " " + "Last" + UUID.randomUUID().toString(); final String externalKey = UUID.randomUUID().toString(); final Account account = Mockito.mock(Account.class); Mockito.when(account.getId()).thenReturn(UUID.randomUUID()); Mockito.when(account.getExternalKey()).thenReturn(externalKey); Mockito.when(account.getName()).thenReturn(name); Mockito.when(account.getFirstNameLength()).thenReturn(10); Mockito.when(account.getPhone()).thenReturn("123-456-7890"); Mockito.when(account.getEmail()).thenReturn(email); Mockito.when(account.getCurrency()).thenReturn(Currency.USD); Mockito.when(account.getBillCycleDayLocal()).thenReturn(1); Mockito.when(account.isMigrated()).thenReturn(false); Mockito.when(account.isNotifiedForInvoices()).thenReturn(false); Mockito.when(AccountApi.getAccountById(Mockito.<UUID>any(), Mockito.<InternalTenantContext>any())).thenReturn(account); Mockito.when(AccountApi.getAccountByKey(Mockito.anyString(), Mockito.<InternalTenantContext>any())).thenReturn(account); if (addPaymentMethod) { final PaymentMethodPlugin pm = new DefaultNoOpPaymentMethodPlugin(UUID.randomUUID().toString(), true, null); addTestPaymentMethod(account, pm); } return account; }
private void addBillingEventsForSubscription(final List<SubscriptionBase> subscriptions, final SubscriptionBaseBundle bundle, final Account account, final InternalCallContext context, final DefaultBillingEventSet result) { boolean updatedAccountBCD = false; for (final SubscriptionBase subscription : subscriptions) { for (final EffectiveSubscriptionInternalEvent transition : subscriptionApi.getBillingTransitions(subscription, context)) { try { final int bcdLocal = bcdCalculator.calculateBcd(bundle, subscription, transition, account, context); if (account.getBillCycleDayLocal() == 0 && !updatedAccountBCD) { final MutableAccountData modifiedData = account.toMutableAccountData(); modifiedData.setBillCycleDayLocal(bcdLocal); accountApi.updateAccount(account.getExternalKey(), modifiedData, context); updatedAccountBCD = true; } final BillingEvent event = new DefaultBillingEvent(account, transition, subscription, bcdLocal, account.getCurrency(), catalogService.getFullCatalog()); result.add(event); } catch (CatalogApiException e) { log.error("Failing to identify catalog components while creating BillingEvent from transition: " + transition.getId().toString(), e); } catch (Exception e) { log.warn("Failed while getting BillingEvent", e); } } } }
@Test(groups = "fast") public void testBillingEventsWithBlock() throws CatalogApiException, AccountApiException { final Plan nextPlan = catalog.findPlan("PickupTrialEvergreen10USD", clock.getUTCNow()); final PlanPhase nextPhase = nextPlan.getAllPhases()[1]; final DateTime now = createSubscriptionCreationEvent(nextPlan, nextPhase); final Account account = createAccount(32); blockingStateDao.setBlockingState(new DefaultBlockingState(bunId, BlockingStateType.SUBSCRIPTION_BUNDLE, DISABLED_BUNDLE, "test", true, true, true, now.plusDays(1)), clock, internalCallContext); blockingStateDao.setBlockingState(new DefaultBlockingState(bunId, BlockingStateType.SUBSCRIPTION_BUNDLE, CLEAR_BUNDLE, "test", false, false, false, now.plusDays(2)), clock, internalCallContext); final SortedSet<BillingEvent> events = billingInternalApi.getBillingEventsForAccountAndUpdateAccountBCD(account.getId(), internalCallContext); Assert.assertEquals(events.size(), 3); final Iterator<BillingEvent> it = events.iterator(); checkEvent(it.next(), nextPlan, account.getBillCycleDayLocal(), subId, now, nextPhase, SubscriptionBaseTransitionType.CREATE.toString(), nextPhase.getFixedPrice(), nextPhase.getRecurringPrice()); checkEvent(it.next(), nextPlan, account.getBillCycleDayLocal(), subId, now.plusDays(1), nextPhase, SubscriptionBaseTransitionType.START_BILLING_DISABLED.toString(), null, null); checkEvent(it.next(), nextPlan, account.getBillCycleDayLocal(), subId, now.plusDays(2), nextPhase, SubscriptionBaseTransitionType.END_BILLING_DISABLED.toString(), nextPhase.getFixedPrice(), nextPhase.getRecurringPrice()); }
@VisibleForTesting int calculateBcdForAlignment(final BillingAlignment alignment, final SubscriptionBaseBundle bundle, final SubscriptionBase subscription, final Account account, final Catalog catalog, final Plan plan, final InternalCallContext context) throws AccountApiException, SubscriptionBaseApiException, CatalogApiException { int result = 0; switch (alignment) { case ACCOUNT: result = account.getBillCycleDayLocal(); if (result == 0) { result = calculateBcdFromSubscription(subscription, plan, account, catalog, context); } break; case BUNDLE: final SubscriptionBase baseSub = subscriptionApi.getBaseSubscription(bundle.getId(), context); Plan basePlan = baseSub.getCurrentPlan(); if (basePlan == null) { // The BP has been cancelled basePlan = baseSub.getLastActivePlan(); } result = calculateBcdFromSubscription(baseSub, basePlan, account, catalog, context); break; case SUBSCRIPTION: result = calculateBcdFromSubscription(subscription, plan, account, catalog, context); break; } if (result == 0) { throw new CatalogApiException(ErrorCode.CAT_INVALID_BILLING_ALIGNMENT, alignment.toString()); } return result; }
@Test(groups = "fast") public void testBillingEventsNoBillingPeriod() throws CatalogApiException, AccountApiException { final Plan nextPlan = catalog.findPlan("PickupTrialEvergreen10USD", clock.getUTCNow()); // The trial has no billing period final PlanPhase nextPhase = nextPlan.getAllPhases()[0]; final DateTime now = createSubscriptionCreationEvent(nextPlan, nextPhase); final Account account = createAccount(10); final SortedSet<BillingEvent> events = billingInternalApi.getBillingEventsForAccountAndUpdateAccountBCD(account.getId(), internalCallContext); checkFirstEvent(events, nextPlan, account.getBillCycleDayLocal(), subId, now, nextPhase, SubscriptionBaseTransitionType.CREATE.toString()); }
public AccountJson(final Account account, final BigDecimal accountBalance, final BigDecimal accountCBA, @Nullable final AccountAuditLogs accountAuditLogs) { super(toAuditLogJson(accountAuditLogs == null ? null : accountAuditLogs.getAuditLogsForAccount())); this.accountCBA = accountCBA; this.accountBalance = accountBalance; this.accountId = account.getId().toString(); this.externalKey = account.getExternalKey(); this.name = account.getName(); this.firstNameLength = account.getFirstNameLength(); this.email = account.getEmail(); this.billCycleDayLocal = account.getBillCycleDayLocal(); this.currency = account.getCurrency() != null ? account.getCurrency().toString() : null; this.paymentMethodId = account.getPaymentMethodId() != null ? account.getPaymentMethodId().toString() : null; this.timeZone = account.getTimeZone().toString(); this.address1 = account.getAddress1(); this.address2 = account.getAddress2(); this.postalCode = account.getPostalCode(); this.company = account.getCompanyName(); this.city = account.getCity(); this.state = account.getStateOrProvince(); this.country = account.getCountry(); this.locale = account.getLocale(); this.phone = account.getPhone(); this.isMigrated = account.isMigrated(); this.isNotifiedForInvoices = account.isNotifiedForInvoices(); }