Search code examples
javapaymentbilling

jBilling: How to create payment programmatically?


How can I create payment in jBilling programmatically from scheduled plugin? The problem is I want to create a payment which is not linked to any invoice, so I try to use

applyPayment(PaymentDTOEx payment, Integer invoiceId, Integer executorUserId)

with invoiceId=null, but it leads to an error:

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

Initially I try:

IPaymentSessionBean psb = Context.getBean(Context.Name.PAYMENT_SESSION);
psb.applyPayment(new PaymentDTOEx(paymentWS), null, userID);

Later I added "userbl.webServicesAuthenticate(user, pass)" before, but result is the same.

I think I missed something important and maybe doing it completely wrong.


Solution

  • I've never used jBilling before, but after a bit of googling it seems like it uses Spring to manage transactions etc.

    From the error you've quoted it looks like you don't have a Hibernate session open. If you were using a web framework (like Spring MVC, for example), the Hibernate session lifecycle is usually managed for you transparently using a servlet filter.

    If you're executing a payment from a scheduled service, then you may need to open and close the Hibernate session yourself in your service. There's some documentation here that describes how to do this programmatically using Spring.

    Also take a look at the @Transactional annotation. It might be as simple as annotating your scheduled job method with this.