wizypay-api-client 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/wizypay.rb +3 -0
- data/lib/wizypay/card.rb +18 -5
- data/lib/wizypay/collection.rb +26 -0
- data/lib/wizypay/merchant.rb +11 -2
- data/lib/wizypay/merchant_category.rb +4 -0
- data/lib/wizypay/resource.rb +10 -0
- data/lib/wizypay/version.rb +1 -1
- data/spec/card/cancel_card_spec.rb +44 -34
- data/spec/card/card_url_spec.rb +26 -16
- data/spec/card/create_card_spec.rb +51 -49
- data/spec/merchant/merchant_spec.rb +72 -43
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 426e05d103a20afb933b420f11f2b2ca440d7c09
|
4
|
+
data.tar.gz: 76eaa542040e90e45323248168db754f41bb6260
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c7d032b91a535f34c00d57ac2d65013f677ae75eadff83316259f83b24320cd8a27d20a94436916a0cb15ab7821cf67bae1e0b7be15f9cda8dbf9b7c9b2f1495
|
7
|
+
data.tar.gz: 04967cd33c55cb9ddaba58d5b0b62b47f320c27efe8673800dacd1040d0a5f84dcc64e6d38f7feb206f4dbcaa1d8c846195fc513f94fda333b1cf6308fd82335
|
data/lib/wizypay.rb
CHANGED
@@ -6,7 +6,10 @@ require 'rest-client'
|
|
6
6
|
require 'simple-hmac'
|
7
7
|
require 'json'
|
8
8
|
require_relative 'wizypay/client'
|
9
|
+
require_relative 'wizypay/resource'
|
10
|
+
require_relative 'wizypay/collection'
|
9
11
|
require_relative 'wizypay/card'
|
12
|
+
require_relative 'wizypay/merchant_category'
|
10
13
|
require_relative 'wizypay/merchant'
|
11
14
|
|
12
15
|
module Wizypay
|
data/lib/wizypay/card.rb
CHANGED
@@ -1,16 +1,25 @@
|
|
1
1
|
module Wizypay
|
2
|
-
class Card
|
2
|
+
class Card < Resource
|
3
3
|
def self.create(reference, amount, currency, merchant_id, user)
|
4
|
-
user
|
5
|
-
|
6
|
-
|
4
|
+
user = user.to_json if user.is_a?(Hash)
|
5
|
+
new(amount: amount, currency: currency, merchant_id: merchant_id, reference: reference, user: user).tap do |card|
|
6
|
+
card.save
|
7
|
+
end
|
7
8
|
end
|
8
9
|
|
9
10
|
def self.cancel(reference)
|
10
|
-
|
11
|
+
new(reference: reference).cancel
|
11
12
|
end
|
12
13
|
|
13
14
|
def self.url(reference)
|
15
|
+
new(reference: reference).url
|
16
|
+
end
|
17
|
+
|
18
|
+
def cancel
|
19
|
+
Client.post("/cards/#{URI::encode_www_form_component reference}/cancel")
|
20
|
+
end
|
21
|
+
|
22
|
+
def url
|
14
23
|
timestamp = Time.now.utc.httpdate
|
15
24
|
security = Base64.strict_encode64(OpenSSL::HMAC.digest('sha256', Client.api_secret, "#{reference}\n#{timestamp}"))
|
16
25
|
query_string = {
|
@@ -21,5 +30,9 @@ module Wizypay
|
|
21
30
|
}.map { |k, v| "#{k}=#{URI::encode_www_form_component(v)}" }.join('&')
|
22
31
|
"#{Client.api_endpoint}/widget?#{query_string}"
|
23
32
|
end
|
33
|
+
|
34
|
+
def save
|
35
|
+
reinitialize Client.post('/cards', to_h)
|
36
|
+
end
|
24
37
|
end
|
25
38
|
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class Wizypay::Collection
|
2
|
+
include Enumerable
|
3
|
+
|
4
|
+
def initialize(klass, data, meta = {})
|
5
|
+
@data = data.map {|h| klass.new(h)}
|
6
|
+
@meta = meta
|
7
|
+
end
|
8
|
+
|
9
|
+
def respond_to?(m)
|
10
|
+
data.respond_to?(m) || meta.has_key?(m.to_s) || super
|
11
|
+
end
|
12
|
+
|
13
|
+
def method_missing(m, *args, &block)
|
14
|
+
if data.respond_to?(m)
|
15
|
+
data.send(m, *args, &block)
|
16
|
+
elsif meta.has_key?(m.to_s)
|
17
|
+
meta[m.to_s]
|
18
|
+
else
|
19
|
+
super
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
attr_reader :data, :meta
|
26
|
+
end
|
data/lib/wizypay/merchant.rb
CHANGED
@@ -1,11 +1,20 @@
|
|
1
1
|
module Wizypay
|
2
|
-
class Merchant
|
2
|
+
class Merchant < Resource
|
3
3
|
def self.where(q)
|
4
|
-
Client.get('/merchants', q)
|
4
|
+
raw = Client.get('/merchants', q)
|
5
|
+
Collection.new(self, raw[:data], raw[:meta])
|
5
6
|
end
|
6
7
|
|
7
8
|
def self.all(q = {})
|
8
9
|
where(q)
|
9
10
|
end
|
11
|
+
|
12
|
+
def primary_ad
|
13
|
+
Resource.new(super['data'])
|
14
|
+
end
|
15
|
+
|
16
|
+
def categories
|
17
|
+
Collection.new(MerchantCategory, super['data'], super['meta'])
|
18
|
+
end
|
10
19
|
end
|
11
20
|
end
|
data/lib/wizypay/version.rb
CHANGED
@@ -1,46 +1,56 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
3
|
+
module Wizypay
|
4
|
+
RSpec.describe 'Card#cancel' do
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
end
|
16
|
-
|
17
|
-
context 'success' do
|
18
|
-
it 'returns empty body' do
|
19
|
-
request = cancel_request.to_return(status: 200, body: '')
|
20
|
-
response = Wizypay::Card.cancel(reference)
|
21
|
-
expect(request).to have_been_made
|
22
|
-
expect(response).to be_empty
|
6
|
+
let(:api_key) { 'key' }
|
7
|
+
let(:api_secret) { 'secret' }
|
8
|
+
let(:api_endpoint) { 'https://api.wizypay.com' }
|
9
|
+
let(:reference) { '85fb584c-72cb-4f35-b307-ac5442ef7153' }
|
10
|
+
let(:cancel_request) { stub_request(:post, "#{api_endpoint}/cards/#{reference}/cancel").
|
11
|
+
with(headers: { 'Accept' => 'application/json',
|
12
|
+
'Authorization' => /WIZYPAY #{api_key}:.*/,
|
13
|
+
'Content-Type' => 'application/x-www-form-urlencoded' }) }
|
14
|
+
before do
|
15
|
+
Wizypay.setup(api_key, api_secret, api_endpoint)
|
23
16
|
end
|
24
|
-
end
|
25
17
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
expect do
|
31
|
-
Wizypay::Card.cancel(reference)
|
32
|
-
end.to raise_error(RestClient::Exception)
|
18
|
+
context 'success' do
|
19
|
+
it 'returns empty body' do
|
20
|
+
request = cancel_request.to_return(status: 200, body: '')
|
21
|
+
response = Card.new(reference: reference).cancel
|
33
22
|
expect(request).to have_been_made
|
23
|
+
expect(response).to be_empty
|
34
24
|
end
|
35
25
|
end
|
36
26
|
|
37
|
-
|
38
|
-
it
|
39
|
-
|
40
|
-
expect
|
41
|
-
|
42
|
-
|
43
|
-
|
27
|
+
describe '.cancel' do
|
28
|
+
it 'delegates to an instance' do
|
29
|
+
expect(Card).to receive(:new).with(reference: reference).and_return(card = double('card'))
|
30
|
+
expect(card).to receive(:cancel)
|
31
|
+
Card.cancel(reference)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context 'failure' do
|
36
|
+
context '4XX' do
|
37
|
+
it "throws #{RestClient::Exception}" do
|
38
|
+
request = cancel_request.to_return(status: 401, body: { errors: { base: ['authentication_failed'] } }.to_json)
|
39
|
+
expect do
|
40
|
+
Card.cancel(reference)
|
41
|
+
end.to raise_error(RestClient::Exception)
|
42
|
+
expect(request).to have_been_made
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
context 'timeout' do
|
47
|
+
it "throws #{RestClient::Exception}" do
|
48
|
+
request = cancel_request.to_timeout
|
49
|
+
expect do
|
50
|
+
Card.cancel(reference)
|
51
|
+
end.to raise_error(RestClient::Exception)
|
52
|
+
expect(request).to have_been_made
|
53
|
+
end
|
44
54
|
end
|
45
55
|
end
|
46
56
|
end
|
data/spec/card/card_url_spec.rb
CHANGED
@@ -1,25 +1,35 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
require 'time'
|
3
3
|
|
4
|
-
|
4
|
+
module Wizypay
|
5
|
+
RSpec.describe 'Card#url' do
|
5
6
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
let(:api_key) { 'key' }
|
8
|
+
let(:api_secret) { 'secret' }
|
9
|
+
let(:api_endpoint) { 'https://api.wizypay.com' }
|
10
|
+
let(:reference) { '85fb584c-72cb-4f35-b307-ac5442ef7153' }
|
10
11
|
|
11
|
-
|
12
|
-
|
13
|
-
|
12
|
+
before do
|
13
|
+
Wizypay.setup(api_key, api_secret, api_endpoint)
|
14
|
+
end
|
15
|
+
|
16
|
+
it 'returns' do
|
17
|
+
t = Time.new('2015-10-30T21:00:00+001')
|
18
|
+
Timecop.freeze(t) do
|
19
|
+
url = Card.new(reference: reference).url
|
20
|
+
expect(url).to eq("#{api_endpoint}/widget?key=#{URI::encode_www_form_component api_key}"+
|
21
|
+
"×tamp=#{URI::encode_www_form_component t.httpdate}" +
|
22
|
+
"&reference=#{URI::encode_www_form_component reference}"+
|
23
|
+
"&security=0fKNCz6hux0IjNCz31JHgzh35gqLTAIM8z3iNhSkC7g%3D")
|
24
|
+
end
|
25
|
+
end
|
14
26
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
"&reference=#{URI::encode_www_form_component reference}"+
|
22
|
-
"&security=0fKNCz6hux0IjNCz31JHgzh35gqLTAIM8z3iNhSkC7g%3D")
|
27
|
+
describe '.url' do
|
28
|
+
it 'delegates to an instance' do
|
29
|
+
expect(Card).to receive(:new).with(reference: reference).and_return(card = double('card'))
|
30
|
+
expect(card).to receive(:url)
|
31
|
+
Card.url(reference)
|
32
|
+
end
|
23
33
|
end
|
24
34
|
end
|
25
35
|
end
|
@@ -1,60 +1,62 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
end
|
22
|
-
|
23
|
-
context 'success' do
|
24
|
-
it 'returns response as hash' do
|
25
|
-
response_body = {
|
26
|
-
reference: reference,
|
27
|
-
type: 'Mastercard',
|
28
|
-
amount: amount,
|
29
|
-
currency: currency,
|
30
|
-
merchant_id: merchant_id,
|
31
|
-
user_id: user[:id]
|
32
|
-
}
|
33
|
-
request = create_request.to_return(status: 200, body: response_body.to_json)
|
34
|
-
response = Wizypay::Card.create(reference, amount, currency, merchant_id, user)
|
35
|
-
expect(request).to have_been_made
|
36
|
-
expect(response).to match(response_body)
|
3
|
+
module Wizypay
|
4
|
+
RSpec.describe 'Card.create' do
|
5
|
+
let(:api_key) { 'key' }
|
6
|
+
let(:api_secret) { 'secret' }
|
7
|
+
let(:api_endpoint) { 'https://api.wizypay.com' }
|
8
|
+
let(:user) { { id: 33, first_name: 'Nicolas', last_name: 'Cage' } }
|
9
|
+
let(:reference) { '85fb584c-72cb-4f35-b307-ac5442ef7153' }
|
10
|
+
let(:merchant_id) { '91' }
|
11
|
+
let(:amount) { '5000' }
|
12
|
+
let(:currency) { 'EUR' }
|
13
|
+
let(:create_request) { stub_request(:post, "#{api_endpoint}/cards").
|
14
|
+
with(body: { amount: amount, currency: currency, merchant_id: merchant_id,
|
15
|
+
reference: reference, user: user.to_json },
|
16
|
+
headers: { 'Accept' => 'application/json',
|
17
|
+
'Authorization' => /WIZYPAY #{api_key}:.*/,
|
18
|
+
'Content-Type' => 'application/x-www-form-urlencoded' }) }
|
19
|
+
before do
|
20
|
+
Wizypay.setup(api_key, api_secret, api_endpoint)
|
37
21
|
end
|
38
|
-
end
|
39
22
|
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
23
|
+
context 'success' do
|
24
|
+
it 'returns a Card with correct data' do
|
25
|
+
response_body = {
|
26
|
+
reference: reference,
|
27
|
+
type: 'Mastercard',
|
28
|
+
amount: amount,
|
29
|
+
currency: currency,
|
30
|
+
merchant_id: merchant_id,
|
31
|
+
user_id: user[:id]
|
32
|
+
}
|
33
|
+
request = create_request.to_return(status: 200, body: response_body.to_json)
|
34
|
+
response = Wizypay::Card.create(reference, amount, currency, merchant_id, user)
|
47
35
|
expect(request).to have_been_made
|
36
|
+
expect(response).to be_a(Wizypay::Card)
|
37
|
+
expect(response.to_h).to match(response_body)
|
48
38
|
end
|
49
39
|
end
|
50
40
|
|
51
|
-
context '
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
41
|
+
context 'failure' do
|
42
|
+
context '4XX' do
|
43
|
+
it "throws #{RestClient::Exception}" do
|
44
|
+
request = create_request.to_return(status: 401, body: { errors: { base: ['authentication_failed'] } }.to_json)
|
45
|
+
expect do
|
46
|
+
Card.create(reference, amount, currency, merchant_id, user)
|
47
|
+
end.to raise_error(RestClient::Exception)
|
48
|
+
expect(request).to have_been_made
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
context 'timeout' do
|
53
|
+
it "throws #{RestClient::Exception}" do
|
54
|
+
request = create_request.to_timeout
|
55
|
+
expect do
|
56
|
+
Card.create(reference, amount, currency, merchant_id, user)
|
57
|
+
end.to raise_error(RestClient::Exception)
|
58
|
+
expect(request).to have_been_made
|
59
|
+
end
|
58
60
|
end
|
59
61
|
end
|
60
62
|
end
|
@@ -1,56 +1,85 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
end
|
17
|
-
|
18
|
-
context 'all' do
|
19
|
-
it 'returns empty body' do
|
20
|
-
request = all_merchants_request.to_return(status: 200, body: '{"data": []}')
|
21
|
-
response = Wizypay::Merchant.all
|
22
|
-
expect(request).to have_been_made
|
23
|
-
expect(response).to be_empty
|
3
|
+
module Wizypay
|
4
|
+
RSpec.describe Merchant do
|
5
|
+
let(:api_key) { 'key' }
|
6
|
+
let(:api_secret) { 'secret' }
|
7
|
+
let(:api_endpoint) { 'https://api.wizypay.com' }
|
8
|
+
let(:all_merchants_request) { stub_request(:get, "#{api_endpoint}/merchants").
|
9
|
+
with(headers: { 'Accept' => 'application/json',
|
10
|
+
'Authorization' => /WIZYPAY #{api_key}:.*/ }) }
|
11
|
+
let(:where_merchants_request) { stub_request(:get, "#{api_endpoint}/merchants?q=hp").
|
12
|
+
with(headers: { 'Accept' => 'application/json',
|
13
|
+
'Authorization' => /WIZYPAY #{api_key}:.*/ }) }
|
14
|
+
before do
|
15
|
+
Wizypay.setup(api_key, api_secret, api_endpoint)
|
24
16
|
end
|
25
|
-
end
|
26
17
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
18
|
+
context 'all' do
|
19
|
+
it 'returns empty body' do
|
20
|
+
request = all_merchants_request.to_return(status: 200, body: '{"data": []}')
|
21
|
+
response = Merchant.all
|
22
|
+
expect(request).to have_been_made
|
23
|
+
expect(response).to be_empty
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'is a collection' do
|
27
|
+
all_merchants_request.to_return(status: 200, body: '{"data": [], "meta": {"total": 0}}')
|
28
|
+
response = Merchant.all
|
29
|
+
expect(response).to be_a(Collection)
|
30
|
+
expect(response.total).to eq 0
|
31
|
+
end
|
32
|
+
|
33
|
+
it 'is a collection of Merchants' do
|
34
|
+
all_merchants_request.to_return(status: 200, body: '{"data": [{"id": 1, "name": "merchant 1"}]}')
|
35
|
+
response = Merchant.all
|
36
|
+
expect(response.first).to be_a(Merchant)
|
37
|
+
expect(response.first.name).to eq 'merchant 1'
|
38
|
+
end
|
39
|
+
|
40
|
+
it 'has a primary ad' do
|
41
|
+
all_merchants_request.to_return(status: 200, body: '{"data": [{"primary_ad": {"data": {"id": 1}}}]}')
|
42
|
+
response = Merchant.all
|
43
|
+
expect(response.first.primary_ad.id).to eq 1
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'has categories' do
|
47
|
+
all_merchants_request.to_return(status: 200, body: '{"data": [{"categories": {"data": [{"id": 1}]}}]}')
|
48
|
+
response = Merchant.all
|
49
|
+
expect(response.first.categories).to be_a(Collection)
|
50
|
+
expect(response.first.categories.first).to be_a(MerchantCategory)
|
51
|
+
expect(response.first.categories.first.id).to eq 1
|
52
|
+
end
|
33
53
|
end
|
34
|
-
end
|
35
54
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
expect do
|
41
|
-
Wizypay::Merchant.all
|
42
|
-
end.to raise_error(RestClient::Exception)
|
55
|
+
context 'where' do
|
56
|
+
it 'returns empty body' do
|
57
|
+
request = where_merchants_request.to_return(status: 200, body: '{"data": []}')
|
58
|
+
response = Merchant.where(q: 'hp')
|
43
59
|
expect(request).to have_been_made
|
60
|
+
expect(response).to be_empty
|
44
61
|
end
|
45
62
|
end
|
46
63
|
|
47
|
-
context '
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
64
|
+
context 'failure' do
|
65
|
+
context '4XX' do
|
66
|
+
it "throws #{RestClient::Exception}" do
|
67
|
+
request = all_merchants_request.to_return(status: 401, body: { errors: { base: ['authentication_failed'] } }.to_json)
|
68
|
+
expect do
|
69
|
+
Merchant.all
|
70
|
+
end.to raise_error(RestClient::Exception)
|
71
|
+
expect(request).to have_been_made
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
context 'timeout' do
|
76
|
+
it "throws #{RestClient::Exception}" do
|
77
|
+
request = all_merchants_request.to_timeout
|
78
|
+
expect do
|
79
|
+
Merchant.all
|
80
|
+
end.to raise_error(RestClient::Exception)
|
81
|
+
expect(request).to have_been_made
|
82
|
+
end
|
54
83
|
end
|
55
84
|
end
|
56
85
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wizypay-api-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chaker Nakhli
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2016-01-
|
13
|
+
date: 2016-01-29 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: simple-hmac
|
@@ -138,7 +138,10 @@ files:
|
|
138
138
|
- lib/wizypay.rb
|
139
139
|
- lib/wizypay/card.rb
|
140
140
|
- lib/wizypay/client.rb
|
141
|
+
- lib/wizypay/collection.rb
|
141
142
|
- lib/wizypay/merchant.rb
|
143
|
+
- lib/wizypay/merchant_category.rb
|
144
|
+
- lib/wizypay/resource.rb
|
142
145
|
- lib/wizypay/version.rb
|
143
146
|
- spec/card/cancel_card_spec.rb
|
144
147
|
- spec/card/card_url_spec.rb
|