Search in sources :

Example 31 with InternalTenantContext

use of org.killbill.billing.callcontext.InternalTenantContext in project killbill by killbill.

the class DefaultApiBase method toPaymentControlPluginNames.

protected List<String> toPaymentControlPluginNames(final PaymentOptions paymentOptions, final CallContext callContext) {
    final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContextWithoutAccountRecordId(callContext);
    // Special path for JAX-RS InvoicePayment endpoints (see JaxRsResourceBase)
    final List<String> controlPluginNames = paymentConfig.getPaymentControlPluginNames(internalTenantContext);
    if (controlPluginNames != null && paymentOptions.getPaymentControlPluginNames() != null && paymentOptions.getPaymentControlPluginNames().size() == 1 && InvoicePaymentControlPluginApi.PLUGIN_NAME.equals(paymentOptions.getPaymentControlPluginNames().get(0))) {
        final List<String> paymentControlPluginNames = new LinkedList<String>(paymentOptions.getPaymentControlPluginNames());
        paymentControlPluginNames.addAll(controlPluginNames);
        return paymentControlPluginNames;
    } else if (paymentOptions.getPaymentControlPluginNames() != null && !paymentOptions.getPaymentControlPluginNames().isEmpty()) {
        return paymentOptions.getPaymentControlPluginNames();
    } else if (controlPluginNames != null && !controlPluginNames.isEmpty()) {
        return controlPluginNames;
    } else {
        return ImmutableList.<String>of();
    }
}
Also used : InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) LinkedList(java.util.LinkedList)

Example 32 with InternalTenantContext

use of org.killbill.billing.callcontext.InternalTenantContext in project killbill by killbill.

the class OverdueListener method insertBusEventIntoNotificationQueue.

private void insertBusEventIntoNotificationQueue(final UUID accountId, final OverdueAsyncBusNotificationAction action, final InternalCallContext callContext) {
    final boolean shouldInsertNotification = shouldInsertNotification(callContext);
    if (shouldInsertNotification) {
        OverdueAsyncBusNotificationKey notificationKey = new OverdueAsyncBusNotificationKey(accountId, action);
        asyncPoster.insertOverdueNotification(accountId, clock.getUTCNow(), OverdueAsyncBusNotifier.OVERDUE_ASYNC_BUS_NOTIFIER_QUEUE, notificationKey, callContext);
        try {
            final List<Account> childrenAccounts = accountApi.getChildrenAccounts(accountId, callContext);
            if (childrenAccounts != null) {
                for (Account childAccount : childrenAccounts) {
                    if (childAccount.isPaymentDelegatedToParent()) {
                        final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContext(childAccount.getId(), callContext);
                        final InternalCallContext accountContext = internalCallContextFactory.createInternalCallContext(internalTenantContext.getAccountRecordId(), callContext);
                        notificationKey = new OverdueAsyncBusNotificationKey(childAccount.getId(), action);
                        asyncPoster.insertOverdueNotification(childAccount.getId(), clock.getUTCNow(), OverdueAsyncBusNotifier.OVERDUE_ASYNC_BUS_NOTIFIER_QUEUE, notificationKey, accountContext);
                    }
                }
            }
        } catch (Exception e) {
            log.error("Error loading child accounts from account " + accountId);
        }
    }
}
Also used : Account(org.killbill.billing.account.api.Account) OverdueAsyncBusNotificationKey(org.killbill.billing.overdue.notification.OverdueAsyncBusNotificationKey) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext) OverdueApiException(org.killbill.billing.overdue.api.OverdueApiException)

Example 33 with InternalTenantContext

use of org.killbill.billing.callcontext.InternalTenantContext in project killbill by killbill.

the class OverdueWrapper method billingState.

public BillingState billingState(final InternalCallContext context) throws OverdueException {
    if ((overdueable.getParentAccountId() != null) && (overdueable.isPaymentDelegatedToParent())) {
        // calculate billing state from parent account
        final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContext(overdueable.getParentAccountId(), context);
        final InternalCallContext parentAccountContext = internalCallContextFactory.createInternalCallContext(internalTenantContext.getAccountRecordId(), context);
        return billingStateCalcuator.calculateBillingState(overdueable, parentAccountContext);
    }
    return billingStateCalcuator.calculateBillingState(overdueable, context);
}
Also used : InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) InternalCallContext(org.killbill.billing.callcontext.InternalCallContext)

Example 34 with InternalTenantContext

use of org.killbill.billing.callcontext.InternalTenantContext in project killbill by killbill.

the class EmailInvoiceNotifier method notify.

@Override
public void notify(final Account account, final Invoice invoice, final TenantContext context) throws InvoiceApiException {
    if (Strings.emptyToNull(account.getEmail()) == null) {
        throw new InvoiceApiException(new IllegalArgumentException("Email for account " + account.getId() + " not specified"), ErrorCode.EMAIL_SENDING_FAILED);
    }
    final InternalTenantContext internalTenantContext = internalCallContextFactory.createInternalTenantContext(account.getId(), context);
    final List<String> to = new ArrayList<String>();
    to.add(account.getEmail());
    final List<AccountEmail> accountEmailList = accountApi.getEmails(account.getId(), internalTenantContext);
    final List<String> cc = new ArrayList<String>();
    for (final AccountEmail email : accountEmailList) {
        cc.add(email.getEmail());
    }
    // Check if this account has the MANUAL_PAY system tag
    boolean manualPay = false;
    final List<Tag> accountTags = tagUserApi.getTags(account.getId(), ObjectType.ACCOUNT, internalTenantContext);
    for (final Tag tag : accountTags) {
        if (ControlTagType.MANUAL_PAY.getId().equals(tag.getTagDefinitionId())) {
            manualPay = true;
            break;
        }
    }
    final HtmlInvoice htmlInvoice;
    try {
        htmlInvoice = generator.generateInvoice(account, invoice, manualPay, internalTenantContext);
    } catch (final IOException e) {
        throw new InvoiceApiException(e, ErrorCode.EMAIL_SENDING_FAILED);
    }
    // take localized subject, or the configured one if the localized one is not available
    String subject = htmlInvoice.getSubject();
    if (subject == null) {
        subject = config.getInvoiceEmailSubject();
    }
    final EmailSender sender = new DefaultEmailSender(config);
    try {
        sender.sendHTMLEmail(to, cc, subject, htmlInvoice.getBody());
    } catch (final EmailApiException e) {
        throw new InvoiceApiException(e, ErrorCode.EMAIL_SENDING_FAILED);
    } catch (final IOException e) {
        throw new InvoiceApiException(e, ErrorCode.EMAIL_SENDING_FAILED);
    }
}
Also used : EmailApiException(org.killbill.billing.util.email.EmailApiException) ArrayList(java.util.ArrayList) AccountEmail(org.killbill.billing.account.api.AccountEmail) HtmlInvoice(org.killbill.billing.invoice.template.HtmlInvoice) DefaultEmailSender(org.killbill.billing.util.email.DefaultEmailSender) EmailSender(org.killbill.billing.util.email.EmailSender) IOException(java.io.IOException) DefaultEmailSender(org.killbill.billing.util.email.DefaultEmailSender) InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) Tag(org.killbill.billing.util.tag.Tag)

Example 35 with InternalTenantContext

use of org.killbill.billing.callcontext.InternalTenantContext in project killbill by killbill.

the class DefaultInvoiceUserApi method getInvoiceAsHTML.

@Override
public String getInvoiceAsHTML(final UUID invoiceId, final TenantContext context) throws AccountApiException, IOException, InvoiceApiException {
    final Invoice invoice = getInvoice(invoiceId, context);
    if (invoice == null) {
        throw new InvoiceApiException(ErrorCode.INVOICE_NOT_FOUND, invoiceId);
    }
    final InternalTenantContext internalContext = internalCallContextFactory.createInternalTenantContext(invoiceId, ObjectType.INVOICE, context);
    final Account account = accountUserApi.getAccountById(invoice.getAccountId(), internalContext);
    // Check if this account has the MANUAL_PAY system tag
    boolean manualPay = false;
    final List<Tag> accountTags = tagApi.getTags(account.getId(), ObjectType.ACCOUNT, internalContext);
    for (final Tag tag : accountTags) {
        if (ControlTagType.MANUAL_PAY.getId().equals(tag.getTagDefinitionId())) {
            manualPay = true;
            break;
        }
    }
    final HtmlInvoice htmlInvoice = generator.generateInvoice(account, invoice, manualPay, internalContext);
    return htmlInvoice.getBody();
}
Also used : Account(org.killbill.billing.account.api.Account) InvoiceApiException(org.killbill.billing.invoice.api.InvoiceApiException) Invoice(org.killbill.billing.invoice.api.Invoice) HtmlInvoice(org.killbill.billing.invoice.template.HtmlInvoice) DefaultInvoice(org.killbill.billing.invoice.model.DefaultInvoice) InternalTenantContext(org.killbill.billing.callcontext.InternalTenantContext) HtmlInvoice(org.killbill.billing.invoice.template.HtmlInvoice) Tag(org.killbill.billing.util.tag.Tag)

Aggregations

InternalTenantContext (org.killbill.billing.callcontext.InternalTenantContext)56 UUID (java.util.UUID)9 ArrayList (java.util.ArrayList)8 CatalogApiException (org.killbill.billing.catalog.api.CatalogApiException)8 InternalCallContext (org.killbill.billing.callcontext.InternalCallContext)7 CacheLoaderArgument (org.killbill.billing.util.cache.CacheLoaderArgument)7 ImmutableList (com.google.common.collect.ImmutableList)6 List (java.util.List)6 LocalDate (org.joda.time.LocalDate)6 Predicate (com.google.common.base.Predicate)5 IOException (java.io.IOException)5 ObjectType (org.killbill.billing.ObjectType)5 InvoiceApiException (org.killbill.billing.invoice.api.InvoiceApiException)5 SubscriptionBaseApiException (org.killbill.billing.subscription.api.user.SubscriptionBaseApiException)5 InputStream (java.io.InputStream)4 URI (java.net.URI)4 DateTime (org.joda.time.DateTime)4 Account (org.killbill.billing.account.api.Account)4 PlanPhasePriceOverride (org.killbill.billing.catalog.api.PlanPhasePriceOverride)4 Invoice (org.killbill.billing.invoice.api.Invoice)4