coinqvest_merchant_sdk 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c98289ad3631f1f67deb9dfeb42c6140bbb09e7a
4
+ data.tar.gz: cb27c4cb49a557007db6d39464f871d8354116b0
5
+ SHA512:
6
+ metadata.gz: a4c98c934545300533a7de7a6c14aced90a5698f6b5d9b3f8923ed31392a619b1f68c94421fcfe6b95c47cd8b9658c790c5e01787de6c9935f52a3d48dbe7151
7
+ data.tar.gz: 5f5ddc8553c65a7e40cd5a6b4d1b58da94cd187371d05368f3c48dbf2a45bbb0b9f8ab038e85bc518b8de9ca6cd518f1bc2db831c5985583688b9b13dd629b5f
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ Gemfile.lock
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'http://rubygems.org'
2
+
3
+ # Specify dependencies in coinqvest_merchant_sdk.gemspec
4
+ gemspec
data/bin/example.rb ADDED
@@ -0,0 +1,116 @@
1
+ #!/usr/bin/env ruby
2
+ require 'coinqvest_merchant_sdk/client'
3
+
4
+ # This file contains examples on how to interact with the COINQVEST Merchant API.
5
+ # All endpoints of the API are documented here: https://www.coinqvest.com/en/api-docs
6
+
7
+ # Create a COINQVEST Merchant API client
8
+ # The constructor takes your API Key, API Secret and an optional log file location as parameters
9
+ # Your API Key and Secret can be obtained here: https://www.coinqvest.com/en/api-settings
10
+ client = CoinqvestMerchantSDK::Client.new(
11
+ 'YOUR-API-KEY',
12
+ 'YOUR-API-SECRET',
13
+ '/var/log/coinqvest-ruby.log' # an optional log file location
14
+ )
15
+
16
+ # Invoke a request to GET /auth-test (https://www.coinqvest.com/en/api-docs#get-auth-test) to see if everything worked
17
+ response = client.get('/auth-test')
18
+
19
+ # The API should return an HTTP status code of 200 if the request was successfully processed, let's have a look.
20
+ print "Status Code: " + response.code.to_s + "\n"
21
+ print "Response Body: " + response.body + "\n"
22
+
23
+ # Check our USD wallet balance using GET /wallet (https://www.coinqvest.com/en/api-docs#get-wallet)
24
+ response = client.get('/wallet', {:assetCode => 'USD'})
25
+ print "Status Code: " + response.code.to_s + "\n"
26
+ print "Response Body: " + response.body + "\n"
27
+
28
+ # Create a checkout and get paid in two easy steps!
29
+ #
30
+ # It's good practice to associate payments with a customer, let's create one!
31
+ # Invoke POST /customer (https://www.coinqvest.com/en/api-docs#post-customer) to create a new customer object.
32
+ # Tip: At a minimum a customer needs an email address, but it's better to provide a full billing address for invoices.
33
+ response = client.post('/customer', {:customer => {
34
+ :email => 'john@tester-1.com',
35
+ :firstname => 'John',
36
+ :lastname => 'Doe',
37
+ :company => 'ACME Inc.',
38
+ :adr1 => '810 Beach St',
39
+ :adr2 => 'Finance Department',
40
+ :zip => 'CA 94103',
41
+ :city => 'San Francisco',
42
+ :countrycode => 'US'
43
+ }})
44
+ print "Status Code: " + response.code.to_s + "\n"
45
+ print "Response Body: " + response.body + "\n"
46
+
47
+ if response.code != 200
48
+ # something went wrong, let's abort and debug by looking at our log file specified above in the client.
49
+ print "Could not create customer, please check the logs."
50
+ exit 1
51
+ end
52
+
53
+ # the customer was created
54
+ data = JSON.parse(response.body)
55
+ # data now contains an object as specified in the success response here: https://www.coinqvest.com/en/api-docs#post-customer
56
+ # extract the customer id to use it in our checkout below
57
+ customer_id = data["customerId"]
58
+
59
+ # We now have a customer. Let's create a checkout for him/her.
60
+ # This creates a hosted checkout, which will provide a payment interface hosted on COINQVEST servers
61
+ response = client.post('/checkout/hosted', {
62
+ :charge => {
63
+ :customerId => customer_id, # associates this charge with a customer
64
+ :currency => 'USD', # specifies the billing currency
65
+ :lineItems => [{ # a list of line items included in this charge
66
+ :description => 'T-Shirt',
67
+ :netAmount => 10, # denominated in the currency specified above
68
+ :quantity => 1
69
+ }],
70
+ :discountItems => [{ # an optional list of discounts
71
+ :description => 'Loyalty Discount',
72
+ :netAmount => 0.5
73
+ }],
74
+ :shippingCostItems => [{ # an optional list of shipping and handling costs
75
+ :description => 'Shipping and Handling',
76
+ :netAmount => 3.99,
77
+ :taxable => FALSE # sometimes shipping costs are taxable
78
+ }],
79
+ :taxItems => [{ # an optional list of taxes
80
+ :name => 'CA Sales Tax',
81
+ :percent => 0.0825 # 8.25% CA sales tax
82
+ }]
83
+ },
84
+ :settlementCurrency => 'EUR' # specifies in which currency you want to settle
85
+ })
86
+ print "Status Code: " + response.code.to_s + "\n"
87
+ print "Response Body: " + response.body + "\n"
88
+
89
+ if response.code != 200
90
+ # something went wrong, let's abort and debug by looking at our log file specified above in the client.
91
+ print "Could not create checkout, please check the logs."
92
+ exit 1
93
+ end
94
+
95
+ # the customer was created
96
+ data = JSON.parse(response.body)
97
+ # data now contains an object as specified in the success response here: https://www.coinqvest.com/en/api-docs#post-customer
98
+ checkout_id = data["checkoutId"] # store this persistently in your database
99
+ url = data["url"] # redirect your customer to this URL to complete the payment
100
+
101
+ # you can update a customer object like this
102
+ response = client.put('/customer', {:customer => {
103
+ :id => customer_id,
104
+ :email => 'john@tester-2.com',
105
+ :firstname => 'John',
106
+ :lastname => 'Doe'
107
+ }})
108
+ print "Status Code: " + response.code.to_s + "\n"
109
+ print "Response Body: " + response.body + "\n"
110
+
111
+ # delete a customer when not needed anymore
112
+ response = client.delete('/customer', {
113
+ :id => customer_id
114
+ })
115
+ print "Status Code: " + response.code.to_s + "\n"
116
+ print "Response Body: " + response.body + "\n"
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'coinqvest_merchant_sdk/config'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'coinqvest_merchant_sdk'
7
+ s.version = CoinqvestMerchantSDK::CLIENT_VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ['COINQVEST Ltd.']
10
+ s.email = ['service@coinqvest.com']
11
+ s.homepage = 'http://www.coinqvest.com'
12
+ s.summary = %q{Ruby Merchant SDK for COINQVEST. Programmatically accept and settle payments in digital currencies.}
13
+ s.licenses = ['Apache-2.0']
14
+ s.required_ruby_version = '>= 2.0.0'
15
+
16
+ s.add_runtime_dependency 'rest-client', '~> 2.1', '>= 2.1.0'
17
+ s.add_runtime_dependency 'json', '~> 1.8', '>= 1.8.3'
18
+
19
+ s.files = `git ls-files`.split("\n")
20
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
21
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
22
+ s.require_paths = ['lib']
23
+ end
@@ -0,0 +1,3 @@
1
+ module CoinqvestMerchantSDK
2
+ # Your code goes here...
3
+ end
@@ -0,0 +1,186 @@
1
+ require 'rest-client'
2
+ require 'json'
3
+ require 'time'
4
+ require 'coinqvest_merchant_sdk/config'
5
+ require 'uri'
6
+ require 'openssl'
7
+
8
+ # Ruby implementation of a REST client for the COINQVEST Merchant API
9
+ # see https://www.coinqvest.com/en/api-docs
10
+ module CoinqvestMerchantSDK
11
+
12
+ class Client
13
+
14
+ # Merchant API client constructor, initialize this with the API key and secret as given by https://www.coinqvest.com/en/api-settings
15
+ # @param key; as given by https://www.coinqvest.com/en/api-settings
16
+ # @param secret; as given by https://www.coinqvest.com/en/api-settings
17
+ # @param log_file; optional log file path
18
+ # @constructor
19
+ def initialize(key, secret, log_file = NIL)
20
+
21
+ # @string The API Key as given by https://www.coinqvest.com/en/api-settings
22
+ @key = key
23
+
24
+ # @string The API Secret as given by https://www.coinqvest.com/en/api-settings
25
+ @secret = secret
26
+
27
+ # @string The API version to which we connect (leave it as is)
28
+ @api_version = CoinqvestMerchantSDK::API_VERSION
29
+
30
+ # @string Used in the HTTP user agent (leave it as is)
31
+ @client_name = CoinqvestMerchantSDK::CLIENT_NAME
32
+
33
+ # @string The current version of this SDK, used in the HTTP user agent (leave it as is)
34
+ @client_version = CoinqvestMerchantSDK::CLIENT_VERSION
35
+
36
+ # @string COINQVEST connect url
37
+ @connect_url = CoinqvestMerchantSDK::CONNECT_URL
38
+
39
+ # @string|NIL Specifies the log file to which to write, if any.
40
+ @log_file = log_file ? log_file : NIL
41
+
42
+ end
43
+
44
+ # Use this method to communicate with GET endpoints
45
+ # @param endpoint (string), e.g. GET /customer
46
+ # @param params (hash), a list of GET parameters to be included in the request
47
+ # @return RestClient::Response, https://github.com/rest-client/rest-client/blob/2c72a2e77e2e87d25ff38feba0cf048d51bd5eca/lib/restclient/response.rb
48
+ def get(endpoint, params = {})
49
+
50
+ path = build_connect_url(endpoint) + '?' + URI.encode_www_form(params)
51
+ headers = build_auth_headers(endpoint, 'GET', params)
52
+
53
+ log "GET " + path
54
+ log headers.to_s
55
+
56
+ begin
57
+ response = RestClient::Request.execute(method: :get, url: path, headers: headers, timeout: 180)
58
+ rescue RestClient::ExceptionWithResponse => e
59
+ log e.http_code.to_s + " " + e.response.to_s
60
+ return e.response
61
+ end
62
+
63
+ log response.code.to_s + " " + response.to_s
64
+
65
+ response
66
+
67
+ end
68
+
69
+ # Use this method to communicate with POST endpoints
70
+ # @param endpoint (string), e.g. POST /checkout/hosted
71
+ # @param params (hash), a list of GET parameters to be included in the request
72
+ # @return RestClient::Response, https://github.com/rest-client/rest-client/blob/2c72a2e77e2e87d25ff38feba0cf048d51bd5eca/lib/restclient/response.rb
73
+ def post(endpoint, params = {})
74
+
75
+ path = build_connect_url(endpoint)
76
+ headers = build_auth_headers(endpoint, 'POST', params)
77
+
78
+ log "POST " + path + " " + params.to_s
79
+ log headers.to_s
80
+
81
+ begin
82
+ response = RestClient::Request.execute(method: :post, url: path, payload: params.to_json, headers: headers, timeout: 180)
83
+ rescue RestClient::ExceptionWithResponse => e
84
+ log e.http_code.to_s + " " + e.response.to_s
85
+ return e.response
86
+ end
87
+
88
+ log response.code.to_s + " " + response.to_s
89
+
90
+ response
91
+
92
+ end
93
+
94
+ # Use this method to communicate with PUT endpoints
95
+ # @param endpoint (string), e.g. PUT /customer
96
+ # @param params (hash), a list of GET parameters to be included in the request
97
+ # @return RestClient::Response, https://github.com/rest-client/rest-client/blob/2c72a2e77e2e87d25ff38feba0cf048d51bd5eca/lib/restclient/response.rb
98
+ def put(endpoint, params = {})
99
+
100
+ path = build_connect_url(endpoint)
101
+ headers = build_auth_headers(endpoint, 'PUT', params)
102
+
103
+ log "PUT " + path + " " + params.to_s
104
+ log headers.to_s
105
+
106
+ begin
107
+ response = RestClient::Request.execute(method: :put, url: path, payload: params.to_json, headers: headers, timeout: 180)
108
+ rescue RestClient::ExceptionWithResponse => e
109
+ log e.http_code.to_s + " " + e.response.to_s
110
+ return e.response
111
+ end
112
+
113
+ log response.code.to_s + " " + response.to_s
114
+
115
+ response
116
+
117
+ end
118
+
119
+ # Use this method to communicate with PUT endpoints
120
+ # @param endpoint (string), e.g. PUT /customer
121
+ # @param params (hash), a list of GET parameters to be included in the request
122
+ # @return RestClient::Response, https://github.com/rest-client/rest-client/blob/2c72a2e77e2e87d25ff38feba0cf048d51bd5eca/lib/restclient/response.rb
123
+ def delete(endpoint, params = {})
124
+
125
+ path = build_connect_url(endpoint)
126
+ headers = build_auth_headers(endpoint, 'DELETE', params)
127
+
128
+ log "DELETE " + path + " " + params.to_s
129
+ log headers.to_s
130
+
131
+ begin
132
+ response = RestClient::Request.execute(method: :delete, url: path, payload: params.to_json, headers: headers, timeout: 180)
133
+ rescue RestClient::ExceptionWithResponse => e
134
+ log e.http_code.to_s + " " + e.response.to_s
135
+ return e.response
136
+ end
137
+
138
+ log response.code.to_s + " " + response.to_s
139
+
140
+ response
141
+
142
+ end
143
+
144
+ # private class to generate connect url on COINQVEST servers
145
+ private
146
+ def build_connect_url(endpoint)
147
+ @connect_url + @api_version + endpoint
148
+ end
149
+
150
+ # private class to generate authentication headers
151
+ private
152
+ def build_auth_headers(endpoint, method, params)
153
+
154
+ timestamp = Time.now.to_i
155
+ body = NIL
156
+ if method != 'GET'
157
+ body = params.length > 0 ? params.to_json : NIL
158
+ end
159
+ data = endpoint + timestamp.to_s + method + body.to_s
160
+
161
+ {
162
+ :"X-Digest-Key" => @key,
163
+ :"X-Digest-Signature" => OpenSSL::HMAC.hexdigest('sha256', @secret, data),
164
+ :"X-Digest-Timestamp" => timestamp
165
+ }
166
+
167
+ end
168
+
169
+
170
+ private
171
+ def log(text)
172
+
173
+ if @log_file == NIL
174
+ return
175
+ end
176
+
177
+ File.open(@log_file, 'a') { |f| f.write(Time.now.utc.rfc822 + " [CoinqvestMerchantSDK] " + text + "\n") }
178
+
179
+ # todo: remove this before commit
180
+ # print Time.now.utc.rfc822 + " [CoinqvestMerchantSDK] " + text + "\n"
181
+
182
+ end
183
+
184
+ end
185
+
186
+ end
@@ -0,0 +1,11 @@
1
+ module CoinqvestMerchantSDK
2
+
3
+ CLIENT_VERSION = '0.0.1'
4
+
5
+ CLIENT_NAME = 'ruby_merchant_sdk'
6
+
7
+ API_VERSION = 'v1'
8
+
9
+ CONNECT_URL = 'https://www.coinqvest.com/api/'
10
+
11
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: coinqvest_merchant_sdk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - COINQVEST Ltd.
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-04-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rest-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.1'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.1.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.1'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.1.0
33
+ - !ruby/object:Gem::Dependency
34
+ name: json
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - "~>"
38
+ - !ruby/object:Gem::Version
39
+ version: '1.8'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 1.8.3
43
+ type: :runtime
44
+ prerelease: false
45
+ version_requirements: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '1.8'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 1.8.3
53
+ description:
54
+ email:
55
+ - service@coinqvest.com
56
+ executables:
57
+ - example.rb
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - ".gitignore"
62
+ - Gemfile
63
+ - bin/example.rb
64
+ - coinqvest_merchant_sdk.gemspec
65
+ - lib/coinqvest_merchant_sdk.rb
66
+ - lib/coinqvest_merchant_sdk/client.rb
67
+ - lib/coinqvest_merchant_sdk/config.rb
68
+ homepage: http://www.coinqvest.com
69
+ licenses:
70
+ - Apache-2.0
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: 2.0.0
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.5.2.3
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Ruby Merchant SDK for COINQVEST. Programmatically accept and settle payments
92
+ in digital currencies.
93
+ test_files: []