payjp_mock 0.4.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1ed8389c900aa4a05ea613665fb45518dbeb7997
4
- data.tar.gz: 580f0e745a5ac4a26fdbc5bc950252702b54af0e
3
+ metadata.gz: ce2b92be77a8cad2b90995d5b057acf480bba444
4
+ data.tar.gz: cfee705b69808d0701e55d33d7df7ffb08bad0d3
5
5
  SHA512:
6
- metadata.gz: 64b46adbd7eda489d58e8c484cb3550473ccda45fe6a2f6f610a0181f368e8e3a351870015399f33ee5edc5d23d3495e3c3c3f8a2cd6fba819f2c5c549cabca8
7
- data.tar.gz: e852d40d5c61bc00dbac628e055c1eba8daed72d12b190a156888480f077745b9c4bed4d3598f419e6701cd62a1aa108ffbd67dee5f510804d065fc707a3dd25
6
+ metadata.gz: 4bf98a117081a42a04365fad466a050b1e3bac79d712c8a860a7dfc87a602a6340c82508fc2203e1e0cf5c7d879b0732be45f39057ba331b151458037d611be0
7
+ data.tar.gz: 7027cb169019359a3ce1b3d52fe60db321deba10d7e16eaa66bf06ab4a042e277cdd7777cd64f82ce1563f008972eb023aba640312ae2da5c7342e77a39b3dd5
data/README.md CHANGED
@@ -47,10 +47,21 @@ specify do
47
47
  payjp_stub({ customer: :cards }, :all)
48
48
  customer.cards.all
49
49
 
50
+ # Specifying response attributes
51
+ payjp_stub(:charge, :retrieve, source: { captured: false, captured_at: nil })
52
+ charge = Payjp::Charge.retrieve('ch_xxxxx')
53
+
54
+ charge.captured #=> false
55
+ charge.captured_at #=> nil
56
+
50
57
  # Stubbing error responses
51
58
  payjp_stub({ customer: :cards }, :create, error: :invalid_request_error)
52
59
  customer.cards.create #=> Raises a Payjp::InvalidRequestError
53
60
 
61
+ # Specifying error response attributes
62
+ payjp_stub(:charges, :create, response: payjp_card_error(code: 'processing_error'))
63
+ Payjp::Charge.create #=> Raises a Payjp::CardError with code processing_error
64
+
54
65
  # snip
55
66
  end
56
67
  ```
@@ -14,6 +14,10 @@ module PayjpMock::Ext
14
14
  def except(*keys)
15
15
  each_with_object({}) { |(k, v), h| h[k] = v unless keys.include?(k) }
16
16
  end
17
+
18
+ def compact
19
+ reject { |_, v| v.nil? }
20
+ end
17
21
  end
18
22
  end
19
23
  end
@@ -3,11 +3,25 @@ module PayjpMock
3
3
  include Util
4
4
  using Ext::Hash
5
5
 
6
- def initialize(resource, operation, params, error)
6
+ def initialize(resource, operation, params, source, error, response)
7
7
  @resource = resource.is_a?(Hash) ? resource.symbolize_keys : resource.to_sym
8
8
  @operation = operation.to_sym
9
9
  @params = params.symbolize_keys
10
- @error = error&.to_sym
10
+ @source = source.symbolize_keys
11
+
12
+ @response = response ||
13
+ case error&.to_sym
14
+ when :card_error
15
+ Response::Error::CardError.new
16
+ when :invalid_request_error
17
+ Response::Error::InvalidRequestError.new
18
+ when :authentication_error
19
+ Response::Error::AuthenticationError.new
20
+ when :api_connection_error
21
+ Response::Error::ApiConnectionError.new
22
+ when :api_error
23
+ Response::Error::ApiError.new
24
+ end
11
25
  end
12
26
 
13
27
  def build
@@ -18,30 +32,38 @@ module PayjpMock
18
32
  when :create
19
33
  [:post, '/charges', Response::Resource::Charge.new(@params)]
20
34
  when :retrieve
21
- [:get, '/charges/{id}', Response::Resource::Charge.new]
35
+ [:get, '/charges/{id}', Response::Resource::Charge.new(@source)]
22
36
  when :save
23
- [:post, '/charges/{id}', Response::Resource::Charge.new(@params)]
37
+ charge = Response::Resource::Charge.new(@source).set(@params)
38
+ [:post, '/charges/{id}', charge]
24
39
  when :refund
25
- warn('parameter `amount` cannot be specified yet') if @params[:amount]
26
-
27
- charge = Response::Resource::Charge.new(@params.except(:amount))
28
- charge.set(amount_refunded: charge.attributes[:amount], refunded: true)
40
+ charge = Response::Resource::Charge.new(@source)
41
+ charge
42
+ .set(@params.except(:amount))
43
+ .set(amount_refunded: @params[:amount] || charge.attributes[:amount], refunded: true)
29
44
 
30
45
  [:post, '/charges/{id}/refund', charge]
31
46
  when :reauth
32
- charge = Response::Resource::Charge.new(
47
+ charge = Response::Resource::Charge.new(@source)
48
+ charge.set(
33
49
  captured: false,
34
50
  captured_at: nil,
35
51
  expiry_days: @params[:expiry_days] || 7
36
52
  )
37
53
  [:post, '/charges/{id}/reauth', charge]
38
54
  when :capture
39
- warn('parameter `amount` cannot be specified yet') if @params[:amount]
40
-
41
- charge = Response::Resource::Charge.new(captured: true, captured_at: Time.now.to_i)
55
+ charge = Response::Resource::Charge.new(@source)
56
+ charge.set(captured: true, captured_at: Time.now.to_i)
57
+ charge.set(
58
+ if @params[:amount]
59
+ { amount_refunded: charge.attributes[:amount] - @params[:amount], refunded: true }
60
+ else
61
+ { amount_refunded: 0, refunded: false }
62
+ end
63
+ )
42
64
  [:post, '/charges/{id}/capture', charge]
43
65
  when :all
44
- list = Response::List.new('/charges') { Response::Resource::Charge.new }
66
+ list = Response::List.new('/charges') { Response::Resource::Charge.new(@source) }
45
67
  [:get, '/charges', list]
46
68
  else
47
69
  raise UnknownOperation, @operation
@@ -51,14 +73,15 @@ module PayjpMock
51
73
  when :create
52
74
  [:post, '/customers', Response::Resource::Customer.new(@params)]
53
75
  when :retrieve
54
- [:get, '/customers/{id}', Response::Resource::Customer.new]
76
+ [:get, '/customers/{id}', Response::Resource::Customer.new(@source)]
55
77
  when :save
56
- [:post, '/customers/{id}', Response::Resource::Customer.new(@params)]
78
+ customer = Response::Resource::Customer.new(@source).set(@params)
79
+ [:post, '/customers/{id}', customer]
57
80
  when :delete
58
81
  cus_id = generate_resource_id(Response::Resource::Customer::PREFIX)
59
82
  [:delete, '/customers/{id}', Response::Deleted.new(cus_id)]
60
83
  when :all
61
- list = Response::List.new('/customers') { Response::Resource::Customer.new }
84
+ list = Response::List.new('/customers') { Response::Resource::Customer.new(@source) }
62
85
  [:get, '/customers', list]
63
86
  else
64
87
  raise UnknownOperation, @operation
@@ -68,15 +91,19 @@ module PayjpMock
68
91
  when :create
69
92
  [:post, '/customers/{customer_id}/cards', Response::Resource::Card.new(@params)]
70
93
  when :retrieve
71
- [:get, '/customers/{customer_id}/cards/{id}', Response::Resource::Card.new]
94
+ card = Response::Resource::Card.new(@source)
95
+ [:get, '/customers/{customer_id}/cards/{id}', card]
72
96
  when :save
73
- [:post, '/customers/{customer_id}/cards/{id}', Response::Resource::Card.new(@params)]
97
+ card = Response::Resource::Card.new(@source).set(@params)
98
+ [:post, '/customers/{customer_id}/cards/{id}', card]
74
99
  when :delete
75
100
  car_id = generate_resource_id(Response::Resource::Card::PREFIX)
76
101
  [:delete, '/customers/{customer_id}/cards/{id}', Response::Deleted.new(car_id)]
77
102
  when :all
78
103
  cus_id = generate_resource_id(Response::Resource::Customer::PREFIX)
79
- list = Response::List.new("/customres/#{cus_id}/cards") { Response::Resource::Card.new }
104
+ list = Response::List.new("/customres/#{cus_id}/cards") do
105
+ Response::Resource::Card.new(@source)
106
+ end
80
107
  [:get, '/customers/{customer_id}/cards', list]
81
108
  else
82
109
  raise UnknownOperation, @operation
@@ -86,14 +113,15 @@ module PayjpMock
86
113
  when :create
87
114
  [:post, '/plans', Response::Resource::Plan.new(@params)]
88
115
  when :retrieve
89
- [:get, '/plans/{id}', Response::Resource::Plan.new]
116
+ [:get, '/plans/{id}', Response::Resource::Plan.new(@source)]
90
117
  when :save
91
- [:post, '/plans/{id}', Response::Resource::Plan.new(@params)]
118
+ plan = Response::Resource::Plan.new(@source).set(@params)
119
+ [:post, '/plans/{id}', plan]
92
120
  when :delete
93
121
  pln_id = generate_resource_id(Response::Resource::Plan::PREFIX)
94
122
  [:delete, '/plans/{id}', Response::Deleted.new(pln_id)]
95
123
  when :all
96
- list = Response::List.new('/plans') { Response::Resource::Plan.new }
124
+ list = Response::List.new('/plans') { Response::Resource::Plan.new(@source) }
97
125
  [:get, '/plans', list]
98
126
  else
99
127
  raise UnknownOperation, @operation
@@ -110,47 +138,50 @@ module PayjpMock
110
138
  end
111
139
  [:post, '/subscriptions', subscription]
112
140
  when :retrieve
113
- [:get, '/subscriptions/{id}', Response::Resource::Subscription.new]
141
+ [:get, '/subscriptions/{id}', Response::Resource::Subscription.new(@source)]
114
142
  when :save
115
- subscription =
143
+ subscription = Response::Resource::Subscription.new(@source)
144
+ subscription.set(
116
145
  if @params[:trial_end] == 'now'
117
146
  warn('parameter `trial_end` cannot be set to `now` yet')
118
- Response::Resource::Subscription.new(@params.except(:trial_end))
147
+ @params.except(:trial_end)
119
148
  else
120
- Response::Resource::Subscription.new(@params)
149
+ @params
121
150
  end
151
+ )
122
152
  [:post, '/subscriptions/{id}', subscription]
123
153
  when :pause
124
- subscription = Response::Resource::Subscription.new(
125
- status: 'paused',
126
- paused_at: Time.now.to_i
127
- )
154
+ subscription = Response::Resource::Subscription.new(@source)
155
+ subscription.set(status: 'paused', paused_at: Time.now.to_i)
156
+
128
157
  [:post, '/subscriptions/{id}/pause', subscription]
129
158
  when :resume
130
- subscription =
131
- case @params[:trial_end]
132
- when nil
133
- Response::Resource::Subscription.new(@params).set(status: 'active')
134
- when 'now'
135
- warn('parameter `trial_end` cannot be set to `now` yet')
136
- Response::Resource::Subscription.new(@params.except(:trial_end)).set(status: 'active')
137
- else
138
- Response::Resource::Subscription.new(@params)
139
- end
159
+ subscription = Response::Resource::Subscription.new(@source)
140
160
  subscription.set(resumed_at: Time.now.to_i)
141
161
 
162
+ case @params[:trial_end]
163
+ when nil
164
+ subscription.set(@params).set(status: 'active')
165
+ when 'now'
166
+ warn('parameter `trial_end` cannot be set to `now` yet')
167
+ subscription.set(@params.except(:trial_end)).set(status: 'active')
168
+ else
169
+ subscription.set(@params)
170
+ end
171
+
142
172
  [:post, '/subscriptions/{id}/resume', subscription]
143
173
  when :cancel
144
- subscription = Response::Resource::Subscription.new(
145
- status: 'canceled',
146
- canceled_at: Time.now.to_i
147
- )
174
+ subscription = Response::Resource::Subscription.new(@source)
175
+ subscription.set(status: 'canceled', canceled_at: Time.now.to_i)
176
+
148
177
  [:post, '/subscriptions/{id}/cancel', subscription]
149
178
  when :delete
150
179
  sub_id = generate_resource_id(Response::Resource::Subscription::PREFIX)
151
180
  [:delete, '/subscriptions/{id}', Response::Deleted.new(sub_id)]
152
181
  when :all
153
- list = Response::List.new('/subscriptions') { Response::Resource::Subscription.new }
182
+ list = Response::List.new('/subscriptions') do
183
+ Response::Resource::Subscription.new(@source)
184
+ end
154
185
  [:get, '/subscriptions', list]
155
186
  else
156
187
  raise UnknownOperation, @operation
@@ -160,16 +191,16 @@ module PayjpMock
160
191
  when :create
161
192
  [:post, '/tokens', Response::Resource::Token.new(@params)]
162
193
  when :retrieve
163
- [:get, '/tokens/{id}', Response::Resource::Token.new]
194
+ [:get, '/tokens/{id}', Response::Resource::Token.new(@source)]
164
195
  else
165
196
  raise UnknownOperation, @operation
166
197
  end
167
198
  when :transfer, :transfers
168
199
  case @operation
169
200
  when :retrieve
170
- [:get, '/transfers/{id}', Response::Resource::Transfer.new]
201
+ [:get, '/transfers/{id}', Response::Resource::Transfer.new(@source)]
171
202
  when :all
172
- list = Response::List.new('/transfers') { Response::Resource::Transfer.new }
203
+ list = Response::List.new('/transfers') { Response::Resource::Transfer.new(@source) }
173
204
  [:get, '/transfers', list]
174
205
  else
175
206
  raise UnknownOperation, @operation
@@ -178,7 +209,9 @@ module PayjpMock
178
209
  case @operation
179
210
  when :all
180
211
  tr_id = generate_resource_id(Response::Resource::Transfer::PREFIX)
181
- list = Response::List.new("/transfers/#{tr_id}/charges") { Response::Resource::Charge.new }
212
+ list = Response::List.new("/transfers/#{tr_id}/charges") do
213
+ Response::Resource::Charge.new(@source)
214
+ end
182
215
  [:get, '/transfers/{transfer_id}/charges', list]
183
216
  else
184
217
  raise UnknownOperation, @operation
@@ -186,9 +219,9 @@ module PayjpMock
186
219
  when :event, :events
187
220
  case @operation
188
221
  when :retrieve
189
- [:get, '/events/{id}', Response::Resource::Event.new]
222
+ [:get, '/events/{id}', Response::Resource::Event.new(@source)]
190
223
  when :all
191
- list = Response::List.new('/events') { Response::Resource::Event.new }
224
+ list = Response::List.new('/events') { Response::Resource::Event.new(@source) }
192
225
  [:get, '/events', list]
193
226
  else
194
227
  raise UnknownOperation, @operation
@@ -196,7 +229,7 @@ module PayjpMock
196
229
  when :account, :accounts
197
230
  case @operation
198
231
  when :retrieve
199
- [:get, '/accounts', Response::Resource::Account.new]
232
+ [:get, '/accounts', Response::Resource::Account.new(@source)]
200
233
  else
201
234
  raise UnknownOperation, @operation
202
235
  end
@@ -204,22 +237,7 @@ module PayjpMock
204
237
  raise UnknownResource, @resource
205
238
  end
206
239
 
207
- response =
208
- case @error
209
- when :card_error
210
- Response::Error::CardError.new
211
- when :invalid_request_error
212
- Response::Error::InvalidRequestError.new
213
- when :authentication_error
214
- Response::Error::AuthenticationError.new
215
- when :api_connection_error
216
- Response::Error::ApiConnectionError.new
217
- when :api_error
218
- Response::Error::ApiError.new
219
- else
220
- success_resp
221
- end
222
- Request.new(method, path_pattern, @params, response)
240
+ Request.new(method, path_pattern, @params, @response || success_resp)
223
241
  end
224
242
 
225
243
  UnknownResource = Class.new(StandardError)
@@ -1,7 +1,11 @@
1
1
  module PayjpMock
2
2
  class Response::Error::Base < Response::Base
3
- def initialize
4
- @attributes = default_attributes
3
+ using Ext::Hash
4
+
5
+ def initialize(code: nil, message: nil, param: nil)
6
+ @attributes = default_attributes.merge(
7
+ { code: code, message: message, param: param }.compact
8
+ )
5
9
  end
6
10
 
7
11
  def body
@@ -1,3 +1,3 @@
1
1
  module PayjpMock
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.0'
3
3
  end
@@ -1,9 +1,31 @@
1
1
  module PayjpMock
2
2
  module WebMockWrapper
3
- def payjp_stub(resource, operation, params: {}, error: nil)
4
- builder = RequestBuilder.new(resource, operation, params, error)
3
+ using Ext::Hash
4
+
5
+ def payjp_stub(resource, operation, params: {}, source: {}, error: nil, response: nil)
6
+ builder = RequestBuilder.new(resource, operation, params, source, error, response)
5
7
  request = builder.build
6
8
  request.stub
7
9
  end
10
+
11
+ def payjp_card_error(attributes = {})
12
+ Response::Error::CardError.new(attributes.symbolize_keys)
13
+ end
14
+
15
+ def payjp_invalid_request_error(attributes = {})
16
+ Response::Error::InvalidRequestError.new(attributes.symbolize_keys)
17
+ end
18
+
19
+ def payjp_authentication_error(attributes = {})
20
+ Response::Error::AuthenticationError.new(attributes.symbolize_keys)
21
+ end
22
+
23
+ def payjp_api_connection_error
24
+ Response::Error::ApiConnectionError.new
25
+ end
26
+
27
+ def payjp_api_error(attributes = {})
28
+ Response::Error::ApiError.new(attributes.symbolize_keys)
29
+ end
8
30
  end
9
31
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: payjp_mock
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - kirikiriyamama
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-04-24 00:00:00.000000000 Z
11
+ date: 2017-05-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable