Skip to content

Commit

Permalink
Merge pull request #13 from reshmabidikar/use-custom-invoice-plugin
Browse files Browse the repository at this point in the history
Use custom invoice plugin
  • Loading branch information
pierre authored Mar 10, 2024
2 parents 5349b07 + 9ae227f commit 082b044
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 4 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>org.kill-bill.billing</groupId>
<artifactId>killbill-oss-parent</artifactId>
<version>0.146.6</version>
<version>0.146.30</version>
</parent>
<groupId>org.kill-bill.billing.plugin.java</groupId>
<artifactId>hello-world-plugin</artifactId>
Expand Down Expand Up @@ -104,7 +104,6 @@
<dependency>
<groupId>org.kill-bill.billing.plugin</groupId>
<artifactId>killbill-plugin-api-invoice</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.kill-bill.billing.plugin</groupId>
Expand Down Expand Up @@ -140,6 +139,7 @@
<artifactId>slf4j-api</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
import org.killbill.billing.plugin.core.resources.jooby.PluginApp;
import org.killbill.billing.plugin.core.resources.jooby.PluginAppBuilder;
import org.osgi.framework.BundleContext;
import org.osgi.util.tracker.ServiceTracker;
import org.killbill.billing.invoice.plugin.api.InvoiceFormatterFactory;

public class HelloWorldActivator extends KillbillActivatorBase {

Expand All @@ -50,6 +52,8 @@ public class HelloWorldActivator extends KillbillActivatorBase {
private OSGIKillbillEventDispatcher.OSGIKillbillEventHandler killbillEventHandler;
private MetricsGeneratorExample metricsGenerator;

private ServiceTracker<InvoiceFormatterFactory, InvoiceFormatterFactory> invoiceFormatterTracker;

@Override
public void start(final BundleContext context) throws Exception {
super.start(context);
Expand All @@ -62,8 +66,13 @@ public void start(final BundleContext context) throws Exception {
.createConfigurable(configProperties.getProperties());
helloWorldConfigurationHandler.setDefaultConfigurable(globalConfiguration);

// create a service tracker for a custom InvoiceFormatter service
invoiceFormatterTracker = new ServiceTracker<>(context, InvoiceFormatterFactory.class, null);
invoiceFormatterTracker.open();


// Register an event listener (optional)
killbillEventHandler = new HelloWorldListener(killbillAPI);
killbillEventHandler = new HelloWorldListener(killbillAPI, invoiceFormatterTracker, configProperties.getProperties());

// As an example, this plugin registers a PaymentPluginApi (this could be
// changed to any other plugin api)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,24 @@

package org.killbill.billing.plugin.helloworld;

import java.util.List;
import java.util.Locale;
import java.util.Properties;

import org.joda.time.LocalDate;
import org.killbill.billing.account.api.Account;
import org.killbill.billing.account.api.AccountApiException;
import org.killbill.billing.invoice.api.Invoice;
import org.killbill.billing.invoice.api.InvoiceItem;
import org.killbill.billing.invoice.api.formatters.InvoiceFormatter;
import org.killbill.billing.invoice.api.formatters.InvoiceItemFormatter;
import org.killbill.billing.invoice.plugin.api.InvoiceFormatterFactory;
import org.killbill.billing.notification.plugin.api.ExtBusEvent;
import org.killbill.billing.osgi.libs.killbill.OSGIKillbillAPI;
import org.killbill.billing.osgi.libs.killbill.OSGIKillbillEventDispatcher;
import org.killbill.billing.plugin.api.PluginTenantContext;
import org.killbill.billing.util.callcontext.TenantContext;
import org.osgi.util.tracker.ServiceTracker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -35,10 +46,18 @@ public class HelloWorldListener implements OSGIKillbillEventDispatcher.OSGIKillb

private final OSGIKillbillAPI osgiKillbillAPI;

public HelloWorldListener(final OSGIKillbillAPI killbillAPI) {
private final ServiceTracker<InvoiceFormatterFactory, InvoiceFormatterFactory> invoiceFormatterTracker;

private final Properties configProperties;

public HelloWorldListener(final OSGIKillbillAPI killbillAPI, final ServiceTracker<InvoiceFormatterFactory, InvoiceFormatterFactory> invoiceFormatterTracker, Properties configProperties) {
this.osgiKillbillAPI = killbillAPI;
this.invoiceFormatterTracker = invoiceFormatterTracker;
this.configProperties = configProperties;
}

private static final String defaultLocale = "en_US";

@Override
public void handleKillbillEvent(final ExtBusEvent killbillEvent) {
logger.info("Received event {} for object id {} of type {}",
Expand All @@ -60,7 +79,36 @@ public void handleKillbillEvent(final ExtBusEvent killbillEvent) {
logger.warn("Unable to find account", e);
}
break;
case INVOICE_CREATION:

final Account account;
try {
account = osgiKillbillAPI.getAccountUserApi().getAccountById(killbillEvent.getAccountId(), context);
} catch (AccountApiException e) {
throw new RuntimeException(e);
}
final List<Invoice> invoices = osgiKillbillAPI.getInvoiceUserApi().getInvoicesByAccount(killbillEvent.getAccountId(), false, false, true, context);
logger.info("Invoices in hello-world-plugin {}: ",invoices.size());

final String invoiceFormatterPluginName = configProperties.getProperty("org.killbill.template.invoiceFormatterFactoryPluginName");
if(invoiceFormatterPluginName == null || invoiceFormatterPluginName.isEmpty()){
logger.info("Invoice formatter plugin not configured. set the org.killbill.template.invoiceFormatterFactoryPluginName property to configure it");
return;
}

final InvoiceFormatterFactory formatterFactory = (invoiceFormatterTracker != null ? invoiceFormatterTracker.getService() : null);
Invoice invoice = invoices.get(0); //For demo purpose, we are only retrieving the formattedEndDate for the first invoice
//TODO Using null for parameters like catalogBundlePath,bundle, defaultBundle, etc. Update this to use correct values if possible or verify that using null values has no adverse effect

InvoiceFormatter invoiceFormatter = formatterFactory.createInvoiceFormatter(defaultLocale, null, invoice, Locale.forLanguageTag(account.getLocale()), osgiKillbillAPI.getCurrencyConversionApi(), null, null);

List<InvoiceItem> items = invoiceFormatter.getInvoiceItems();
logger.info("hello-world-plugin got items:{}",items.size());
for(InvoiceItem item:items) {
final InvoiceItemFormatter invoiceItemFormatter = (InvoiceItemFormatter)item;
final String formattedEndDate = invoiceItemFormatter.getFormattedEndDate();
logger.info("hello-world-plugin formattedEndDate:{}",formattedEndDate);
}
// Nothing
default:
break;
Expand Down

0 comments on commit 082b044

Please sign in to comment.