bitpay-client 2.4.0 → 2.5.1905
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +5 -1
- data/.travis.yml +6 -1
- data/CHANGELOG.md +24 -0
- data/GUIDE.md +233 -0
- data/Gemfile +0 -4
- data/LICENSE.md +2 -2
- data/README.md +8 -79
- data/Rakefile +57 -2
- data/bitpay-client.gemspec +15 -16
- data/config/constants.rb +12 -1
- data/features/creating_invoices.feature +28 -0
- data/features/pairing.feature +19 -0
- data/features/refunds.feature +23 -0
- data/features/retrieving_invoices.feature +11 -0
- data/features/step_definitions/invoice_steps.rb +31 -0
- data/features/step_definitions/keygen_steps.rb +56 -0
- data/features/step_definitions/refund_steps.rb +37 -0
- data/features/step_definitions/step_helpers.rb +31 -0
- data/lib/bitpay/cacert.pem +1295 -1760
- data/lib/bitpay/client.rb +175 -0
- data/lib/bitpay/rest_connector.rb +106 -0
- data/lib/bitpay/version.rb +2 -2
- data/lib/bitpay_sdk.rb +28 -0
- data/spec/client_spec.rb +153 -6
- data/spec/fixtures/invoices-POST.json +29 -0
- data/spec/fixtures/invoices_{id}-GET.json +35 -0
- data/spec/fixtures/invoices_{id}_refunds-GET.json +17 -0
- data/spec/fixtures/invoices_{id}_refunds-POST.json +9 -0
- data/spec/fixtures/invoices_{id}_refunds_{refund_id}-GET.json +11 -0
- data/spec/fixtures/response-nodata.json +10 -0
- data/spec/spec_helper.rb +16 -18
- metadata +77 -82
- data/bin/bitpay +0 -3
- data/lib/bitpay.rb +0 -21
- data/lib/bitpay/cli.rb +0 -64
- data/lib/bitpay/cli_client.rb +0 -24
- data/lib/bitpay/cli_key_utils.rb +0 -40
- data/spec/features/pair_spec.rb +0 -25
- data/spec/key_utils_spec.rb +0 -26
- data/spec/set_constants.sh +0 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ec9264c9adfd34beec539276916e9c6edc02f522e479c937e21fd1ae65f0bbb8
|
4
|
+
data.tar.gz: 91398067b63bde27f49a30a329d3ec4f653daa1981c255933be3a376f386634f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ef6c129c0d8a95221a8385301ce27928388ee6561afdbbef61efbea969be92b5e316f7dea8eefee4e1d15f7d0811d38dccf7503f9e34d32a6cc525e49a128b40
|
7
|
+
data.tar.gz: 36d60973b7f38b87ac15187fc5c82686ceba5f82577beafbaba66e4053ba9d23545063121e359cafb570efcd2384a9612573cf0289efc5582159204380261a9d
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# Change Log
|
2
|
+
All notable changes to this project will be documented in this file.
|
3
|
+
This project adheres to [Semantic Versioning](http://semver.org/).
|
4
|
+
|
5
|
+
## [2.4.4] - 2015-04-14
|
6
|
+
### Changed
|
7
|
+
- Separated key utilities into its own Gem
|
8
|
+
|
9
|
+
## [2.4.3] - 2015-04-13
|
10
|
+
### Changed
|
11
|
+
- Loosened production gem requirements from patch level to major level
|
12
|
+
|
13
|
+
## [2.4.2] - 2015-03-11
|
14
|
+
### Fixed
|
15
|
+
- GitHub issue 39: handling post paths that include a ? and require a token. A workaround exists for this issue.
|
16
|
+
|
17
|
+
## [2.4.1] - 2015-03-11
|
18
|
+
### Fixed
|
19
|
+
- GitHub issue 40: error for endpoints that did not return a 'data' field
|
20
|
+
|
21
|
+
## [2.4.0] - 2015-03-05
|
22
|
+
### Changed
|
23
|
+
- Add feature: Accept refunds
|
24
|
+
- Fix Bug: Accept bitcoin payments like 0.003
|
data/GUIDE.md
ADDED
@@ -0,0 +1,233 @@
|
|
1
|
+
# Using the BitPay Ruby Client Library
|
2
|
+
## Prerequisites
|
3
|
+
You must have a BitPay merchant account to use this library. It's free to [sign-up for a BitPay merchant account](https://bitpay.com/start).
|
4
|
+
|
5
|
+
Once you have a BitPay merchant account, you will need [a working BitPay Access Token](/api/getting-access.html) – this can be done either [via the library](#pairing) or manually in [the BitPay Dashboard](https://bitpay.com/tokens).
|
6
|
+
|
7
|
+
## Quick Start
|
8
|
+
### Installation
|
9
|
+
```bash
|
10
|
+
gem install bitpay-sdk
|
11
|
+
```
|
12
|
+
|
13
|
+
In your Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'bitpay-sdk', :require => 'bitpay_sdk'
|
17
|
+
```
|
18
|
+
|
19
|
+
Or directly:
|
20
|
+
```ruby
|
21
|
+
require 'bitpay_sdk'
|
22
|
+
```
|
23
|
+
|
24
|
+
### Configuration
|
25
|
+
The bitpay client creates a cryptographically secure connection to your server by pairing an API code with keys generated by the library. The client can be initialized with pre-existing keys passed in as a pem file, or paired if initialized with a pem file and a tokens hash. Examples can be found in the cucumber step helpers.
|
26
|
+
|
27
|
+
## Pairing
|
28
|
+
Most calls to the BitPay REST API require that your client is paired with the bitpay.com server. To pair with bitpay.com you need to have an approved merchant account.
|
29
|
+
|
30
|
+
Your client can be paired via the `pos` (point-of-sale) or `merchant` facade (or both). The `pos` facade allows for invoices to be created. The `merchant` facade has broader privileges to view all invoices, bills, and ledger entries, as well as to issue refunds. Consider the level of access required when you pair your client.
|
31
|
+
|
32
|
+
### A quick note on keys
|
33
|
+
|
34
|
+
The BitPay client gem includes the BitPay KeyUtilities gem, which can be used to generate new public private key pairs which it returns in PEM format. However, there are no methods which save the keys anywhere, so it is your responsibility to store the PEM file somewhere secure.
|
35
|
+
|
36
|
+
### BitPay authentication
|
37
|
+
|
38
|
+
BitPay authentication depends on four parts:
|
39
|
+
|
40
|
+
1. An account on our servers.
|
41
|
+
1. A token shared between the client and the server.
|
42
|
+
1. A public key, shared between the client and the server.
|
43
|
+
1. A private key, held exclusively by the client.
|
44
|
+
|
45
|
+
In order to complete authentication, you have to associate your private key with a token, and associate that token with an account. Once this authentication is complete, as long as you have the private key, you never have to authenticate again. The token you created will always be associated with that private key, so any time you create a new bitpay client object with that key, it is authenticated with BitPay. This is true whether you use the ruby-client, python client, or no client at all, the key is the important thing.
|
46
|
+
|
47
|
+
There are two ways to authenticate, from the client side or the server side. The Ruby Client supports both.
|
48
|
+
|
49
|
+
To pair from the server side, you log in to the BitPay server, navigate to dashboard/merchant/api-tokens, and create a new token. This creates a new token, which is associated with your account. It is not associated with a key, so we provide a pairing code that you can use as a one time secret to associate the token with a key. From the client side, you can use the client.pair_pos_client(<pairing_code>) method to associate that method with a key held by the client.
|
50
|
+
|
51
|
+
To pair from the client side, you use the client to call the /tokens endpoint on the server with no parameters. This creates a token on the server and associates that token with a public key. What it doesn't do is associate that token to an account (because we don't know what account to associate with). This call returns a pairing code, which is a one time secret that allows you to find the token you just created. In order to associate the token with an account, you log in to the BitPay server, and use the dashboard/merchant/api-tokens interface to associate the token with a specific account. And example of client side pairing is shown below.
|
52
|
+
|
53
|
+
### Pairing Programatically
|
54
|
+
|
55
|
+
If you are developing a client with built-in pairing capability, you can pair programattically using the `pair_client` method. This method can be called in two ways:
|
56
|
+
|
57
|
+
* `pair_client()` will perform a client-initiated pairing, and will provide a pairing code that can be entered at https://bitpay.com/dashboard/merchant/api-tokens to assign either `merchant` or `pos` facade.
|
58
|
+
* `pair_client('pairing_code')` will complete a server-initiated pairing, when provided a pre-generated pairing code from https://bitpay.com/dashboard/merchant/api-tokens. In this case, the `pos` facade will be automatically assigned.
|
59
|
+
|
60
|
+
This is an example of creating a paired client with the BitPay toolset.
|
61
|
+
|
62
|
+
```bash
|
63
|
+
$ gem install bitpay-sdk
|
64
|
+
Successfully installed bitpay-sdk-2.2.0
|
65
|
+
1 gem installed
|
66
|
+
$ irb
|
67
|
+
2.1.1 :001 > require 'bitpay_sdk'
|
68
|
+
=> true
|
69
|
+
2.1.2 :002 > pem = BitPay::KeyUtils.generate_pem
|
70
|
+
=> "-----BEGIN EC PRIVATE KEY-----\nMHQCAQEEIH8oSTRm8lVMTVOsDZleIB8AmkiuHnp+ctEknqeUmZahoAcGBSuBBAAK\noUQDQgAEbjhdKA+X8NEKgcbHhyJaBMvePV7Sj6AQuOMQzuZYdskdkPY1/jlfQwNG\n4GVd/zSw4uhfukw/SDBOEKlQGVAmxQ==\n-----END EC PRIVATE KEY-----\n"
|
71
|
+
2.1.1 :002 > client = BitPay::SDK::Client.new(api_uri: 'https://test.bitpay.com', pem: pem)
|
72
|
+
=> #<BitPay::SDK::Client:0x000000019c6d40 @pem="---... @tokens={}>
|
73
|
+
2.1.1 :003 > client.pair_client()
|
74
|
+
=> {"data"=>[{"policies"=>[{"policy"=>"id", "method"=>"inactive", "params"=>["Tf49SFeiUAtytFEW2EUqZgWj32nP51PK73M"]}], "token"=>"BKQyVdaGQZAArdkkSuvtZN5gcN2355c8vXLj5eFPkfuK", "dateCreated"=>1422474475162, "pairingExpiration"=>1422560875162, "pairingCode"=>"Vy76yTh"}]}
|
75
|
+
```
|
76
|
+
|
77
|
+
As described above, using the value from the `pairingCode` element, visit https://test.bitpay.com/api-tokens and search to register for the appropriate facade. That client is now paired. As previously mentioned, you must save the pem string you generated in order to use the client again.
|
78
|
+
|
79
|
+
## General Usage
|
80
|
+
|
81
|
+
### Initialize the client
|
82
|
+
|
83
|
+
```ruby
|
84
|
+
client = BitPay::SDK::Client.new(pem: File.read('bitpaykey.pem'))
|
85
|
+
```
|
86
|
+
|
87
|
+
Optional parameters:
|
88
|
+
* `api_uri` - specify a different api endpoint (e.g. 'https://test.bitpay.com'). Ensure no trailing slash.
|
89
|
+
* `tokens` - pass a stored hash of bitpay API tokens
|
90
|
+
* `user-agent` - specify a custom user-agent value
|
91
|
+
* `debug: true` - enable HTTP request logging to $stdout
|
92
|
+
* `insecure: true` - disable HTTPs certificate validation (for local test environments)
|
93
|
+
|
94
|
+
### Create a new bitcoin invoice
|
95
|
+
|
96
|
+
```ruby
|
97
|
+
invoice = client.create_invoice(price: <price>, currency: <currency>)
|
98
|
+
```
|
99
|
+
|
100
|
+
With invoice creation, `price` and `currency` are the only required fields. If you are sending a customer from your website to make a purchase, setting `redirectURL` will redirect the customer to your website when the invoice is paid.
|
101
|
+
|
102
|
+
Response will be a hash with information on your newly created invoice. Send your customer to the `url` to complete payment:
|
103
|
+
|
104
|
+
```javascript
|
105
|
+
{
|
106
|
+
"url": "https://bitpay.com/invoice?id=NKaqMuZWy3BAcP77RdkEEv",
|
107
|
+
"paymentUrls": {
|
108
|
+
"BIP21": "bitcoin:mvYRECDxKPaPHnjNz9ZxiTpbx29xYNoRy4?amount=0.3745",
|
109
|
+
"BIP72": "bitcoin:mvYRECDxKPaPHnjNz9ZxiTpbx29xYNoRy4?amount=0.3745&r=https://bitpay.com/i/NKaqMuZWy3BAcP77RdkEEv",
|
110
|
+
"BIP72b": "bitcoin:?r=https://bitpay.com/i/NKaqMuZWy3BAcP77RdkEEv",
|
111
|
+
"BIP73": "https://bitpay.com/i/NKaqMuZWy3BAcP77RdkEEv"
|
112
|
+
},
|
113
|
+
"status": "new",
|
114
|
+
"btcPrice": "0.3745",
|
115
|
+
"btcDue": "0.3745",
|
116
|
+
"price": 148,
|
117
|
+
"currency": "USD",
|
118
|
+
"exRates": {
|
119
|
+
"USD": 395.20000000000005
|
120
|
+
},
|
121
|
+
"invoiceTime": 1415987168612,
|
122
|
+
"expirationTime": 1415988068612,
|
123
|
+
"currentTime": 1415987168629,
|
124
|
+
"guid": "438e8237-fff1-483c-81b4-dc7dba28922a",
|
125
|
+
"id": "NKaqMuZWy3BAcP77RdkEEv",
|
126
|
+
"transactions": [
|
127
|
+
|
128
|
+
],
|
129
|
+
"btcPaid": "0.0000",
|
130
|
+
"rate": 395.2,
|
131
|
+
"exceptionStatus": false,
|
132
|
+
"token": "9kZgUXFb5AC6qMuLaMpP9WopbM8X2UjMhkphKKdaprRbSKgUJNE6JNTX8bGsmgxKKv",
|
133
|
+
"buyer": {
|
134
|
+
}
|
135
|
+
}
|
136
|
+
```
|
137
|
+
|
138
|
+
There are many options available when creating invoices, which are listed in the [BitPay API documentation](https://bitpay.com/bitcoin-payment-gateway-api).
|
139
|
+
|
140
|
+
### Get invoice status
|
141
|
+
The ruby library provides two methods for fetching an existing invoice:
|
142
|
+
|
143
|
+
```ruby
|
144
|
+
# For authorized clients with a 'merchant' token
|
145
|
+
client.get_invoice(id: 'PvVhgBfA7wKPWhuVC24rJo')
|
146
|
+
|
147
|
+
# For non-authenticated clients (public facade)
|
148
|
+
# Returns the public subset of invoice fields
|
149
|
+
client.get_public_invoice(id: 'PvVhgBfA7wKPWhuVC24rJo')
|
150
|
+
```
|
151
|
+
|
152
|
+
### Create a refund request
|
153
|
+
|
154
|
+
Clients with a `merchant` token can initiate a refund request for a paid invoice:
|
155
|
+
|
156
|
+
```ruby
|
157
|
+
client.refund_invoice(id: '6pbV13VBZfGFJ8BBmXmLZ8', params: {amount: 10, currency: 'USD'})
|
158
|
+
```
|
159
|
+
|
160
|
+
Refund rules:
|
161
|
+
|
162
|
+
* Invoices cannot be refunded prior to 6 blockchain confirmations
|
163
|
+
* Invoices without `["flags"]["refundable"] == true` must specify a `bitcoinAddress` param (one was not provided as part of the transaction)
|
164
|
+
* Invoices that are paid in full must specify an `amount` and `currency` param to indicate the amount to be refunded
|
165
|
+
|
166
|
+
### View Refund Requests
|
167
|
+
|
168
|
+
The ruby library provides two methods for viewing refund requests. Both require a `merchant` token.
|
169
|
+
|
170
|
+
```ruby
|
171
|
+
# To get an array of all refunds against a specific invoice
|
172
|
+
client.get_all_refunds_for_invoice(id: 'PvVhgBfA7wKPWhuVC24rJo')
|
173
|
+
|
174
|
+
# To get a specific refund for a specific invoice
|
175
|
+
client.get_refund(id: 'JB49z2MsDH7FunczeyDS8j', request_id: '4evCrXq4EDXk4oqDXdWQhX')
|
176
|
+
```
|
177
|
+
### Cancel Refund Requests
|
178
|
+
|
179
|
+
Requires a `merchant` token.
|
180
|
+
|
181
|
+
```ruby
|
182
|
+
client.cancel_refund(id: 'JB49z2MsDH7FunczeyDS8j', request_id: '4evCrXq4EDXk4oqDXdWQhX')
|
183
|
+
```
|
184
|
+
|
185
|
+
### Make a HTTP request directly against the REST API
|
186
|
+
|
187
|
+
For API tasks which lack a dedicated library method, BitPay provides methods that will automatically apply the proper cryptographic parameters to a request.
|
188
|
+
|
189
|
+
```ruby
|
190
|
+
client.send_request("GET", "invoices/JB49z2MsDH7FunczeyDS8j", facade: 'merchant')
|
191
|
+
## This request is identical to:
|
192
|
+
token = client.get_token("merchant")
|
193
|
+
client.get(path: "invoices/JB49z2MsDH7FunczeyDS8j", token: token)
|
194
|
+
|
195
|
+
## post requests are also possible
|
196
|
+
token = client.get_token("merchant")
|
197
|
+
client.post(path: "tokens", token: token, params: {facade: "pos"}) #returns a new token with pairing code
|
198
|
+
## equivalent to
|
199
|
+
client.send_request("POST", "tokens", facade: 'merchant', params: {facade: 'pos'})
|
200
|
+
```
|
201
|
+
Usage:
|
202
|
+
* Specify HTTP verb and REST endpoint
|
203
|
+
* Specifying a `facade` will fetch and apply the corresponding `token`
|
204
|
+
* Alternatively provide a `token` explicitly
|
205
|
+
* For `POST` requests, the `params` hash will be included as the message body
|
206
|
+
|
207
|
+
## Testnet Usage
|
208
|
+
|
209
|
+
During development and testing, take advantage of the [Bitcoin TestNet](https://en.bitcoin.it/wiki/Testnet) by passing a custom `api_uri` option on initialization:
|
210
|
+
|
211
|
+
```ruby
|
212
|
+
BitPay::SDK::Client.new({api_uri: "https://test.bitpay.com/api"})
|
213
|
+
```
|
214
|
+
|
215
|
+
Note that in order to pair with testnet, you will need a pairing code from test.bitpay.com and will need to use the bitpay client with the --test option.
|
216
|
+
|
217
|
+
## API Documentation
|
218
|
+
|
219
|
+
API Documentation is available on the [BitPay site](https://bitpay.com/api).
|
220
|
+
|
221
|
+
## Running the Tests
|
222
|
+
|
223
|
+
In order to run the tests, you must have phantomjs installed and on your PATH.
|
224
|
+
|
225
|
+
The tests require that environment variables be set for the bitpay server, user name, password, an invoice id for refunds and a valid testnet bitcoin address for refunds. First run:
|
226
|
+
|
227
|
+
```bash
|
228
|
+
$ source ./spec/set_constants.sh https://test.bitpay.com <yourusername> <yourpassword> <a-confirmed-invoice-id> <a-valid-testnet-address>
|
229
|
+
$ bundle install
|
230
|
+
$ bundle exec rake
|
231
|
+
```
|
232
|
+
|
233
|
+
Tests are likely to run up against rate limiters on test.bitpay.com if used too frequently. Rake tasks which interact directly with BitPay will not run for the general public.
|
data/Gemfile
CHANGED
data/LICENSE.md
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
Copyright (C)
|
1
|
+
Copyright (C) 2014 BitPay
|
2
2
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
4
|
|
5
5
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
6
|
|
7
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,88 +1,17 @@
|
|
1
|
-
# BitPay Library for Ruby
|
1
|
+
# BitPay Library for Ruby
|
2
2
|
|
3
|
-
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://raw.githubusercontent.com/bitpay/ruby-
|
4
|
-
[![Travis](https://img.shields.io/travis/bitpay/ruby-
|
5
|
-
[![Gem](https://img.shields.io/gem/v/bitpay-
|
6
|
-
[![
|
7
|
-
[![Code Climate](https://img.shields.io/codeclimate/github/bitpay/ruby-
|
3
|
+
[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square)](https://raw.githubusercontent.com/bitpay/ruby-client/master/LICENSE.md)
|
4
|
+
[![Travis](https://img.shields.io/travis/bitpay/ruby-client.svg?style=flat-square)](https://travis-ci.org/bitpay/ruby-client)
|
5
|
+
[![Gem](https://img.shields.io/gem/v/bitpay-sdk.svg?style=flat-square)](https://rubygems.org/gems/bitpay-sdk)
|
6
|
+
[![Code Coverage](https://img.shields.io/coveralls/bitpay/ruby-client.svg?style=flat-square)](https://coveralls.io/r/bitpay/ruby-client?branch=master)
|
7
|
+
[![Code Climate](https://img.shields.io/codeclimate/github/bitpay/ruby-client.svg?style=flat-square)](https://codeclimate.com/github/bitpay/ruby-client)
|
8
8
|
|
9
9
|
Powerful, flexible, lightweight interface to the BitPay Bitcoin Payment Gateway API.
|
10
10
|
|
11
|
-
|
11
|
+
The `bitpay-sdk` gem provides all the programattic tools required to implement a ruby client application for the BitPay REST API.
|
12
12
|
|
13
|
-
|
13
|
+
## [Getting Started »](https://github.com/bitpay/ruby-client/blob/master/GUIDE.md)
|
14
14
|
|
15
|
-
In your Gemfile:
|
16
|
-
|
17
|
-
gem 'bitpay-client', :require => 'bitpay'
|
18
|
-
|
19
|
-
Or directly:
|
20
|
-
|
21
|
-
require 'bitpay'
|
22
|
-
|
23
|
-
## Configuration
|
24
|
-
|
25
|
-
The bitpay client creates a cryptographically secure connection to your server by pairing an API code with keys stored on your server. The library generates the keys as a .pem file, which is stored in `$HOME/.bitpay/bitpay.pem` or preferentially in an environment variable.
|
26
|
-
|
27
|
-
The client will generate a key when initialized if one does not already exist.
|
28
|
-
|
29
|
-
## Basic Usage
|
30
|
-
|
31
|
-
### Pairing with Bitpay.com
|
32
|
-
|
33
|
-
To pair with bitpay.com you need to have an approved merchant account.
|
34
|
-
1. Login to your account
|
35
|
-
2. Navigate to bitpay.com/api-tokens (Dashboard > My Account > API Tokens)
|
36
|
-
3. Copy an existing pairing code or create a new token and copy the pairing code.
|
37
|
-
4. Use the bitpay command line tool to pair with bitpay.com `bitpay pair <pairing_code>`
|
38
|
-
|
39
|
-
### To create an invoice with a paired client:
|
40
|
-
|
41
|
-
The command line utility will save a PEM file in the file $HOME/.bitpay/bitpay.pem. When creating new clients, this pem file is needed in order to have an authorized client.
|
42
|
-
|
43
|
-
client = BitPay::Client.new(pem: File.read("#{ENV['HOME']}/.bitpay/bitpay.pem"))
|
44
|
-
invoice = client.create_invoice (id: <id>, price: <price>, currency: <currency>, facade: <facade>)
|
45
|
-
|
46
|
-
With invoice creation, `price` and `currency` are the only required fields. If you are sending a customer from your website to make a purchase, setting `redirectURL` will redirect the customer to your website when the invoice is paid.
|
47
|
-
|
48
|
-
Response will be a hash with information on your newly created invoice. Send your customer to the `url` to complete payment:
|
49
|
-
|
50
|
-
{
|
51
|
-
"id" => "DGrAEmbsXe9bavBPMJ8kuk",
|
52
|
-
"url" => "https://bitpay.com/invoice?id=DGrAEmbsXe9bavBPMJ8kuk",
|
53
|
-
"status" => "new",
|
54
|
-
"btcPrice" => "0.0495",
|
55
|
-
"price" => 10,
|
56
|
-
"currency" => "USD",
|
57
|
-
"invoiceTime" => 1383265343674,
|
58
|
-
"expirationTime" => 1383266243674,
|
59
|
-
"currentTime" => 1383265957613
|
60
|
-
}
|
61
|
-
|
62
|
-
There are many options available when creating invoices, which are listed in the [BitPay API documentation](https://bitpay.com/bitcoin-payment-gateway-api).
|
63
|
-
|
64
|
-
To get updated information on this invoice, make a get call with the id returned:
|
65
|
-
|
66
|
-
invoice = client.get_public_invoice(DGrAEmbsXe9bavBPMJ8kuk)'
|
67
|
-
|
68
|
-
The BitPay CLI gem requires the BitPay SDK gem. See the [BitPay SDK Gem](https://github.com/bitpay/ruby-client/blob/master/GUIDE.md) documentation for more details on using the BitPay Client SDK.
|
69
|
-
|
70
|
-
## Testnet Usage
|
71
|
-
|
72
|
-
In order to pair with testnet, you will need a pairing code from test.bitpay.com and will need to use the bitpay client with the --test option.
|
73
|
-
|
74
|
-
## API Documentation
|
75
|
-
|
76
|
-
API Documentation is available on the [BitPay site](https://bitpay.com/api).
|
77
|
-
|
78
|
-
## RDoc/YARD Documentation
|
79
|
-
The code has been fully code documented, and the latest version is always available at the [Rubydoc Site](http://rubydoc.info/gems/bitpay-client).
|
80
|
-
|
81
|
-
## Running the Tests
|
82
|
-
|
83
|
-
$ bundle install
|
84
|
-
$ bundle exec rake
|
85
|
-
jj
|
86
15
|
## Found a bug?
|
87
16
|
Let us know! Send a pull request or a patch. Questions? Ask! We're here to help. We will respond to all filed issues.
|
88
17
|
|
data/Rakefile
CHANGED
@@ -1,14 +1,48 @@
|
|
1
1
|
require "bundler/gem_tasks"
|
2
2
|
require 'rspec/core/rake_task'
|
3
|
-
|
3
|
+
require 'mongo'
|
4
|
+
require 'cucumber'
|
5
|
+
require 'cucumber/rake/task'
|
4
6
|
require_relative 'config/constants.rb'
|
5
7
|
|
6
8
|
RSpec::Core::RakeTask.new(:spec)
|
7
9
|
|
8
|
-
task :default => :spec
|
10
|
+
#task :default => :spec
|
11
|
+
task :default => :default_tasks
|
12
|
+
|
13
|
+
Cucumber::Rake::Task.new(:features) do |t|
|
14
|
+
t.cucumber_opts = "features --format pretty"
|
15
|
+
end
|
16
|
+
|
17
|
+
desc "Run BitPay tests"
|
18
|
+
task :default_tasks do
|
19
|
+
Rake::Task["spec"].invoke
|
20
|
+
Rake::Task["features"].invoke
|
21
|
+
end
|
9
22
|
|
10
23
|
desc "Bitpay Tasks"
|
11
24
|
namespace :bitpay do
|
25
|
+
|
26
|
+
desc "Clear all claim codes from the test server."
|
27
|
+
task :clear_claim_codes do
|
28
|
+
puts "clearing claim codes"
|
29
|
+
client = Mongo::MongoClient.new
|
30
|
+
db = client['bitpay-dev']
|
31
|
+
coll = db['tokenaccesses']
|
32
|
+
coll.remove()
|
33
|
+
puts "claim codes cleared"
|
34
|
+
end
|
35
|
+
|
36
|
+
desc "Clear rate limiters from local mongo host"
|
37
|
+
task :clear_rate_limiters do
|
38
|
+
puts "clearing rate limiters"
|
39
|
+
client = Mongo::MongoClient.new
|
40
|
+
db = client['bitpay-dev']
|
41
|
+
coll = db['ratelimiters']
|
42
|
+
coll.remove()
|
43
|
+
puts "rate limiters cleared"
|
44
|
+
end
|
45
|
+
|
12
46
|
desc "Clear local pem and token file"
|
13
47
|
task :clear_local_files do
|
14
48
|
puts "clearing local files"
|
@@ -19,4 +53,25 @@ namespace :bitpay do
|
|
19
53
|
File.delete(TOKEN_FILE) if File.file?(TOKEN_FILE)
|
20
54
|
puts "local files cleared"
|
21
55
|
end
|
56
|
+
|
57
|
+
desc "Clear tokens, rate limiters, and local files."
|
58
|
+
task :clear do
|
59
|
+
["bitpay:clear_local_files", "bitpay:clear_rate_limiters", "bitpay:clear_claim_codes"].each{|task| Rake::Task[task].reenable}
|
60
|
+
["bitpay:clear_local_files", "bitpay:clear_rate_limiters", "bitpay:clear_claim_codes"].each{|task| Rake::Task[task].invoke}
|
61
|
+
end
|
62
|
+
|
63
|
+
desc "Run specs and clear claim codes and rate_limiters."
|
64
|
+
task :spec_clear => ['spec', 'clear_claim_codes', 'clear_rate_limiters']
|
65
|
+
|
66
|
+
desc "Run specs, clear data, run cukes, clear data"
|
67
|
+
task :tests_clear do
|
68
|
+
Rake::Task["bitpay:clear"].invoke
|
69
|
+
Rake::Task["spec"].invoke
|
70
|
+
Rake::Task["bitpay:clear"].reenable
|
71
|
+
Rake::Task["bitpay:clear"].invoke
|
72
|
+
Rake::Task["features"].invoke
|
73
|
+
Rake::Task["bitpay:clear"].reenable
|
74
|
+
Rake::Task["bitpay:clear"].invoke
|
75
|
+
end
|
76
|
+
|
22
77
|
end
|