private OverdueChangeInternalEvent createOverdueEvent(final Account overdueable, final String previousOverdueStateName, final String nextOverdueStateName, final boolean isBlockedBilling, final boolean isUnblockedBilling, final InternalCallContext context) throws BlockingApiException { return new DefaultOverdueChangeEvent(overdueable.getId(), previousOverdueStateName, nextOverdueStateName, isBlockedBilling, isUnblockedBilling, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()); }
public InternalCallContext createInternalCallContext(final Long accountRecordId, final InternalCallContext context) { return new InternalCallContext(context.getTenantRecordId(), accountRecordId, context.getUserToken(), context.getCreatedBy(), context.getCallOrigin(), context.getContextUserType(), context.getReasonCode(), context.getComments(), context.getCreatedDate(), context.getUpdatedDate()); }
public BlockingStateModelDao(final BlockingState src, final InternalCallContext context) { this(src, context.getCreatedDate(), context.getUpdatedDate()); }
@Override public TagDefinitionModelDao inTransaction(final EntitySqlDaoWrapperFactory<EntitySqlDao> entitySqlDaoWrapperFactory) throws Exception { final TagDefinitionSqlDao tagDefinitionSqlDao = entitySqlDaoWrapperFactory.become(TagDefinitionSqlDao.class); // Make sure the tag definition doesn't exist already final TagDefinitionModelDao existingDefinition = tagDefinitionSqlDao.getByName(definitionName, context); if (existingDefinition != null) { throw new TagDefinitionApiException(ErrorCode.TAG_DEFINITION_ALREADY_EXISTS, definitionName); } // Create it final TagDefinitionModelDao tagDefinition = new TagDefinitionModelDao(context.getCreatedDate(), definitionName, description); tagDefinitionSqlDao.create(tagDefinition, context); // Post an event to the bus final boolean isControlTag = TagModelDaoHelper.isControlTag(tagDefinition.getName()); final TagDefinitionInternalEvent tagDefinitionEvent; if (isControlTag) { tagDefinitionEvent = tagEventBuilder.newControlTagDefinitionCreationEvent(tagDefinition.getId(), tagDefinition, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()); } else { tagDefinitionEvent = tagEventBuilder.newUserTagDefinitionCreationEvent(tagDefinition.getId(), tagDefinition, context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()); } try { bus.postFromTransaction(tagDefinitionEvent, entitySqlDaoWrapperFactory.getSqlDao()); } catch (PersistentBus.EventBusException e) { log.warn("Failed to post tag definition creation event for tag " + tagDefinition.getId(), e); } return tagDefinition; } });
private Payment processPaymentWithAccountLocked(final PaymentPluginApi plugin, final Account account, final Invoice invoice, final PaymentModelDao paymentInput, final PaymentAttemptModelDao attemptInput, final boolean isInstantPayment, final InternalCallContext context) throws PaymentApiException { final UUID tenantId = nonEntityDao.retrieveIdFromObject(context.getTenantRecordId(), ObjectType.TENANT); try { paymentPluginInfo = plugin.processPayment(account.getId(), paymentInput.getId(), attemptInput.getPaymentMethodId(), attemptInput.getRequestedAmount(), account.getCurrency(), context.toCallContext(tenantId)); } catch (RuntimeException e) { paymentStatus, payment.getEffectiveDate(), context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken()); break; context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken() ); throw new PaymentApiException(ErrorCode.PAYMENT_CREATE_PAYMENT, account.getId(), paymentPluginInfo.getGatewayError()); context.getAccountRecordId(), context.getTenantRecordId(), context.getUserToken() ); throw new PaymentApiException(ErrorCode.PAYMENT_CREATE_PAYMENT, account.getId(), e.toString());
q.bind("userName", callContext.getCreatedBy()); if (callContext.getCreatedDate() == null) { q.bindNull("createdDate", Types.DATE); } else { q.bind("createdDate", callContext.getCreatedDate().toDate()); if (callContext.getUpdatedDate() == null) { q.bindNull("updatedDate", Types.DATE); } else { q.bind("updatedDate", callContext.getUpdatedDate().toDate()); q.bind("reasonCode", callContext.getReasonCode()); q.bind("comments", callContext.getComments()); q.bind("userToken", (callContext.getUserToken() != null) ? callContext.getUserToken().toString() : null);
private void notifyBusOfInvoiceAdjustment(final EntitySqlDaoWrapperFactory<EntitySqlDao> entitySqlDaoWrapperFactory, final UUID invoiceId, final UUID accountId, final UUID userToken, final InternalCallContext context) { try { eventBus.postFromTransaction(new DefaultInvoiceAdjustmentEvent(invoiceId, accountId, context.getAccountRecordId(), context.getTenantRecordId(), userToken), entitySqlDaoWrapperFactory.getSqlDao()); } catch (EventBusException e) { log.warn("Failed to post adjustment event for invoice " + invoiceId, e); } } }
@Override public InvoiceItemModelDao inTransaction(final EntitySqlDaoWrapperFactory<EntitySqlDao> entitySqlDaoWrapperFactory) throws Exception { final InvoiceItemModelDao invoiceItemAdjustment = invoiceDaoHelper.createAdjustmentItem(entitySqlDaoWrapperFactory, invoiceId, invoiceItemId, positiveAdjAmount, currency, effectiveDate, context); invoiceDaoHelper.insertItem(entitySqlDaoWrapperFactory, invoiceItemAdjustment, context); cbaDao.doCBAComplexity(accountId, entitySqlDaoWrapperFactory, context); notifyBusOfInvoiceAdjustment(entitySqlDaoWrapperFactory, invoiceId, accountId, context.getUserToken(), context); return invoiceItemAdjustment; } });
@Override public InvoiceItemModelDao inTransaction(final EntitySqlDaoWrapperFactory<EntitySqlDao> entitySqlDaoWrapperFactory) throws Exception { final InvoiceSqlDao transactional = entitySqlDaoWrapperFactory.become(InvoiceSqlDao.class); UUID invoiceIdForCredit = invoiceId; // Create an invoice for that credit if it doesn't exist if (invoiceIdForCredit == null) { final InvoiceModelDao invoiceForCredit = new InvoiceModelDao(accountId, effectiveDate, effectiveDate, currency); transactional.create(invoiceForCredit, context); invoiceIdForCredit = invoiceForCredit.getId(); } // Note! The amount is negated here! final InvoiceItemModelDao credit = new InvoiceItemModelDao(context.getCreatedDate(), InvoiceItemType.CREDIT_ADJ, invoiceIdForCredit, accountId, null, null, null, null, effectiveDate, null, positiveCreditAmount.negate(), null, currency, null); invoiceDaoHelper.insertItem(entitySqlDaoWrapperFactory, credit, context); cbaDao.doCBAComplexity(accountId, entitySqlDaoWrapperFactory, context); // Notify the bus since the balance of the invoice changed notifyBusOfInvoiceAdjustment(entitySqlDaoWrapperFactory, invoiceId, accountId, context.getUserToken(), context); return credit; } });
@VisibleForTesting <T extends OverdueCheckNotificationKey> Collection<NotificationEventWithMetadata<T>> getFutureNotificationsForAccountInTransaction(final EntitySqlDaoWrapperFactory<EntitySqlDao> entitySqlDaoWrapperFactory, final NotificationQueue checkOverdueQueue, final UUID accountId, final Class<T> clazz, final InternalCallContext context) { final List<NotificationEventWithMetadata<T>> notifications = checkOverdueQueue.getFutureNotificationFromTransactionForSearchKey1(clazz, context.getAccountRecordId(), entitySqlDaoWrapperFactory.getSqlDao()); /* final Collection<NotificationEventWithMetadata<T>> notificationsFiltered = Collections2.filter(notifications, new Predicate<NotificationEventWithMetadata<T>>() { @Override public boolean apply(@Nullable final NotificationEventWithMetadata<T> input) { final OverdueCheckNotificationKey notificationKey = input.getEvent(); return (accountId.equals(notificationKey.getUuidKey())); } }); return notificationsFiltered; */ return notifications; }
private void verifyAuditLogsForTag(final List<AuditLog> auditLogs, final AuditLevel level) { if (AuditLevel.NONE.equals(level)) { Assert.assertEquals(auditLogs.size(), 0); return; } Assert.assertEquals(auditLogs.size(), 1); Assert.assertEquals(auditLogs.get(0).getUserToken(), internalCallContext.getUserToken().toString()); Assert.assertEquals(auditLogs.get(0).getChangeType(), ChangeType.INSERT); Assert.assertEquals(auditLogs.get(0).getComment(), internalCallContext.getComments()); Assert.assertEquals(auditLogs.get(0).getReasonCode(), internalCallContext.getReasonCode()); Assert.assertEquals(auditLogs.get(0).getUserName(), internalCallContext.getCreatedBy()); Assert.assertNotNull(auditLogs.get(0).getCreatedDate()); } }
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); } } }); }
computeEntitlementsToCancel(account, toBeCancelled, context); final UUID tenantId = nonEntityDao.retrieveIdFromObject(context.getTenantRecordId(), ObjectType.TENANT); for (final Entitlement cur : toBeCancelled) { try { cur.cancelEntitlementWithDateOverrideBillingPolicy(new LocalDate(clock.getUTCNow(), account.getTimeZone()), actionPolicy, context.toCallContext(tenantId)); } catch (EntitlementApiException e) {
@Override public void addTag(final UUID objectId, final ObjectType objectType, final UUID tagDefinitionId, final InternalCallContext context) throws TagApiException { final TagModelDao tag = new TagModelDao(context.getCreatedDate(), tagDefinitionId, objectId, objectType); tagDao.create(tag, context); }
@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); } } });
private void processEntitlementNotification(final EntitlementNotificationKey key, final UUID tenantId, final InternalCallContext internalCallContext) { final Entitlement entitlement; try { entitlement = entitlementApi.getEntitlementForId(key.getEntitlementId(), internalCallContext.toTenantContext(tenantId)); } catch (final EntitlementApiException e) { log.error("Error retrieving entitlement for id " + key.getEntitlementId(), e); return; } if (!(entitlement instanceof DefaultEntitlement)) { log.error("Entitlement service received an unexpected entitlement class type {}" + entitlement.getClass().getName()); return; } final EntitlementNotificationKeyAction entitlementNotificationKeyAction = key.getEntitlementNotificationKeyAction(); try { if (EntitlementNotificationKeyAction.CHANGE.equals(entitlementNotificationKeyAction) || EntitlementNotificationKeyAction.CANCEL.equals(entitlementNotificationKeyAction)) { ((DefaultEntitlement) entitlement).blockAddOnsIfRequired(key.getEffectiveDate(), internalCallContext.toTenantContext(tenantId), internalCallContext); } else if (EntitlementNotificationKeyAction.PAUSE.equals(entitlementNotificationKeyAction)) { entitlementApi.pause(key.getBundleId(), key.getEffectiveDate().toLocalDate(), internalCallContext.toCallContext(tenantId)); } else if (EntitlementNotificationKeyAction.RESUME.equals(entitlementNotificationKeyAction)) { entitlementApi.resume(key.getBundleId(), key.getEffectiveDate().toLocalDate(), internalCallContext.toCallContext(tenantId)); } } catch (final EntitlementApiException e) { log.error("Error processing event for entitlement {}" + entitlement.getId(), e); } }
@Override public Void withHandle(final Handle handle) throws Exception { handle.execute("drop table if exists " + tableNameA); handle.execute("create table " + tableNameA + "(record_id int(11) unsigned not null auto_increment," + "a_column char default 'a'," + "account_record_id int(11) unsigned not null," + "tenant_record_id int(11) unsigned default 0," + "primary key(record_id));"); handle.execute("drop table if exists " + tableNameB); handle.execute("create table " + tableNameB + "(record_id int(11) unsigned not null auto_increment," + "b_column char default 'b'," + "account_record_id int(11) unsigned not null," + "tenant_record_id int(11) unsigned default 0," + "primary key(record_id));"); handle.execute("insert into " + tableNameA + " (account_record_id, tenant_record_id) values (?, ?)", internalCallContext.getAccountRecordId(), internalCallContext.getTenantRecordId()); handle.execute("insert into " + tableNameB + " (account_record_id, tenant_record_id) values (?, ?)", internalCallContext.getAccountRecordId(), internalCallContext.getTenantRecordId()); // Add row in accounts table handle.execute("insert into accounts (record_id, id, email, name, first_name_length, is_notified_for_invoices, created_date, created_by, updated_date, updated_by, tenant_record_id) " + "values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", internalCallContext.getAccountRecordId(), accountId, accountEmail, accountName, firstNameLength, isNotifiedForInvoices, createdDate, createdBy, updatedDate, updatedBy, internalCallContext.getTenantRecordId()); return null; } });
@Test(groups = "fast") public void testNewControlTagDefinitionCreationEvent() throws Exception { final UUID tagDefinitionId = UUID.randomUUID(); final String tagDefinitionName = UUID.randomUUID().toString(); final String tagDefinitionDescription = UUID.randomUUID().toString(); final boolean controlTag = true; final TagDefinition tagDefinition = new DefaultTagDefinition(tagDefinitionId, tagDefinitionName, tagDefinitionDescription, controlTag); final UUID userToken = internalCallContext.getUserToken(); final TagEventBuilder tagEventBuilder = new TagEventBuilder(); final TagDefinitionInternalEvent event = tagEventBuilder.newControlTagDefinitionCreationEvent(tagDefinitionId, new TagDefinitionModelDao(tagDefinition), 1L, 2L, UUID.randomUUID()); Assert.assertTrue(event instanceof ControlTagDefinitionCreationInternalEvent); Assert.assertEquals(event, new DefaultControlTagDefinitionCreationEvent(tagDefinitionId, tagDefinition, 1L, 2L, UUID.randomUUID())); Assert.assertTrue(event.equals(new DefaultControlTagDefinitionCreationEvent(tagDefinitionId, tagDefinition, 1L, 2L, UUID.randomUUID()))); verifyTagDefinitionEvent(tagDefinitionId, tagDefinitionName, tagDefinitionDescription, tagDefinition, userToken, event); }
@Override public InvoicePaymentModelDao inTransaction(final EntitySqlDaoWrapperFactory<EntitySqlDao> entitySqlDaoWrapperFactory) throws Exception { final InvoicePaymentSqlDao transactional = entitySqlDaoWrapperFactory.become(InvoicePaymentSqlDao.class); final BigDecimal maxChargedBackAmount = invoiceDaoHelper.getRemainingAmountPaidFromTransaction(invoicePaymentId, entitySqlDaoWrapperFactory, context); final BigDecimal requestedChargedBackAmount = (amount == null) ? maxChargedBackAmount : amount; if (requestedChargedBackAmount.compareTo(BigDecimal.ZERO) <= 0) { throw new InvoiceApiException(ErrorCode.CHARGE_BACK_AMOUNT_IS_NEGATIVE); } if (requestedChargedBackAmount.compareTo(maxChargedBackAmount) > 0) { throw new InvoiceApiException(ErrorCode.CHARGE_BACK_AMOUNT_TOO_HIGH, requestedChargedBackAmount, maxChargedBackAmount); } final InvoicePaymentModelDao payment = entitySqlDaoWrapperFactory.become(InvoicePaymentSqlDao.class).getById(invoicePaymentId.toString(), context); if (payment == null) { throw new InvoiceApiException(ErrorCode.INVOICE_PAYMENT_NOT_FOUND, invoicePaymentId.toString()); } final InvoicePaymentModelDao chargeBack = new InvoicePaymentModelDao(UUID.randomUUID(), context.getCreatedDate(), InvoicePaymentType.CHARGED_BACK, payment.getInvoiceId(), payment.getPaymentId(), context.getCreatedDate(), requestedChargedBackAmount.negate(), payment.getCurrency(), payment.getProcessedCurrency(), null, payment.getId()); transactional.create(chargeBack, context); // Notify the bus since the balance of the invoice changed final UUID accountId = transactional.getAccountIdFromInvoicePaymentId(chargeBack.getId().toString(), context); cbaDao.doCBAComplexity(accountId, entitySqlDaoWrapperFactory, context); notifyBusOfInvoiceAdjustment(entitySqlDaoWrapperFactory, payment.getInvoiceId(), accountId, context.getUserToken(), context); return chargeBack; } });
private boolean isAccountTaggedWith_OVERDUE_ENFORCEMENT_OFF(final InternalCallContext context) throws OverdueException { try { final UUID accountId = accountApi.getByRecordId(context.getAccountRecordId(), context); final List<Tag> accountTags = tagApi.getTags(accountId, ObjectType.ACCOUNT, context); for (Tag cur : accountTags) { if (cur.getTagDefinitionId().equals(ControlTagType.OVERDUE_ENFORCEMENT_OFF.getId())) { return true; } } return false; } catch (AccountApiException e) { throw new OverdueException(e); } } }