Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
149 changes: 68 additions & 81 deletions GUIDE.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
## Using BitPay Java Client Library
## Using the BitPay Java client

This SDK provides a convenient abstraction of BitPay's [cryptographically-secure API](https://bitpay.com/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 SDK optionally provides the flexibility for developers to have control over important details, including the handling of private keys needed for client-server communication.

This SDK implements BitPay's remote client authentication and authorization strategy. No private or shared-secret information is ever transmitted over the wire.
It also implements BitPay's remote client authentication and authorization strategy. No private or shared-secret information is ever transmitted over the wire.

### Dependencies

Expand All @@ -15,126 +15,113 @@ If you need testnet bitcoin please visit a testnet faucet, e.g. https://testnet.

For more information about testing, please see https://bitpay.com/docs/testing

### Handling your client private key

### Usage
Each client paired with the BitPay server requires a ECDSA key. This key provides the security mechanism for all client interaction with the BitPay server. The public key is used to derive the specific client identity that is displayed on your BitPay dashboard. The public key is also used for securely signing all API requests from the client. See the [BitPay API](https://bitpay.com/api) for more information.

This library was built and tested using the Eclipse IDE; the source code tree is directly compatible with Eclipse.
Library dependencies can be downloaded by executing the following command at the root of the library:

```
mvn clean dependency:copy-dependencies -DoutputDirectory=./lib
```
The private key should be stored in the client environment such that it cannot be compromised. If your private key is compromised you should revoke the compromised client identity from the BitPay server and re-pair your client, see the [API tokens](https://bitpay.com/api-tokens) for more information.

### Handling your client private key
On a Windows machine:

Each client paired with the BitPay server requires a public and private key. This provides the security mechanism for all client interaction with the BitPay server. The public key is used to derive the specific client identity that is displayed on your BitPay dashboard. The public key is also used for securely signing all API requests from the client. See the [BitPay API](https://bitpay.com/api) for more information.
The [BitPay.Net Setup utility](https://github.com/bitpay/csharp-bitpay-client/releases/download/v2.0.1904/BitPay.Net_Setup_utility.zip) helps to generate the private key, as well as a environment file formatted in JSON which contains all configuration requirements, that should be stored in the client local file system. It is not recommended to transmit the private key over any public or unsecure networks.

The private key should be stored in the client environment such that it cannot be compromised. If your private key is compromised you should revoke the compromised client identity from the BitPay server and re-pair your client, see the [API tokens](https://bitpay.com/api-tokens) for more information.
Follow the guide [BitPay.Net Setup utility guide](https://github.com/bitpay/csharp-bitpay-client/blob/master/BitPaySetup/README.md) that assist you to create the environment file which you will be able to modify it, either manually or by using the BitPay.Net Setup utility, later on by asking you to provide the path to your existing JSON file.

This SDK provides the capability of internally storing the private key on the client local file system. If the local file system is secure then this is a good option. It is also possible to generate the key yourself (using the SDK) and store the key as required. It is not recommended to transmit the private key over any public or unsecured networks.
The environment file can be either generated by the BitPay.Net Setup utility or created manually by copying the following Json structure:

```java
// Let the SDK store the private key on the clients local file system.
BitPay bitpay = new BitPay();
```json
{
"BitPayConfiguration": {
"Environment": "",
"EnvConfig": {
"Test": {
"PrivateKeyPath": "",
"ApiTokens": {
"pos": "",
"merchant": "",
"payroll": ""
}
},
"Prod": {
"PrivateKeyPath": "",
"ApiTokens": {
"pos": "",
"merchant": "",
"payroll": ""
}
}
}
}
}
```

```java
// Create the private key using the SDK, store it as required, and inject the private key into the SDK.
ECKey key = KeyUtils.createEcKey();
this.bitpay = new BitPay(key);
```

```java
// Create the private key external to the SDK, store it in a file, and inject the private key into the SDK.
String privateKey = KeyUtils.getKeyStringFromFile(privateKeyFile);
ECKey key = KeyUtils.createEcKeyFromHexString(privateKey);
this.bitpay = new BitPay(key);
```
### Usage

### Pair your client with BitPay
This library was built and tested using the Intellij IDE; the source code tree is directly compatible with Other Java IDEs.
Library dependencies can be downloaded by executing the following command at the root of the library:

Your client must be paired with the BitPay server. The pairing initializes authentication and authorization for your client to communicate with BitPay for your specific merchant account. There are two pairing modes available; client initiated and server initiated.
You can also look ar the full JavaDoc for reference [here](http://htmlpreview.github.io/?https://github.com/bitpay/java-bitpay-client/blob/master/doc/index.html)

#### Client initiated pairing
### Initializing your BitPay client

Pairing is accomplished by having your client request a pairing code from the BitPay server. The pairing code is then entered into the BitPay merchant dashboard for the desired merchant. Your interactive authentication at https://bitpay.com/login provides the authentication needed to create finalize the client-server pairing request.
Once you have the environment file (JSON previously generated) you can initialize the client on two different ways:

```java
String clientName = "server 1";
BitPay bitpay = new BitPay(clientName);

if (!bitpay.clientIsAuthorized(BitPay.FACADE_POS))
{
// Get POS facade authorization code.
String pairingCode = bitpay.requestClientAuthorization(BitPay.FACADE_POS);

// Signal the device operator that this client needs to be paired with a merchant account.
System.out.print("Info: Pair this client with your merchant account using the pairing code: " + pairingCode);
throw new BitPayException("Error: client is not authorized for POS facade.");
}
```Java
// Provide the full path to the env file which you have previously stored securely.

Client bitpay = new Client("[FULL_PATH_TO_THE_PRIVATE_KEY]");
```

#### Server initiated pairing
```Java
// Initialize with separate variables.

Client bitpay = new Client(
Env.Test,
"[FULL_PATH_TO_THE_PRIVATE_KEY]",
new Env.Tokens(){{
pos = "AvJdGrEqTW9HVsJit9zabAnrJabqaQDhWHRacHYgfgxK";
merchant = "2smKkjA1ACPKWUGN7wUEEqdWi3rhXYhDX6AKgG4njKvj";
payroll = "9pJ7fzW1GGeuDQfj32aNATCDnyY6YAacVMcDrs7HHUNo";
}}
);
```

Pairing is accomplished by obtaining a pairing code from the BitPay server. The pairing code is then injected into your client (typically during client initialization/configuration). Your interactive authentication at https://bitpay.com/login provides the authentication needed to create finalize the client-server pairing request.
### Pair your client with BitPay

```java
// Obtain a pairingCode from your BitPay account administrator.
String pairingCode = "xxxxxxx";
String clientName = "server 1";
BitPay bitpay = new BitPay(clientName);
Your client must be paired with the BitPay server. The pairing initializes authentication and authorization for your client to communicate with BitPay for your specific merchant account.

// Is this client already authorized to use the POS facade?
if (!bitpay.clientIsAuthorized(BitPay.FACADE_POS))
{
// Get POS facade authorization.
bitpay.authorizeClient(pairingCode);
}
```
Pairing is accomplished by having the BitPay.Net Setup utility request a pairing code from the BitPay server.
Meanwhile a new pairing code is generated, the BitPay.Net Setup utility will ask you to activate it in your BitPay account. It will also store the paired token in the environment file.

The pairing code is then entered into the BitPay merchant dashboard for the desired merchant. Your interactive authentication at https://bitpay.com/login provides the authentication needed to create finalize the client-server pairing request.

### Create an invoice

```java
Invoice invoice = bitpay.createInvoice(100, "USD");
Invoice invoice = bitpay.createInvoice(new Invoice(50.0, "USD"));

String invoiceUrl = invoice.getURL();

String status = invoice.getStatus();
```

### Create an invoice (extended)

You can add optional attributes to the invoice. Attributes that are not set are ignored or given default values. For example:

```java
InvoiceBuyer buyer = new InvoiceBuyer();
buyer.setName("Satoshi");
buyer.setEmail("satoshi@emaildomainyourwebsite.com");

Invoice invoice = new Invoice(100.0, "USD");
invoice.setBuyer(buyer);
invoice.setFullNotifications(true);
invoice.setNotificationEmail("yourmerchant@emaildomainyourwebsite.com");
invoice.setPosData("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");

invoice = this.bitpay.createInvoice(invoice);
```

### Retreive an invoice
### Retrieve an invoice

```java
invoice = bitpay.getInvoice(invoice.getId());
Invoice invoice = bitpay.getInvoice(invoice.getId());
```

### Get exchange Rates

You can retrieve BitPay's [BBB exchange rates](https://bitpay.com/bitcoin-exchange-rates):
You can retrieve BitPay's [BBB exchange rates](https://bitpay.com/exchange-rates).

```java
Rates rates = this.bitpay.getRates();
Rates rates = bitpay.getRates();

double rate = rates.getRate("USD");

rates.update();
```
See also the test package for more examples of API calls.

64 changes: 9 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,74 +1,28 @@
java-bitpay-client
==================
<img src="https://bitpay.com/_nuxt/img/1c0494b.svg" width="150">

This is the Java client library for the BitPay Payment Gateway. This library implements BitPay's [Cryptographically Secure RESTful API](https://bitpay.com/api).
# BitPay Java client
[![License](https://img.shields.io/github/license/bitpay/java-bitpay-client.svg?style=for-the-badge&logo=github)](https://raw.githubusercontent.com/bitpay/java-bitpay-client/master/LICENSE)

## Quick Start Guide
Full implementation of the BitPay Payment Gateway. This library implements BitPay's [Cryptographically Secure RESTful API](https://bitpay.com/api).

To get up and running with our Java library quickly, see the GUIDE here: https://github.com/bitpay/java-bitpay-client/blob/master/GUIDE.md
## Getting Started

## Eclipse Project Setup

1. Import the project from git repository.

```
From Project Explorer > Import > Projects from Git ...
```

2. Convert project to a Java project - locate and edit the .project file in your Eclipse workspace directory to include the following.

```xml
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
```

3. Download project dependencies using maven.

```
cd <root directory of project>
mvn dependency:copy-dependencies -DoutputDirectory=lib
```

4. Add dependencies - add external JAR files downloaded by maven to the Eclipse project.

```
Project > Properties > Java Build Path > Libraries > Add External JARs > (choose all JARs in lib directory)
```

5. Add JUnit Library to the project.

```
Project > Properties > Java Build Path > Libraries > Add Library > Unit > Unit 4
```

6. Run tests.

```
src/test/BitPayTest.java > Run As > JUnit Test
```
To get up and running with our C# library quickly, follow [The GUIDE](https://github.com/bitpay/java-bitpay-client/blob/master/GUIDE.md)

## Support

* https://github.com/bitpay/java-bitpay-client/issues
* https://help.bitpay.com/
* https://support.bitpay.com

## Contribute

To contribute to this project, please fork and submit a pull request.

## License

The MIT License (MIT)
MIT License

Copyright (c) 2014-2018 BitPay, Inc.
Copyright (c) 2019 BitPay

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
Loading