Welcome to bytebang » The blog about all and nothing » Implement cryptocoin payments with Java

Implement cryptocoin payments with Java

Jul 25 2017

The Problem

If you run a web shop or any other service where your users have to pay for services or goods the you will be confronted with the fact that you need a billing system. Of course you could use PayPal or other services but usually these services will cost you something.

If your goods or services are very cheap, then it is often the case that the payment provider fees make up a huge fraction of the overall price. Such small payments can also be fulfilled with crypto currencies like Bitcoin, Decred, Ethereum and so on. (If you have no idea what crypto currencies are all about then take a look at this introduction to bitcoin)


It would be wrong to say that there are no fees - because in the most crypto currencies the miners will demand a small transaction fee, but this fee is (compared to other services) relatively cheap. One exception thereof is IOTA - but this will be content of another blog article.

It is technically not a big problem to implement payments for one currency but if you plan to accept multiple different ones then things start to become complicated.

The Solution

The fact that it is complicated to accept multiple crypto-currencies is the reason why there are some cryptocoin payment gateways which have a unified API that are abstracting the technical quirks from you. One of them is coinpayments.net.

It is a simple and straight forward service which allows its users to accept multiple currencies, to store the eraned coins at internal wallets, to cash it out as fiat money and so on. The API is straight forward and easy to use and there are plugins for popular shopping carts but until now there was no implementation for the Java programming language.

This is why we decided to write a Java wrapper for the coinpayments.nat API and to share it under the GPLv3 License. It ts pretty straight forward to use - lets have a look at an example which instantiates the API and sells a Sample Item which is worth 314.15 Euros. The accepted crypto currency is BTC (Bitcoin).

If you want to run this example on your machine then be sure that you have created an API keypair, that you have allowed the execution of the corresponding API calls and that you have allowed that Bitcoin is accepted.
// Set up the API
CoinPaymentsAPI api = new CoinPaymentsAPI("YOUR_PUBLIC_KEY", "YOUR_PRIVATE_KEY");

// Create the transaction
JsonObject tx = api.set("amount",  314.15)
  .set("currency1", "EUR")
  .set("currency2", "BTC")
  .set("item_name", "Sample Item")
  .set("item_number", "007-4711-0815")

// Read out the transaction id
String myTransactionId = tx.get("txn_id").getAsString();

In the last line you get a transaction id which can be used to query the payment status:

// Create the transaction
JsonObject txinfo = api.set("txid", CpnetTests.transactionId).call("get_tx_info");

// Query the payment status
float amount = txinfo.get("amountf").getAsFloat();
float received = txinfo.get("receivedf").getAsFloat();
float remeaning = amount - received;

// Generate a status message
String info = "We have received " + received + " of " + amount + " Bitcoins. Please transfer the reameaning " + remeaning + " coins within the next few minutes to the address " + txinfo.get("payment_address").getAsString();

// And print it

You see - it can be that simple. If you like it then please consider to donate some Bitcoins (see the qrcode and link at the right side of the page).

Happy Coding

Get Social

(c) 2023, by bytebang e.U. - Impressum - Datenschutz / Nutzungsbedingungen