stripe-ruby-mock 3.1.0.rc2 → 3.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (117) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/rspec_tests.yml +38 -0
  3. data/.rspec +2 -1
  4. data/CHANGELOG.md +34 -0
  5. data/README.md +5 -3
  6. data/lib/stripe_mock/api/webhooks.rb +65 -26
  7. data/lib/stripe_mock/data.rb +80 -11
  8. data/lib/stripe_mock/instance.rb +6 -2
  9. data/lib/stripe_mock/request_handlers/accounts.rb +17 -6
  10. data/lib/stripe_mock/request_handlers/charges.rb +5 -0
  11. data/lib/stripe_mock/request_handlers/checkout_session.rb +165 -2
  12. data/lib/stripe_mock/request_handlers/events.rb +30 -3
  13. data/lib/stripe_mock/request_handlers/helpers/coupon_helpers.rb +1 -0
  14. data/lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb +28 -9
  15. data/lib/stripe_mock/request_handlers/invoices.rb +6 -1
  16. data/lib/stripe_mock/request_handlers/payment_intents.rb +13 -2
  17. data/lib/stripe_mock/request_handlers/payment_methods.rb +5 -1
  18. data/lib/stripe_mock/request_handlers/prices.rb +18 -0
  19. data/lib/stripe_mock/request_handlers/promotion_codes.rb +43 -0
  20. data/lib/stripe_mock/request_handlers/refunds.rb +13 -2
  21. data/lib/stripe_mock/request_handlers/setup_intents.rb +16 -9
  22. data/lib/stripe_mock/request_handlers/subscriptions.rb +93 -4
  23. data/lib/stripe_mock/request_handlers/transfers.rb +12 -1
  24. data/lib/stripe_mock/request_handlers/validators/param_validators.rb +5 -8
  25. data/lib/stripe_mock/server.rb +2 -2
  26. data/lib/stripe_mock/test_strategies/base.rb +51 -25
  27. data/lib/stripe_mock/version.rb +1 -1
  28. data/lib/stripe_mock/webhook_fixtures/account.updated.json +1 -1
  29. data/lib/stripe_mock/webhook_fixtures/balance.available.json +26 -20
  30. data/lib/stripe_mock/webhook_fixtures/charge.captured.json +143 -0
  31. data/lib/stripe_mock/webhook_fixtures/charge.dispute.created.json +63 -16
  32. data/lib/stripe_mock/webhook_fixtures/charge.failed.json +49 -120
  33. data/lib/stripe_mock/webhook_fixtures/charge.refund.updated.json +35 -0
  34. data/lib/stripe_mock/webhook_fixtures/charge.refunded.json +145 -50
  35. data/lib/stripe_mock/webhook_fixtures/charge.succeeded.json +114 -43
  36. data/lib/stripe_mock/webhook_fixtures/checkout.session.completed.json +79 -0
  37. data/lib/stripe_mock/webhook_fixtures/checkout.session.completed.payment_mode.json +53 -0
  38. data/lib/stripe_mock/webhook_fixtures/checkout.session.completed.setup_mode.json +45 -0
  39. data/lib/stripe_mock/webhook_fixtures/customer.created.json +37 -46
  40. data/lib/stripe_mock/webhook_fixtures/customer.deleted.json +36 -32
  41. data/lib/stripe_mock/webhook_fixtures/customer.source.created.json +31 -22
  42. data/lib/stripe_mock/webhook_fixtures/customer.source.updated.json +36 -25
  43. data/lib/stripe_mock/webhook_fixtures/customer.subscription.created.json +135 -47
  44. data/lib/stripe_mock/webhook_fixtures/customer.subscription.deleted.json +134 -45
  45. data/lib/stripe_mock/webhook_fixtures/customer.subscription.updated.json +135 -56
  46. data/lib/stripe_mock/webhook_fixtures/customer.updated.json +38 -47
  47. data/lib/stripe_mock/webhook_fixtures/invoice.created.json +176 -49
  48. data/lib/stripe_mock/webhook_fixtures/invoice.finalized.json +171 -0
  49. data/lib/stripe_mock/webhook_fixtures/invoice.paid.json +171 -0
  50. data/lib/stripe_mock/webhook_fixtures/invoice.payment_action_required.json +171 -0
  51. data/lib/stripe_mock/webhook_fixtures/invoice.payment_failed.json +149 -83
  52. data/lib/stripe_mock/webhook_fixtures/invoice.payment_succeeded.json +149 -90
  53. data/lib/stripe_mock/webhook_fixtures/invoice.upcoming.json +70 -0
  54. data/lib/stripe_mock/webhook_fixtures/invoice.updated.json +178 -50
  55. data/lib/stripe_mock/webhook_fixtures/invoiceitem.created.json +87 -13
  56. data/lib/stripe_mock/webhook_fixtures/invoiceitem.updated.json +88 -14
  57. data/lib/stripe_mock/webhook_fixtures/mandate.updated.json +34 -0
  58. data/lib/stripe_mock/webhook_fixtures/payment_intent.amount_capturable_updated.json +170 -0
  59. data/lib/stripe_mock/webhook_fixtures/payment_intent.canceled.json +73 -0
  60. data/lib/stripe_mock/webhook_fixtures/payment_intent.created.json +86 -0
  61. data/lib/stripe_mock/webhook_fixtures/payment_intent.payment_failed.json +118 -79
  62. data/lib/stripe_mock/webhook_fixtures/payment_intent.processing.json +162 -0
  63. data/lib/stripe_mock/webhook_fixtures/payment_intent.requires_action.json +191 -0
  64. data/lib/stripe_mock/webhook_fixtures/payment_intent.succeeded.json +85 -53
  65. data/lib/stripe_mock/webhook_fixtures/payment_link.created.json +47 -0
  66. data/lib/stripe_mock/webhook_fixtures/payment_link.updated.json +50 -0
  67. data/lib/stripe_mock/webhook_fixtures/payment_method.attached.json +63 -0
  68. data/lib/stripe_mock/webhook_fixtures/payment_method.detached.json +62 -0
  69. data/lib/stripe_mock/webhook_fixtures/payout.created.json +40 -0
  70. data/lib/stripe_mock/webhook_fixtures/payout.paid.json +40 -0
  71. data/lib/stripe_mock/webhook_fixtures/payout.updated.json +46 -0
  72. data/lib/stripe_mock/webhook_fixtures/plan.created.json +30 -13
  73. data/lib/stripe_mock/webhook_fixtures/plan.deleted.json +30 -13
  74. data/lib/stripe_mock/webhook_fixtures/plan.updated.json +34 -14
  75. data/lib/stripe_mock/webhook_fixtures/price.created.json +42 -0
  76. data/lib/stripe_mock/webhook_fixtures/price.deleted.json +42 -0
  77. data/lib/stripe_mock/webhook_fixtures/price.updated.json +48 -0
  78. data/lib/stripe_mock/webhook_fixtures/product.created.json +19 -13
  79. data/lib/stripe_mock/webhook_fixtures/product.deleted.json +20 -14
  80. data/lib/stripe_mock/webhook_fixtures/product.updated.json +24 -15
  81. data/lib/stripe_mock/webhook_fixtures/quote.accepted.json +92 -0
  82. data/lib/stripe_mock/webhook_fixtures/quote.canceled.json +92 -0
  83. data/lib/stripe_mock/webhook_fixtures/quote.created.json +92 -0
  84. data/lib/stripe_mock/webhook_fixtures/quote.finalized.json +92 -0
  85. data/lib/stripe_mock/webhook_fixtures/setup_intent.canceled.json +46 -0
  86. data/lib/stripe_mock/webhook_fixtures/setup_intent.created.json +51 -0
  87. data/lib/stripe_mock/webhook_fixtures/setup_intent.setup_failed.json +100 -0
  88. data/lib/stripe_mock/webhook_fixtures/setup_intent.succeeded.json +46 -0
  89. data/lib/stripe_mock/webhook_fixtures/subscription_schedule.canceled.json +119 -0
  90. data/lib/stripe_mock/webhook_fixtures/subscription_schedule.created.json +114 -0
  91. data/lib/stripe_mock/webhook_fixtures/subscription_schedule.released.json +111 -0
  92. data/lib/stripe_mock/webhook_fixtures/subscription_schedule.updated.json +125 -0
  93. data/lib/stripe_mock/webhook_fixtures/tax_rate.created.json +32 -0
  94. data/lib/stripe_mock/webhook_fixtures/tax_rate.updated.json +37 -0
  95. data/lib/stripe_mock.rb +3 -1
  96. data/spec/instance_spec.rb +3 -1
  97. data/spec/integration_examples/completing_checkout_sessions_example.rb +37 -0
  98. data/spec/readme_spec.rb +1 -1
  99. data/spec/shared_stripe_examples/account_examples.rb +9 -1
  100. data/spec/shared_stripe_examples/checkout_session_examples.rb +99 -0
  101. data/spec/shared_stripe_examples/invoice_examples.rb +21 -0
  102. data/spec/shared_stripe_examples/payment_intent_examples.rb +74 -0
  103. data/spec/shared_stripe_examples/payment_method_examples.rb +32 -27
  104. data/spec/shared_stripe_examples/price_examples.rb +42 -2
  105. data/spec/shared_stripe_examples/product_examples.rb +1 -9
  106. data/spec/shared_stripe_examples/promotion_code_examples.rb +68 -0
  107. data/spec/shared_stripe_examples/refund_examples.rb +13 -0
  108. data/spec/shared_stripe_examples/setup_intent_examples.rb +17 -0
  109. data/spec/shared_stripe_examples/subscription_examples.rb +213 -2
  110. data/spec/shared_stripe_examples/transfer_examples.rb +10 -1
  111. data/spec/shared_stripe_examples/webhook_event_examples.rb +51 -5
  112. data/spec/support/stripe_examples.rb +3 -1
  113. data/stripe-ruby-mock.gemspec +1 -1
  114. metadata +54 -11
  115. data/.travis.yml +0 -25
  116. data/lib/stripe_mock/request_handlers/checkout.rb +0 -15
  117. data/spec/shared_stripe_examples/checkout_examples.rb +0 -38
@@ -3,12 +3,175 @@ module StripeMock
3
3
  module Checkout
4
4
  module Session
5
5
  def Session.included(klass)
6
- klass.add_handler 'get /v1/checkout/sessions/(.*)', :get_checkout_session
6
+ klass.add_handler 'post /v1/checkout/sessions', :new_session
7
+ klass.add_handler 'get /v1/checkout/sessions', :list_checkout_sessions
8
+ klass.add_handler 'get /v1/checkout/sessions/([^/]*)', :get_checkout_session
9
+ klass.add_handler 'get /v1/checkout/sessions/([^/]*)/line_items', :list_line_items
10
+ end
11
+
12
+ def new_session(route, method_url, params, headers)
13
+ id = params[:id] || new_id('cs')
14
+
15
+ [:cancel_url, :success_url].each do |p|
16
+ require_param(p) if params[p].nil? || params[p].empty?
17
+ end
18
+
19
+ line_items = nil
20
+ if params[:line_items]
21
+ line_items = params[:line_items].each_with_index.map do |line_item, i|
22
+ throw Stripe::InvalidRequestError("Quantity is required. Add `quantity` to `line_items[#{i}]`") unless line_item[:quantity]
23
+ unless line_item[:price] || line_item[:price_data] || (line_item[:amount] && line_item[:currency] && line_item[:name])
24
+ throw Stripe::InvalidRequestError("Price or amount and currency is required. Add `price`, `price_data`, or `amount`, `currency` and `name` to `line_items[#{i}]`")
25
+ end
26
+ {
27
+ id: new_id("li"),
28
+ price: if line_item[:price]
29
+ line_item[:price]
30
+ elsif line_item[:price_data]
31
+ new_price(nil, nil, line_item[:price_data], nil)[:id]
32
+ else
33
+ new_price(nil, nil, {
34
+ unit_amount: line_item[:amount],
35
+ currency: line_item[:currency],
36
+ product_data: {
37
+ name: line_item[:name]
38
+ }
39
+ }, nil)[:id]
40
+ end,
41
+ quantity: line_item[:quantity]
42
+ }
43
+ end
44
+ end
45
+
46
+ amount = nil
47
+ currency = nil
48
+ if line_items
49
+ amount = 0
50
+
51
+ line_items.each do |line_item|
52
+ price = prices[line_item[:price]]
53
+
54
+ if price.nil?
55
+ raise StripeMock::StripeMockError.new("Price not found for ID: #{line_item[:price]}")
56
+ end
57
+
58
+ amount += (price[:unit_amount] * line_item[:quantity])
59
+ end
60
+
61
+ currency = prices[line_items.first[:price]][:currency]
62
+ end
63
+
64
+ payment_status = "unpaid"
65
+ payment_intent = nil
66
+ setup_intent = nil
67
+ case params[:mode]
68
+ when nil, "payment"
69
+ params[:customer] ||= new_customer(nil, nil, {email: params[:customer_email]}, nil)[:id]
70
+ require_param(:line_items) if params[:line_items].nil? || params[:line_items].empty?
71
+ payment_intent = new_payment_intent(nil, nil, {
72
+ amount: amount,
73
+ currency: currency,
74
+ customer: params[:customer],
75
+ payment_method_options: params[:payment_method_options],
76
+ payment_method_types: params[:payment_method_types]
77
+ }.merge(params[:payment_intent_data] || {}), nil)[:id]
78
+ checkout_session_line_items[id] = line_items
79
+ when "setup"
80
+ if !params[:line_items].nil? && !params[:line_items].empty?
81
+ throw Stripe::InvalidRequestError.new("You cannot pass `line_items` in `setup` mode", :line_items, http_status: 400)
82
+ end
83
+ setup_intent = new_setup_intent(nil, nil, {
84
+ customer: params[:customer],
85
+ payment_method_options: params[:payment_method_options],
86
+ payment_method_types: params[:payment_method_types]
87
+ }.merge(params[:setup_intent_data] || {}), nil)[:id]
88
+ payment_status = "no_payment_required"
89
+ when "subscription"
90
+ params[:customer] ||= new_customer(nil, nil, {email: params[:customer_email]}, nil)[:id]
91
+ require_param(:line_items) if params[:line_items].nil? || params[:line_items].empty?
92
+ checkout_session_line_items[id] = line_items
93
+ else
94
+ throw Stripe::InvalidRequestError.new("Invalid mode: must be one of payment, setup, or subscription", :mode, http_status: 400)
95
+ end
96
+
97
+ checkout_sessions[id] = {
98
+ id: id,
99
+ object: "checkout.session",
100
+ allow_promotion_codes: nil,
101
+ amount_subtotal: amount,
102
+ amount_total: amount,
103
+ automatic_tax: {
104
+ enabled: false,
105
+ status: nil
106
+ },
107
+ billing_address_collection: nil,
108
+ cancel_url: params[:cancel_url],
109
+ client_reference_id: nil,
110
+ currency: currency,
111
+ customer: params[:customer],
112
+ customer_details: nil,
113
+ customer_email: params[:customer_email],
114
+ livemode: false,
115
+ locale: nil,
116
+ metadata: params[:metadata],
117
+ mode: params[:mode],
118
+ payment_intent: payment_intent,
119
+ payment_method_options: params[:payment_method_options],
120
+ payment_method_types: params[:payment_method_types],
121
+ payment_status: payment_status,
122
+ setup_intent: setup_intent,
123
+ shipping: nil,
124
+ shipping_address_collection: nil,
125
+ submit_type: nil,
126
+ subscription: nil,
127
+ success_url: params[:success_url],
128
+ total_details: nil,
129
+ url: URI.join(StripeMock.checkout_base, id).to_s
130
+ }
131
+ end
132
+
133
+ def list_checkout_sessions(route, method_url, params, headers)
134
+ Data.mock_list_object(checkout_sessions.values)
7
135
  end
8
136
 
9
137
  def get_checkout_session(route, method_url, params, headers)
10
138
  route =~ method_url
11
- assert_existence :checkout_session, $1, checkout_sessions[$1]
139
+ checkout_session = assert_existence :checkout_session, $1, checkout_sessions[$1]
140
+
141
+ checkout_session = checkout_session.clone
142
+ if params[:expand]&.include?('setup_intent') && checkout_session[:setup_intent]
143
+ checkout_session[:setup_intent] = setup_intents[checkout_session[:setup_intent]]
144
+ end
145
+ checkout_session
146
+ end
147
+
148
+ def list_line_items(route, method_url, params, headers)
149
+ route =~ method_url
150
+ checkout_session = assert_existence :checkout_session, $1, checkout_sessions[$1]
151
+
152
+ case checkout_session[:mode]
153
+ when "payment", "subscription"
154
+ line_items = assert_existence :checkout_session_line_items, $1, checkout_session_line_items[$1]
155
+ line_items.map do |line_item|
156
+ price = prices[line_item[:price]].clone
157
+
158
+ if price.nil?
159
+ raise StripeMock::StripeMockError.new("Price not found for ID: #{line_item[:price]}")
160
+ end
161
+
162
+ {
163
+ id: line_item[:id],
164
+ object: "item",
165
+ amount_subtotal: price[:unit_amount] * line_item[:quantity],
166
+ amount_total: price[:unit_amount] * line_item[:quantity],
167
+ currency: price[:currency],
168
+ price: price.clone,
169
+ quantity: line_item[:quantity]
170
+ }
171
+ end
172
+ else
173
+ throw Stripe::InvalidRequestError("Only payment and subscription sessions have line items")
174
+ end
12
175
  end
13
176
  end
14
177
  end
@@ -4,7 +4,7 @@ module StripeMock
4
4
 
5
5
  def Events.included(klass)
6
6
  klass.add_handler 'get /v1/events/(.*)', :retrieve_event
7
- klass.add_handler 'get /v1/events', :list_events
7
+ klass.add_handler 'get /v1/events', :list_events
8
8
  end
9
9
 
10
10
  def retrieve_event(route, method_url, params, headers)
@@ -13,9 +13,36 @@ module StripeMock
13
13
  end
14
14
 
15
15
  def list_events(route, method_url, params, headers)
16
- Data.mock_list_object(events.values, params)
16
+ values = filter_by_created(events.values, params: params)
17
+ Data.mock_list_object(values, params)
17
18
  end
18
-
19
+
20
+ private
21
+
22
+ def filter_by_created(event_list, params:)
23
+ if params[:created].nil?
24
+ return event_list
25
+ end
26
+
27
+ if params[:created].is_a?(Hash)
28
+ if params[:created][:gt]
29
+ event_list = event_list.select { |event| event[:created] > params[:created][:gt].to_i }
30
+ end
31
+ if params[:created][:gte]
32
+ event_list = event_list.select { |event| event[:created] >= params[:created][:gte].to_i }
33
+ end
34
+ if params[:created][:lt]
35
+ event_list = event_list.select { |event| event[:created] < params[:created][:lt].to_i }
36
+ end
37
+ if params[:created][:lte]
38
+ event_list = event_list.select { |event| event[:created] <= params[:created][:lte].to_i }
39
+ end
40
+ else
41
+ event_list = event_list.select { |event| event[:created] == params[:created].to_i }
42
+ end
43
+ event_list
44
+ end
45
+
19
46
  end
20
47
  end
21
48
  end
@@ -7,6 +7,7 @@ module StripeMock
7
7
  attrs[:coupon] = coupon
8
8
  attrs[:start] = Time.now.to_i
9
9
  attrs[:end] = (DateTime.now >> coupon[:duration_in_months].to_i).to_time.to_i if coupon[:duration] == 'repeating'
10
+ attrs[:id] = new_id("di")
10
11
  end
11
12
 
12
13
  object[:discount] = Stripe::Discount.construct_from(discount_attrs)
@@ -13,11 +13,15 @@ module StripeMock
13
13
  subscription[:items][:data] = plans.map do |plan|
14
14
  matching_item = items && items.detect { |item| [item[:price], item[:plan]].include? plan[:id] }
15
15
  if matching_item
16
- quantity = matching_item[:quantity] || 1
17
- id = matching_item[:id] || new_id('si')
18
- Data.mock_subscription_item({ plan: plan, quantity: quantity, id: id })
16
+ matching_item[:quantity] ||= 1
17
+ matching_item[:id] ||= new_id('si')
18
+ params = matching_item.merge(plan: plan)
19
+ params[:price] = plan if plan[:object] == "price"
20
+ Data.mock_subscription_item(params)
19
21
  else
20
- Data.mock_subscription_item({ plan: plan, id: new_id('si') })
22
+ params = { plan: plan, id: new_id('si') }
23
+ params[:price] = plan if plan[:object] == "price"
24
+ Data.mock_subscription_item(params)
21
25
  end
22
26
  end
23
27
  subscription
@@ -46,10 +50,10 @@ module StripeMock
46
50
 
47
51
  if (((plan && plan[:trial_period_days]) || 0) == 0 && options[:trial_end].nil?) || options[:trial_end] == "now"
48
52
  end_time = options[:billing_cycle_anchor] || get_ending_time(start_time, plan)
49
- params.merge!({status: 'active', current_period_end: end_time, trial_start: nil, trial_end: nil, billing_cycle_anchor: options[:billing_cycle_anchor]})
53
+ params.merge!({status: 'active', current_period_end: end_time, trial_start: nil, trial_end: nil, billing_cycle_anchor: options[:billing_cycle_anchor] || created_time})
50
54
  else
51
55
  end_time = options[:trial_end] || (Time.now.utc.to_i + plan[:trial_period_days]*86400)
52
- params.merge!({status: 'trialing', current_period_end: end_time, trial_start: start_time, trial_end: end_time, billing_cycle_anchor: nil})
56
+ params.merge!({status: 'trialing', current_period_end: end_time, trial_start: start_time, trial_end: end_time, billing_cycle_anchor: options[:billing_cycle_anchor] || created_time})
53
57
  end
54
58
 
55
59
  params
@@ -86,11 +90,13 @@ module StripeMock
86
90
  def get_ending_time(start_time, plan, intervals = 1)
87
91
  return start_time unless plan
88
92
 
89
- case plan[:interval]
93
+ interval = plan[:interval] || plan.dig(:recurring, :interval)
94
+ interval_count = plan[:interval_count] || plan.dig(:recurring, :interval_count) || 1
95
+ case interval
90
96
  when "week"
91
- start_time + (604800 * (plan[:interval_count] || 1) * intervals)
97
+ start_time + (604800 * (interval_count) * intervals)
92
98
  when "month"
93
- (Time.at(start_time).to_datetime >> ((plan[:interval_count] || 1) * intervals)).to_time.to_i
99
+ (Time.at(start_time).to_datetime >> ((interval_count) * intervals)).to_time.to_i
94
100
  when "year"
95
101
  (Time.at(start_time).to_datetime >> (12 * intervals)).to_time.to_i # max period is 1 year
96
102
  else
@@ -119,6 +125,19 @@ module StripeMock
119
125
  end
120
126
  total
121
127
  end
128
+
129
+ def filter_by_timestamp(subscriptions, field:, value:)
130
+ if value.is_a?(Hash)
131
+ operator_mapping = { gt: :>, gte: :>=, lt: :<, lte: :<= }
132
+ subscriptions.filter do |sub|
133
+ sub[field].public_send(operator_mapping[value.keys[0]], value.values[0])
134
+ end
135
+ else
136
+ subscriptions.filter do |sub|
137
+ sub[field] == value
138
+ end
139
+ end
140
+ end
122
141
  end
123
142
  end
124
143
  end
@@ -53,7 +53,12 @@ module StripeMock
53
53
  route =~ method_url
54
54
  assert_existence :invoice, $1, invoices[$1]
55
55
  charge = invoice_charge(invoices[$1])
56
- invoices[$1].merge!(:paid => true, :attempted => true, :charge => charge[:id])
56
+ invoices[$1].merge!(
57
+ :paid => true,
58
+ :status => "paid",
59
+ :attempted => true,
60
+ :charge => charge[:id],
61
+ )
57
62
  end
58
63
 
59
64
  def upcoming_invoice(route, method_url, params, headers = {})
@@ -81,6 +81,10 @@ module StripeMock
81
81
  route =~ method_url
82
82
  payment_intent = assert_existence :payment_intent, $1, payment_intents[$1]
83
83
 
84
+ if params[:payment_method]
85
+ payment_intent[:payment_method] = params[:payment_method]
86
+ end
87
+
84
88
  succeeded_payment_intent(payment_intent)
85
89
  end
86
90
 
@@ -169,12 +173,19 @@ module StripeMock
169
173
  payment_intent[:status] = 'succeeded'
170
174
  btxn = new_balance_transaction('txn', { source: payment_intent[:id] })
171
175
 
172
- payment_intent[:charges][:data] << Data.mock_charge(
176
+ charge_id = new_id('ch')
177
+
178
+ charges[charge_id] = Data.mock_charge(
179
+ id: charge_id,
173
180
  balance_transaction: btxn,
181
+ payment_intent: payment_intent[:id],
174
182
  amount: payment_intent[:amount],
175
- currency: payment_intent[:currency]
183
+ currency: payment_intent[:currency],
184
+ payment_method: payment_intent[:payment_method]
176
185
  )
177
186
 
187
+ payment_intent[:charges][:data] << charges[charge_id].clone
188
+
178
189
  payment_intent
179
190
  end
180
191
  end
@@ -78,7 +78,11 @@ module StripeMock
78
78
 
79
79
  # post /v1/payment_methods/:id
80
80
  def update_payment_method(route, method_url, params, headers)
81
- allowed_params = [:billing_details, :card, :ideal, :sepa_debit, :metadata]
81
+ allowed_params = [:billing_details, :card, :metadata]
82
+ disallowed_params = params.keys - allowed_params
83
+ unless disallowed_params.empty?
84
+ raise Stripe::InvalidRequestError.new("Received unknown parameter: #{disallowed_params.first}", disallowed_params.first)
85
+ end
82
86
 
83
87
  id = method_url.match(route)[1]
84
88
 
@@ -11,6 +11,12 @@ module StripeMock
11
11
 
12
12
  def new_price(route, method_url, params, headers)
13
13
  params[:id] ||= new_id('price')
14
+
15
+ if params[:product_data]
16
+ params[:product] = create_product(nil, nil, params[:product_data], nil)[:id] unless params[:product]
17
+ params.delete(:product_data)
18
+ end
19
+
14
20
  validate_create_price_params(params)
15
21
  prices[ params[:id] ] = Data.mock_price(params)
16
22
  end
@@ -37,6 +43,18 @@ module StripeMock
37
43
  end
38
44
  end
39
45
 
46
+ if params.key?(:currency)
47
+ price_data.select! do |price|
48
+ params[:currency] == price[:currency]
49
+ end
50
+ end
51
+
52
+ if params.key?(:product)
53
+ price_data.select! do |price|
54
+ params[:product] == price[:product]
55
+ end
56
+ end
57
+
40
58
  Data.mock_list_object(price_data.first(limit), params.merge!(limit: limit))
41
59
  end
42
60
  end
@@ -0,0 +1,43 @@
1
+ module StripeMock
2
+ module RequestHandlers
3
+ module PromotionCodes
4
+
5
+ def PromotionCodes.included(klass)
6
+ klass.add_handler 'post /v1/promotion_codes', :new_promotion_code
7
+ klass.add_handler 'post /v1/promotion_codes/([^/]*)', :update_promotion_code
8
+ klass.add_handler 'get /v1/promotion_codes/([^/]*)', :get_promotion_code
9
+ klass.add_handler 'get /v1/promotion_codes', :list_promotion_code
10
+ end
11
+
12
+ def new_promotion_code(route, method_url, params, headers)
13
+ params[:id] ||= new_id("promo")
14
+ raise Stripe::InvalidRequestError.new("Missing required param: coupon", "promotion_code", http_status: 400) unless params[:coupon]
15
+
16
+ if params[:restrictions]
17
+ if params[:restrictions][:minimum_amount] && !params[:restrictions][:minimum_amount_currency]
18
+ raise Stripe::InvalidRequestError.new(
19
+ "You must pass minimum_amount_currency when passing minimum_amount", "minimum_amount_currency", http_status: 400
20
+ )
21
+ end
22
+ end
23
+
24
+ promotion_codes[ params[:id] ] = Data.mock_promotion_code(params)
25
+ end
26
+
27
+ def update_promotion_code(route, method_url, params, headers)
28
+ route =~ method_url
29
+ assert_existence :promotion_code, $1, promotion_codes[$1]
30
+ promotion_codes[$1].merge!(params)
31
+ end
32
+
33
+ def get_promotion_code(route, method_url, params, headers)
34
+ route =~ method_url
35
+ assert_existence :promotion_code, $1, promotion_codes[$1]
36
+ end
37
+
38
+ def list_promotion_code(route, method_url, params, headers)
39
+ Data.mock_list_object(promotion_codes.values, params)
40
+ end
41
+ end
42
+ end
43
+ end
@@ -18,7 +18,18 @@ module StripeMock
18
18
  end
19
19
  end
20
20
 
21
- charge = assert_existence :charge, params[:charge], charges[params[:charge]]
21
+ if params[:payment_intent]
22
+ payment_intent = assert_existence(
23
+ :payment_intent,
24
+ params[:payment_intent],
25
+ payment_intents[params[:payment_intent]]
26
+ )
27
+ charge = {}
28
+ else
29
+ charge = assert_existence :charge, params[:charge], charges[params[:charge]]
30
+ payment_intent = {}
31
+ end
32
+ params[:amount] ||= payment_intent[:amount]
22
33
  params[:amount] ||= charge[:amount]
23
34
  id = new_id('re')
24
35
  bal_trans_params = {
@@ -32,7 +43,7 @@ module StripeMock
32
43
  :id => id,
33
44
  :charge => charge[:id],
34
45
  )
35
- add_refund_to_charge(refund, charge)
46
+ add_refund_to_charge(refund, charge) unless charge.empty?
36
47
  refunds[id] = refund
37
48
 
38
49
  if params[:expand] == ['balance_transaction']
@@ -15,20 +15,22 @@ module StripeMock
15
15
  ]
16
16
 
17
17
  def SetupIntents.included(klass)
18
- klass.add_handler 'post /v1/setup_intents', :new_setup_intent
19
- klass.add_handler 'get /v1/setup_intents', :get_setup_intents
20
- klass.add_handler 'get /v1/setup_intents/(.*)', :get_setup_intent
21
- klass.add_handler 'post /v1/setup_intents/(.*)/confirm', :confirm_setup_intent
22
- klass.add_handler 'post /v1/setup_intents/(.*)/cancel', :cancel_setup_intent
23
- klass.add_handler 'post /v1/setup_intents/(.*)', :update_setup_intent
18
+ klass.add_handler 'post /v1/setup_intents', :new_setup_intent
19
+ klass.add_handler 'get /v1/setup_intents', :get_setup_intents
20
+ klass.add_handler 'get /v1/setup_intents/(.*)', :get_setup_intent
21
+ klass.add_handler 'post /v1/setup_intents/(.*)/confirm', :confirm_setup_intent
22
+ klass.add_handler 'post /v1/setup_intents/(.*)/cancel', :cancel_setup_intent
23
+ klass.add_handler 'post /v1/setup_intents/(.*)', :update_setup_intent
24
24
  end
25
25
 
26
26
  def new_setup_intent(route, method_url, params, headers)
27
27
  id = new_id('si')
28
+ status = params[:payment_method] ? 'requires_action' : 'requires_payment_method'
28
29
 
29
30
  setup_intents[id] = Data.mock_setup_intent(
30
31
  params.merge(
31
- id: id
32
+ id: id,
33
+ status: status
32
34
  )
33
35
  )
34
36
 
@@ -40,7 +42,7 @@ module StripeMock
40
42
  id = $1
41
43
 
42
44
  setup_intent = assert_existence :setup_intent, id, setup_intents[id]
43
- setup_intents[id] = Util.rmerge(setup_intent, params.select{ |k,v| ALLOWED_PARAMS.include?(k)})
45
+ setup_intents[id] = Util.rmerge(setup_intent, params.select { |k, v| ALLOWED_PARAMS.include?(k) })
44
46
  end
45
47
 
46
48
  def get_setup_intents(route, method_url, params, headers)
@@ -50,7 +52,7 @@ module StripeMock
50
52
  clone = setup_intents.clone
51
53
 
52
54
  if params[:customer]
53
- clone.delete_if { |k,v| v[:customer] != params[:customer] }
55
+ clone.delete_if { |k, v| v[:customer] != params[:customer] }
54
56
  end
55
57
 
56
58
  Data.mock_list_object(clone.values, params)
@@ -62,6 +64,11 @@ module StripeMock
62
64
  setup_intent = assert_existence :setup_intent, setup_intent_id, setup_intents[setup_intent_id]
63
65
 
64
66
  setup_intent = setup_intent.clone
67
+
68
+ if params[:expand]&.include?("payment_method")
69
+ setup_intent[:payment_method] = assert_existence :payment_method, setup_intent[:payment_method], payment_methods[setup_intent[:payment_method]]
70
+ end
71
+
65
72
  setup_intent
66
73
  end
67
74