stripe-ruby-mock 3.1.0.rc3 → 4.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/rspec_tests.yml +38 -0
- data/.rspec +2 -1
- data/CHANGELOG.md +36 -0
- data/Gemfile +0 -5
- data/README.md +6 -4
- data/lib/stripe_mock/api/client.rb +1 -1
- data/lib/stripe_mock/api/webhooks.rb +65 -26
- data/lib/stripe_mock/data.rb +80 -11
- data/lib/stripe_mock/instance.rb +7 -3
- data/lib/stripe_mock/request_handlers/accounts.rb +17 -6
- data/lib/stripe_mock/request_handlers/charges.rb +25 -1
- data/lib/stripe_mock/request_handlers/checkout_session.rb +158 -1
- data/lib/stripe_mock/request_handlers/customers.rb +12 -1
- data/lib/stripe_mock/request_handlers/events.rb +30 -3
- data/lib/stripe_mock/request_handlers/helpers/coupon_helpers.rb +1 -0
- data/lib/stripe_mock/request_handlers/helpers/search_helpers.rb +67 -0
- data/lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb +28 -9
- data/lib/stripe_mock/request_handlers/invoices.rb +16 -2
- data/lib/stripe_mock/request_handlers/payment_intents.rb +23 -3
- data/lib/stripe_mock/request_handlers/payment_methods.rb +5 -1
- data/lib/stripe_mock/request_handlers/prices.rb +31 -4
- data/lib/stripe_mock/request_handlers/products.rb +14 -5
- data/lib/stripe_mock/request_handlers/promotion_codes.rb +43 -0
- data/lib/stripe_mock/request_handlers/refunds.rb +13 -2
- data/lib/stripe_mock/request_handlers/setup_intents.rb +16 -9
- data/lib/stripe_mock/request_handlers/subscriptions.rb +103 -5
- data/lib/stripe_mock/request_handlers/transfers.rb +12 -1
- data/lib/stripe_mock/request_handlers/validators/param_validators.rb +5 -4
- data/lib/stripe_mock/test_strategies/base.rb +51 -24
- data/lib/stripe_mock/version.rb +1 -1
- data/lib/stripe_mock/webhook_fixtures/account.updated.json +1 -1
- data/lib/stripe_mock/webhook_fixtures/balance.available.json +26 -20
- data/lib/stripe_mock/webhook_fixtures/charge.captured.json +143 -0
- data/lib/stripe_mock/webhook_fixtures/charge.dispute.created.json +63 -16
- data/lib/stripe_mock/webhook_fixtures/charge.failed.json +49 -120
- data/lib/stripe_mock/webhook_fixtures/charge.refund.updated.json +35 -0
- data/lib/stripe_mock/webhook_fixtures/charge.refunded.json +145 -50
- data/lib/stripe_mock/webhook_fixtures/charge.succeeded.json +114 -43
- data/lib/stripe_mock/webhook_fixtures/checkout.session.completed.json +79 -0
- data/lib/stripe_mock/webhook_fixtures/checkout.session.completed.payment_mode.json +53 -0
- data/lib/stripe_mock/webhook_fixtures/checkout.session.completed.setup_mode.json +45 -0
- data/lib/stripe_mock/webhook_fixtures/customer.created.json +37 -46
- data/lib/stripe_mock/webhook_fixtures/customer.deleted.json +36 -32
- data/lib/stripe_mock/webhook_fixtures/customer.source.created.json +31 -22
- data/lib/stripe_mock/webhook_fixtures/customer.source.updated.json +36 -25
- data/lib/stripe_mock/webhook_fixtures/customer.subscription.created.json +135 -47
- data/lib/stripe_mock/webhook_fixtures/customer.subscription.deleted.json +134 -45
- data/lib/stripe_mock/webhook_fixtures/customer.subscription.updated.json +135 -56
- data/lib/stripe_mock/webhook_fixtures/customer.updated.json +38 -47
- data/lib/stripe_mock/webhook_fixtures/invoice.created.json +176 -49
- data/lib/stripe_mock/webhook_fixtures/invoice.finalized.json +171 -0
- data/lib/stripe_mock/webhook_fixtures/invoice.paid.json +171 -0
- data/lib/stripe_mock/webhook_fixtures/invoice.payment_action_required.json +171 -0
- data/lib/stripe_mock/webhook_fixtures/invoice.payment_failed.json +149 -83
- data/lib/stripe_mock/webhook_fixtures/invoice.payment_succeeded.json +149 -90
- data/lib/stripe_mock/webhook_fixtures/invoice.upcoming.json +70 -0
- data/lib/stripe_mock/webhook_fixtures/invoice.updated.json +178 -50
- data/lib/stripe_mock/webhook_fixtures/invoiceitem.created.json +87 -13
- data/lib/stripe_mock/webhook_fixtures/invoiceitem.updated.json +88 -14
- data/lib/stripe_mock/webhook_fixtures/mandate.updated.json +34 -0
- data/lib/stripe_mock/webhook_fixtures/payment_intent.amount_capturable_updated.json +170 -0
- data/lib/stripe_mock/webhook_fixtures/payment_intent.canceled.json +73 -0
- data/lib/stripe_mock/webhook_fixtures/payment_intent.created.json +86 -0
- data/lib/stripe_mock/webhook_fixtures/payment_intent.payment_failed.json +118 -79
- data/lib/stripe_mock/webhook_fixtures/payment_intent.processing.json +162 -0
- data/lib/stripe_mock/webhook_fixtures/payment_intent.requires_action.json +191 -0
- data/lib/stripe_mock/webhook_fixtures/payment_intent.succeeded.json +85 -53
- data/lib/stripe_mock/webhook_fixtures/payment_link.created.json +47 -0
- data/lib/stripe_mock/webhook_fixtures/payment_link.updated.json +50 -0
- data/lib/stripe_mock/webhook_fixtures/payment_method.attached.json +63 -0
- data/lib/stripe_mock/webhook_fixtures/payment_method.detached.json +62 -0
- data/lib/stripe_mock/webhook_fixtures/payout.created.json +40 -0
- data/lib/stripe_mock/webhook_fixtures/payout.paid.json +40 -0
- data/lib/stripe_mock/webhook_fixtures/payout.updated.json +46 -0
- data/lib/stripe_mock/webhook_fixtures/plan.created.json +30 -13
- data/lib/stripe_mock/webhook_fixtures/plan.deleted.json +30 -13
- data/lib/stripe_mock/webhook_fixtures/plan.updated.json +34 -14
- data/lib/stripe_mock/webhook_fixtures/price.created.json +42 -0
- data/lib/stripe_mock/webhook_fixtures/price.deleted.json +42 -0
- data/lib/stripe_mock/webhook_fixtures/price.updated.json +48 -0
- data/lib/stripe_mock/webhook_fixtures/product.created.json +19 -13
- data/lib/stripe_mock/webhook_fixtures/product.deleted.json +20 -14
- data/lib/stripe_mock/webhook_fixtures/product.updated.json +24 -15
- data/lib/stripe_mock/webhook_fixtures/quote.accepted.json +92 -0
- data/lib/stripe_mock/webhook_fixtures/quote.canceled.json +92 -0
- data/lib/stripe_mock/webhook_fixtures/quote.created.json +92 -0
- data/lib/stripe_mock/webhook_fixtures/quote.finalized.json +92 -0
- data/lib/stripe_mock/webhook_fixtures/setup_intent.canceled.json +46 -0
- data/lib/stripe_mock/webhook_fixtures/setup_intent.created.json +51 -0
- data/lib/stripe_mock/webhook_fixtures/setup_intent.setup_failed.json +100 -0
- data/lib/stripe_mock/webhook_fixtures/setup_intent.succeeded.json +46 -0
- data/lib/stripe_mock/webhook_fixtures/subscription_schedule.canceled.json +119 -0
- data/lib/stripe_mock/webhook_fixtures/subscription_schedule.created.json +114 -0
- data/lib/stripe_mock/webhook_fixtures/subscription_schedule.released.json +111 -0
- data/lib/stripe_mock/webhook_fixtures/subscription_schedule.updated.json +125 -0
- data/lib/stripe_mock/webhook_fixtures/tax_rate.created.json +32 -0
- data/lib/stripe_mock/webhook_fixtures/tax_rate.updated.json +37 -0
- data/lib/stripe_mock.rb +4 -1
- data/spec/instance_spec.rb +3 -1
- data/spec/integration_examples/completing_checkout_sessions_example.rb +37 -0
- data/spec/readme_spec.rb +1 -1
- data/spec/shared_stripe_examples/account_examples.rb +9 -1
- data/spec/shared_stripe_examples/bank_token_examples.rb +5 -7
- data/spec/shared_stripe_examples/charge_examples.rb +97 -0
- data/spec/shared_stripe_examples/checkout_session_examples.rb +99 -0
- data/spec/shared_stripe_examples/customer_examples.rb +56 -0
- data/spec/shared_stripe_examples/invoice_examples.rb +107 -1
- data/spec/shared_stripe_examples/payment_intent_examples.rb +136 -0
- data/spec/shared_stripe_examples/payment_method_examples.rb +32 -27
- data/spec/shared_stripe_examples/price_examples.rb +111 -2
- data/spec/shared_stripe_examples/product_examples.rb +68 -0
- data/spec/shared_stripe_examples/promotion_code_examples.rb +68 -0
- data/spec/shared_stripe_examples/refund_examples.rb +13 -0
- data/spec/shared_stripe_examples/setup_intent_examples.rb +17 -0
- data/spec/shared_stripe_examples/subscription_examples.rb +276 -6
- data/spec/shared_stripe_examples/transfer_examples.rb +10 -1
- data/spec/shared_stripe_examples/webhook_event_examples.rb +51 -5
- data/spec/spec_helper.rb +1 -1
- data/spec/support/stripe_examples.rb +3 -1
- data/stripe-ruby-mock.gemspec +3 -4
- metadata +59 -29
- data/.travis.yml +0 -25
- data/lib/stripe_mock/request_handlers/checkout.rb +0 -15
- data/spec/shared_stripe_examples/checkout_examples.rb +0 -47
@@ -3,7 +3,135 @@ module StripeMock
|
|
3
3
|
module Checkout
|
4
4
|
module Session
|
5
5
|
def Session.included(klass)
|
6
|
-
klass.add_handler '
|
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)
|
@@ -16,6 +144,35 @@ module StripeMock
|
|
16
144
|
end
|
17
145
|
checkout_session
|
18
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
|
175
|
+
end
|
19
176
|
end
|
20
177
|
end
|
21
178
|
end
|
@@ -5,9 +5,10 @@ module StripeMock
|
|
5
5
|
def Customers.included(klass)
|
6
6
|
klass.add_handler 'post /v1/customers', :new_customer
|
7
7
|
klass.add_handler 'post /v1/customers/([^/]*)', :update_customer
|
8
|
-
klass.add_handler 'get /v1/customers/([^/]*)',
|
8
|
+
klass.add_handler 'get /v1/customers/((?!search)[^/]*)', :get_customer
|
9
9
|
klass.add_handler 'delete /v1/customers/([^/]*)', :delete_customer
|
10
10
|
klass.add_handler 'get /v1/customers', :list_customers
|
11
|
+
klass.add_handler 'get /v1/customers/search', :search_customers
|
11
12
|
klass.add_handler 'delete /v1/customers/([^/]*)/discount', :delete_customer_discount
|
12
13
|
end
|
13
14
|
|
@@ -140,6 +141,16 @@ module StripeMock
|
|
140
141
|
Data.mock_list_object(customers[stripe_account]&.values, params)
|
141
142
|
end
|
142
143
|
|
144
|
+
SEARCH_FIELDS = ["email", "name", "phone"].freeze
|
145
|
+
def search_customers(route, method_url, params, headers)
|
146
|
+
require_param(:query) unless params[:query]
|
147
|
+
|
148
|
+
stripe_account = headers && headers[:stripe_account] || Stripe.api_key
|
149
|
+
all_customers = customers[stripe_account]&.values
|
150
|
+
results = search_results(all_customers, params[:query], fields: SEARCH_FIELDS, resource_name: "customers")
|
151
|
+
Data.mock_list_object(results, params)
|
152
|
+
end
|
153
|
+
|
143
154
|
def delete_customer_discount(route, method_url, params, headers)
|
144
155
|
stripe_account = headers && headers[:stripe_account] || Stripe.api_key
|
145
156
|
route =~ method_url
|
@@ -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
|
-
|
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)
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module StripeMock
|
2
|
+
module RequestHandlers
|
3
|
+
module Helpers
|
4
|
+
# Only supports exact matches on a single field, e.g.
|
5
|
+
# - 'amount:100'
|
6
|
+
# - 'email:"name@domain.com"'
|
7
|
+
# - 'name:"Foo Bar"'
|
8
|
+
# - 'metadata["foo"]:"bar"'
|
9
|
+
QUERYSTRING_PATTERN = /\A(?<field>[\w\.]+)(\[['"](?<metadata_key>[^'"]*)['"]\])?:['"]?(?<value>[^'"]*)['"]?\z/
|
10
|
+
def search_results(all_values, querystring, fields: [], resource_name:)
|
11
|
+
values = all_values.dup
|
12
|
+
query_match = QUERYSTRING_PATTERN.match(querystring)
|
13
|
+
raise Stripe::InvalidRequestError.new(
|
14
|
+
'We were unable to parse your search query.' \
|
15
|
+
' Try using the format `metadata["key"]:"value"` to query for metadata or key:"value" to query for other fields.',
|
16
|
+
nil,
|
17
|
+
http_status: 400,
|
18
|
+
) unless query_match
|
19
|
+
|
20
|
+
case query_match[:field]
|
21
|
+
when *fields
|
22
|
+
values = values.select { |resource|
|
23
|
+
exact_match?(actual: field_value(resource, field: query_match[:field]), expected: query_match[:value])
|
24
|
+
}
|
25
|
+
when "metadata"
|
26
|
+
values = values.select { |resource|
|
27
|
+
resource[:metadata] &&
|
28
|
+
exact_match?(actual: resource[:metadata][query_match[:metadata_key].to_sym], expected: query_match[:value])
|
29
|
+
}
|
30
|
+
else
|
31
|
+
raise Stripe::InvalidRequestError.new(
|
32
|
+
"Field `#{query_match[:field]}` is an unsupported search field for resource `#{resource_name}`." \
|
33
|
+
" See http://stripe.com/docs/search#query-fields-for-#{resource_name.gsub('_', '-')} for a list of supported fields.",
|
34
|
+
nil,
|
35
|
+
http_status: 400,
|
36
|
+
)
|
37
|
+
end
|
38
|
+
|
39
|
+
values
|
40
|
+
end
|
41
|
+
|
42
|
+
def exact_match?(actual:, expected:)
|
43
|
+
# allow comparisons of integers
|
44
|
+
if actual.respond_to?(:to_i) && actual.to_i == actual
|
45
|
+
expected = expected.to_i
|
46
|
+
end
|
47
|
+
# allow comparisons of boolean
|
48
|
+
case expected
|
49
|
+
when "true"
|
50
|
+
expected = true
|
51
|
+
when "false"
|
52
|
+
expected = false
|
53
|
+
end
|
54
|
+
|
55
|
+
actual == expected
|
56
|
+
end
|
57
|
+
|
58
|
+
def field_value(resource, field:)
|
59
|
+
value = resource
|
60
|
+
field.split('.').each do |segment|
|
61
|
+
value = value[segment.to_sym]
|
62
|
+
end
|
63
|
+
value
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -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
|
-
|
17
|
-
|
18
|
-
|
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
|
-
|
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:
|
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
|
-
|
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 * (
|
97
|
+
start_time + (604800 * (interval_count) * intervals)
|
92
98
|
when "month"
|
93
|
-
(Time.at(start_time).to_datetime >> ((
|
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
|
@@ -6,7 +6,8 @@ module StripeMock
|
|
6
6
|
klass.add_handler 'post /v1/invoices', :new_invoice
|
7
7
|
klass.add_handler 'get /v1/invoices/upcoming', :upcoming_invoice
|
8
8
|
klass.add_handler 'get /v1/invoices/(.*)/lines', :get_invoice_line_items
|
9
|
-
klass.add_handler 'get /v1/invoices/(.*)',
|
9
|
+
klass.add_handler 'get /v1/invoices/((?!search).*)', :get_invoice
|
10
|
+
klass.add_handler 'get /v1/invoices/search', :search_invoices
|
10
11
|
klass.add_handler 'get /v1/invoices', :list_invoices
|
11
12
|
klass.add_handler 'post /v1/invoices/(.*)/pay', :pay_invoice
|
12
13
|
klass.add_handler 'post /v1/invoices/(.*)', :update_invoice
|
@@ -25,6 +26,14 @@ module StripeMock
|
|
25
26
|
invoices[$1].merge!(params)
|
26
27
|
end
|
27
28
|
|
29
|
+
SEARCH_FIELDS = ["currency", "customer", "number", "receipt_number", "subscription", "total"].freeze
|
30
|
+
def search_invoices(route, method_url, params, headers)
|
31
|
+
require_param(:query) unless params[:query]
|
32
|
+
|
33
|
+
results = search_results(invoices.values, params[:query], fields: SEARCH_FIELDS, resource_name: "invoices")
|
34
|
+
Data.mock_list_object(results, params)
|
35
|
+
end
|
36
|
+
|
28
37
|
def list_invoices(route, method_url, params, headers)
|
29
38
|
params[:offset] ||= 0
|
30
39
|
params[:limit] ||= 10
|
@@ -53,7 +62,12 @@ module StripeMock
|
|
53
62
|
route =~ method_url
|
54
63
|
assert_existence :invoice, $1, invoices[$1]
|
55
64
|
charge = invoice_charge(invoices[$1])
|
56
|
-
invoices[$1].merge!(
|
65
|
+
invoices[$1].merge!(
|
66
|
+
:paid => true,
|
67
|
+
:status => "paid",
|
68
|
+
:attempted => true,
|
69
|
+
:charge => charge[:id],
|
70
|
+
)
|
57
71
|
end
|
58
72
|
|
59
73
|
def upcoming_invoice(route, method_url, params, headers = {})
|
@@ -6,7 +6,8 @@ module StripeMock
|
|
6
6
|
def PaymentIntents.included(klass)
|
7
7
|
klass.add_handler 'post /v1/payment_intents', :new_payment_intent
|
8
8
|
klass.add_handler 'get /v1/payment_intents', :get_payment_intents
|
9
|
-
klass.add_handler 'get /v1/payment_intents/(.*)',
|
9
|
+
klass.add_handler 'get /v1/payment_intents/((?!search).*)', :get_payment_intent
|
10
|
+
klass.add_handler 'get /v1/payment_intents/search', :search_payment_intents
|
10
11
|
klass.add_handler 'post /v1/payment_intents/(.*)/confirm', :confirm_payment_intent
|
11
12
|
klass.add_handler 'post /v1/payment_intents/(.*)/capture', :capture_payment_intent
|
12
13
|
klass.add_handler 'post /v1/payment_intents/(.*)/cancel', :cancel_payment_intent
|
@@ -70,6 +71,14 @@ module StripeMock
|
|
70
71
|
payment_intent
|
71
72
|
end
|
72
73
|
|
74
|
+
SEARCH_FIELDS = ["amount", "currency", "customer", "status"].freeze
|
75
|
+
def search_payment_intents(route, method_url, params, headers)
|
76
|
+
require_param(:query) unless params[:query]
|
77
|
+
|
78
|
+
results = search_results(payment_intents.values, params[:query], fields: SEARCH_FIELDS, resource_name: "payment_intents")
|
79
|
+
Data.mock_list_object(results, params)
|
80
|
+
end
|
81
|
+
|
73
82
|
def capture_payment_intent(route, method_url, params, headers)
|
74
83
|
route =~ method_url
|
75
84
|
payment_intent = assert_existence :payment_intent, $1, payment_intents[$1]
|
@@ -81,6 +90,10 @@ module StripeMock
|
|
81
90
|
route =~ method_url
|
82
91
|
payment_intent = assert_existence :payment_intent, $1, payment_intents[$1]
|
83
92
|
|
93
|
+
if params[:payment_method]
|
94
|
+
payment_intent[:payment_method] = params[:payment_method]
|
95
|
+
end
|
96
|
+
|
84
97
|
succeeded_payment_intent(payment_intent)
|
85
98
|
end
|
86
99
|
|
@@ -169,12 +182,19 @@ module StripeMock
|
|
169
182
|
payment_intent[:status] = 'succeeded'
|
170
183
|
btxn = new_balance_transaction('txn', { source: payment_intent[:id] })
|
171
184
|
|
172
|
-
|
185
|
+
charge_id = new_id('ch')
|
186
|
+
|
187
|
+
charges[charge_id] = Data.mock_charge(
|
188
|
+
id: charge_id,
|
173
189
|
balance_transaction: btxn,
|
190
|
+
payment_intent: payment_intent[:id],
|
174
191
|
amount: payment_intent[:amount],
|
175
|
-
currency: payment_intent[:currency]
|
192
|
+
currency: payment_intent[:currency],
|
193
|
+
payment_method: payment_intent[:payment_method]
|
176
194
|
)
|
177
195
|
|
196
|
+
payment_intent[:charges][:data] << charges[charge_id].clone
|
197
|
+
|
178
198
|
payment_intent
|
179
199
|
end
|
180
200
|
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, :
|
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
|
|
@@ -3,14 +3,21 @@ module StripeMock
|
|
3
3
|
module Prices
|
4
4
|
|
5
5
|
def Prices.included(klass)
|
6
|
-
klass.add_handler 'post /v1/prices',
|
7
|
-
klass.add_handler 'post /v1/prices/(.*)',
|
8
|
-
klass.add_handler 'get /v1/prices/(.*)',
|
9
|
-
klass.add_handler 'get /v1/prices', :
|
6
|
+
klass.add_handler 'post /v1/prices', :new_price
|
7
|
+
klass.add_handler 'post /v1/prices/(.*)', :update_price
|
8
|
+
klass.add_handler 'get /v1/prices/((?!search).*)', :get_price
|
9
|
+
klass.add_handler 'get /v1/prices/search', :search_prices
|
10
|
+
klass.add_handler 'get /v1/prices', :list_prices
|
10
11
|
end
|
11
12
|
|
12
13
|
def new_price(route, method_url, params, headers)
|
13
14
|
params[:id] ||= new_id('price')
|
15
|
+
|
16
|
+
if params[:product_data]
|
17
|
+
params[:product] = create_product(nil, nil, params[:product_data], nil)[:id] unless params[:product]
|
18
|
+
params.delete(:product_data)
|
19
|
+
end
|
20
|
+
|
14
21
|
validate_create_price_params(params)
|
15
22
|
prices[ params[:id] ] = Data.mock_price(params)
|
16
23
|
end
|
@@ -37,8 +44,28 @@ module StripeMock
|
|
37
44
|
end
|
38
45
|
end
|
39
46
|
|
47
|
+
if params.key?(:currency)
|
48
|
+
price_data.select! do |price|
|
49
|
+
params[:currency] == price[:currency]
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
if params.key?(:product)
|
54
|
+
price_data.select! do |price|
|
55
|
+
params[:product] == price[:product]
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
40
59
|
Data.mock_list_object(price_data.first(limit), params.merge!(limit: limit))
|
41
60
|
end
|
61
|
+
|
62
|
+
SEARCH_FIELDS = ["active", "currency", "lookup_key", "product", "type"].freeze
|
63
|
+
def search_prices(route, method_url, params, headers)
|
64
|
+
require_param(:query) unless params[:query]
|
65
|
+
|
66
|
+
results = search_results(prices.values, params[:query], fields: SEARCH_FIELDS, resource_name: "prices")
|
67
|
+
Data.mock_list_object(results, params)
|
68
|
+
end
|
42
69
|
end
|
43
70
|
end
|
44
71
|
end
|
@@ -2,11 +2,12 @@ module StripeMock
|
|
2
2
|
module RequestHandlers
|
3
3
|
module Products
|
4
4
|
def self.included(base)
|
5
|
-
base.add_handler 'post /v1/products',
|
6
|
-
base.add_handler 'get /v1/products/(.*)',
|
7
|
-
base.add_handler '
|
8
|
-
base.add_handler '
|
9
|
-
base.add_handler '
|
5
|
+
base.add_handler 'post /v1/products', :create_product
|
6
|
+
base.add_handler 'get /v1/products/((?!search).*)', :retrieve_product
|
7
|
+
base.add_handler 'get /v1/products/search', :search_products
|
8
|
+
base.add_handler 'post /v1/products/(.*)', :update_product
|
9
|
+
base.add_handler 'get /v1/products', :list_products
|
10
|
+
base.add_handler 'delete /v1/products/(.*)', :destroy_product
|
10
11
|
end
|
11
12
|
|
12
13
|
def create_product(_route, _method_url, params, _headers)
|
@@ -32,6 +33,14 @@ module StripeMock
|
|
32
33
|
Data.mock_list_object(products.values.take(limit), params)
|
33
34
|
end
|
34
35
|
|
36
|
+
SEARCH_FIELDS = ["active", "description", "name", "shippable", "url"].freeze
|
37
|
+
def search_products(route, method_url, params, headers)
|
38
|
+
require_param(:query) unless params[:query]
|
39
|
+
|
40
|
+
results = search_results(products.values, params[:query], fields: SEARCH_FIELDS, resource_name: "products")
|
41
|
+
Data.mock_list_object(results, params)
|
42
|
+
end
|
43
|
+
|
35
44
|
def destroy_product(route, method_url, _params, _headers)
|
36
45
|
id = method_url.match(route).captures.first
|
37
46
|
assert_existence :product, id, products[id]
|
@@ -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
|
-
|
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']
|