Add bitcoin payments to java app using BitcoinJ

Not really for BitcoinJ, but a general approach.

You generate new address for every user you register. Store it in your DB with the userID for future reference.

Create a getAPI taking a String input. use your API endpoint and open the Bitcoin.conf file.

walletnotify=curl <your API>

If you want to have confirmation logic as well, you may need one more getAPI, and get the endpoint. then update your bitcoin.conf file again with following:

blocknotify=curl <your Block API> 

Now when user makes the deposit to the address you generated, Bitcoin node will notify your app by calling the wallet notify API. You get the transaction hash. Use that txHash and make a Post call to your daemon with RPc command:

getTransaction <txHash>

Now you’ll have entire transaction object to find which user has made the deposit.

Free tip: Bitcoin node calls walletnotify two times:

  1. When a transaction is added to the chain.
  2. When that transaction receives first confirmation.

If you don’t want more than one confirmation, then blockNotify is not needed.

If you want a practical example, you can check my Github repo, where I have done it. It’s not based on BitcoinJ but the logic remains the same: