coinbase_commerce 0.8.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (40) hide show
  1. checksums.yaml +7 -0
  2. data/.circleci/config.yml +38 -0
  3. data/.gitignore +51 -0
  4. data/Gemfile +4 -0
  5. data/LICENSE +21 -0
  6. data/README.md +268 -0
  7. data/Rakefile +6 -0
  8. data/coinbase_commerce.gemspec +28 -0
  9. data/examples/charge.rb +38 -0
  10. data/examples/checkout.rb +61 -0
  11. data/examples/event.rb +26 -0
  12. data/examples/webhook.rb +35 -0
  13. data/lib/coinbase_commerce.rb +42 -0
  14. data/lib/coinbase_commerce/api_errors.rb +157 -0
  15. data/lib/coinbase_commerce/api_resources/base/api_object.rb +206 -0
  16. data/lib/coinbase_commerce/api_resources/base/api_resource.rb +25 -0
  17. data/lib/coinbase_commerce/api_resources/base/create.rb +15 -0
  18. data/lib/coinbase_commerce/api_resources/base/delete.rb +16 -0
  19. data/lib/coinbase_commerce/api_resources/base/list.rb +25 -0
  20. data/lib/coinbase_commerce/api_resources/base/save.rb +18 -0
  21. data/lib/coinbase_commerce/api_resources/base/update.rb +15 -0
  22. data/lib/coinbase_commerce/api_resources/charge.rb +14 -0
  23. data/lib/coinbase_commerce/api_resources/checkout.rb +19 -0
  24. data/lib/coinbase_commerce/api_resources/event.rb +13 -0
  25. data/lib/coinbase_commerce/api_response.rb +48 -0
  26. data/lib/coinbase_commerce/client.rb +120 -0
  27. data/lib/coinbase_commerce/util.rb +59 -0
  28. data/lib/coinbase_commerce/version.rb +3 -0
  29. data/lib/coinbase_commerce/webhooks.rb +52 -0
  30. data/spec/api_resources/base/api_object_spec.rb +156 -0
  31. data/spec/api_resources/base/api_resource_spec.rb +32 -0
  32. data/spec/api_resources/charge_spec.rb +19 -0
  33. data/spec/api_resources/checkout_spec.rb +31 -0
  34. data/spec/api_resources/event_spec.rb +12 -0
  35. data/spec/endpont_spec.rb +103 -0
  36. data/spec/error_spec.rb +58 -0
  37. data/spec/response_spec.rb +43 -0
  38. data/spec/spec_helper.rb +15 -0
  39. data/spec/webhook_spec.rb +36 -0
  40. metadata +161 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1dee073c40e29def47eb4a103180fc7e1b8accdde4055428582d3ee733366f72
4
+ data.tar.gz: 5778183970471bd73b3bf38d36f87b30b74e77b589f4b70d325fa88752ffbdcd
5
+ SHA512:
6
+ metadata.gz: 4ec3bd366e061a470bdf8a35ac9c1592c9978aed1c7494a9151c868c063981caae8ff9cb9c36e0daffb55330f3968302496c026632a6b6224a043429d4c2c66b
7
+ data.tar.gz: 65a4df3e72bb254c296c350e5205e04e151267781b98c85c4801963d7bdcb783b939438d7e75439f2a34ccd71108229ca10b03ddaf87ff697c835f9f5f120ad0
@@ -0,0 +1,38 @@
1
+ version: 2
2
+ general:
3
+ branches:
4
+ only:
5
+ - master
6
+
7
+ .build_template: &build_steps
8
+ steps:
9
+ - checkout
10
+ - run: bundle install --jobs=4 --retry=4 --path vendor/bundle
11
+ - run: bundle exec rspec
12
+
13
+ jobs:
14
+ build_ruby2_3:
15
+ <<: *build_steps
16
+ docker:
17
+ - image: ruby:2.3
18
+ build_ruby2_4:
19
+ <<: *build_steps
20
+ docker:
21
+ - image: ruby:2.4
22
+ build_ruby2_5:
23
+ <<: *build_steps
24
+ docker:
25
+ - image: ruby:2.5
26
+ build_ruby2_6rc:
27
+ <<: *build_steps
28
+ docker:
29
+ - image: ruby:2.6-rc
30
+
31
+ workflows:
32
+ version: 2
33
+ build_ruby_versions:
34
+ jobs:
35
+ - build_ruby2_3
36
+ - build_ruby2_4
37
+ - build_ruby2_5
38
+ - build_ruby2_6rc
data/.gitignore ADDED
@@ -0,0 +1,51 @@
1
+ *.gem
2
+ *.rbc
3
+ .idea/
4
+ /.config
5
+ /coverage/
6
+ /InstalledFiles
7
+ /pkg/
8
+ /spec/reports/
9
+ /spec/examples.txt
10
+ /test/tmp/
11
+ /test/version_tmp/
12
+ /tmp/
13
+
14
+ # Used by dotenv library to load environment variables.
15
+ # .env
16
+
17
+ ## Specific to RubyMotion:
18
+ .dat*
19
+ .repl_history
20
+ build/
21
+ *.bridgesupport
22
+ build-iPhoneOS/
23
+ build-iPhoneSimulator/
24
+
25
+ ## Specific to RubyMotion (use of CocoaPods):
26
+ #
27
+ # We recommend against adding the Pods directory to your .gitignore. However
28
+ # you should judge for yourself, the pros and cons are mentioned at:
29
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
30
+ #
31
+ # vendor/Pods/
32
+
33
+ ## Documentation cache and generated files:
34
+ /.yardoc/
35
+ /_yardoc/
36
+ /doc/
37
+ /rdoc/
38
+
39
+ ## Environment normalization:
40
+ /.bundle/
41
+ /vendor/bundle
42
+ /lib/bundler/man/
43
+
44
+ # for a library or gem, you might want to ignore these files since the code is
45
+ # intended to run in multiple environments; otherwise, check them in:
46
+ # Gemfile.lock
47
+ # .ruby-version
48
+ # .ruby-gemset
49
+
50
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
51
+ .rvmrc
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in coinbase_commerce.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,268 @@
1
+ [![CircleCI](https://circleci.com/gh/coinbase/coinbase-commerce-ruby.svg?style=svg)](https://circleci.com/gh/coinbase/coinbase-commerce-ruby)
2
+
3
+ # coinbase-commerce-ruby
4
+ Coinbase Commerce Ruby Gem
5
+
6
+ # Table of contents
7
+
8
+ <!--ts-->
9
+ * [Ruby Versions](#ruby-version)
10
+ * [Third Party Libraries and Dependencies](#third-party-libraries-and-dependencies)
11
+ * [Documentation](#documentation)
12
+ * [Installation](#installation)
13
+ * [Usage](#usage)
14
+ * [Checkouts](#checkouts)
15
+ * [Charges](#charges)
16
+ * [Events](#events)
17
+ * [Validating webhook signatures](#validating-webhook-signatures)
18
+ * [Testing and Contributing](#testing-and-contributing)
19
+ <!--te-->
20
+
21
+ ## Ruby Version
22
+ Ruby 2.3, 2.4, 2.5, 2.6RC are supported and tested.
23
+
24
+ ## Third Party Libraries and Dependencies
25
+
26
+ The following libraries will be installed when you install the client library:
27
+ * [faraday](https://github.com/lostisland/faraday)
28
+
29
+ ## Documentation
30
+
31
+ For more details visit [Coinbase API docs](https://commerce.coinbase.com/docs/api/).
32
+
33
+ To start using library, you'll need to [create a Coinbase Commmerce account](https://commerce.coinbase.com/signup).
34
+ Once you've created your Coinbase Commerce account, create an ``API_KEY`` in Settings.
35
+
36
+ Next create a ``Client`` object for interacting with the API:
37
+ ```ruby
38
+ require 'coinbase_commerce'
39
+
40
+ API_KEY = "API KEY"
41
+ client = CoinbaseCommerce::Client.new(api_key: API_KEY)
42
+
43
+ ```
44
+
45
+ ``Client`` contains links to every Ruby Class representations of the API resources
46
+ ``Checkout, Charge, Event``
47
+
48
+ You can call ``list, auto_paging, create, retrieve, modify`` methods from API resource classes
49
+
50
+ ```ruby
51
+ client.charge.create
52
+ client.checkout.auto_paging
53
+ client.event.list
54
+ client.charge.retrieve
55
+ client.checkout.modify
56
+ ```
57
+ as well as ``save, delete, refresh`` methods from API resource class instances.
58
+ ```python
59
+ checkout = client.checkout.retrieve <id>
60
+ checkout.refresh
61
+ checkout.save
62
+ checkout.delete
63
+ ```
64
+
65
+ Each API method returns an ``APIObject`` representing the JSON response from the API, all of the models support hash and JSON representation.\
66
+ Also when the response data is parsed into Ruby objects, the appropriate ``APIObject`` subclasses will be used automatically.
67
+ All subclasses of ``APIResource`` class support ``refresh`` method. This will update their attributes and all nested data by making a fresh ``GET`` request to the relevant API endpoint.
68
+
69
+ The client supports handling of common API errors and warnings.
70
+ All errors occuring during the interaction with the API will be raised as exceptions.
71
+
72
+
73
+ | Error | Status Code |
74
+ |--------------------------|-------------|
75
+ | APIError | * |
76
+ | InvalidRequestError | 400 |
77
+ | ParamRequiredError | 400 |
78
+ | ValidationError | 400 |
79
+ | AuthenticationError | 401 |
80
+ | ResourceNotFoundError | 404 |
81
+ | RateLimitExceededError | 429 |
82
+ | InternalServerError | 500 |
83
+ | ServiceUnavailableError | 503 |
84
+
85
+ ## Installation
86
+
87
+ Add this line to your application's Gemfile:
88
+
89
+ ```ruby
90
+ gem 'coinbase_commerce'
91
+ ```
92
+ Then execute:
93
+
94
+ ```sh
95
+ bundle install
96
+ ```
97
+
98
+ Or install it yourself as:
99
+
100
+ ```sh
101
+ gem install coinbase_commerce
102
+ ```
103
+
104
+ ## Usage
105
+ ```ruby
106
+ require 'coinbase_commerce'
107
+
108
+ client = CoinbaseCommerce::Client.new(api_key: 'your_api_key')
109
+ ```
110
+ ## Checkouts
111
+ [Checkouts API docs](https://commerce.coinbase.com/docs/api/#checkouts)
112
+ ### Retrieve
113
+ ```ruby
114
+ checkout = client.checkout.retrieve <checkout_id>
115
+ ```
116
+ ### Create
117
+ ```ruby
118
+ checkout_info = {
119
+ "name": "The Sovereign Individual",
120
+ "description": "Mastering the Transition to the Information Age",
121
+ "pricing_type": "fixed_price",
122
+ "local_price": {
123
+ "amount": "1.00",
124
+ "currency": "USD"
125
+ },
126
+ "requested_info": ["name", "email"]
127
+ }
128
+ checkout = client.checkout.create(checkout_info)
129
+
130
+ # or
131
+
132
+ checkout = client.checkout.create(:name=>'The Sovereign Individual',
133
+ :description=>'Mastering the Transition to the Information Age',
134
+ :pricing_type=>'fixed_price',
135
+ :local_price=>{
136
+ "amount": "100.00",
137
+ "currency": "USD"
138
+ },
139
+ :requested_info=>["name", "email"])
140
+ ```
141
+ ### Update
142
+ ```ruby
143
+ checkout = client.checkout.retrieve <checkout_id>
144
+ checkout.name = 'new name'
145
+ checkout.save
146
+
147
+ # or
148
+
149
+ client.checkout.modify(<checkout_id>, "local_price": {
150
+ "amount": "10000.00",
151
+ "currency": "USD"
152
+ })
153
+ ```
154
+ ### Delete
155
+ ```ruby
156
+ checkout.delete
157
+ ```
158
+ ### List
159
+ ```ruby
160
+ checkouts = client.checkout.list
161
+ ```
162
+ ### Paging list iterations
163
+ ```ruby
164
+ client.checkout.auto_paging do |ch|
165
+ puts ch.id
166
+ end
167
+ ```
168
+ ## Charges
169
+ [Charges API docs](https://commerce.coinbase.com/docs/api/#charges)
170
+ ### Retrieve
171
+ ```ruby
172
+ charge = client.charge.retrieve <charge_id>
173
+ ```
174
+ ### Create
175
+ ```ruby
176
+ charge_info = {
177
+ "name": "The Sovereign Individual",
178
+ "description": "Mastering the Transition to the Information Age",
179
+ "pricing_type": "fixed_price",
180
+ "local_price": {
181
+ "amount": "1.00",
182
+ "currency": "USD"
183
+ },
184
+ "requested_info": ["name", "email"]
185
+ }
186
+ charge = client.charge.create(charge_info)
187
+
188
+ # or
189
+
190
+ charge = client.charge.create(:name=>'The Sovereign Individual',
191
+ :description=>'Mastering the Transition to the Information Age',
192
+ :pricing_type=>'fixed_price',
193
+ :local_price=>{
194
+ "amount": "100.00",
195
+ "currency": "USD"
196
+ })
197
+ ```
198
+ ### List
199
+ ```ruby
200
+ charges_list = client.charge.list
201
+ ```
202
+ ### Paging list iterations
203
+ ```ruby
204
+ client.charge.auto_paging do |charge|
205
+ puts charge.id
206
+ end
207
+ ```
208
+ ## Events
209
+ [Events API Docs](https://commerce.coinbase.com/docs/api/#events)
210
+ ### Retrieve
211
+ ```ruby
212
+ event = client.event.retrieve <event_id>
213
+ ```
214
+ ### List
215
+ ```ruby
216
+ events = client.event.list
217
+ ```
218
+ ### Paging list iterations
219
+ ```ruby
220
+ client.event.auto_paging do |event|
221
+ puts event.id
222
+ end
223
+ ```
224
+
225
+ ## Validating webhook signatures
226
+ You should verify the webhook signatures using our library.
227
+ To perform the verification you'll need to provide the event data, a webhook signature from the request header, and the endpoint’s secret.
228
+ In case of an invalid request signature or request payload, you will receive an appropriate error message.
229
+ ```ruby
230
+ WEBHOOK_SECRET = 'your_webhook_secret'
231
+
232
+ # Using Sinatra
233
+ post '/webhooks' do
234
+ request_payload = request.body.read
235
+ sig_header = request.env['HTTP_X_CC_WEBHOOK_SIGNATURE']
236
+
237
+ begin
238
+ event = CoinbaseCommerce::Webhook.construct_event(request_payload, sig_header, WEBHOOK_SECRET)
239
+ # event handle
240
+ puts "Received event id=#{event.id}, type=#{event.type}"
241
+ status 200
242
+ # errors handle
243
+ rescue JSON::ParserError => e
244
+ puts "json parse error"
245
+ status 400
246
+ return
247
+ rescue CoinbaseCommerce::Errors::SignatureVerificationError => e
248
+ puts "signature verification error"
249
+ status 400
250
+ return
251
+ rescue CoinbaseCommerce::Errors::WebhookInvalidPayload => e
252
+ puts "missing request or headers data"
253
+ status 400
254
+ return
255
+ end
256
+ end
257
+ ```
258
+
259
+ ### Testing and Contributing
260
+ Any and all contributions are welcome! The process is simple: fork this repo, make your changes, add tests, run the test suite, and submit a pull request. Tests are run via rspec. To run the tests, clone the repository and then:
261
+
262
+ # Install the requirements
263
+ gem install coinbase_commerce
264
+ rspec spec
265
+
266
+ # or via Bundle
267
+ bundle install
268
+ bundle exec rspec spec
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task default: :spec
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), "lib"))
4
+ require 'coinbase_commerce/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "coinbase_commerce"
8
+ gem.version = CoinbaseCommerce::VERSION
9
+ gem.license = "MIT"
10
+ gem.required_ruby_version = ">= 2.0.0"
11
+ gem.authors = ["Coinbase Commerce",]
12
+
13
+ gem.description = "Client library for Coinbase Commerce API"
14
+ gem.summary = "Client library for Coinbase Commerce API"
15
+ gem.homepage = "https://commerce.coinbase.com/docs/api/"
16
+
17
+ gem.files = `git ls-files`.split($/)
18
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
19
+ gem.test_files = gem.files.grep(%r{^(spec|gem|features)/})
20
+ gem.require_paths = ["lib"]
21
+
22
+ gem.add_dependency("faraday", "~> 0.10")
23
+
24
+ gem.add_development_dependency "rake"
25
+ gem.add_development_dependency "rspec"
26
+ gem.add_development_dependency "webmock"
27
+ gem.add_development_dependency "pry-byebug"
28
+ end
@@ -0,0 +1,38 @@
1
+ require 'coinbase_commerce'
2
+
3
+ client = CoinbaseCommerce::Client.new(api_key: 'your_api_key')
4
+
5
+ # create charge
6
+ data = {
7
+ "name": "The Sovereign Individual",
8
+ "description": "Mastering the Transition to the Information Age",
9
+ "local_price": {
10
+ "amount": "100.00",
11
+ "currency": "USD"
12
+ },
13
+ "pricing_type": "fixed_price"
14
+
15
+ }
16
+ charge = client.charge.create(data)
17
+
18
+ # or retrieve it if you know charge id
19
+ charge = client.charge.retrieve charge.id
20
+
21
+ # get charges list
22
+ charges_list = client.charge.list
23
+
24
+ # in case you need provide additional params
25
+ charges_list = client.charge.list(limit: 10)
26
+
27
+ # or get results from another page
28
+ charges_list = client.charge.list(starting_after: charge.id, limit: 3)
29
+
30
+ # charges list could be iterated like
31
+ charges_list.data.each do |charge|
32
+ puts charge.id
33
+ end
34
+
35
+ # iterate over all charges with per-page limitation
36
+ client.charge.auto_paging limit: 20 do |charge|
37
+ puts charge.id
38
+ end