There are two key factors that allow jBilling to work as a rating engine. The first is that you can add pricing rules without changing code, it is very simple to add a “50% off in October” rule because jBilling uses JBoss rules to do pricing. The second one is the jBilling API that allows another system to ask jBilling at any time the price of a product, or group of them.
Let’s go over the various rating scenarios, and identify the method from the jBilling API that would help …
api.getItem(Integer itemId, Integer userId, PricingFields[])
This will return an item that is priced. You can pass only the item ID, this is fine if the customer is not important for resolve the price. For example, a price rule that is ‘50% off for October’ will work with just the item ID, but if you want to do ‘50% for Peter’ then the user ID is needed. Pricing fields will also allow you to pass arbitrary data that will be made available to the rules engine to build conditions.
api.rateOrder(OrderWS order)
An order can have multiple lines, and each line has a quantity field. jBilling will return this order with the changes done by the rating plug-ins. If you pass one line with a quantity of 3, the system could change the price to the equivalent of 2 units, implementing then the “buy two, get the third one free”.
This type of call is useful too for calculating taxes. jBilling will call a tax engine (or resolve the taxes on its own through rules) and add these taxes as new lines in the returned order.
The billing system needs to first resolve what this even means: in the example a call can be a call to a voice mail (price zero) a local call (maybe price zero, but if is a mobile phone things can change), a long distance call or an international call (prize definitely not zero). This product resolution and pricing can get complicated fast.
What we need here is real-time mediation. This is covered in detail in the telecom and integration guides. The main API method you need to work with is ‘Validate Purchase’. It will tell you how many of whatever the event translates to the customer can buy, given their current balance or credit limits. For this, the system will mediate the event first, by determining which product or products are being bought with this event, and then rate the products.