private void tagAddedForAccount(final UUID accountId, final String name, final InternalCallContext context) { final Account account; try { account = accountApi.getAccountById(accountId, context); } catch (AccountApiException e) { log.warn("Ignoring tag addition of {} for account id {} (account does not exist)", name, accountId.toString()); return; } final String accountKey = account.getExternalKey(); accountTagSqlDao.addTag(accountId.toString(), accountKey, name, context); }
@Override public Account getAccountByKey(final String key, final TenantContext context) { for (final Account account : accounts) { if (key.equals(account.getExternalKey())) { return account; } } return null; }
public void notifyPendingPaymentOfStateChanged(final Account account, final UUID paymentId, final boolean isSuccess, final InternalCallContext context) throws PaymentApiException { new WithAccountLock<Void>().processAccountWithLock(locker, account.getExternalKey(), new WithAccountLockCallback<Void>() { @Override public Void doOperation() throws PaymentApiException { final PaymentModelDao payment = paymentDao.getPayment(paymentId, context); if (payment == null) { throw new PaymentApiException(ErrorCode.PAYMENT_NO_SUCH_PAYMENT, paymentId); } if (payment.getPaymentStatus() != PaymentStatus.PENDING) { throw new PaymentApiException(ErrorCode.PAYMENT_NOT_PENDING, paymentId); } final List<PaymentAttemptModelDao> attempts = paymentDao.getAttemptsForPayment(paymentId, context); final PaymentAttemptModelDao lastAttempt = attempts.get(attempts.size() - 1); final PaymentStatus newPaymentStatus = isSuccess ? PaymentStatus.SUCCESS : PaymentStatus.PAYMENT_FAILURE_ABORTED; paymentDao.updatePaymentAndAttemptOnCompletion(paymentId, newPaymentStatus, payment.getProcessedAmount(), payment.getProcessedCurrency(), lastAttempt.getId(), null, null, context); return null; } }); }
public void notifyPendingRefundOfStateChanged(final Account account, final UUID refundId, final boolean isSuccess, final InternalCallContext context) throws PaymentApiException { new WithAccountLock<Void>().processAccountWithLock(locker, account.getExternalKey(), new WithAccountLockCallback<Void>() { @Override public Void doOperation() throws PaymentApiException { try { final RefundModelDao refund = paymentDao.getRefund(refundId, context); if (refund == null) { throw new PaymentApiException(ErrorCode.PAYMENT_NO_SUCH_REFUND, refundId); } if (refund.getRefundStatus() != RefundStatus.PENDING) { throw new PaymentApiException(ErrorCode.PAYMENT_NOT_PENDING, refundId); } // TODO STEPH : Model is broken if we had an invoice item adjustements as we lost track of them invoiceApi.createRefund(refund.getPaymentId(), refund.getAmount(), refund.isAdjusted(), Collections.<UUID, BigDecimal>emptyMap(), refund.getId(), context); paymentDao.updateRefundStatus(refund.getId(), RefundStatus.COMPLETED, refund.getAmount(), refund.getCurrency(), context); } catch (InvoiceApiException e) { } return null; } }); }
@Override public UUID getIdFromKey(final String externalKey, final TenantContext context) { for (final Account account : accounts) { if (externalKey.equals(account.getExternalKey())) { return account.getId(); } } return null; }
@Override public BusinessSnapshot getBusinessSnapshot(final Account account, final TenantContext context) { final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContext(context); // Find account final BusinessAccount businessAccount = getAccountByKey(account.getExternalKey(), context); // Find all transitions for all bundles for that account, and associated overdue statuses final List<SubscriptionBundle> bundles = entitlementInternalApi.getBundlesForAccount(account.getId(), internalTenantContext); final Collection<BusinessSubscriptionTransition> businessSubscriptionTransitions = new ArrayList<BusinessSubscriptionTransition>(); final Collection<BusinessOverdueStatus> businessOverdueStatuses = new ArrayList<BusinessOverdueStatus>(); for (final SubscriptionBundle bundle : bundles) { businessSubscriptionTransitions.addAll(getTransitionsForBundle(bundle.getExternalKey(), context)); businessOverdueStatuses.addAll(getOverdueStatusesForBundle(bundle.getExternalKey(), context)); } // Find all invoices for that account final Collection<BusinessInvoice> businessInvoices = getInvoicesForAccount(account.getExternalKey(), context); // Find all payments for that account final Collection<BusinessInvoicePayment> businessInvoicePayments = getInvoicePaymentsForAccount(account.getExternalKey(), context); // Find all tags for that account // TODO add other tag types final Collection<BusinessTag> businessTags = getTagsForAccount(account.getExternalKey(), context); // TODO find custom fields final Collection<BusinessField> businessFields = ImmutableList.<BusinessField>of(); return new DefaultBusinessSnapshot(businessAccount, businessSubscriptionTransitions, businessInvoices, businessInvoicePayments, businessOverdueStatuses, businessTags, businessFields); }
private void tagAddedForBundle(final UUID bundleId, final String name, final InternalCallContext context) { final SubscriptionBundle bundle; try { bundle = entitlementApi.getBundleFromId(bundleId, context); } catch (EntitlementUserApiException e) { log.warn("Ignoring tag addition of {} for bundle id {} (bundle does not exist)", name, bundleId.toString()); return; } final Account account; try { account = accountApi.getAccountById(bundle.getAccountId(), context); } catch (AccountApiException e) { log.warn("Ignoring tag addition of {} for bundle id {} and account id {} (account does not exist)", new Object[]{name, bundleId.toString(), bundle.getAccountId()}); return; } /* * Note: we store tags associated to bundles, not to subscriptions. */ final String accountKey = account.getExternalKey(); final String externalKey = bundle.getExternalKey(); subscriptionTransitionTagSqlDao.addTag(accountKey, bundleId.toString(), externalKey, name, context); }
@Override public Void inTransaction(final BusinessInvoicePaymentSqlDao transactional, final TransactionStatus status) throws Exception { // Delete the existing payment if it exists - this is to make the call idempotent transactional.deleteInvoicePayment(payment.getId().toString(), context); // Create the bip record transactional.createInvoicePayment(businessInvoicePayment, context); if (invoice != null) { // Update bin to get the latest invoice balance final BusinessInvoiceSqlDao invoiceSqlDao = transactional.become(BusinessInvoiceSqlDao.class); invoiceDao.rebuildInvoiceInTransaction(account.getExternalKey(), invoice, invoiceSqlDao, context); } // Update bac to get the latest account balance, total invoice balance, etc. final BusinessAccountSqlDao accountSqlDao = transactional.become(BusinessAccountSqlDao.class); accountDao.updateAccountInTransaction(bac, accountSqlDao, context); log.info("Added payment {}", businessInvoicePayment); return null; } });
private Account createAccount() { final Account account = Mockito.mock(Account.class); Mockito.when(account.getExternalKey()).thenReturn("1234abcd"); Mockito.when(account.getName()).thenReturn("Jim Smith"); Mockito.when(account.getFirstNameLength()).thenReturn(3); Mockito.when(account.getEmail()).thenReturn("jim.smith@mail.com"); Mockito.when(account.getLocale()).thenReturn(Locale.US.toString()); Mockito.when(account.getAddress1()).thenReturn("123 Some Street"); Mockito.when(account.getAddress2()).thenReturn("Apt 456"); Mockito.when(account.getCity()).thenReturn("Some City"); Mockito.when(account.getStateOrProvince()).thenReturn("Some State"); Mockito.when(account.getPostalCode()).thenReturn("12345-6789"); Mockito.when(account.getCountry()).thenReturn("USA"); Mockito.when(account.getPhone()).thenReturn("123-456-7890"); return account; }
@Override public Pagination<Account> searchAccounts(final String searchKey, final Long offset, final Long limit, final TenantContext tenantContext) { final List<Account> results = new LinkedList<Account>(); for (final Account account : accounts) { if ((account.getName() != null && account.getName().contains(searchKey)) || (account.getEmail() != null && account.getEmail().contains(searchKey)) || (account.getExternalKey() != null && account.getExternalKey().contains(searchKey)) || (account.getCompanyName() != null && account.getCompanyName().contains(searchKey))) { results.add(account); } } return DefaultPagination.<Account>build(offset, limit, results); }
public void setDefaultPaymentMethod(final Account account, final UUID paymentMethodId, final InternalCallContext context) throws PaymentApiException { final UUID tenantId = nonEntityDao.retrieveIdFromObject(context.getTenantRecordId(), ObjectType.TENANT); new WithAccountLock<Void>().processAccountWithLock(locker, account.getExternalKey(), new WithAccountLockCallback<Void>() { @Override public Void doOperation() throws PaymentApiException { final PaymentMethodModelDao paymentMethodModel = paymentDao.getPaymentMethod(paymentMethodId, context); if (paymentMethodModel == null) { throw new PaymentApiException(ErrorCode.PAYMENT_NO_SUCH_PAYMENT_METHOD, paymentMethodId); } try { final PaymentPluginApi pluginApi = getPluginApi(paymentMethodId, context); pluginApi.setDefaultPaymentMethod(account.getId(), paymentMethodId, context.toCallContext(tenantId)); accountInternalApi.updatePaymentMethod(account.getId(), paymentMethodId, context); return null; } catch (PaymentPluginApiException e) { throw new PaymentApiException(ErrorCode.PAYMENT_UPD_PAYMENT_METHOD, account.getId(), e.getErrorMessage()); } catch (AccountApiException e) { throw new PaymentApiException(e); } } }); }
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; }
@Test(groups = "slow") public void testAddAndRemoveTagsForAccount() throws Exception { final String name = UUID.randomUUID().toString().substring(0, 20); final String accountKey = UUID.randomUUID().toString(); final Account accountData = new MockAccountBuilder(UUID.randomUUID()) .externalKey(accountKey) .currency(Currency.MXN) .build(); Mockito.when(accountInternalApi.getAccountById(Mockito.eq(accountData.getId()), Mockito.<InternalCallContext>any())).thenReturn(accountData); Mockito.when(accountInternalApi.getAccountByKey(Mockito.eq(accountData.getExternalKey()), Mockito.<InternalCallContext>any())).thenReturn(accountData); final UUID accountId = accountData.getId(); Assert.assertEquals(accountTagSqlDao.getTagsForAccountByKey(accountKey, internalCallContext).size(), 0); tagDao.tagAdded(ObjectType.ACCOUNT, accountId, name, internalCallContext); Assert.assertEquals(accountTagSqlDao.getTagsForAccountByKey(accountKey, internalCallContext).size(), 1); tagDao.tagRemoved(ObjectType.ACCOUNT, accountId, name, internalCallContext); Assert.assertEquals(accountTagSqlDao.getTagsForAccountByKey(accountKey, internalCallContext).size(), 0); }
@Test(groups = "slow") public void testAddAndRemoveTagsForBundle() throws Exception { final String name = UUID.randomUUID().toString().substring(0, 20); final String externalKey = UUID.randomUUID().toString(); final Account accountData = new MockAccountBuilder() .currency(Currency.MXN) .build(); Mockito.when(accountInternalApi.getAccountById(Mockito.eq(accountData.getId()), Mockito.<InternalCallContext>any())).thenReturn(accountData); Mockito.when(accountInternalApi.getAccountByKey(Mockito.eq(accountData.getExternalKey()), Mockito.<InternalCallContext>any())).thenReturn(accountData); final UUID bundleId = UUID.randomUUID(); final SubscriptionBundle bundle = Mockito.mock(SubscriptionBundle.class); Mockito.when(bundle.getId()).thenReturn(bundleId); Mockito.when(bundle.getAccountId()).thenReturn(accountData.getId()); Mockito.when(bundle.getExternalKey()).thenReturn(externalKey); Mockito.when(entitlementInternalApi.getBundleFromId(Mockito.eq(bundleId), Mockito.<InternalCallContext>any())).thenReturn(bundle); Assert.assertEquals(subscriptionTransitionTagSqlDao.getTagsForBusinessSubscriptionTransitionByKey(externalKey, internalCallContext).size(), 0); tagDao.tagAdded(ObjectType.BUNDLE, bundleId, name, internalCallContext); Assert.assertEquals(subscriptionTransitionTagSqlDao.getTagsForBusinessSubscriptionTransitionByKey(externalKey, internalCallContext).size(), 1); tagDao.tagRemoved(ObjectType.BUNDLE, bundleId, name, internalCallContext); Assert.assertEquals(subscriptionTransitionTagSqlDao.getTagsForBusinessSubscriptionTransitionByKey(externalKey, internalCallContext).size(), 0); } }
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 testGetExternalPaymentProviderPlugin() throws Exception { final UUID accountId = UUID.randomUUID(); final Account account = Mockito.mock(Account.class); Mockito.when(account.getId()).thenReturn(accountId); Mockito.when(account.getExternalKey()).thenReturn(accountId.toString()); Assert.assertEquals(paymentMethodProcessor.getPaymentMethods(account, false, internalCallContext).size(), 0); // The first call should create the payment method final ExternalPaymentProviderPlugin providerPlugin = paymentMethodProcessor.getExternalPaymentProviderPlugin(account, internalCallContext); final List<PaymentMethod> paymentMethods = paymentMethodProcessor.getPaymentMethods(account, false, internalCallContext); Assert.assertEquals(paymentMethods.size(), 1); Assert.assertEquals(paymentMethods.get(0).getPluginName(), ExternalPaymentProviderPlugin.PLUGIN_NAME); Assert.assertEquals(paymentMethods.get(0).getAccountId(), account.getId()); // The succeeding calls should not create any other payment method final UUID externalPaymentMethodId = paymentMethods.get(0).getId(); for (int i = 0; i < 50; i++) { final ExternalPaymentProviderPlugin foundProviderPlugin = paymentMethodProcessor.getExternalPaymentProviderPlugin(account, internalCallContext); Assert.assertNotNull (foundProviderPlugin); final List<PaymentMethod> foundPaymentMethods = paymentMethodProcessor.getPaymentMethods(account, false, internalCallContext); Assert.assertEquals(foundPaymentMethods.size(), 1); Assert.assertEquals(foundPaymentMethods.get(0).getPluginName(), ExternalPaymentProviderPlugin.PLUGIN_NAME); Assert.assertEquals(foundPaymentMethods.get(0).getAccountId(), account.getId()); Assert.assertEquals(foundPaymentMethods.get(0).getId(), externalPaymentMethodId); } } }
@Override public void handleKillbillEvent(final ExtBusEvent killbillEvent) { logService.log(LogService.LOG_INFO, "Received external event " + killbillEvent.toString()); // Only looking at account creation if (killbillEvent.getEventType() != ExtBusEventType.ACCOUNT_CREATION) { return; } final TenantContext tenantContext = new TenantContext() { @Override public UUID getTenantId() { return null; } }; try { Account account = killbillAPI.getAccountUserApi().getAccountById(killbillEvent.getAccountId(), tenantContext); testDao.insertAccountExternalKey(account.getExternalKey()); } catch (AccountApiException e) { logService.log(LogService.LOG_ERROR, e.getMessage()); } } }
public BusinessAccountModelDao(final Account account) { super(account.getId(), account.getCreatedDate(), account.getUpdatedDate()); this.accountId = account.getId(); this.name = account.getName(); this.key = account.getExternalKey(); if (account.getCurrency() != null) { this.currency = account.getCurrency().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(); }
@Override public void handleKillbillEvent(final ExtBusEvent killbillEvent) { logService.log(LogService.LOG_INFO, "Received external event " + killbillEvent.toString()); // Only looking at account creation if (killbillEvent.getEventType() != ExtBusEventType.ACCOUNT_CREATION) { return; } final TenantContext tenantContext = new TenantContext() { @Override public UUID getTenantId() { return null; } }; try { Account account = killbillAPI.getAccountUserApi().getAccountById(killbillEvent.getAccountId(), tenantContext); testDao.insertAccountExternalKey(account.getExternalKey()); } catch (AccountApiException e) { logService.log(LogService.LOG_ERROR, e.getMessage()); } } }