apruve 0.11.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  module Apruve
2
- class LineItem < Apruve::ApruveObject
2
+ class OrderItem < Apruve::ApruveObject
3
3
  attr_accessor :id, :title, :amount_cents, :price_ea_cents, :quantity, :description, :merchant_notes,
4
4
  :variant_info, :sku, :vendor, :view_product_url, :plan_code, :line_item_api_url,
5
5
  :subscription_url
@@ -1,5 +1,5 @@
1
1
  module Apruve
2
- class Subscription < Apruve::LineItem
2
+ class Subscription < Apruve::OrderItem
3
3
  attr_accessor :id, :start_at, :next_charge_at, :last_charge_at, :end_at, :canceled_at
4
4
 
5
5
  def self.find(id)
@@ -0,0 +1,33 @@
1
+ module Apruve
2
+ class WebhookEndpoint < Apruve::ApruveObject
3
+ attr_accessor :id, :version, :url, :merchant_id
4
+
5
+ def self.find(merchant_id, id)
6
+ response = Apruve.get("merchants/#{merchant_id}/webhook_endpoints/#{id}")
7
+ logger.debug response.body
8
+ WebhookEndpoint.new(response.body.merge(merchant_id: merchant_id))
9
+ end
10
+
11
+ def self.where(merchant_id)
12
+ response = Apruve.get("merchants/#{merchant_id}/webhook_endpoints")
13
+ logger.debug response.body
14
+ ret = []
15
+ response.body.each do |i|
16
+ ret << WebhookEndpoint.new(i.merge(merchant_id: merchant_id))
17
+ end
18
+ ret
19
+ end
20
+
21
+ def destroy!
22
+ response = Apruve.delete("merchants/#{merchant_id}/webhook_endpoints/#{id}")
23
+ logger.debug response.body
24
+ response.status
25
+ end
26
+
27
+ def create!
28
+ response = Apruve.post("merchants/#{merchant_id}/webhook_endpoints", {webhook_endpoint: self}.to_json )
29
+ logger.debug response.body
30
+ initialize response.body
31
+ end
32
+ end
33
+ end
@@ -1,3 +1,3 @@
1
1
  module Apruve
2
- VERSION = '0.11.0'
2
+ VERSION = '1.0.0'
3
3
  end
@@ -8,35 +8,35 @@ describe 'Apruve' do
8
8
 
9
9
  describe '#js' do
10
10
  describe 'test' do
11
- let (:script_tag) { '<script type="text/javascript" src="https://test.apruve.com/js/apruve.js"></script>' }
11
+ let (:script_tag) { '<script type="text/javascript" src="https://test.apruve.com/js/v4/apruve.js"></script>' }
12
12
  it 'should print the tag' do
13
13
  Apruve.configure(nil, 'test')
14
14
  expect(Apruve.js).to eq script_tag
15
15
  end
16
16
  end
17
17
  describe 'prod' do
18
- let (:script_tag) { '<script type="text/javascript" src="http://localhost:3000/js/apruve.js"></script>' }
18
+ let (:script_tag) { '<script type="text/javascript" src="http://localhost:3000/js/v4/apruve.js"></script>' }
19
19
  it 'should print the tag' do
20
20
  Apruve.configure(nil)
21
21
  expect(Apruve.js).to eq script_tag
22
22
  end
23
23
  end
24
24
  describe 'local' do
25
- let (:script_tag) { '<script type="text/javascript" src="https://www.apruve.com/js/apruve.js"></script>' }
25
+ let (:script_tag) { '<script type="text/javascript" src="https://www.apruve.com/js/v4/apruve.js"></script>' }
26
26
  it 'should print the tag' do
27
27
  Apruve.configure(nil, 'prod')
28
28
  expect(Apruve.js).to eq script_tag
29
29
  end
30
30
  end
31
31
  describe 'compact' do
32
- let (:script_tag) { '<script type="text/javascript" src="http://localhost:3000/js/apruve.js?display=compact"></script>' }
32
+ let (:script_tag) { '<script type="text/javascript" src="http://localhost:3000/js/v4/apruve.js?display=compact"></script>' }
33
33
  it 'should print the tag' do
34
34
  Apruve.configure(nil)
35
35
  expect(Apruve.js('compact')).to eq script_tag
36
36
  end
37
37
  end
38
38
  describe 'overrides' do
39
- let (:script_tag) { '<script type="text/javascript" src="mailto://google.com:4567/js/apruve.js"></script>' }
39
+ let (:script_tag) { '<script type="text/javascript" src="mailto://google.com:4567/js/v4/apruve.js"></script>' }
40
40
  it 'should print the tag' do
41
41
  Apruve.configure(nil, 'prod', {scheme: 'mailto', host: 'google.com', port: 4567})
42
42
  expect(Apruve.js).to eq script_tag
@@ -1,12 +1,13 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Apruve::PaymentItem do
4
- let (:payment_item) {Apruve::PaymentItem.new}
5
- subject { payment_item }
3
+ describe Apruve::InvoiceItem do
4
+ let (:invoice_item) {Apruve::InvoiceItem.new}
5
+ subject { invoice_item }
6
6
 
7
7
  it { should respond_to(:title) }
8
- it { should respond_to(:amount_cents) }
8
+ it { should_not respond_to(:amount_cents) }
9
9
  it { should respond_to(:price_ea_cents) }
10
+ it { should respond_to(:price_total_cents) }
10
11
  it { should respond_to(:quantity) }
11
12
  it { should respond_to(:description) }
12
13
  it { should respond_to(:variant_info) }
@@ -0,0 +1,291 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apruve::Invoice do
4
+ before :each do
5
+ Apruve.configure('f5fbe71d68772d1f562ed6f598b995b3', 'local')
6
+ end
7
+
8
+ let (:amount_cents) { 12340 }
9
+ let (:notes) { 'notes from merchant' }
10
+ let (:order_id) { '9999' }
11
+ let (:invoice) do
12
+ Apruve::Invoice.new(
13
+ order_id: order_id,
14
+ amount_cents: amount_cents,
15
+ )
16
+ end
17
+ subject { invoice }
18
+
19
+ it { should respond_to(:id) }
20
+ it { should respond_to(:order_id) }
21
+ it { should respond_to(:status) }
22
+ it { should respond_to(:amount_cents) }
23
+ it { should respond_to(:currency) }
24
+ it { should respond_to(:merchant_notes) }
25
+ it { should respond_to(:merchant_invoice_id) }
26
+ it { should respond_to(:shipping_cents) }
27
+ it { should respond_to(:tax_cents) }
28
+ it { should respond_to(:invoice_items) }
29
+ it { should respond_to(:created_at) }
30
+ it { should respond_to(:opened_at) }
31
+ it { should respond_to(:due_at) }
32
+ it { should respond_to(:final_state_at) }
33
+ it { should respond_to(:links) }
34
+
35
+ describe '#to_json' do
36
+ let(:expected) do
37
+ '{"order_id":"9999","amount_cents":12340,"invoice_items":[],"currency":"USD"}'
38
+ end
39
+ its(:to_json) { should eq expected }
40
+ end
41
+
42
+ describe '#validate' do
43
+ describe 'no errors' do
44
+ it 'should not raise' do
45
+ expect { invoice.validate }.not_to raise_error
46
+ end
47
+ end
48
+ describe 'errors' do
49
+ before :each do
50
+ invoice.amount_cents = nil
51
+ end
52
+ it 'should raise on no merchant_id' do
53
+ expect { invoice.validate }.to raise_error(Apruve::ValidationError, '["amount_cents must be set"]')
54
+ end
55
+ end
56
+ end
57
+
58
+ describe '#find' do
59
+ let (:id) { '89ea2488fe0a5c7bb38aa7f9b088874a' }
60
+ describe 'success' do
61
+ let! (:stubs) do
62
+ faraday_stubs do |stub|
63
+ stub.get("/api/v4/invoices/#{id}") { [200, {}, '{}'] }
64
+ end
65
+ end
66
+ it 'should do a get' do
67
+ Apruve::Invoice.find(id)
68
+ stubs.verify_stubbed_calls
69
+ end
70
+ end
71
+
72
+ describe 'not found' do
73
+ let! (:stubs) do
74
+ faraday_stubs do |stub|
75
+ stub.get("/api/v4/invoices/#{id}") { [404, {}, 'Not Found'] }
76
+ end
77
+ end
78
+ it 'should raise' do
79
+ expect { Apruve::Invoice.find(id) }.to raise_error(Apruve::NotFound)
80
+ stubs.verify_stubbed_calls
81
+ end
82
+ end
83
+ end
84
+
85
+ describe '#save' do
86
+ let (:id) { '89ea2488fe0a5c7bb38aa7f9b088874a' }
87
+ let (:status) { 'pending' }
88
+ let (:api_url) { Faker::Internet.url }
89
+ let (:view_url) { Faker::Internet.url }
90
+ let (:response) do
91
+ {
92
+ id: id,
93
+ status: status,
94
+ api_url: api_url,
95
+ view_url: view_url
96
+ }
97
+ end
98
+ describe 'success' do
99
+ let! (:stubs) do
100
+ faraday_stubs do |stub|
101
+ stub.post(
102
+ "/api/v4/invoices",
103
+ invoice.to_json,
104
+ ) { [200, {}, response.to_json] }
105
+ end
106
+ end
107
+ it 'should do a post' do
108
+ invoice.save!
109
+ expect(invoice.id).to eq id
110
+ expect(invoice.status).to eq status
111
+ stubs.verify_stubbed_calls
112
+ end
113
+ end
114
+
115
+ describe 'invoice request not found' do
116
+ let! (:stubs) do
117
+ faraday_stubs do |stub|
118
+ stub.post(
119
+ "/api/v4/invoices",
120
+ invoice.to_json,
121
+ ) { [404, {}, 'Not Found'] }
122
+ end
123
+ end
124
+ it 'should raise' do
125
+ expect { invoice.save! }.to raise_error(Apruve::NotFound)
126
+ stubs.verify_stubbed_calls
127
+ end
128
+ end
129
+ end
130
+
131
+ describe '#issue' do
132
+ let (:id) { '89ea2488fe0a5c7bb38aa7f9b088874a' }
133
+ let (:status) { 'open' }
134
+ let (:api_url) { Faker::Internet.url }
135
+ let (:view_url) { Faker::Internet.url }
136
+ let (:response) do
137
+ {
138
+ id: id,
139
+ status: status,
140
+ api_url: api_url,
141
+ view_url: view_url
142
+ }
143
+ end
144
+ let (:invoice) { Apruve::Invoice.new({id: id, order_id: order_id, amount_cents: amount_cents})}
145
+ describe 'success' do
146
+ let! (:stubs) do
147
+ faraday_stubs do |stub|
148
+ stub.post("/api/v4/invoices/#{id}/issue") { [200, {}, response.to_json] }
149
+ end
150
+ end
151
+ it 'should do a post' do
152
+ invoice.issue!
153
+ expect(invoice.id).to eq id
154
+ expect(invoice.status).to eq status
155
+ stubs.verify_stubbed_calls
156
+ end
157
+ end
158
+
159
+ describe 'invoice request not found' do
160
+ let! (:stubs) do
161
+ faraday_stubs do |stub|
162
+ stub.post("/api/v4/invoices/#{id}/issue") { [404, {}, 'Not Found'] }
163
+ end
164
+ end
165
+ it 'should raise' do
166
+ expect { invoice.issue! }.to raise_error(Apruve::NotFound)
167
+ stubs.verify_stubbed_calls
168
+ end
169
+ end
170
+ end
171
+
172
+ describe '#update' do
173
+ let (:id) { '89ea2488fe0a5c7bb38aa7f9b088874a' }
174
+ let (:status) { 'pending' }
175
+ let (:api_url) { Faker::Internet.url }
176
+ let (:view_url) { Faker::Internet.url }
177
+ let (:response) do
178
+ {
179
+ id: id,
180
+ status: status,
181
+ api_url: api_url,
182
+ view_url: view_url
183
+ }
184
+ end
185
+ let (:invoice) { Apruve::Invoice.new({id: id, order_id: order_id, amount_cents: amount_cents})}
186
+ describe 'success' do
187
+ let! (:stubs) do
188
+ faraday_stubs do |stub|
189
+ stub.patch("/api/v4/invoices/#{id}", invoice.to_json) { [200, {}, response.to_json] }
190
+ end
191
+ end
192
+ it 'should do a patch' do
193
+ invoice.update!
194
+ expect(invoice.status).to eq status
195
+ stubs.verify_stubbed_calls
196
+ end
197
+ end
198
+
199
+ describe 'invoice request not found' do
200
+ let! (:stubs) do
201
+ faraday_stubs do |stub|
202
+ stub.patch("/api/v4/invoices/#{id}", invoice.to_json) { [404, {}, 'Not Found'] }
203
+ end
204
+ end
205
+ it 'should raise' do
206
+ expect { invoice.update! }.to raise_error(Apruve::NotFound)
207
+ stubs.verify_stubbed_calls
208
+ end
209
+ end
210
+ end
211
+
212
+ describe '#close' do
213
+ let (:id) { '89ea2488fe0a5c7bb38aa7f9b088874a' }
214
+ let (:status) { 'pending' }
215
+ let (:api_url) { Faker::Internet.url }
216
+ let (:view_url) { Faker::Internet.url }
217
+ let (:response) do
218
+ {
219
+ id: id,
220
+ status: status,
221
+ api_url: api_url,
222
+ view_url: view_url
223
+ }
224
+ end
225
+ let (:invoice) { Apruve::Invoice.new({id: id, order_id: order_id, amount_cents: amount_cents})}
226
+ describe 'success' do
227
+ let! (:stubs) do
228
+ faraday_stubs do |stub|
229
+ stub.post("/api/v4/invoices/#{id}/close") { [200, {}, response.to_json] }
230
+ end
231
+ end
232
+ it 'should do a post' do
233
+ invoice.close!
234
+ expect(invoice.status).to eq status
235
+ stubs.verify_stubbed_calls
236
+ end
237
+ end
238
+
239
+ describe 'invoice request not found' do
240
+ let! (:stubs) do
241
+ faraday_stubs do |stub|
242
+ stub.post("/api/v4/invoices/#{id}/close") { [404, {}, 'Not Found'] }
243
+ end
244
+ end
245
+ it 'should raise' do
246
+ expect { invoice.close! }.to raise_error(Apruve::NotFound)
247
+ stubs.verify_stubbed_calls
248
+ end
249
+ end
250
+ end
251
+
252
+ describe '#cancel' do
253
+ let (:id) { '89ea2488fe0a5c7bb38aa7f9b088874a' }
254
+ let (:status) { 'pending' }
255
+ let (:api_url) { Faker::Internet.url }
256
+ let (:view_url) { Faker::Internet.url }
257
+ let (:response) do
258
+ {
259
+ id: id,
260
+ status: status,
261
+ api_url: api_url,
262
+ view_url: view_url
263
+ }
264
+ end
265
+ let (:invoice) { Apruve::Invoice.new({id: id, order_id: order_id, amount_cents: amount_cents})}
266
+ describe 'success' do
267
+ let! (:stubs) do
268
+ faraday_stubs do |stub|
269
+ stub.post("/api/v4/invoices/#{id}/cancel") { [200, {}, response.to_json] }
270
+ end
271
+ end
272
+ it 'should do a post' do
273
+ invoice.cancel!
274
+ expect(invoice.status).to eq status
275
+ stubs.verify_stubbed_calls
276
+ end
277
+ end
278
+
279
+ describe 'invoice request not found' do
280
+ let! (:stubs) do
281
+ faraday_stubs do |stub|
282
+ stub.post("/api/v4/invoices/#{id}/cancel") { [404, {}, 'Not Found'] }
283
+ end
284
+ end
285
+ it 'should raise' do
286
+ expect { invoice.cancel! }.to raise_error(Apruve::NotFound)
287
+ stubs.verify_stubbed_calls
288
+ end
289
+ end
290
+ end
291
+ end
@@ -0,0 +1,56 @@
1
+ require 'spec_helper'
2
+
3
+ describe Apruve::Merchant do
4
+ before :each do
5
+ Apruve.configure('f5fbe71d68772d1f562ed6f598b995b3', 'local')
6
+ end
7
+
8
+ let (:id) { '89ea2488fe0a5c7bb38aa7f9b088874a' }
9
+ let (:name) { 'A title' }
10
+ let (:web_url) { Faker::Internet.url }
11
+ let (:email) { Faker::Internet.email }
12
+ let (:phone) { '651-555-1234' }
13
+ let (:merchant) do
14
+ Apruve::Merchant.new(
15
+ id: id,
16
+ name: name,
17
+ web_url: web_url,
18
+ email: email,
19
+ phone: phone
20
+ )
21
+ end
22
+ subject { merchant }
23
+
24
+ # from line_item
25
+ it { should respond_to(:id) }
26
+ it { should respond_to(:name) }
27
+ it { should respond_to(:web_url) }
28
+ it { should respond_to(:email) }
29
+ it { should respond_to(:phone) }
30
+
31
+ describe '#find' do
32
+ describe 'success' do
33
+ let! (:stubs) do
34
+ faraday_stubs do |stub|
35
+ stub.get("/api/v4/merchants/#{id}") { [200, {}, '{}'] }
36
+ end
37
+ end
38
+ it 'should do a get' do
39
+ Apruve::Merchant.find(id)
40
+ stubs.verify_stubbed_calls
41
+ end
42
+ end
43
+
44
+ describe 'not found' do
45
+ let! (:stubs) do
46
+ faraday_stubs do |stub|
47
+ stub.get("/api/v4/merchants/#{id}") { [404, {}, 'Not Found'] }
48
+ end
49
+ end
50
+ it 'should raise' do
51
+ expect { Apruve::Merchant.find(id) }.to raise_error(Apruve::NotFound)
52
+ stubs.verify_stubbed_calls
53
+ end
54
+ end
55
+ end
56
+ end