paysio 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.travis.yml +0 -4
- data/Gemfile.lock +4 -18
- data/README.rdoc +20 -3
- data/Rakefile +0 -10
- data/lib/paysio.rb +32 -245
- data/lib/paysio/actions/create.rb +17 -0
- data/lib/paysio/actions/destroy.rb +10 -0
- data/lib/paysio/actions/find.rb +16 -0
- data/lib/paysio/actions/list.rb +28 -0
- data/lib/paysio/actions/update.rb +11 -0
- data/lib/paysio/client.rb +55 -0
- data/lib/paysio/json.rb +7 -15
- data/lib/paysio/resource.rb +98 -0
- data/lib/paysio/resources/charge.rb +18 -0
- data/lib/paysio/resources/coupon.rb +13 -0
- data/lib/paysio/resources/customer.rb +13 -0
- data/lib/paysio/resources/event.rb +7 -0
- data/lib/paysio/resources/list.rb +19 -0
- data/lib/paysio/resources/log.rb +7 -0
- data/lib/paysio/resources/reward.rb +13 -0
- data/lib/paysio/resources/wallet.rb +13 -0
- data/lib/paysio/version.rb +1 -1
- data/paysio.gemspec +2 -4
- metadata +26 -79
- data/gemfiles/default-with-activesupport.gemfile +0 -3
- data/gemfiles/json.gemfile +0 -4
- data/gemfiles/yajl.gemfile +0 -4
- data/lib/data/ca-certificates.crt +0 -3918
- data/lib/paysio/account.rb +0 -4
- data/lib/paysio/api_operations/create.rb +0 -16
- data/lib/paysio/api_operations/delete.rb +0 -11
- data/lib/paysio/api_operations/list.rb +0 -16
- data/lib/paysio/api_operations/update.rb +0 -15
- data/lib/paysio/api_resource.rb +0 -33
- data/lib/paysio/charge.rb +0 -39
- data/lib/paysio/coupon.rb +0 -7
- data/lib/paysio/customer.rb +0 -51
- data/lib/paysio/errors/api_connection_error.rb +0 -4
- data/lib/paysio/errors/api_error.rb +0 -4
- data/lib/paysio/errors/authentication_error.rb +0 -4
- data/lib/paysio/errors/card_error.rb +0 -11
- data/lib/paysio/errors/invalid_request_error.rb +0 -10
- data/lib/paysio/errors/paysio_error.rb +0 -20
- data/lib/paysio/event.rb +0 -5
- data/lib/paysio/list_object.rb +0 -14
- data/lib/paysio/paysio_object.rb +0 -159
- data/lib/paysio/singleton_api_resource.rb +0 -20
- data/lib/paysio/util.rb +0 -100
- data/test/test_helper.rb +0 -162
- data/test/test_paysio.rb +0 -479
- data/test/test_paysio_with_active_support.rb +0 -2
@@ -1,20 +0,0 @@
|
|
1
|
-
module Paysio
|
2
|
-
class SingletonAPIResource < APIResource
|
3
|
-
def self.url()
|
4
|
-
if self == SingletonAPIResource
|
5
|
-
raise NotImplementedError.new('SingletonAPIResource is an abstract class. You should perform actions on its subclasses (Account, etc.)')
|
6
|
-
end
|
7
|
-
"/v1/#{CGI.escape(class_name.downcase)}"
|
8
|
-
end
|
9
|
-
|
10
|
-
def url
|
11
|
-
self.class.url
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.retrieve(api_key=nil)
|
15
|
-
instance = self.new(nil, api_key)
|
16
|
-
instance.refresh
|
17
|
-
instance
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
data/lib/paysio/util.rb
DELETED
@@ -1,100 +0,0 @@
|
|
1
|
-
module Paysio
|
2
|
-
module Util
|
3
|
-
def self.objects_to_ids(h)
|
4
|
-
case h
|
5
|
-
when APIResource
|
6
|
-
h.id
|
7
|
-
when Hash
|
8
|
-
res = {}
|
9
|
-
h.each { |k, v| res[k] = objects_to_ids(v) unless v.nil? }
|
10
|
-
res
|
11
|
-
when Array
|
12
|
-
h.map { |v| objects_to_ids(v) }
|
13
|
-
else
|
14
|
-
h
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
def self.convert_to_paysio_object(resp, api_key)
|
19
|
-
types = {
|
20
|
-
'charge' => Charge,
|
21
|
-
'customer' => Customer,
|
22
|
-
'coupon' => Coupon,
|
23
|
-
'event' => Event,
|
24
|
-
'list' => ListObject
|
25
|
-
}
|
26
|
-
case resp
|
27
|
-
when Array
|
28
|
-
resp.map { |i| convert_to_paysio_object(i, api_key) }
|
29
|
-
when Hash
|
30
|
-
# Try converting to a known object class. If none available, fall back to generic APIResource
|
31
|
-
if klass_name = resp[:object]
|
32
|
-
klass = types[klass_name]
|
33
|
-
end
|
34
|
-
klass ||= PaysioObject
|
35
|
-
klass.construct_from(resp, api_key)
|
36
|
-
else
|
37
|
-
resp
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
def self.file_readable(file)
|
42
|
-
begin
|
43
|
-
File.open(file) { |f| }
|
44
|
-
rescue
|
45
|
-
false
|
46
|
-
else
|
47
|
-
true
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
def self.symbolize_names(object)
|
52
|
-
case object
|
53
|
-
when Hash
|
54
|
-
new = {}
|
55
|
-
object.each do |key, value|
|
56
|
-
key = (key.to_sym rescue key) || key
|
57
|
-
new[key] = symbolize_names(value)
|
58
|
-
end
|
59
|
-
new
|
60
|
-
when Array
|
61
|
-
object.map { |value| symbolize_names(value) }
|
62
|
-
else
|
63
|
-
object
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
def self.url_encode(key)
|
68
|
-
URI.escape(key.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
|
69
|
-
end
|
70
|
-
|
71
|
-
def self.flatten_params(params, parent_key=nil)
|
72
|
-
result = []
|
73
|
-
params.each do |key, value|
|
74
|
-
calculated_key = parent_key ? "#{parent_key}[#{url_encode(key)}]" : url_encode(key)
|
75
|
-
if value.is_a?(Hash)
|
76
|
-
result += flatten_params(value, calculated_key)
|
77
|
-
elsif value.is_a?(Array)
|
78
|
-
result += flatten_params_array(value, calculated_key)
|
79
|
-
else
|
80
|
-
result << [calculated_key, value]
|
81
|
-
end
|
82
|
-
end
|
83
|
-
result
|
84
|
-
end
|
85
|
-
|
86
|
-
def self.flatten_params_array(value, calculated_key)
|
87
|
-
result = []
|
88
|
-
value.each do |elem|
|
89
|
-
if elem.is_a?(Hash)
|
90
|
-
result += flatten_params(elem, calculated_key)
|
91
|
-
elsif elem.is_a?(Array)
|
92
|
-
result += flatten_params_array(elem, calculated_key)
|
93
|
-
else
|
94
|
-
result << ["#{calculated_key}[]", elem]
|
95
|
-
end
|
96
|
-
end
|
97
|
-
result
|
98
|
-
end
|
99
|
-
end
|
100
|
-
end
|
data/test/test_helper.rb
DELETED
@@ -1,162 +0,0 @@
|
|
1
|
-
require 'stringio'
|
2
|
-
require 'test/unit'
|
3
|
-
require 'paysio'
|
4
|
-
require 'mocha'
|
5
|
-
include Mocha
|
6
|
-
|
7
|
-
#monkeypatch request methods
|
8
|
-
module Paysio
|
9
|
-
@mock_rest_client = nil
|
10
|
-
|
11
|
-
def self.mock_rest_client=(mock_client)
|
12
|
-
@mock_rest_client = mock_client
|
13
|
-
end
|
14
|
-
|
15
|
-
def self.execute_request(opts)
|
16
|
-
get_params = (opts[:headers] || {})[:params]
|
17
|
-
post_params = opts[:payload]
|
18
|
-
case opts[:method]
|
19
|
-
when :get then @mock_rest_client.get opts[:url], get_params, post_params
|
20
|
-
when :post then @mock_rest_client.post opts[:url], get_params, post_params
|
21
|
-
when :delete then @mock_rest_client.delete opts[:url], get_params, post_params
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_response(body, code=200)
|
27
|
-
# When an exception is raised, restclient clobbers method_missing. Hence we
|
28
|
-
# can't just use the stubs interface.
|
29
|
-
body = MultiJson.dump(body) if !(body.kind_of? String)
|
30
|
-
m = mock
|
31
|
-
m.instance_variable_set('@paysio_values', { :body => body, :code => code })
|
32
|
-
def m.body; @paysio_values[:body]; end
|
33
|
-
def m.code; @paysio_values[:code]; end
|
34
|
-
m
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_customer(params={})
|
38
|
-
{
|
39
|
-
:subscription_history => [],
|
40
|
-
:bills => [],
|
41
|
-
:charges => [],
|
42
|
-
:livemode => false,
|
43
|
-
:object => "customer",
|
44
|
-
:id => "c_test_customer",
|
45
|
-
:active_card => {
|
46
|
-
:type => "Visa",
|
47
|
-
:last4 => "4242",
|
48
|
-
:exp_month => 11,
|
49
|
-
:country => "US",
|
50
|
-
:exp_year => 2012,
|
51
|
-
:id => "cc_test_card",
|
52
|
-
:object => "card"
|
53
|
-
},
|
54
|
-
:created => 1304114758
|
55
|
-
}.merge(params)
|
56
|
-
end
|
57
|
-
|
58
|
-
def test_customer_array
|
59
|
-
{
|
60
|
-
:data => [test_customer, test_customer, test_customer],
|
61
|
-
:object => 'list',
|
62
|
-
:url => '/v1/customers'
|
63
|
-
}
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_charge(params={})
|
67
|
-
{
|
68
|
-
:refunded => false,
|
69
|
-
:paid => true,
|
70
|
-
:amount => 100,
|
71
|
-
:card => {
|
72
|
-
:type => "Visa",
|
73
|
-
:last4 => "4242",
|
74
|
-
:exp_month => 11,
|
75
|
-
:country => "US",
|
76
|
-
:exp_year => 2012,
|
77
|
-
:id => "cc_test_card",
|
78
|
-
:object => "card"
|
79
|
-
},
|
80
|
-
:id => "ch_test_charge",
|
81
|
-
:reason => "execute_charge",
|
82
|
-
:livemode => false,
|
83
|
-
:currency => "usd",
|
84
|
-
:object => "charge",
|
85
|
-
:created => 1304114826
|
86
|
-
}.merge(params)
|
87
|
-
end
|
88
|
-
|
89
|
-
def test_charge_array
|
90
|
-
{
|
91
|
-
:data => [test_charge, test_charge, test_charge],
|
92
|
-
:object => 'list',
|
93
|
-
:url => '/v1/charges'
|
94
|
-
}
|
95
|
-
end
|
96
|
-
|
97
|
-
def test_card(params={})
|
98
|
-
{
|
99
|
-
:type => "Visa",
|
100
|
-
:last4 => "4242",
|
101
|
-
:exp_month => 11,
|
102
|
-
:country => "US",
|
103
|
-
:exp_year => 2012,
|
104
|
-
:id => "cc_test_card",
|
105
|
-
:object => "card"
|
106
|
-
}.merge(params)
|
107
|
-
end
|
108
|
-
|
109
|
-
def test_coupon(params={})
|
110
|
-
{
|
111
|
-
:duration => 'repeating',
|
112
|
-
:duration_in_months => 3,
|
113
|
-
:percent_off => 25,
|
114
|
-
:id => "co_test_coupon",
|
115
|
-
:object => "coupon"
|
116
|
-
}.merge(params)
|
117
|
-
end
|
118
|
-
|
119
|
-
def test_invalid_api_key_error
|
120
|
-
{
|
121
|
-
"error" => {
|
122
|
-
"type" => "invalid_request_error",
|
123
|
-
"message" => "Invalid API Key provided: invalid"
|
124
|
-
}
|
125
|
-
}
|
126
|
-
end
|
127
|
-
|
128
|
-
def test_invalid_exp_year_error
|
129
|
-
{
|
130
|
-
"error" => {
|
131
|
-
"code" => "invalid_expiry_year",
|
132
|
-
"param" => "exp_year",
|
133
|
-
"type" => "card_error",
|
134
|
-
"message" => "Your card's expiration year is invalid"
|
135
|
-
}
|
136
|
-
}
|
137
|
-
end
|
138
|
-
|
139
|
-
def test_missing_id_error
|
140
|
-
{
|
141
|
-
:error => {
|
142
|
-
:param => "id",
|
143
|
-
:type => "invalid_request_error",
|
144
|
-
:message => "Missing id"
|
145
|
-
}
|
146
|
-
}
|
147
|
-
end
|
148
|
-
|
149
|
-
def test_api_error
|
150
|
-
{
|
151
|
-
:error => {
|
152
|
-
:type => "api_error"
|
153
|
-
}
|
154
|
-
}
|
155
|
-
end
|
156
|
-
|
157
|
-
def test_delete_discount_response
|
158
|
-
{
|
159
|
-
:deleted => true,
|
160
|
-
:id => "di_test_coupon"
|
161
|
-
}
|
162
|
-
end
|
data/test/test_paysio.rb
DELETED
@@ -1,479 +0,0 @@
|
|
1
|
-
# -*- coding: utf-8 -*-
|
2
|
-
require File.expand_path('../test_helper', __FILE__)
|
3
|
-
require 'test/unit'
|
4
|
-
require 'shoulda'
|
5
|
-
require 'mocha'
|
6
|
-
require 'pp'
|
7
|
-
require 'rest-client'
|
8
|
-
require 'cgi'
|
9
|
-
require 'uri'
|
10
|
-
|
11
|
-
class TestPaysioRuby < Test::Unit::TestCase
|
12
|
-
include Mocha
|
13
|
-
|
14
|
-
context "Util" do
|
15
|
-
should "symbolize_names should convert names to symbols" do
|
16
|
-
start = {
|
17
|
-
'foo' => 'bar',
|
18
|
-
'array' => [{ 'foo' => 'bar' }],
|
19
|
-
'nested' => {
|
20
|
-
1 => 2,
|
21
|
-
:symbol => 9,
|
22
|
-
'string' => nil
|
23
|
-
}
|
24
|
-
}
|
25
|
-
finish = {
|
26
|
-
:foo => 'bar',
|
27
|
-
:array => [{ :foo => 'bar' }],
|
28
|
-
:nested => {
|
29
|
-
1 => 2,
|
30
|
-
:symbol => 9,
|
31
|
-
:string => nil
|
32
|
-
}
|
33
|
-
}
|
34
|
-
|
35
|
-
symbolized = Paysio::Util.symbolize_names(start)
|
36
|
-
assert_equal(finish, symbolized)
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
context "API Bindings" do
|
41
|
-
setup do
|
42
|
-
@mock = mock
|
43
|
-
Paysio.mock_rest_client = @mock
|
44
|
-
end
|
45
|
-
|
46
|
-
teardown do
|
47
|
-
Paysio.mock_rest_client = nil
|
48
|
-
end
|
49
|
-
|
50
|
-
should "creating a new APIResource should not fetch over the network" do
|
51
|
-
@mock.expects(:get).never
|
52
|
-
c = Paysio::Customer.new("someid")
|
53
|
-
end
|
54
|
-
|
55
|
-
should "creating a new APIResource from a hash should not fetch over the network" do
|
56
|
-
@mock.expects(:get).never
|
57
|
-
c = Paysio::Customer.construct_from({
|
58
|
-
:id => "somecustomer",
|
59
|
-
:card => {:id => "somecard", :object => "card"},
|
60
|
-
:object => "customer"
|
61
|
-
})
|
62
|
-
end
|
63
|
-
|
64
|
-
should "setting an attribute should not cause a network request" do
|
65
|
-
@mock.expects(:get).never
|
66
|
-
@mock.expects(:post).never
|
67
|
-
c = Paysio::Customer.new("test_customer");
|
68
|
-
c.card = {:id => "somecard", :object => "card"}
|
69
|
-
end
|
70
|
-
|
71
|
-
should "accessing id should not issue a fetch" do
|
72
|
-
@mock.expects(:get).never
|
73
|
-
c = Paysio::Customer.new("test_customer");
|
74
|
-
c.id
|
75
|
-
end
|
76
|
-
|
77
|
-
should "not specifying api credentials should raise an exception" do
|
78
|
-
Paysio.api_key = nil
|
79
|
-
assert_raises Paysio::AuthenticationError do
|
80
|
-
Paysio::Customer.new("test_customer").refresh
|
81
|
-
end
|
82
|
-
end
|
83
|
-
|
84
|
-
should "specifying invalid api credentials should raise an exception" do
|
85
|
-
Paysio.api_key = "invalid"
|
86
|
-
response = test_response(test_invalid_api_key_error, 401)
|
87
|
-
assert_raises Paysio::AuthenticationError do
|
88
|
-
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 401))
|
89
|
-
Paysio::Customer.retrieve("failing_customer")
|
90
|
-
end
|
91
|
-
end
|
92
|
-
|
93
|
-
should "AuthenticationErrors should have an http status, http body, and JSON body" do
|
94
|
-
Paysio.api_key = "invalid"
|
95
|
-
response = test_response(test_invalid_api_key_error, 401)
|
96
|
-
begin
|
97
|
-
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 401))
|
98
|
-
Paysio::Customer.retrieve("failing_customer")
|
99
|
-
rescue Paysio::AuthenticationError => e
|
100
|
-
assert_equal(401, e.http_status)
|
101
|
-
assert_equal(true, !!e.http_body)
|
102
|
-
assert_equal(true, !!e.json_body[:error][:message])
|
103
|
-
assert_equal(test_invalid_api_key_error['error']['message'], e.json_body[:error][:message])
|
104
|
-
end
|
105
|
-
end
|
106
|
-
|
107
|
-
context "with valid credentials" do
|
108
|
-
setup do
|
109
|
-
Paysio.api_key="foo"
|
110
|
-
end
|
111
|
-
|
112
|
-
teardown do
|
113
|
-
Paysio.api_key=nil
|
114
|
-
end
|
115
|
-
|
116
|
-
should "urlencode values in GET params" do
|
117
|
-
response = test_response(test_charge_array)
|
118
|
-
@mock.expects(:get).with("#{Paysio.api_base}/v1/charges?customer=test%20customer", nil, nil).returns(response)
|
119
|
-
charges = Paysio::Charge.all(:customer => 'test customer').data
|
120
|
-
assert charges.kind_of? Array
|
121
|
-
end
|
122
|
-
|
123
|
-
should "a 400 should give an InvalidRequestError with http status, body, and JSON body" do
|
124
|
-
response = test_response(test_missing_id_error, 400)
|
125
|
-
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 404))
|
126
|
-
begin
|
127
|
-
Paysio::Customer.retrieve("foo")
|
128
|
-
rescue Paysio::InvalidRequestError => e
|
129
|
-
assert_equal(400, e.http_status)
|
130
|
-
assert_equal(true, !!e.http_body)
|
131
|
-
assert_equal(true, e.json_body.kind_of?(Hash))
|
132
|
-
end
|
133
|
-
end
|
134
|
-
|
135
|
-
should "a 401 should give an AuthenticationError with http status, body, and JSON body" do
|
136
|
-
response = test_response(test_missing_id_error, 401)
|
137
|
-
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 404))
|
138
|
-
begin
|
139
|
-
Paysio::Customer.retrieve("foo")
|
140
|
-
rescue Paysio::AuthenticationError => e
|
141
|
-
assert_equal(401, e.http_status)
|
142
|
-
assert_equal(true, !!e.http_body)
|
143
|
-
assert_equal(true, e.json_body.kind_of?(Hash))
|
144
|
-
end
|
145
|
-
end
|
146
|
-
|
147
|
-
should "a 402 should give a CardError with http status, body, and JSON body" do
|
148
|
-
response = test_response(test_missing_id_error, 402)
|
149
|
-
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 404))
|
150
|
-
begin
|
151
|
-
Paysio::Customer.retrieve("foo")
|
152
|
-
rescue Paysio::CardError => e
|
153
|
-
assert_equal(402, e.http_status)
|
154
|
-
assert_equal(true, !!e.http_body)
|
155
|
-
assert_equal(true, e.json_body.kind_of?(Hash))
|
156
|
-
end
|
157
|
-
end
|
158
|
-
|
159
|
-
should "a 404 should give an InvalidRequestError with http status, body, and JSON body" do
|
160
|
-
response = test_response(test_missing_id_error, 404)
|
161
|
-
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 404))
|
162
|
-
begin
|
163
|
-
Paysio::Customer.retrieve("foo")
|
164
|
-
rescue Paysio::InvalidRequestError => e
|
165
|
-
assert_equal(404, e.http_status)
|
166
|
-
assert_equal(true, !!e.http_body)
|
167
|
-
assert_equal(true, e.json_body.kind_of?(Hash))
|
168
|
-
end
|
169
|
-
end
|
170
|
-
|
171
|
-
should "setting a nil value for a param should exclude that param from the request" do
|
172
|
-
@mock.expects(:get).with do |url, api_key, params|
|
173
|
-
uri = URI(url)
|
174
|
-
query = CGI.parse(uri.query)
|
175
|
-
(url =~ %r{^#{Paysio.api_base}/v1/charges?} &&
|
176
|
-
query.keys.sort == ['offset', 'sad'])
|
177
|
-
end.returns(test_response({ :count => 1, :data => [test_charge] }))
|
178
|
-
c = Paysio::Charge.all(:count => nil, :offset => 5, :sad => false)
|
179
|
-
|
180
|
-
@mock.expects(:post).with do |url, api_key, params|
|
181
|
-
url == "#{Paysio.api_base}/v1/charges" && api_key.nil? && CGI.parse(params) == { 'amount' => ['50'], 'currency' => ['usd'] }
|
182
|
-
end.returns(test_response({ :count => 1, :data => [test_charge] }))
|
183
|
-
c = Paysio::Charge.create(:amount => 50, :currency => 'usd', :card => { :number => nil })
|
184
|
-
end
|
185
|
-
|
186
|
-
should "requesting with a unicode ID should result in a request" do
|
187
|
-
response = test_response(test_missing_id_error, 404)
|
188
|
-
@mock.expects(:get).once.with("#{Paysio.api_base}/v1/customers/%E2%98%83", nil, nil).raises(RestClient::ExceptionWithResponse.new(response, 404))
|
189
|
-
c = Paysio::Customer.new("☃")
|
190
|
-
assert_raises(Paysio::InvalidRequestError) { c.refresh }
|
191
|
-
end
|
192
|
-
|
193
|
-
should "requesting with no ID should result in an InvalidRequestError with no request" do
|
194
|
-
c = Paysio::Customer.new
|
195
|
-
assert_raises(Paysio::InvalidRequestError) { c.refresh }
|
196
|
-
end
|
197
|
-
|
198
|
-
should "making a GET request with parameters should have a query string and no body" do
|
199
|
-
params = { :limit => 1 }
|
200
|
-
@mock.expects(:get).once.with("#{Paysio.api_base}/v1/charges?limit=1", nil, nil).returns(test_response([test_charge]))
|
201
|
-
c = Paysio::Charge.all(params)
|
202
|
-
end
|
203
|
-
|
204
|
-
should "making a POST request with parameters should have a body and no query string" do
|
205
|
-
params = { :amount => 100, :currency => 'usd', :card => 'sc_token' }
|
206
|
-
@mock.expects(:post).once.with do |url, get, post|
|
207
|
-
get.nil? && CGI.parse(post) == {'amount' => ['100'], 'currency' => ['usd'], 'card' => ['sc_token']}
|
208
|
-
end.returns(test_response(test_charge))
|
209
|
-
c = Paysio::Charge.create(params)
|
210
|
-
end
|
211
|
-
|
212
|
-
should "loading an object should issue a GET request" do
|
213
|
-
@mock.expects(:get).once.returns(test_response(test_customer))
|
214
|
-
c = Paysio::Customer.new("test_customer")
|
215
|
-
c.refresh
|
216
|
-
end
|
217
|
-
|
218
|
-
should "using array accessors should be the same as the method interface" do
|
219
|
-
@mock.expects(:get).once.returns(test_response(test_customer))
|
220
|
-
c = Paysio::Customer.new("test_customer")
|
221
|
-
c.refresh
|
222
|
-
assert_equal c.created, c[:created]
|
223
|
-
assert_equal c.created, c['created']
|
224
|
-
c['created'] = 12345
|
225
|
-
assert_equal c.created, 12345
|
226
|
-
end
|
227
|
-
|
228
|
-
should "accessing a property other than id or parent on an unfetched object should fetch it" do
|
229
|
-
@mock.expects(:get).once.returns(test_response(test_customer))
|
230
|
-
c = Paysio::Customer.new("test_customer")
|
231
|
-
c.charges
|
232
|
-
end
|
233
|
-
|
234
|
-
should "updating an object should issue a POST request with only the changed properties" do
|
235
|
-
@mock.expects(:post).with do |url, api_key, params|
|
236
|
-
url == "#{Paysio.api_base}/v1/customers/c_test_customer" && api_key.nil? && CGI.parse(params) == {'mnemonic' => ['another_mn']}
|
237
|
-
end.once.returns(test_response(test_customer))
|
238
|
-
c = Paysio::Customer.construct_from(test_customer)
|
239
|
-
c.mnemonic = "another_mn"
|
240
|
-
c.save
|
241
|
-
end
|
242
|
-
|
243
|
-
should "updating should merge in returned properties" do
|
244
|
-
@mock.expects(:post).once.returns(test_response(test_customer))
|
245
|
-
c = Paysio::Customer.new("c_test_customer")
|
246
|
-
c.mnemonic = "another_mn"
|
247
|
-
c.save
|
248
|
-
assert_equal false, c.livemode
|
249
|
-
end
|
250
|
-
|
251
|
-
should "deleting should send no props and result in an object that has no props other deleted" do
|
252
|
-
@mock.expects(:get).never
|
253
|
-
@mock.expects(:post).never
|
254
|
-
@mock.expects(:delete).with("#{Paysio.api_base}/v1/customers/c_test_customer", nil, nil).once.returns(test_response({ "id" => "test_customer", "deleted" => true }))
|
255
|
-
|
256
|
-
c = Paysio::Customer.construct_from(test_customer)
|
257
|
-
c.delete
|
258
|
-
assert_equal true, c.deleted
|
259
|
-
|
260
|
-
assert_raises NoMethodError do
|
261
|
-
c.livemode
|
262
|
-
end
|
263
|
-
end
|
264
|
-
|
265
|
-
should "loading an object with properties that have specific types should instantiate those classes" do
|
266
|
-
@mock.expects(:get).once.returns(test_response(test_charge))
|
267
|
-
c = Paysio::Charge.retrieve("test_charge")
|
268
|
-
assert c.card.kind_of?(Paysio::PaysioObject) && c.card.object == 'card'
|
269
|
-
end
|
270
|
-
|
271
|
-
should "loading all of an APIResource should return an array of recursively instantiated objects" do
|
272
|
-
@mock.expects(:get).once.returns(test_response(test_charge_array))
|
273
|
-
c = Paysio::Charge.all.data
|
274
|
-
assert c.kind_of? Array
|
275
|
-
assert c[0].kind_of? Paysio::Charge
|
276
|
-
assert c[0].card.kind_of?(Paysio::PaysioObject) && c[0].card.object == 'card'
|
277
|
-
end
|
278
|
-
|
279
|
-
context "account tests" do
|
280
|
-
should "account should be retrievable" do
|
281
|
-
resp = {:email => "test+bindings@stripe.com", :charge_enabled => false, :details_submitted => false}
|
282
|
-
@mock.expects(:get).once.returns(test_response(resp))
|
283
|
-
a = Paysio::Account.retrieve
|
284
|
-
assert_equal "test+bindings@stripe.com", a.email
|
285
|
-
assert !a.charge_enabled
|
286
|
-
assert !a.details_submitted
|
287
|
-
end
|
288
|
-
end
|
289
|
-
|
290
|
-
context "list tests" do
|
291
|
-
should "be able to retrieve full lists given a listobject" do
|
292
|
-
@mock.expects(:get).twice.returns(test_response(test_charge_array))
|
293
|
-
c = Paysio::Charge.all
|
294
|
-
assert c.kind_of?(Paysio::ListObject)
|
295
|
-
assert_equal('/v1/charges', c.url)
|
296
|
-
all = c.all
|
297
|
-
assert all.kind_of?(Paysio::ListObject)
|
298
|
-
assert_equal('/v1/charges', all.url)
|
299
|
-
assert all.data.kind_of?(Array)
|
300
|
-
end
|
301
|
-
end
|
302
|
-
|
303
|
-
context "charge tests" do
|
304
|
-
|
305
|
-
should "charges should be listable" do
|
306
|
-
@mock.expects(:get).once.returns(test_response(test_charge_array))
|
307
|
-
c = Paysio::Charge.all
|
308
|
-
assert c.data.kind_of? Array
|
309
|
-
c.each do |charge|
|
310
|
-
assert charge.kind_of?(Paysio::Charge)
|
311
|
-
end
|
312
|
-
end
|
313
|
-
|
314
|
-
should "charges should be refundable" do
|
315
|
-
@mock.expects(:get).never
|
316
|
-
@mock.expects(:post).once.returns(test_response({:id => "ch_test_charge", :refunded => true}))
|
317
|
-
c = Paysio::Charge.new("test_charge")
|
318
|
-
c.refund
|
319
|
-
assert c.refunded
|
320
|
-
end
|
321
|
-
|
322
|
-
should "charges should not be deletable" do
|
323
|
-
assert_raises NoMethodError do
|
324
|
-
@mock.expects(:get).once.returns(test_response(test_charge))
|
325
|
-
c = Paysio::Charge.retrieve("test_charge")
|
326
|
-
c.delete
|
327
|
-
end
|
328
|
-
end
|
329
|
-
|
330
|
-
should "charges should be updateable" do
|
331
|
-
@mock.expects(:get).once.returns(test_response(test_charge))
|
332
|
-
@mock.expects(:post).once.returns(test_response(test_charge))
|
333
|
-
c = Paysio::Charge.new("test_charge")
|
334
|
-
c.refresh
|
335
|
-
c.mnemonic = "New charge description"
|
336
|
-
c.save
|
337
|
-
end
|
338
|
-
|
339
|
-
should "charges should have Card objects associated with their Card property" do
|
340
|
-
@mock.expects(:get).once.returns(test_response(test_charge))
|
341
|
-
c = Paysio::Charge.retrieve("test_charge")
|
342
|
-
assert c.card.kind_of?(Paysio::PaysioObject) && c.card.object == 'card'
|
343
|
-
end
|
344
|
-
|
345
|
-
should "execute should return a new, fully executed charge when passed correct parameters" do
|
346
|
-
@mock.expects(:post).with do |url, api_key, params|
|
347
|
-
url == "#{Paysio.api_base}/v1/charges" && api_key.nil? && CGI.parse(params) == {
|
348
|
-
'currency' => ['usd'], 'amount' => ['100'],
|
349
|
-
'card[exp_year]' => ['2012'],
|
350
|
-
'card[number]' => ['4242424242424242'],
|
351
|
-
'card[exp_month]' => ['11']
|
352
|
-
}
|
353
|
-
end.once.returns(test_response(test_charge))
|
354
|
-
|
355
|
-
c = Paysio::Charge.create({
|
356
|
-
:amount => 100,
|
357
|
-
:card => {
|
358
|
-
:number => "4242424242424242",
|
359
|
-
:exp_month => 11,
|
360
|
-
:exp_year => 2012,
|
361
|
-
},
|
362
|
-
:currency => "usd"
|
363
|
-
})
|
364
|
-
assert c.paid
|
365
|
-
end
|
366
|
-
|
367
|
-
end
|
368
|
-
|
369
|
-
context "customer tests" do
|
370
|
-
|
371
|
-
should "customers should be listable" do
|
372
|
-
@mock.expects(:get).once.returns(test_response(test_customer_array))
|
373
|
-
c = Paysio::Customer.all.data
|
374
|
-
assert c.kind_of? Array
|
375
|
-
assert c[0].kind_of? Paysio::Customer
|
376
|
-
end
|
377
|
-
|
378
|
-
should "customers should be deletable" do
|
379
|
-
@mock.expects(:delete).once.returns(test_response(test_customer({:deleted => true})))
|
380
|
-
c = Paysio::Customer.new("test_customer")
|
381
|
-
c.delete
|
382
|
-
assert c.deleted
|
383
|
-
end
|
384
|
-
|
385
|
-
should "customers should be updateable" do
|
386
|
-
@mock.expects(:get).once.returns(test_response(test_customer({:mnemonic => "foo"})))
|
387
|
-
@mock.expects(:post).once.returns(test_response(test_customer({:mnemonic => "bar"})))
|
388
|
-
c = Paysio::Customer.new("test_customer").refresh
|
389
|
-
assert_equal c.mnemonic, "foo"
|
390
|
-
c.mnemonic = "bar"
|
391
|
-
c.save
|
392
|
-
assert_equal c.mnemonic, "bar"
|
393
|
-
end
|
394
|
-
|
395
|
-
should "customers should have Card objects associated with their active_ard property" do
|
396
|
-
@mock.expects(:get).once.returns(test_response(test_customer))
|
397
|
-
c = Paysio::Customer.retrieve("test_customer")
|
398
|
-
assert c.active_card.kind_of?(Paysio::PaysioObject) && c.active_card.object == 'card'
|
399
|
-
end
|
400
|
-
|
401
|
-
should "create should return a new customer" do
|
402
|
-
@mock.expects(:post).once.returns(test_response(test_customer))
|
403
|
-
c = Paysio::Customer.create
|
404
|
-
assert_equal "c_test_customer", c.id
|
405
|
-
end
|
406
|
-
|
407
|
-
should "be able to delete a customer's discount" do
|
408
|
-
@mock.expects(:get).once.returns(test_response(test_customer))
|
409
|
-
c = Paysio::Customer.retrieve("test_customer")
|
410
|
-
|
411
|
-
@mock.expects(:delete).once.with("#{Paysio.api_base}/v1/customers/c_test_customer/discount", nil, nil).returns(test_response(test_delete_discount_response))
|
412
|
-
s = c.delete_discount
|
413
|
-
assert_equal nil, c.discount
|
414
|
-
end
|
415
|
-
end
|
416
|
-
|
417
|
-
context "coupon tests" do
|
418
|
-
should "create should return a new coupon" do
|
419
|
-
@mock.expects(:post).once.returns(test_response(test_coupon))
|
420
|
-
c = Paysio::Coupon.create
|
421
|
-
assert_equal "co_test_coupon", c.id
|
422
|
-
end
|
423
|
-
end
|
424
|
-
|
425
|
-
context "error checking" do
|
426
|
-
|
427
|
-
should "404s should raise an InvalidRequestError" do
|
428
|
-
response = test_response(test_missing_id_error, 404)
|
429
|
-
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 404))
|
430
|
-
|
431
|
-
begin
|
432
|
-
Paysio::Customer.new("test_customer").refresh
|
433
|
-
assert false #shouldn't get here either
|
434
|
-
rescue Paysio::InvalidRequestError => e # we don't use assert_raises because we want to examine e
|
435
|
-
assert e.kind_of? Paysio::InvalidRequestError
|
436
|
-
assert_equal "id", e.param
|
437
|
-
assert_equal "Missing id", e.message
|
438
|
-
return
|
439
|
-
end
|
440
|
-
|
441
|
-
assert false #shouldn't get here
|
442
|
-
end
|
443
|
-
|
444
|
-
should "5XXs should raise an APIError" do
|
445
|
-
response = test_response(test_api_error, 500)
|
446
|
-
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 500))
|
447
|
-
|
448
|
-
begin
|
449
|
-
Paysio::Customer.new("test_customer").refresh
|
450
|
-
assert false #shouldn't get here either
|
451
|
-
rescue Paysio::APIError => e # we don't use assert_raises because we want to examine e
|
452
|
-
assert e.kind_of? Paysio::APIError
|
453
|
-
return
|
454
|
-
end
|
455
|
-
|
456
|
-
assert false #shouldn't get here
|
457
|
-
end
|
458
|
-
|
459
|
-
should "402s should raise a CardError" do
|
460
|
-
response = test_response(test_invalid_exp_year_error, 402)
|
461
|
-
@mock.expects(:get).once.raises(RestClient::ExceptionWithResponse.new(response, 402))
|
462
|
-
|
463
|
-
begin
|
464
|
-
Paysio::Customer.new("test_customer").refresh
|
465
|
-
assert false #shouldn't get here either
|
466
|
-
rescue Paysio::CardError => e # we don't use assert_raises because we want to examine e
|
467
|
-
assert e.kind_of? Paysio::CardError
|
468
|
-
assert_equal "invalid_expiry_year", e.code
|
469
|
-
assert_equal "exp_year", e.param
|
470
|
-
assert_equal "Your card's expiration year is invalid", e.message
|
471
|
-
return
|
472
|
-
end
|
473
|
-
|
474
|
-
assert false #shouldn't get here
|
475
|
-
end
|
476
|
-
end
|
477
|
-
end
|
478
|
-
end
|
479
|
-
end
|