payjp_mock 0.3.0 → 0.4.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: c7f977063630656f5c88303550868aa9236b9e4a
4
- data.tar.gz: a2e2189498c05968358a6fb49a27b56402905cee
3
+ metadata.gz: 1ed8389c900aa4a05ea613665fb45518dbeb7997
4
+ data.tar.gz: 580f0e745a5ac4a26fdbc5bc950252702b54af0e
5
5
  SHA512:
6
- metadata.gz: 3ea678234fd4172642bf3b2d339723b5a2fc87e589d123193d59bbd2667c7a936a9e42c7885bc05d20dd083382716b5ab25012035b8f0145da32a43b5f9db272
7
- data.tar.gz: 80607c4f44c6c192814301d70b5231d9f303dc6049fd5a87e6a4ff9ce87360dd2d96065a980f3288c095238e9f91361f9b6a05fc1e9416a3cf57eb9032d166ee
6
+ metadata.gz: 64b46adbd7eda489d58e8c484cb3550473ccda45fe6a2f6f610a0181f368e8e3a351870015399f33ee5edc5d23d3495e3c3c3f8a2cd6fba819f2c5c549cabca8
7
+ data.tar.gz: e852d40d5c61bc00dbac628e055c1eba8daed72d12b190a156888480f077745b9c4bed4d3598f419e6701cd62a1aa108ffbd67dee5f510804d065fc707a3dd25
data/README.md CHANGED
@@ -37,7 +37,7 @@ require 'payjp'
37
37
 
38
38
  specify do
39
39
  # Stubbing charge creation
40
- payjp_stub(:charges, :create)
40
+ payjp_stub(:charges, :create, params: { amount: 3500, card: 'tok_xxxxx', currency: 'jpy' })
41
41
  Payjp::Charge.create(amount: 3500, card: 'tok_xxxxx', currency: 'jpy')
42
42
 
43
43
  # Stubbing nested resource operations such as customer's card list retrival
@@ -4,6 +4,16 @@ module PayjpMock::Ext
4
4
  def symbolize_keys
5
5
  each_with_object({}) { |(k, v), h| h[k.to_sym] = v }
6
6
  end
7
+
8
+ def deep_transform_values(&block)
9
+ each_with_object({}) do |(k, v), h|
10
+ h[k] = v.is_a?(::Hash) ? v.deep_transform_values(&block) : block.call(v)
11
+ end
12
+ end
13
+
14
+ def except(*keys)
15
+ each_with_object({}) { |(k, v), h| h[k] = v unless keys.include?(k) }
16
+ end
7
17
  end
8
18
  end
9
19
  end
@@ -9,14 +9,16 @@ class PayjpMock::Request
9
9
  API_VERSION = 'v1'.freeze
10
10
  API_BASE = "https://#{API_HOST}/#{API_VERSION}".freeze
11
11
 
12
- def initialize(method, path_pattern, response)
12
+ def initialize(method, path_pattern, params, response)
13
13
  @method = method
14
14
  @url = Addressable::Template.new(API_BASE + path_pattern)
15
+ @params = params.deep_transform_values(&:to_s)
15
16
  @response = response
16
17
  end
17
18
 
18
19
  def stub
19
20
  stub_request(@method, @url)
21
+ .with(body: @params)
20
22
  .to_return(body: @response.body, status: @response.status, exception: @response.exception)
21
23
 
22
24
  JSON.parse(@response.body || '{}').symbolize_keys
@@ -3,9 +3,10 @@ module PayjpMock
3
3
  include Util
4
4
  using Ext::Hash
5
5
 
6
- def initialize(resource, operation, error)
6
+ def initialize(resource, operation, params, error)
7
7
  @resource = resource.is_a?(Hash) ? resource.symbolize_keys : resource.to_sym
8
8
  @operation = operation.to_sym
9
+ @params = params.symbolize_keys
9
10
  @error = error&.to_sym
10
11
  end
11
12
 
@@ -15,27 +16,30 @@ module PayjpMock
15
16
  when :charge, :charges
16
17
  case @operation
17
18
  when :create
18
- [:post, '/charges', Response::Resource::Charge.new]
19
+ [:post, '/charges', Response::Resource::Charge.new(@params)]
19
20
  when :retrieve
20
21
  [:get, '/charges/{id}', Response::Resource::Charge.new]
21
22
  when :save
22
- [:post, '/charges/{id}', Response::Resource::Charge.new]
23
+ [:post, '/charges/{id}', Response::Resource::Charge.new(@params)]
23
24
  when :refund
24
- charge = Response::Resource::Charge.new(
25
- amount: 1000,
26
- amount_refunded: 1000,
27
- refunded: true
28
- )
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)
29
+
29
30
  [:post, '/charges/{id}/refund', charge]
30
31
  when :reauth
31
32
  charge = Response::Resource::Charge.new(
32
33
  captured: false,
33
34
  captured_at: nil,
34
- expired_at: Time.now.to_i + 7.days
35
+ expiry_days: @params[:expiry_days] || 7
35
36
  )
36
37
  [:post, '/charges/{id}/reauth', charge]
37
38
  when :capture
38
- [:post, '/charges/{id}/capture', Response::Resource::Charge.new(captured: true)]
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)
42
+ [:post, '/charges/{id}/capture', charge]
39
43
  when :all
40
44
  list = Response::List.new('/charges') { Response::Resource::Charge.new }
41
45
  [:get, '/charges', list]
@@ -45,11 +49,11 @@ module PayjpMock
45
49
  when :customer, :customers
46
50
  case @operation
47
51
  when :create
48
- [:post, '/customers', Response::Resource::Customer.new]
52
+ [:post, '/customers', Response::Resource::Customer.new(@params)]
49
53
  when :retrieve
50
54
  [:get, '/customers/{id}', Response::Resource::Customer.new]
51
55
  when :save
52
- [:post, '/customers/{id}', Response::Resource::Customer.new]
56
+ [:post, '/customers/{id}', Response::Resource::Customer.new(@params)]
53
57
  when :delete
54
58
  cus_id = generate_resource_id(Response::Resource::Customer::PREFIX)
55
59
  [:delete, '/customers/{id}', Response::Deleted.new(cus_id)]
@@ -62,11 +66,11 @@ module PayjpMock
62
66
  when { customer: :card }, { customer: :cards }, { customers: :card }, { customers: :cards }
63
67
  case @operation
64
68
  when :create
65
- [:post, '/customers/{customer_id}/cards', Response::Resource::Card.new]
69
+ [:post, '/customers/{customer_id}/cards', Response::Resource::Card.new(@params)]
66
70
  when :retrieve
67
71
  [:get, '/customers/{customer_id}/cards/{id}', Response::Resource::Card.new]
68
72
  when :save
69
- [:post, '/customers/{customer_id}/cards/{id}', Response::Resource::Card.new]
73
+ [:post, '/customers/{customer_id}/cards/{id}', Response::Resource::Card.new(@params)]
70
74
  when :delete
71
75
  car_id = generate_resource_id(Response::Resource::Card::PREFIX)
72
76
  [:delete, '/customers/{customer_id}/cards/{id}', Response::Deleted.new(car_id)]
@@ -80,11 +84,11 @@ module PayjpMock
80
84
  when :plan, :plans
81
85
  case @operation
82
86
  when :create
83
- [:post, '/plans', Response::Resource::Plan.new]
87
+ [:post, '/plans', Response::Resource::Plan.new(@params)]
84
88
  when :retrieve
85
89
  [:get, '/plans/{id}', Response::Resource::Plan.new]
86
90
  when :save
87
- [:post, '/plans/{id}', Response::Resource::Plan.new]
91
+ [:post, '/plans/{id}', Response::Resource::Plan.new(@params)]
88
92
  when :delete
89
93
  pln_id = generate_resource_id(Response::Resource::Plan::PREFIX)
90
94
  [:delete, '/plans/{id}', Response::Deleted.new(pln_id)]
@@ -97,11 +101,25 @@ module PayjpMock
97
101
  when :subscription, :subscriptions
98
102
  case @operation
99
103
  when :create
100
- [:post, '/subscriptions', Response::Resource::Subscription.new]
104
+ subscription =
105
+ if @params[:trial_end] == 'now'
106
+ warn('parameter `trial_end` cannot be set to `now` yet')
107
+ Response::Resource::Subscription.new(@params.except(:trial_end))
108
+ else
109
+ Response::Resource::Subscription.new(@params)
110
+ end
111
+ [:post, '/subscriptions', subscription]
101
112
  when :retrieve
102
113
  [:get, '/subscriptions/{id}', Response::Resource::Subscription.new]
103
114
  when :save
104
- [:post, '/subscriptions/{id}', Response::Resource::Subscription.new]
115
+ subscription =
116
+ if @params[:trial_end] == 'now'
117
+ warn('parameter `trial_end` cannot be set to `now` yet')
118
+ Response::Resource::Subscription.new(@params.except(:trial_end))
119
+ else
120
+ Response::Resource::Subscription.new(@params)
121
+ end
122
+ [:post, '/subscriptions/{id}', subscription]
105
123
  when :pause
106
124
  subscription = Response::Resource::Subscription.new(
107
125
  status: 'paused',
@@ -109,10 +127,18 @@ module PayjpMock
109
127
  )
110
128
  [:post, '/subscriptions/{id}/pause', subscription]
111
129
  when :resume
112
- subscription = Response::Resource::Subscription.new(
113
- status: 'active',
114
- resumed_at: Time.now.to_i
115
- )
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
140
+ subscription.set(resumed_at: Time.now.to_i)
141
+
116
142
  [:post, '/subscriptions/{id}/resume', subscription]
117
143
  when :cancel
118
144
  subscription = Response::Resource::Subscription.new(
@@ -132,7 +158,7 @@ module PayjpMock
132
158
  when :token, :tokens
133
159
  case @operation
134
160
  when :create
135
- [:post, '/tokens', Response::Resource::Token.new]
161
+ [:post, '/tokens', Response::Resource::Token.new(@params)]
136
162
  when :retrieve
137
163
  [:get, '/tokens/{id}', Response::Resource::Token.new]
138
164
  else
@@ -193,7 +219,7 @@ module PayjpMock
193
219
  else
194
220
  success_resp
195
221
  end
196
- Request.new(method, path_pattern, response)
222
+ Request.new(method, path_pattern, @params, response)
197
223
  end
198
224
 
199
225
  UnknownResource = Class.new(StandardError)
@@ -2,14 +2,15 @@ module PayjpMock::Response::Error
2
2
  class ApiError < Base
3
3
  def default_attributes
4
4
  {
5
- message: 'API server is currently unavailable',
5
+ code: 'under_maintenance',
6
+ message: 'PAY.JP is currently undergoing maintenance. Please try again later.',
6
7
  status: status,
7
8
  type: 'server_error'
8
9
  }
9
10
  end
10
11
 
11
12
  def status
12
- 500
13
+ 503
13
14
  end
14
15
  end
15
16
  end
@@ -1,16 +1,18 @@
1
- module PayjpMock::Response::Resource
2
- class Account < Base
3
- PREFIX = 'acct'.freeze
4
- OBJECT = 'account'.freeze
1
+ module PayjpMock
2
+ module Response::Resource
3
+ class Account < Base
4
+ PREFIX = 'acct'.freeze
5
+ OBJECT = 'account'.freeze
5
6
 
6
- def default_attributes
7
- {
8
- created: Time.now.to_i,
9
- email: 'liveaccount@example.com',
10
- id: generate_resource_id(PREFIX),
11
- merchant: Merchant.new.to_h,
12
- object: OBJECT
13
- }
7
+ def default_attributes
8
+ {
9
+ created: Time.now.to_i,
10
+ email: 'liveaccount@example.com',
11
+ id: generate_resource_id(PREFIX),
12
+ merchant: Merchant.new.to_h,
13
+ object: OBJECT
14
+ }
15
+ end
14
16
  end
15
17
  end
16
18
  end
@@ -4,7 +4,19 @@ module PayjpMock
4
4
  using Ext::Hash
5
5
 
6
6
  def initialize(attributes = {})
7
- @attributes = default_attributes.merge(attributes.symbolize_keys)
7
+ @attributes = default_attributes
8
+ set(attributes.symbolize_keys)
9
+ end
10
+
11
+ def set(attributes)
12
+ attributes.each do |k, v|
13
+ @attributes.merge!(canonicalize(k, v))
14
+ end
15
+ self
16
+ end
17
+
18
+ def canonicalize(key, value)
19
+ { key => value }
8
20
  end
9
21
 
10
22
  def status
@@ -1,31 +1,44 @@
1
- module PayjpMock::Response::Resource
2
- class Card < Base
3
- PREFIX = 'car'.freeze
4
- OBJECT = 'card'.freeze
1
+ module PayjpMock
2
+ module Response::Resource
3
+ class Card < Base
4
+ PREFIX = 'car'.freeze
5
+ OBJECT = 'card'.freeze
5
6
 
6
- def default_attributes
7
- {
8
- address_city: nil,
9
- address_line1: nil,
10
- address_line2: nil,
11
- address_state: nil,
12
- address_zip: nil,
13
- address_zip_check: 'unchecked',
14
- brand: 'Visa',
15
- country: nil,
16
- created: Time.now.to_i,
17
- customer: generate_resource_id(Customer::PREFIX),
18
- cvc_check: 'unchecked',
19
- exp_month: 2,
20
- exp_year: 2020,
21
- fingerprint: generate_fingerprint,
22
- id: generate_resource_id(PREFIX),
23
- last4: '4242',
24
- livemode: false,
25
- metadata: nil,
26
- name: nil,
27
- object: OBJECT
28
- }
7
+ def default_attributes
8
+ {
9
+ address_city: nil,
10
+ address_line1: nil,
11
+ address_line2: nil,
12
+ address_state: nil,
13
+ address_zip: nil,
14
+ address_zip_check: 'unchecked',
15
+ brand: 'Visa',
16
+ country: nil,
17
+ created: Time.now.to_i,
18
+ customer: generate_resource_id(Customer::PREFIX),
19
+ cvc_check: 'unchecked',
20
+ exp_month: 2,
21
+ exp_year: 2020,
22
+ fingerprint: generate_fingerprint,
23
+ id: generate_resource_id(PREFIX),
24
+ last4: '4242',
25
+ livemode: false,
26
+ metadata: nil,
27
+ name: nil,
28
+ object: OBJECT
29
+ }
30
+ end
31
+
32
+ def canonicalize(key, value)
33
+ case key
34
+ when :number
35
+ { last4: value[-4..-1] }
36
+ when :cvc
37
+ {}
38
+ else
39
+ super
40
+ end
41
+ end
29
42
  end
30
43
  end
31
44
  end
@@ -1,33 +1,57 @@
1
- module PayjpMock::Response::Resource
2
- class Charge < Base
3
- PREFIX = 'ch'.freeze
4
- OBJECT = 'charge'.freeze
1
+ module PayjpMock
2
+ module Response::Resource
3
+ class Charge < Base
4
+ using Ext::Integer
5
5
 
6
- def default_attributes
7
- now = Time.now.to_i
6
+ PREFIX = 'ch'.freeze
7
+ OBJECT = 'charge'.freeze
8
8
 
9
- {
10
- amount: 3500,
11
- amount_refunded: 0,
12
- captured: true,
13
- captured_at: now,
14
- card: Card.new.to_h,
15
- created: now,
16
- currency: 'jpy',
17
- customer: nil,
18
- description: nil,
19
- expired_at: nil,
20
- failure_code: nil,
21
- failure_message: nil,
22
- id: generate_resource_id(PREFIX),
23
- livemode: false,
24
- metadata: nil,
25
- object: OBJECT,
26
- paid: true,
27
- refund_reason: nil,
28
- refunded: false,
29
- subscription: nil
30
- }
9
+ def default_attributes
10
+ now = Time.now.to_i
11
+
12
+ {
13
+ amount: 3500,
14
+ amount_refunded: 0,
15
+ captured: true,
16
+ captured_at: now,
17
+ card: Card.new.to_h,
18
+ created: now,
19
+ currency: 'jpy',
20
+ customer: nil,
21
+ description: nil,
22
+ expired_at: nil,
23
+ failure_code: nil,
24
+ failure_message: nil,
25
+ id: generate_resource_id(PREFIX),
26
+ livemode: false,
27
+ metadata: nil,
28
+ object: OBJECT,
29
+ paid: true,
30
+ refund_reason: nil,
31
+ refunded: false,
32
+ subscription: nil
33
+ }
34
+ end
35
+
36
+ def canonicalize(key, value)
37
+ case key
38
+ when :card
39
+ { card: Card.new(value.is_a?(Hash)? value : {}).to_h }
40
+ when :capture
41
+ { captured: value, captured_at: value ? Time.now.to_i : nil }
42
+ when :expiry_days
43
+ expired_at =
44
+ if value == 60
45
+ expired_date = Time.now + 59.days
46
+ Time.local(expired_date.year, expired_date.month, expired_date.day, 23, 59, 59)
47
+ else
48
+ Time.now + value.days
49
+ end
50
+ { expired_at: expired_at.to_i }
51
+ else
52
+ super
53
+ end
54
+ end
31
55
  end
32
56
  end
33
57
  end
@@ -1,26 +1,42 @@
1
- module PayjpMock::Response::Resource
2
- class Customer < Base
3
- PREFIX = 'cus'.freeze
4
- OBJECT = 'customer'.freeze
1
+ module PayjpMock
2
+ module Response::Resource
3
+ class Customer < Base
4
+ PREFIX = 'cus'.freeze
5
+ OBJECT = 'customer'.freeze
5
6
 
6
- def default_attributes
7
- id = generate_resource_id(PREFIX)
7
+ def default_attributes
8
+ id = generate_resource_id(PREFIX)
8
9
 
9
- {
10
- cards: PayjpMock::Response::List.new("/customers/#{id}/cards").to_h,
11
- created: Time.now.to_i,
12
- default_card: nil,
13
- description: 'test',
14
- email: nil,
15
- id: id,
16
- livemode: false,
17
- metadata: nil,
18
- object: OBJECT,
19
- subscriptions: PayjpMock::Response::List.new("/customers/#{id}/subscriptions").to_h
20
- }
21
- end
10
+ {
11
+ cards: Response::List.new("/customers/#{id}/cards").to_h,
12
+ created: Time.now.to_i,
13
+ default_card: nil,
14
+ description: 'test',
15
+ email: nil,
16
+ id: id,
17
+ livemode: false,
18
+ metadata: nil,
19
+ object: OBJECT,
20
+ subscriptions: Response::List.new("/customers/#{id}/subscriptions").to_h
21
+ }
22
+ end
22
23
 
23
- def override(attributes)
24
+ def canonicalize(key, value)
25
+ case key
26
+ when :card
27
+ cards = Response::List.new("/customers/#{@attributes[:id]}/cards", count: 1) do
28
+ Card.new(value.is_a?(Hash) ? value : {})
29
+ end
30
+ { cards: cards.to_h, default_card: cards.attributes[:data][0][:id] }
31
+ when :default_card
32
+ cards = Response::List.new("/customers/#{@attributes[:id]}/cards", count: 1) do
33
+ Card.new(id: value)
34
+ end
35
+ { cards: cards.to_h, default_card: value }
36
+ else
37
+ super
38
+ end
39
+ end
24
40
  end
25
41
  end
26
42
  end
@@ -1,18 +1,20 @@
1
- module PayjpMock::Response::Resource
2
- class Event < Base
3
- PREFIX = 'evnt'.freeze
4
- OBJECT = 'event'.freeze
1
+ module PayjpMock
2
+ module Response::Resource
3
+ class Event < Base
4
+ PREFIX = 'evnt'.freeze
5
+ OBJECT = 'event'.freeze
5
6
 
6
- def default_attributes
7
- {
8
- created: Time.now.to_i,
9
- data: Customer.new.to_h,
10
- id: generate_resource_id(PREFIX),
11
- livemode: false,
12
- object: OBJECT,
13
- pending_webhooks: 1,
14
- type: 'customer.updated'
15
- }
7
+ def default_attributes
8
+ {
9
+ created: Time.now.to_i,
10
+ data: Customer.new.to_h,
11
+ id: generate_resource_id(PREFIX),
12
+ livemode: false,
13
+ object: OBJECT,
14
+ pending_webhooks: 1,
15
+ type: 'customer.updated'
16
+ }
17
+ end
16
18
  end
17
19
  end
18
20
  end
@@ -1,30 +1,32 @@
1
- module PayjpMock::Response::Resource
2
- class Merchant < Base
3
- PREFIX = 'acct_mch'.freeze
4
- OBJECT = 'merchant'.freeze
1
+ module PayjpMock
2
+ module Response::Resource
3
+ class Merchant < Base
4
+ PREFIX = 'acct_mch'.freeze
5
+ OBJECT = 'merchant'.freeze
5
6
 
6
- def default_attributes
7
- {
8
- bank_enabled: false,
9
- brands_accepted: ['Visa', 'MasterCard', 'JCB', 'American Express', 'Diners Club', 'Discover'],
10
- business_type: nil,
11
- charge_type: nil,
12
- contact_phone: nil,
13
- country: 'JP',
14
- created: Time.now.to_i,
15
- currencies_supported: ['jpy'],
16
- default_currency: 'jpy',
17
- details_submitted: false,
18
- id: generate_resource_id(PREFIX),
19
- livemode_activated_at: nil,
20
- livemode_enabled: false,
21
- object: OBJECT,
22
- product_detail: nil,
23
- product_name: nil,
24
- product_type: nil,
25
- site_published: nil,
26
- url: nil
27
- }
7
+ def default_attributes
8
+ {
9
+ bank_enabled: false,
10
+ brands_accepted: ['Visa', 'MasterCard', 'JCB', 'American Express', 'Diners Club', 'Discover'],
11
+ business_type: nil,
12
+ charge_type: nil,
13
+ contact_phone: nil,
14
+ country: 'JP',
15
+ created: Time.now.to_i,
16
+ currencies_supported: ['jpy'],
17
+ default_currency: 'jpy',
18
+ details_submitted: false,
19
+ id: generate_resource_id(PREFIX),
20
+ livemode_activated_at: nil,
21
+ livemode_enabled: false,
22
+ object: OBJECT,
23
+ product_detail: nil,
24
+ product_name: nil,
25
+ product_type: nil,
26
+ site_published: nil,
27
+ url: nil
28
+ }
29
+ end
28
30
  end
29
31
  end
30
32
  end
@@ -1,22 +1,24 @@
1
- module PayjpMock::Response::Resource
2
- class Plan < Base
3
- PREFIX = 'pln'.freeze
4
- OBJECT = 'plan'.freeze
1
+ module PayjpMock
2
+ module Response::Resource
3
+ class Plan < Base
4
+ PREFIX = 'pln'.freeze
5
+ OBJECT = 'plan'.freeze
5
6
 
6
- def default_attributes
7
- {
8
- amount: 500,
9
- billing_day: nil,
10
- created: Time.now.to_i,
11
- currency: 'jpy',
12
- id: generate_resource_id(PREFIX),
13
- interval: 'month',
14
- livemode: false,
15
- metadata: nil,
16
- name: nil,
17
- object: OBJECT,
18
- trial_days: 30
19
- }
7
+ def default_attributes
8
+ {
9
+ amount: 500,
10
+ billing_day: nil,
11
+ created: Time.now.to_i,
12
+ currency: 'jpy',
13
+ id: generate_resource_id(PREFIX),
14
+ interval: 'month',
15
+ livemode: false,
16
+ metadata: nil,
17
+ name: nil,
18
+ object: OBJECT,
19
+ trial_days: 30
20
+ }
21
+ end
20
22
  end
21
23
  end
22
24
  end
@@ -1,32 +1,46 @@
1
- module PayjpMock::Response::Resource
2
- class Subscription < Base
3
- using PayjpMock::Ext::Integer
1
+ module PayjpMock
2
+ module Response::Resource
3
+ class Subscription < Base
4
+ using Ext::Integer
4
5
 
5
- PREFIX = 'sub'.freeze
6
- OBJECT = 'subscription'.freeze
6
+ PREFIX = 'sub'.freeze
7
+ OBJECT = 'subscription'.freeze
7
8
 
8
- def default_attributes
9
- now = Time.now.to_i
9
+ def default_attributes
10
+ now = Time.now.to_i
10
11
 
11
- {
12
- canceled_at: nil,
13
- created: now,
14
- current_period_end: now + 15.days,
15
- current_period_start: now - 15.days,
16
- customer: generate_resource_id(Customer::PREFIX),
17
- id: generate_resource_id(PREFIX),
18
- livemode: false,
19
- metadata: nil,
20
- object: OBJECT,
21
- paused_at: nil,
22
- plan: Plan.new.to_h,
23
- resumed_at: nil,
24
- start: now - 15.days,
25
- status: 'active',
26
- trial_end: nil,
27
- trial_start: nil,
28
- prorate: false
29
- }
12
+ {
13
+ canceled_at: nil,
14
+ created: now,
15
+ current_period_end: now + 15.days,
16
+ current_period_start: now - 15.days,
17
+ customer: generate_resource_id(Customer::PREFIX),
18
+ id: generate_resource_id(PREFIX),
19
+ livemode: false,
20
+ metadata: nil,
21
+ object: OBJECT,
22
+ paused_at: nil,
23
+ plan: Plan.new.to_h,
24
+ resumed_at: nil,
25
+ start: now - 15.days,
26
+ status: 'active',
27
+ trial_end: nil,
28
+ trial_start: nil,
29
+ prorate: false
30
+ }
31
+ end
32
+
33
+ def canonicalize(key, value)
34
+ case key
35
+ when :plan
36
+ { plan: Plan.new(id: value).to_h }
37
+ when :trial_end
38
+ now = Time.now.to_i
39
+ { status: 'trial', trial_start: now - 10.days, trial_end: value }
40
+ else
41
+ super
42
+ end
43
+ end
30
44
  end
31
45
  end
32
46
  end
@@ -1,17 +1,19 @@
1
- module PayjpMock::Response::Resource
2
- class Token < Base
3
- PREFIX = 'tok'.freeze
4
- OBJECT = 'token'.freeze
1
+ module PayjpMock
2
+ module Response::Resource
3
+ class Token < Base
4
+ PREFIX = 'tok'.freeze
5
+ OBJECT = 'token'.freeze
5
6
 
6
- def default_attributes
7
- {
8
- card: Card.new.to_h,
9
- created: Time.now.to_i,
10
- id: generate_resource_id(PREFIX),
11
- livemode: false,
12
- object: OBJECT,
13
- used: false
14
- }
7
+ def default_attributes
8
+ {
9
+ card: Card.new.to_h,
10
+ created: Time.now.to_i,
11
+ id: generate_resource_id(PREFIX),
12
+ livemode: false,
13
+ object: OBJECT,
14
+ used: false
15
+ }
16
+ end
15
17
  end
16
18
  end
17
19
  end
@@ -1,49 +1,51 @@
1
- module PayjpMock::Response::Resource
2
- class Transfer < Base
3
- using PayjpMock::Ext::Integer
1
+ module PayjpMock
2
+ module Response::Resource
3
+ class Transfer < Base
4
+ using Ext::Integer
4
5
 
5
- PREFIX = 'tr'.freeze
6
- OBJECT = 'transfer'.freeze
6
+ PREFIX = 'tr'.freeze
7
+ OBJECT = 'transfer'.freeze
7
8
 
8
- def default_attributes
9
- id = generate_resource_id(PREFIX)
10
- now = Time.now
9
+ def default_attributes
10
+ id = generate_resource_id(PREFIX)
11
+ now = Time.now
11
12
 
12
- charged_at = (now + 30.days).to_i
13
- charges = PayjpMock::Response::List.new("/transfers/#{id}/charges", count: 1) do
14
- Charge.new(
15
- amount: 1000,
16
- captured_at: charged_at,
17
- card: Card.new(created: charged_at).to_h,
18
- created: charged_at
19
- )
20
- end
13
+ charged_at = (now + 30.days).to_i
14
+ charges = Response::List.new("/transfers/#{id}/charges", count: 1) do
15
+ Charge.new(
16
+ amount: 1000,
17
+ captured_at: charged_at,
18
+ card: Card.new(created: charged_at).to_h,
19
+ created: charged_at
20
+ )
21
+ end
21
22
 
22
- {
23
- amount: 1000,
24
- carried_balance: nil,
25
- charges: charges.to_h,
26
- created: now.to_i,
27
- currency: 'jpy',
28
- description: nil,
29
- id: id,
30
- livemode: false,
31
- object: OBJECT,
32
- scheduled_date: (now + 45.days).strftime('%Y-%m-%d'),
33
- status: 'pending',
34
- summary: {
35
- charge_count: 1,
36
- charge_fee: 0,
37
- charge_gross: 1000,
38
- net: 1000,
39
- refund_amount: 0,
40
- refund_count: 0
41
- },
42
- term_end: (now + 15.days).to_i,
43
- term_start: now.to_i,
44
- transfer_amount: nil,
45
- transfer_date: nil
46
- }
23
+ {
24
+ amount: 1000,
25
+ carried_balance: nil,
26
+ charges: charges.to_h,
27
+ created: now.to_i,
28
+ currency: 'jpy',
29
+ description: nil,
30
+ id: id,
31
+ livemode: false,
32
+ object: OBJECT,
33
+ scheduled_date: (now + 45.days).strftime('%Y-%m-%d'),
34
+ status: 'pending',
35
+ summary: {
36
+ charge_count: 1,
37
+ charge_fee: 0,
38
+ charge_gross: 1000,
39
+ net: 1000,
40
+ refund_amount: 0,
41
+ refund_count: 0
42
+ },
43
+ term_end: (now + 15.days).to_i,
44
+ term_start: now.to_i,
45
+ transfer_amount: nil,
46
+ transfer_date: nil
47
+ }
48
+ end
47
49
  end
48
50
  end
49
51
  end
@@ -1,3 +1,3 @@
1
1
  module PayjpMock
2
- VERSION = '0.3.0'
2
+ VERSION = '0.4.0'
3
3
  end
@@ -1,7 +1,7 @@
1
1
  module PayjpMock
2
2
  module WebMockWrapper
3
- def payjp_stub(resource, operation, error: nil)
4
- builder = RequestBuilder.new(resource, operation, error)
3
+ def payjp_stub(resource, operation, params: {}, error: nil)
4
+ builder = RequestBuilder.new(resource, operation, params, error)
5
5
  request = builder.build
6
6
  request.stub
7
7
  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.3.0
4
+ version: 0.4.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-22 00:00:00.000000000 Z
11
+ date: 2017-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: addressable