Package org.wso2.carbon.billing.core

Examples of org.wso2.carbon.billing.core.BillingEngine


        return billingPeriods;
    }

    private void sendPaymentReceivedEmail(Payment payment) throws Exception{
        BillingManager billingManager = Util.getBillingManager();
        BillingEngine billingEngine =
                billingManager.getBillingEngine(StratosConstants.MULTITENANCY_SCHEDULED_TASK_ID);
        if(payment.getInvoice()!=null){
            Invoice invoice = billingEngine.getInvoice(payment.getInvoice().getId());
            if(invoice!=null){
                Customer customer = invoice.getCustomer();
                if(customer!=null){
                    Map<String, String> mailParameters = new HashMap<String, String>();
                    mailParameters.put("date",
                            new SimpleDateFormat("yyyy/MM/dd HH:mm:ss").format(payment.getDate()));
                    mailParameters.put("transaction-id", payment.getDescription());
                    mailParameters.put("amount", payment.getAmount().toString());
                    mailParameters.put("invoice-id", String.valueOf(payment.getInvoice().getId()));

                    try{
                        TenantManager tenantManager = Util.getRealmService().getTenantManager();
                        Tenant tenant = (Tenant) tenantManager.getTenant(customer.getId());
                        String customerName =
                                ClaimsMgtUtil.getFirstName(Util.getRealmService(), tenant, customer.getId());
                        if(customerName!=null){
                            mailParameters.put("customer-name", customerName);
                        }else{
                            mailParameters.put("customer-name", "");
                        }

                    }catch(Exception e){
                        log.error("Could not get tenant information for tenant: " +
                                customer.getName() + "\n" + e.getMessage());
                        mailParameters.put("customer-name", "");
                    }

                    //sending the mail to the customer
                    billingEngine.sendPaymentReceivedEmail(
                            customer.getEmail(),
                            BillingConstants.PAYMENT_RECEIVED_EMAIL_CUSTOMER_FILE,
                            mailParameters);

                    String financeEmail = CommonUtil.getStratosConfig().getFinanceNotificationEmail();
                    //customer's first name is not important to finance team. Therefore it is
                    //being replace with the domain name
                    mailParameters.put("customer-name", customer.getName());
                    billingEngine.sendPaymentReceivedEmail(
                            financeEmail,
                            BillingConstants.PAYMENT_RECEIVED_EMAIL_WSO2_FILE,
                            mailParameters
                    );
                }else{
View Full Code Here


public class BillingJob implements Job {
    private static final Log log = LogFactory.getLog(BillingJob.class);

    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        // first we generate the bill
        BillingEngine billingEngine =
                (BillingEngine) jobExecutionContext.getMergedJobDataMap().get(
                        BillingConstants.BILLING_ENGINE_KEY);
        SchedulerContext schedulerContext =
                (SchedulerContext) jobExecutionContext.getMergedJobDataMap().get(
                        BillingConstants.SCHEDULER_CONTEXT);
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            long startTime = System.currentTimeMillis();
            log.info("Bill generation started at " + dateFormat.format(new Date(System.currentTimeMillis())));
            billingEngine.generateBill(schedulerContext);
            log.info("Bill generation completed at " + dateFormat.format(new Date(System.currentTimeMillis())));
            long timeTaken = System.currentTimeMillis() - startTime;
            log.info("Time taken for bill generation: " + timeTaken/1000 + "s");
        } catch (BillingException e) {
            String msg = "Error in generating the bill";
View Full Code Here

        return subscriptionInfoBean;
    }

    public static void cancelSubscriptionInfo(UserRegistry userRegistry) throws Exception {
        BillingEngine billingEngine = billingManager.getBillingEngine(StratosConstants.MULTITENANCY_SCHEDULED_TASK_ID);

        Subscription subscription = getCurrentSubscription(userRegistry);
        if (subscription == null) {
            // nothing to un-subscribe
            return;
        }
        // what we are doing here is, set the activeUntil to today and save it
        subscription.setActiveUntil(new Date());
        billingEngine.updateSubscription(subscription);
    }
View Full Code Here

        if (subscription != null && subscription.getItem() != null &&
                subscription.getItem().getName().equals(packageName)) {
            // then we are just extending (or just shortning) the subscription
            subscription.setActiveUntil(activeUntilDate);
            BillingEngine billingEngine =
                    billingManager.getBillingEngine(StratosConstants.MULTITENANCY_SCHEDULED_TASK_ID);

            billingEngine.updateSubscription(subscription);
        }
        else {
            cancelSubscriptionInfo(userRegistry);
            SubscriptionInfoBean subscriptionInfoBean = new SubscriptionInfoBean();
            subscriptionInfoBean.setActiveSince(new Date());
View Full Code Here

        }
    }

    public static void addSubscriptionInfo(SubscriptionInfoBean subscriptionInfoBean,
                                           UserRegistry userRegistry) throws Exception {
        BillingEngine billingEngine = billingManager.getBillingEngine(StratosConstants.MULTITENANCY_SCHEDULED_TASK_ID);

        Customer customer = getCurrentCustomer(userRegistry);
        // if customer doesn't exist, we are making a one
        if (customer == null) {
            int currentTenantId = userRegistry.getTenantId();
            TenantManager tenantManger = getRealmService().getTenantManager();
            Tenant currentTenant = (Tenant) tenantManger.getTenant(currentTenantId);
            if (currentTenant == null || currentTenant.getDomain() == null) {
                String msg = "Error in getting the customer information.";
                throw new Exception(msg);
            }
            customer = new Customer();
            customer.setName(currentTenant.getDomain());
            customer.setEmail(currentTenant.getEmail());
            customer.setStartedDate(new Date());

            billingEngine.addCustomer(customer);
        }

        String itemName = subscriptionInfoBean.getPackageName();
        if (itemName.toLowerCase().contains("free")) {
            return; //nothing to upgrade in a free package
        }
        List<Item> items = billingManager.getBillingEngine(StratosConstants.MULTITENANCY_SCHEDULED_TASK_ID).
                getItemsWithName(itemName);
        if (items == null || items.size() == 0) {
            String msg = "Invalid item name: " + itemName + ".";
            throw new Exception(msg);
        }
        Item item = items.get(0);

        // adding the subscription
        Subscription subscription = new Subscription();
        subscription.setItem(item);
        subscription.setCustomer(customer);
        subscription.setActive(true);
        subscription.setActiveSince(subscriptionInfoBean.getActiveSince());
        subscription.setActiveUntil(subscriptionInfoBean.getActiveUntil());

        billingEngine.addSubscription(subscription);
    }
View Full Code Here

        billingEngine.addSubscription(subscription);
    }

    public static Subscription getCurrentSubscription(UserRegistry userRegistry) throws Exception {
        BillingEngine billingEngine = billingManager.getBillingEngine(StratosConstants.MULTITENANCY_SCHEDULED_TASK_ID);

        Customer customer = getCurrentCustomer(userRegistry);
        if (customer == null) {
            return null;
        }
        List<Subscription> subscriptions = billingEngine.getActiveSubscriptions(customer);
        if (subscriptions == null || subscriptions.size() == 0) {
            return null;
        }
        Subscription subscription = subscriptions.get(0);
        if (subscription.getActiveUntil().getTime() <= System.currentTimeMillis()) {
            return null;
        }
        int itemId = subscription.getItem().getId();
        // fill with a correct item
        Item item =  billingEngine.getItem(itemId);
        subscription.setItem(item);
        return subscription;
    }
View Full Code Here

    public static Customer getCurrentCustomer(UserRegistry userRegistry) throws Exception {
        int currentTenantId = userRegistry.getTenantId();
        TenantManager tenantManger = getRealmService().getTenantManager();
        Tenant currentTenant = (Tenant) tenantManger.getTenant(currentTenantId);
        BillingEngine billingEngine =
                billingManager.getBillingEngine(StratosConstants.MULTITENANCY_SCHEDULED_TASK_ID);
        List<Customer> customers = billingEngine.getCustomersWithName(currentTenant.getDomain());
        if (customers == null || customers.size() == 0) {
            return null;
        }
        return customers.get(0);
    }
View Full Code Here

        if (tenant == null) {
            return null;
        }
        String customerName = tenant.getDomain();

        BillingEngine billingEngine =
                billingManager.getBillingEngine(StratosConstants.MULTITENANCY_SCHEDULED_TASK_ID);
        Customer customer;
        try {
            List<Customer> customers = billingEngine.getCustomersWithName(customerName);
            if (customers == null || customers.size() == 0) {
                customer = null;
            } else {
                customer = customers.get(0);
            }
View Full Code Here

     * @param tenantId, tenant id
     * @throws RegistryException, if getting the current subscription type failed.
     * @return, Subscripiton
     */
    public static Subscription getCurrentSubscription(int tenantId) throws RegistryException {
        BillingEngine billingEngine =
                billingManager.getBillingEngine(StratosConstants.MULTITENANCY_SCHEDULED_TASK_ID);

        Customer customer = getCurrentBillingCustomer(tenantId);
        if (customer == null) {
            return null;
        }
        List<Subscription> subscriptions;
        try {
            subscriptions = billingEngine.getActiveSubscriptions(customer);
        } catch (BillingException e) {
            String msg = "Error in getting the current subscription.";
            log.error(msg, e);
            throw new RegistryException(msg, e);
        }
        if (subscriptions == null || subscriptions.size() == 0) {
            return null;
        }
        Subscription subscription = subscriptions.get(0);
        if (subscription.getActiveUntil().getTime() <= System.currentTimeMillis()) {
            return null;
        }
        int itemId = subscription.getItem().getId();
        // fill with a correct item
        Item item;
        try {
            item = billingEngine.getItem(itemId);
        } catch (BillingException e) {
            String msg = "Error in getting the item for item id: " + itemId + ".";
            log.error(msg, e);
            throw new RegistryException(msg, e);
        }
View Full Code Here

        }
        if (tenant == null) {
            return null;
        }
        String customerName = tenant.getDomain();
        BillingEngine billingEngine =
                billingManager.getBillingEngine(StratosConstants.MULTITENANCY_VIEWING_TASK_ID);
        Customer customer;
        try {
            if (billingEngine != null) {
                List<Customer> customers = billingEngine.getCustomersWithName(customerName);
                if (customers == null || customers.size() == 0) {
                    customer = null;
                } else {
                    customer = customers.get(0);
                }
View Full Code Here

TOP

Related Classes of org.wso2.carbon.billing.core.BillingEngine

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.