Skip to content

Commit a8a3cb6

Browse files
committed
use custom invoice plugin
1 parent 5349b07 commit a8a3cb6

3 files changed

Lines changed: 50 additions & 4 deletions

File tree

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<parent>
2323
<groupId>org.kill-bill.billing</groupId>
2424
<artifactId>killbill-oss-parent</artifactId>
25-
<version>0.146.6</version>
25+
<version>0.146.30</version>
2626
</parent>
2727
<groupId>org.kill-bill.billing.plugin.java</groupId>
2828
<artifactId>hello-world-plugin</artifactId>
@@ -104,7 +104,6 @@
104104
<dependency>
105105
<groupId>org.kill-bill.billing.plugin</groupId>
106106
<artifactId>killbill-plugin-api-invoice</artifactId>
107-
<scope>provided</scope>
108107
</dependency>
109108
<dependency>
110109
<groupId>org.kill-bill.billing.plugin</groupId>
@@ -140,6 +139,7 @@
140139
<artifactId>slf4j-api</artifactId>
141140
<scope>provided</scope>
142141
</dependency>
142+
143143
<dependency>
144144
<groupId>org.testng</groupId>
145145
<artifactId>testng</artifactId>

src/main/java/org/killbill/billing/plugin/helloworld/HelloWorldActivator.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import org.killbill.billing.plugin.core.resources.jooby.PluginApp;
3838
import org.killbill.billing.plugin.core.resources.jooby.PluginAppBuilder;
3939
import org.osgi.framework.BundleContext;
40+
import org.osgi.util.tracker.ServiceTracker;
41+
import org.killbill.billing.invoice.plugin.api.InvoiceFormatterFactory;
4042

4143
public class HelloWorldActivator extends KillbillActivatorBase {
4244

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

55+
private ServiceTracker<InvoiceFormatterFactory, InvoiceFormatterFactory> invoiceFormatterTracker;
56+
5357
@Override
5458
public void start(final BundleContext context) throws Exception {
5559
super.start(context);
@@ -62,8 +66,13 @@ public void start(final BundleContext context) throws Exception {
6266
.createConfigurable(configProperties.getProperties());
6367
helloWorldConfigurationHandler.setDefaultConfigurable(globalConfiguration);
6468

69+
// create a service tracker for a custom InvoiceFormatter service
70+
invoiceFormatterTracker = new ServiceTracker<>(context, InvoiceFormatterFactory.class, null);
71+
invoiceFormatterTracker.open();
72+
73+
6574
// Register an event listener (optional)
66-
killbillEventHandler = new HelloWorldListener(killbillAPI);
75+
killbillEventHandler = new HelloWorldListener(killbillAPI, invoiceFormatterTracker);
6776

6877
// As an example, this plugin registers a PaymentPluginApi (this could be
6978
// changed to any other plugin api)

src/main/java/org/killbill/billing/plugin/helloworld/HelloWorldListener.java

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,23 @@
1919

2020
package org.killbill.billing.plugin.helloworld;
2121

22+
import java.util.List;
23+
import java.util.Locale;
24+
25+
import org.joda.time.LocalDate;
2226
import org.killbill.billing.account.api.Account;
2327
import org.killbill.billing.account.api.AccountApiException;
28+
import org.killbill.billing.invoice.api.Invoice;
29+
import org.killbill.billing.invoice.api.InvoiceItem;
30+
import org.killbill.billing.invoice.api.formatters.InvoiceFormatter;
31+
import org.killbill.billing.invoice.api.formatters.InvoiceItemFormatter;
32+
import org.killbill.billing.invoice.plugin.api.InvoiceFormatterFactory;
2433
import org.killbill.billing.notification.plugin.api.ExtBusEvent;
2534
import org.killbill.billing.osgi.libs.killbill.OSGIKillbillAPI;
2635
import org.killbill.billing.osgi.libs.killbill.OSGIKillbillEventDispatcher;
2736
import org.killbill.billing.plugin.api.PluginTenantContext;
2837
import org.killbill.billing.util.callcontext.TenantContext;
38+
import org.osgi.util.tracker.ServiceTracker;
2939
import org.slf4j.Logger;
3040
import org.slf4j.LoggerFactory;
3141

@@ -35,10 +45,15 @@ public class HelloWorldListener implements OSGIKillbillEventDispatcher.OSGIKillb
3545

3646
private final OSGIKillbillAPI osgiKillbillAPI;
3747

38-
public HelloWorldListener(final OSGIKillbillAPI killbillAPI) {
48+
private final ServiceTracker<InvoiceFormatterFactory, InvoiceFormatterFactory> invoiceFormatterTracker;
49+
50+
public HelloWorldListener(final OSGIKillbillAPI killbillAPI, final ServiceTracker<InvoiceFormatterFactory, InvoiceFormatterFactory> invoiceFormatterTracker) {
3951
this.osgiKillbillAPI = killbillAPI;
52+
this.invoiceFormatterTracker = invoiceFormatterTracker;
4053
}
4154

55+
private static final String defaultLocale = "en_US";
56+
4257
@Override
4358
public void handleKillbillEvent(final ExtBusEvent killbillEvent) {
4459
logger.info("Received event {} for object id {} of type {}",
@@ -60,7 +75,29 @@ public void handleKillbillEvent(final ExtBusEvent killbillEvent) {
6075
logger.warn("Unable to find account", e);
6176
}
6277
break;
78+
case INVOICE_CREATION:
79+
80+
final Account account;
81+
try {
82+
account = osgiKillbillAPI.getAccountUserApi().getAccountById(killbillEvent.getAccountId(), context);
83+
} catch (AccountApiException e) {
84+
throw new RuntimeException(e);
85+
}
86+
final List<Invoice> invoices = osgiKillbillAPI.getInvoiceUserApi().getInvoicesByAccount(killbillEvent.getAccountId(), false, false, true, context);
87+
logger.info("Invoices in hello-world-plugin {}: ",invoices.size());
88+
final InvoiceFormatterFactory formatterFactory = (invoiceFormatterTracker != null ? invoiceFormatterTracker.getService() : null);
89+
Invoice invoice = invoices.get(0); //For demo purpose, we are only retrieving the formattedEndDate for the first invoice
90+
//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
6391

92+
InvoiceFormatter invoiceFormatter = formatterFactory.createInvoiceFormatter(defaultLocale, null, invoice, Locale.forLanguageTag(account.getLocale()), osgiKillbillAPI.getCurrencyConversionApi(), null, null);
93+
94+
List<InvoiceItem> items = invoiceFormatter.getInvoiceItems();
95+
logger.info("hello-world-plugin got items:{}",items.size());
96+
for(InvoiceItem item:items) {
97+
final InvoiceItemFormatter invoiceItemFormatter = (InvoiceItemFormatter)item;
98+
final String formattedEndDate = invoiceItemFormatter.getFormattedEndDate();
99+
logger.info("hello-world-plugin formattedEndDate:{}",formattedEndDate);
100+
}
64101
// Nothing
65102
default:
66103
break;

0 commit comments

Comments
 (0)