Using the BitPay Java light client
This library provides a convenient abstraction of BitPay's cryptographically-secure API and allows payment gateway developers to focus on payment flow/e-commerce integration rather than on the specific details of client-server interaction using the API. This library optionally provides the flexibility for developers to have control over important details, including the handling of private tokens needed for client-server communication.
Dependencies
You must have a BitPay merchant account to use this library. It's free to sign-up for a BitPay merchant account.
If you need a test account, please visit https://test.bitpay.com/dashboard/signup and register for a BitPay merchant test account. Please fill in all questions, so you get a fully working test account. If you are looking for a testnet bitcoin wallet to test with, please visit https://bitpay.com/wallet and create a new wallet. If you need testnet bitcoin please visit a testnet faucet, e.g. https://testnet.coinfaucet.eu/en/ or http://tpfaucet.appspot.com/
For more information about testing, please see https://bitpay.com/docs/testing
Usage
This library was built and tested using the Intellij IDE; the source code tree is directly compatible with Other Java IDEs. You can also look ar the full JavaDoc for reference here
Getting your client token
First of all, you need to generate a new POS token on your BitPay's account which will be required to securely connect to the BitPay's API.
For testing purposes use:
https://test.bitpay.com/dashboard/merchant/api-tokens
For production use:
https://bitpay.com/dashboard/merchant/api-tokens
Click on 'Add New Token', give a name on the Token Label input, leave the 'Require Authentication' checkbox unchecked and click on 'Add Token'. The new token will appear and ready to use.
Initializing your BitPay client
Once you have the token, you can initialize the client for the desired environment:
// Testing
import com.bitpay.sdk_light.Client;
import com.bitpay.sdk_light.Env;
Client bitpay = new Client("H78Yiu78uh78Gjht6g67gjh6767ghj", Env.Test);
// Production [The environment is selected by default]
import com.bitpay.sdk_light.Client;
Client bitpay = new Client("uh78Gjht6g67gjH78Yiu78h6767ghj");
Create an invoice
Invoice invoice = bitpay.createInvoice(new Invoice(100.0, "USD"));
String invoiceUrl = invoice.Url;
String status = invoice.Status;
Create an invoice (extended)
You can add optional attributes to the invoice. Atributes that are not set are ignored or given default values.
Buyer buyerData = new Buyer();
buyerData.setName("Satoshi");
buyerData.setAddress1("street");
buyerData.setAddress2("911");
buyerData.setLocality("Washington");
buyerData.setRegion("District of Columbia");
buyerData.setPostalCode("20000");
buyerData.setCountry("USA");
buyerData.setEmail("satoshi@buyeremaildomain.com");
buyerData.setNotify(true);
Invoice invoice = new Invoice(100.0, Currency.USD)
invoice.setBuyer(buyerData);
invoice = bitpay.createInvoice(invoice);
Retreive an invoice
Invoice invoice = bitpay.getInvoice(invoice.getId());
Get exchange rates
You can retrieve BitPay's BBB exchange rates.
Rates rates = bitpay.getRates();
Double rate = rates.getRate("USD");
rates.update();
Create a bill
// Create a list of items to add in the bill
List<Item> items = new ArrayList<>();
items.add(new Item() {{
setPrice(30.0);
setQuantity(9);
setDescription("product-a");
}});
items.add(new Item() {{
setPrice(14.0);
setQuantity(16);
setDescription("product-b");
}});
Bill bill = new Bill("1111", Currency.USD, "email@merchantsemail.com", items);
bill = bitpay.createBill(bill);
Retreive a bill
Bill bill = bitpay.getBill(bill.Id);
Deliver a bill
String deliveryResult = bitpay.deliverBill(bill.getId(), bill.getToken());
See also the tests project for more examples of API calls.
Comments
0 comments
Please sign in to leave a comment.