epopia-stripe-ruby-mock 2.5.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.env +2 -0
- data/.gitignore +8 -0
- data/.rspec +1 -0
- data/.travis.yml +28 -0
- data/Gemfile +12 -0
- data/LICENSE.txt +22 -0
- data/README.md +413 -0
- data/Rakefile +14 -0
- data/bin/stripe-mock-server +19 -0
- data/lib/stripe_mock.rb +95 -0
- data/lib/stripe_mock/api/account_balance.rb +14 -0
- data/lib/stripe_mock/api/bank_tokens.rb +13 -0
- data/lib/stripe_mock/api/card_tokens.rb +13 -0
- data/lib/stripe_mock/api/client.rb +41 -0
- data/lib/stripe_mock/api/conversion_rate.rb +14 -0
- data/lib/stripe_mock/api/debug.rb +11 -0
- data/lib/stripe_mock/api/errors.rb +65 -0
- data/lib/stripe_mock/api/global_id_prefix.rb +22 -0
- data/lib/stripe_mock/api/instance.rb +38 -0
- data/lib/stripe_mock/api/live.rb +15 -0
- data/lib/stripe_mock/api/server.rb +39 -0
- data/lib/stripe_mock/api/test_helpers.rb +24 -0
- data/lib/stripe_mock/api/webhooks.rb +88 -0
- data/lib/stripe_mock/client.rb +127 -0
- data/lib/stripe_mock/data.rb +1193 -0
- data/lib/stripe_mock/data/list.rb +73 -0
- data/lib/stripe_mock/error_queue.rb +27 -0
- data/lib/stripe_mock/errors/closed_client_connection_error.rb +9 -0
- data/lib/stripe_mock/errors/server_timeout_error.rb +12 -0
- data/lib/stripe_mock/errors/stripe_mock_error.rb +15 -0
- data/lib/stripe_mock/errors/uninitialized_instance_error.rb +9 -0
- data/lib/stripe_mock/errors/unstarted_state_error.rb +9 -0
- data/lib/stripe_mock/errors/unsupported_request_error.rb +4 -0
- data/lib/stripe_mock/instance.rb +237 -0
- data/lib/stripe_mock/request_handlers/accounts.rb +86 -0
- data/lib/stripe_mock/request_handlers/balance.rb +17 -0
- data/lib/stripe_mock/request_handlers/balance_transactions.rb +37 -0
- data/lib/stripe_mock/request_handlers/cards.rb +35 -0
- data/lib/stripe_mock/request_handlers/charges.rb +177 -0
- data/lib/stripe_mock/request_handlers/country_spec.rb +22 -0
- data/lib/stripe_mock/request_handlers/coupons.rb +35 -0
- data/lib/stripe_mock/request_handlers/customers.rb +137 -0
- data/lib/stripe_mock/request_handlers/disputes.rb +35 -0
- data/lib/stripe_mock/request_handlers/ephemeral_key.rb +13 -0
- data/lib/stripe_mock/request_handlers/events.rb +21 -0
- data/lib/stripe_mock/request_handlers/external_accounts.rb +55 -0
- data/lib/stripe_mock/request_handlers/helpers/bank_account_helpers.rb +14 -0
- data/lib/stripe_mock/request_handlers/helpers/card_helpers.rb +127 -0
- data/lib/stripe_mock/request_handlers/helpers/charge_helpers.rb +16 -0
- data/lib/stripe_mock/request_handlers/helpers/coupon_helpers.rb +17 -0
- data/lib/stripe_mock/request_handlers/helpers/external_account_helpers.rb +49 -0
- data/lib/stripe_mock/request_handlers/helpers/subscription_helpers.rb +119 -0
- data/lib/stripe_mock/request_handlers/helpers/token_helpers.rb +44 -0
- data/lib/stripe_mock/request_handlers/invoice_items.rb +45 -0
- data/lib/stripe_mock/request_handlers/invoices.rb +177 -0
- data/lib/stripe_mock/request_handlers/orders.rb +80 -0
- data/lib/stripe_mock/request_handlers/payment_intents.rb +203 -0
- data/lib/stripe_mock/request_handlers/payment_methods.rb +112 -0
- data/lib/stripe_mock/request_handlers/payouts.rb +32 -0
- data/lib/stripe_mock/request_handlers/plans.rb +42 -0
- data/lib/stripe_mock/request_handlers/products.rb +43 -0
- data/lib/stripe_mock/request_handlers/recipients.rb +60 -0
- data/lib/stripe_mock/request_handlers/refunds.rb +91 -0
- data/lib/stripe_mock/request_handlers/sources.rb +55 -0
- data/lib/stripe_mock/request_handlers/subscription_items.rb +36 -0
- data/lib/stripe_mock/request_handlers/subscriptions.rb +296 -0
- data/lib/stripe_mock/request_handlers/tax_rates.rb +36 -0
- data/lib/stripe_mock/request_handlers/tokens.rb +75 -0
- data/lib/stripe_mock/request_handlers/transfers.rb +65 -0
- data/lib/stripe_mock/request_handlers/validators/param_validators.rb +32 -0
- data/lib/stripe_mock/server.rb +93 -0
- data/lib/stripe_mock/test_strategies/base.rb +81 -0
- data/lib/stripe_mock/test_strategies/live.rb +40 -0
- data/lib/stripe_mock/test_strategies/mock.rb +27 -0
- data/lib/stripe_mock/util.rb +44 -0
- data/lib/stripe_mock/version.rb +4 -0
- data/lib/stripe_mock/webhook_fixtures/account.application.deauthorized.json +12 -0
- data/lib/stripe_mock/webhook_fixtures/account.external_account.created.json +27 -0
- data/lib/stripe_mock/webhook_fixtures/account.external_account.deleted.json +27 -0
- data/lib/stripe_mock/webhook_fixtures/account.external_account.updated.json +27 -0
- data/lib/stripe_mock/webhook_fixtures/account.updated.json +26 -0
- data/lib/stripe_mock/webhook_fixtures/balance.available.json +25 -0
- data/lib/stripe_mock/webhook_fixtures/charge.dispute.closed.json +22 -0
- data/lib/stripe_mock/webhook_fixtures/charge.dispute.created.json +22 -0
- data/lib/stripe_mock/webhook_fixtures/charge.dispute.funds_reinstated.json +88 -0
- data/lib/stripe_mock/webhook_fixtures/charge.dispute.funds_withdrawn.json +88 -0
- data/lib/stripe_mock/webhook_fixtures/charge.dispute.updated.json +25 -0
- data/lib/stripe_mock/webhook_fixtures/charge.failed.json +56 -0
- data/lib/stripe_mock/webhook_fixtures/charge.refunded.json +69 -0
- data/lib/stripe_mock/webhook_fixtures/charge.succeeded.json +55 -0
- data/lib/stripe_mock/webhook_fixtures/charge.updated.json +58 -0
- data/lib/stripe_mock/webhook_fixtures/coupon.created.json +23 -0
- data/lib/stripe_mock/webhook_fixtures/coupon.deleted.json +23 -0
- data/lib/stripe_mock/webhook_fixtures/customer.created.json +54 -0
- data/lib/stripe_mock/webhook_fixtures/customer.deleted.json +42 -0
- data/lib/stripe_mock/webhook_fixtures/customer.discount.created.json +28 -0
- data/lib/stripe_mock/webhook_fixtures/customer.discount.deleted.json +28 -0
- data/lib/stripe_mock/webhook_fixtures/customer.discount.updated.json +43 -0
- data/lib/stripe_mock/webhook_fixtures/customer.source.created.json +32 -0
- data/lib/stripe_mock/webhook_fixtures/customer.source.deleted.json +32 -0
- data/lib/stripe_mock/webhook_fixtures/customer.source.updated.json +36 -0
- data/lib/stripe_mock/webhook_fixtures/customer.subscription.created.json +66 -0
- data/lib/stripe_mock/webhook_fixtures/customer.subscription.deleted.json +65 -0
- data/lib/stripe_mock/webhook_fixtures/customer.subscription.trial_will_end.json +65 -0
- data/lib/stripe_mock/webhook_fixtures/customer.subscription.updated.json +78 -0
- data/lib/stripe_mock/webhook_fixtures/customer.updated.json +57 -0
- data/lib/stripe_mock/webhook_fixtures/invoice.created.json +71 -0
- data/lib/stripe_mock/webhook_fixtures/invoice.payment_failed.json +105 -0
- data/lib/stripe_mock/webhook_fixtures/invoice.payment_succeeded.json +112 -0
- data/lib/stripe_mock/webhook_fixtures/invoice.updated.json +74 -0
- data/lib/stripe_mock/webhook_fixtures/invoiceitem.created.json +21 -0
- data/lib/stripe_mock/webhook_fixtures/invoiceitem.deleted.json +21 -0
- data/lib/stripe_mock/webhook_fixtures/invoiceitem.updated.json +24 -0
- data/lib/stripe_mock/webhook_fixtures/plan.created.json +20 -0
- data/lib/stripe_mock/webhook_fixtures/plan.deleted.json +20 -0
- data/lib/stripe_mock/webhook_fixtures/plan.updated.json +23 -0
- data/lib/stripe_mock/webhook_fixtures/transfer.created.json +89 -0
- data/lib/stripe_mock/webhook_fixtures/transfer.failed.json +89 -0
- data/lib/stripe_mock/webhook_fixtures/transfer.paid.json +89 -0
- data/lib/stripe_mock/webhook_fixtures/transfer.updated.json +92 -0
- data/lib/trollop.rb +782 -0
- data/spec/_dummy/webhooks/dummy.event.json +6 -0
- data/spec/api/instance_spec.rb +30 -0
- data/spec/fixtures/create_refund.yml +126 -0
- data/spec/fixtures/stripe_webhooks/account.updated.json +7 -0
- data/spec/fixtures/stripe_webhooks/custom.account.updated.json +5 -0
- data/spec/instance_spec.rb +100 -0
- data/spec/integration_examples/charge_token_examples.rb +51 -0
- data/spec/integration_examples/customer_card_examples.rb +42 -0
- data/spec/integration_examples/prepare_error_examples.rb +38 -0
- data/spec/list_spec.rb +140 -0
- data/spec/readme_spec.rb +75 -0
- data/spec/server_spec.rb +139 -0
- data/spec/shared_stripe_examples/account_examples.rb +96 -0
- data/spec/shared_stripe_examples/balance_examples.rb +11 -0
- data/spec/shared_stripe_examples/balance_transaction_examples.rb +63 -0
- data/spec/shared_stripe_examples/bank_examples.rb +229 -0
- data/spec/shared_stripe_examples/bank_token_examples.rb +59 -0
- data/spec/shared_stripe_examples/card_examples.rb +307 -0
- data/spec/shared_stripe_examples/card_token_examples.rb +185 -0
- data/spec/shared_stripe_examples/charge_examples.rb +510 -0
- data/spec/shared_stripe_examples/country_specs_examples.rb +18 -0
- data/spec/shared_stripe_examples/coupon_examples.rb +85 -0
- data/spec/shared_stripe_examples/customer_examples.rb +453 -0
- data/spec/shared_stripe_examples/dispute_examples.rb +98 -0
- data/spec/shared_stripe_examples/ephemeral_key_examples.rb +17 -0
- data/spec/shared_stripe_examples/error_mock_examples.rb +162 -0
- data/spec/shared_stripe_examples/external_account_examples.rb +170 -0
- data/spec/shared_stripe_examples/extra_features_examples.rb +36 -0
- data/spec/shared_stripe_examples/invoice_examples.rb +524 -0
- data/spec/shared_stripe_examples/invoice_item_examples.rb +69 -0
- data/spec/shared_stripe_examples/payment_intent_examples.rb +131 -0
- data/spec/shared_stripe_examples/payment_method_examples.rb +175 -0
- data/spec/shared_stripe_examples/payout_examples.rb +68 -0
- data/spec/shared_stripe_examples/plan_examples.rb +194 -0
- data/spec/shared_stripe_examples/product_example.rb +65 -0
- data/spec/shared_stripe_examples/recipient_examples.rb +118 -0
- data/spec/shared_stripe_examples/refund_examples.rb +472 -0
- data/spec/shared_stripe_examples/subscription_examples.rb +1148 -0
- data/spec/shared_stripe_examples/subscription_items_examples.rb +75 -0
- data/spec/shared_stripe_examples/tax_rate_examples.rb +42 -0
- data/spec/shared_stripe_examples/transfer_examples.rb +130 -0
- data/spec/shared_stripe_examples/validation_examples.rb +19 -0
- data/spec/shared_stripe_examples/webhook_event_examples.rb +261 -0
- data/spec/spec_helper.rb +58 -0
- data/spec/stripe_mock_spec.rb +123 -0
- data/spec/support/stripe_examples.rb +42 -0
- data/spec/util_spec.rb +121 -0
- data/stripe-ruby-mock.gemspec +27 -0
- metadata +344 -0
@@ -0,0 +1,112 @@
|
|
1
|
+
module StripeMock
|
2
|
+
module RequestHandlers
|
3
|
+
module PaymentMethods
|
4
|
+
def PaymentMethods.included(klass)
|
5
|
+
klass.add_handler 'post /v1/payment_methods', :new_payment_method
|
6
|
+
klass.add_handler 'get /v1/payment_methods/(.*)', :get_payment_method
|
7
|
+
klass.add_handler 'get /v1/payment_methods', :get_payment_methods
|
8
|
+
klass.add_handler 'post /v1/payment_methods/(.*)/attach', :attach_payment_method
|
9
|
+
klass.add_handler 'post /v1/payment_methods/(.*)/detach', :detach_payment_method
|
10
|
+
klass.add_handler 'post /v1/payment_methods/(.*)', :update_payment_method
|
11
|
+
end
|
12
|
+
|
13
|
+
# post /v1/payment_methods
|
14
|
+
def new_payment_method(route, method_url, params, headers)
|
15
|
+
id = new_id('pm')
|
16
|
+
|
17
|
+
ensure_payment_method_required_params(params)
|
18
|
+
|
19
|
+
payment_methods[id] = Data.mock_payment_method(
|
20
|
+
params.merge(
|
21
|
+
id: id
|
22
|
+
)
|
23
|
+
)
|
24
|
+
|
25
|
+
payment_methods[id].clone
|
26
|
+
end
|
27
|
+
|
28
|
+
# get /v1/payment_methods/:id
|
29
|
+
def get_payment_method(route, method_url, params, headers)
|
30
|
+
id = method_url.match(route)[1] || params[:payment_method]
|
31
|
+
payment_method = assert_existence :payment_method, id, payment_methods[id]
|
32
|
+
|
33
|
+
payment_method.clone
|
34
|
+
end
|
35
|
+
|
36
|
+
# get /v1/payment_methods
|
37
|
+
def get_payment_methods(route, method_url, params, headers)
|
38
|
+
params[:offset] ||= 0
|
39
|
+
params[:limit] ||= 10
|
40
|
+
|
41
|
+
clone = payment_methods.clone
|
42
|
+
|
43
|
+
if params[:customer]
|
44
|
+
clone.delete_if { |_k, v| v[:customer] != params[:customer] }
|
45
|
+
end
|
46
|
+
|
47
|
+
Data.mock_list_object(clone.values, params)
|
48
|
+
end
|
49
|
+
|
50
|
+
# post /v1/payment_methods/:id/attach
|
51
|
+
def attach_payment_method(route, method_url, params, headers)
|
52
|
+
allowed_params = [:customer]
|
53
|
+
|
54
|
+
id = method_url.match(route)[1]
|
55
|
+
|
56
|
+
assert_existence :customer, params[:customer], customers[params[:customer]]
|
57
|
+
|
58
|
+
payment_method = assert_existence :payment_method, id, payment_methods[id]
|
59
|
+
payment_methods[id] = Util.rmerge(payment_method, params.select { |k, _v| allowed_params.include?(k) })
|
60
|
+
payment_methods[id].clone
|
61
|
+
end
|
62
|
+
|
63
|
+
# post /v1/payment_methods/:id/detach
|
64
|
+
def detach_payment_method(route, method_url, params, headers)
|
65
|
+
id = method_url.match(route)[1]
|
66
|
+
|
67
|
+
payment_method = assert_existence :payment_method, id, payment_methods[id]
|
68
|
+
payment_method[:customer] = nil
|
69
|
+
|
70
|
+
payment_method.clone
|
71
|
+
end
|
72
|
+
|
73
|
+
# post /v1/payment_methods/:id
|
74
|
+
def update_payment_method(route, method_url, params, headers)
|
75
|
+
allowed_params = [:billing_details, :card, :metadata]
|
76
|
+
|
77
|
+
id = method_url.match(route)[1]
|
78
|
+
|
79
|
+
payment_method = assert_existence :payment_method, id, payment_methods[id]
|
80
|
+
|
81
|
+
if payment_method[:customer].nil?
|
82
|
+
raise Stripe::InvalidRequestError.new(
|
83
|
+
'You must save this PaymentMethod to a customer before you can update it.',
|
84
|
+
http_status: 400
|
85
|
+
)
|
86
|
+
end
|
87
|
+
|
88
|
+
payment_methods[id] =
|
89
|
+
Util.rmerge(payment_method, params.select { |k, _v| allowed_params.include?(k)} )
|
90
|
+
|
91
|
+
payment_methods[id].clone
|
92
|
+
end
|
93
|
+
|
94
|
+
private
|
95
|
+
|
96
|
+
def ensure_payment_method_required_params(params)
|
97
|
+
require_param(:type) if params[:type].nil?
|
98
|
+
|
99
|
+
if invalid_type?(params[:type])
|
100
|
+
raise Stripe::InvalidRequestError.new(
|
101
|
+
'Invalid type: must be one of card or card_present',
|
102
|
+
http_status: 400
|
103
|
+
)
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def invalid_type?(type)
|
108
|
+
!['card', 'card_present'].include?(type)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module StripeMock
|
2
|
+
module RequestHandlers
|
3
|
+
module Payouts
|
4
|
+
|
5
|
+
def Payouts.included(klass)
|
6
|
+
klass.add_handler 'post /v1/payouts', :new_payout
|
7
|
+
klass.add_handler 'get /v1/payouts', :list_payouts
|
8
|
+
klass.add_handler 'get /v1/payouts/(.*)', :get_payout
|
9
|
+
end
|
10
|
+
|
11
|
+
def new_payout(route, method_url, params, headers)
|
12
|
+
id = new_id('po')
|
13
|
+
|
14
|
+
unless params[:amount].is_a?(Integer) || (params[:amount].is_a?(String) && /^\d+$/.match(params[:amount]))
|
15
|
+
raise Stripe::InvalidRequestError.new("Invalid integer: #{params[:amount]}", 'amount', http_status: 400)
|
16
|
+
end
|
17
|
+
|
18
|
+
payouts[id] = Data.mock_payout(params.merge :id => id)
|
19
|
+
end
|
20
|
+
|
21
|
+
def list_payouts(route, method_url, params, headers)
|
22
|
+
Data.mock_list_object(payouts.clone.values, params)
|
23
|
+
end
|
24
|
+
|
25
|
+
def get_payout(route, method_url, params, headers)
|
26
|
+
route =~ method_url
|
27
|
+
assert_existence :payout, $1, payouts[$1]
|
28
|
+
payouts[$1] ||= Data.mock_payout(:id => $1)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
module StripeMock
|
2
|
+
module RequestHandlers
|
3
|
+
module Plans
|
4
|
+
|
5
|
+
def Plans.included(klass)
|
6
|
+
klass.add_handler 'post /v1/plans', :new_plan
|
7
|
+
klass.add_handler 'post /v1/plans/(.*)', :update_plan
|
8
|
+
klass.add_handler 'get /v1/plans/(.*)', :get_plan
|
9
|
+
klass.add_handler 'delete /v1/plans/(.*)', :delete_plan
|
10
|
+
klass.add_handler 'get /v1/plans', :list_plans
|
11
|
+
end
|
12
|
+
|
13
|
+
def new_plan(route, method_url, params, headers)
|
14
|
+
params[:id] ||= new_id('plan')
|
15
|
+
validate_create_plan_params(params)
|
16
|
+
plans[ params[:id] ] = Data.mock_plan(params)
|
17
|
+
end
|
18
|
+
|
19
|
+
def update_plan(route, method_url, params, headers)
|
20
|
+
route =~ method_url
|
21
|
+
assert_existence :plan, $1, plans[$1]
|
22
|
+
plans[$1].merge!(params)
|
23
|
+
end
|
24
|
+
|
25
|
+
def get_plan(route, method_url, params, headers)
|
26
|
+
route =~ method_url
|
27
|
+
assert_existence :plan, $1, plans[$1]
|
28
|
+
end
|
29
|
+
|
30
|
+
def delete_plan(route, method_url, params, headers)
|
31
|
+
route =~ method_url
|
32
|
+
assert_existence :plan, $1, plans.delete($1)
|
33
|
+
end
|
34
|
+
|
35
|
+
def list_plans(route, method_url, params, headers)
|
36
|
+
limit = params[:limit] ? params[:limit] : 10
|
37
|
+
Data.mock_list_object(plans.values.first(limit), limit: limit)
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
module StripeMock
|
2
|
+
module RequestHandlers
|
3
|
+
module Products
|
4
|
+
def self.included(base)
|
5
|
+
base.add_handler 'post /v1/products', :create_product
|
6
|
+
base.add_handler 'get /v1/products/(.*)', :retrieve_product
|
7
|
+
base.add_handler 'post /v1/products/(.*)', :update_product
|
8
|
+
base.add_handler 'get /v1/products', :list_products
|
9
|
+
base.add_handler 'delete /v1/products/(.*)', :destroy_product
|
10
|
+
end
|
11
|
+
|
12
|
+
def create_product(_route, _method_url, params, _headers)
|
13
|
+
params[:id] ||= new_id('prod')
|
14
|
+
products[params[:id]] = Data.mock_product(params)
|
15
|
+
end
|
16
|
+
|
17
|
+
def retrieve_product(route, method_url, _params, _headers)
|
18
|
+
id = method_url.match(route).captures.first
|
19
|
+
assert_existence :product, id, products[id]
|
20
|
+
end
|
21
|
+
|
22
|
+
def update_product(route, method_url, params, _headers)
|
23
|
+
id = method_url.match(route).captures.first
|
24
|
+
product = assert_existence :product, id, products[id]
|
25
|
+
|
26
|
+
product.merge!(params)
|
27
|
+
end
|
28
|
+
|
29
|
+
def list_products(_route, _method_url, params, _headers)
|
30
|
+
limit = params[:limit] || 10
|
31
|
+
Data.mock_list_object(products.values.take(limit), params)
|
32
|
+
end
|
33
|
+
|
34
|
+
def destroy_product(route, method_url, _params, _headers)
|
35
|
+
id = method_url.match(route).captures.first
|
36
|
+
assert_existence :product, id, products[id]
|
37
|
+
|
38
|
+
products.delete(id)
|
39
|
+
{ id: id, object: 'product', deleted: true }
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
module StripeMock
|
2
|
+
module RequestHandlers
|
3
|
+
module Recipients
|
4
|
+
|
5
|
+
def Recipients.included(klass)
|
6
|
+
klass.add_handler 'post /v1/recipients', :new_recipient
|
7
|
+
klass.add_handler 'post /v1/recipients/(.*)', :update_recipient
|
8
|
+
klass.add_handler 'get /v1/recipients/(.*)', :get_recipient
|
9
|
+
end
|
10
|
+
|
11
|
+
def new_recipient(route, method_url, params, headers)
|
12
|
+
params[:id] ||= new_id('rp')
|
13
|
+
cards = []
|
14
|
+
|
15
|
+
if params[:name].nil?
|
16
|
+
raise StripeMock::StripeMockError.new("Missing required parameter name for recipients.")
|
17
|
+
end
|
18
|
+
|
19
|
+
if params[:type].nil?
|
20
|
+
raise StripeMock::StripeMockError.new("Missing required parameter type for recipients.")
|
21
|
+
end
|
22
|
+
|
23
|
+
unless %w(individual corporation).include?(params[:type])
|
24
|
+
raise StripeMock::StripeMockError.new("Type must be either individual or corporation..")
|
25
|
+
end
|
26
|
+
|
27
|
+
if params[:bank_account]
|
28
|
+
params[:active_account] = get_bank_by_token(params.delete(:bank_account))
|
29
|
+
end
|
30
|
+
|
31
|
+
if params[:card]
|
32
|
+
cards << get_card_by_token(params.delete(:card))
|
33
|
+
params[:default_card] = cards.first[:id]
|
34
|
+
end
|
35
|
+
|
36
|
+
recipients[ params[:id] ] = Data.mock_recipient(cards, params)
|
37
|
+
recipients[ params[:id] ]
|
38
|
+
end
|
39
|
+
|
40
|
+
def update_recipient(route, method_url, params, headers)
|
41
|
+
route =~ method_url
|
42
|
+
recipient = assert_existence :recipient, $1, recipients[$1]
|
43
|
+
recipient.merge!(params)
|
44
|
+
|
45
|
+
if params[:card]
|
46
|
+
new_card = get_card_by_token(params.delete(:card))
|
47
|
+
add_card_to_object(:recipient, new_card, recipient, true)
|
48
|
+
recipient[:default_card] = new_card[:id]
|
49
|
+
end
|
50
|
+
|
51
|
+
recipient
|
52
|
+
end
|
53
|
+
|
54
|
+
def get_recipient(route, method_url, params, headers)
|
55
|
+
route =~ method_url
|
56
|
+
assert_existence :recipient, $1, recipients[$1]
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,91 @@
|
|
1
|
+
module StripeMock
|
2
|
+
module RequestHandlers
|
3
|
+
module Refunds
|
4
|
+
|
5
|
+
def Refunds.included(klass)
|
6
|
+
klass.add_handler 'post /v1/refunds', :new_refund
|
7
|
+
klass.add_handler 'get /v1/refunds', :get_refunds
|
8
|
+
klass.add_handler 'get /v1/refunds/(.*)', :get_refund
|
9
|
+
klass.add_handler 'post /v1/refunds/(.*)', :update_refund
|
10
|
+
end
|
11
|
+
|
12
|
+
def new_refund(route, method_url, params, headers)
|
13
|
+
if headers && headers[:idempotency_key]
|
14
|
+
params[:idempotency_key] = headers[:idempotency_key]
|
15
|
+
if refunds.any?
|
16
|
+
original_refund = refunds.values.find { |c| c[:idempotency_key] == headers[:idempotency_key]}
|
17
|
+
return refunds[original_refund[:id]] if original_refund
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
charge = assert_existence :charge, params[:charge], charges[params[:charge]]
|
22
|
+
params[:amount] ||= charge[:amount]
|
23
|
+
id = new_id('re')
|
24
|
+
bal_trans_params = {
|
25
|
+
amount: params[:amount] * -1,
|
26
|
+
source: id,
|
27
|
+
type: 'refund'
|
28
|
+
}
|
29
|
+
balance_transaction_id = new_balance_transaction('txn', bal_trans_params)
|
30
|
+
refund = Data.mock_refund params.merge(
|
31
|
+
:balance_transaction => balance_transaction_id,
|
32
|
+
:id => id,
|
33
|
+
:charge => charge[:id],
|
34
|
+
)
|
35
|
+
add_refund_to_charge(refund, charge)
|
36
|
+
refunds[id] = refund
|
37
|
+
|
38
|
+
if params[:expand] == ['balance_transaction']
|
39
|
+
refunds[id][:balance_transaction] =
|
40
|
+
balance_transactions[balance_transaction_id]
|
41
|
+
end
|
42
|
+
refund
|
43
|
+
end
|
44
|
+
|
45
|
+
def update_refund(route, method_url, params, headers)
|
46
|
+
route =~ method_url
|
47
|
+
id = $1
|
48
|
+
|
49
|
+
refund = assert_existence :refund, id, refunds[id]
|
50
|
+
allowed = allowed_refund_params(params)
|
51
|
+
disallowed = params.keys - allowed
|
52
|
+
if disallowed.count > 0
|
53
|
+
raise Stripe::InvalidRequestError.new("Received unknown parameters: #{disallowed.join(', ')}" , '', http_status: 400)
|
54
|
+
end
|
55
|
+
|
56
|
+
refunds[id] = Util.rmerge(refund, params)
|
57
|
+
end
|
58
|
+
|
59
|
+
def get_refunds(route, method_url, params, headers)
|
60
|
+
params[:offset] ||= 0
|
61
|
+
params[:limit] ||= 10
|
62
|
+
|
63
|
+
clone = refunds.clone
|
64
|
+
|
65
|
+
Data.mock_list_object(clone.values, params)
|
66
|
+
end
|
67
|
+
|
68
|
+
def get_refund(route, method_url, params, headers)
|
69
|
+
route =~ method_url
|
70
|
+
refund_id = $1 || params[:refund]
|
71
|
+
assert_existence :refund, refund_id, refunds[refund_id]
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
def ensure_refund_required_params(params)
|
77
|
+
if non_integer_charge_amount?(params)
|
78
|
+
raise Stripe::InvalidRequestError.new("Invalid integer: #{params[:amount]}", 'amount', http_status: 400)
|
79
|
+
elsif non_positive_charge_amount?(params)
|
80
|
+
raise Stripe::InvalidRequestError.new('Invalid positive integer', 'amount', http_status: 400)
|
81
|
+
elsif params[:charge].nil?
|
82
|
+
raise Stripe::InvalidRequestError.new('Must provide the identifier of the charge to refund.', nil)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def allowed_refund_params(params)
|
87
|
+
[:metadata]
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module StripeMock
|
2
|
+
module RequestHandlers
|
3
|
+
module Sources
|
4
|
+
|
5
|
+
def Sources.included(klass)
|
6
|
+
klass.add_handler 'get /v1/customers/(.*)/sources', :retrieve_sources
|
7
|
+
klass.add_handler 'post /v1/customers/(.*)/sources', :create_source
|
8
|
+
klass.add_handler 'post /v1/customers/(.*)/sources/(.*)/verify', :verify_source
|
9
|
+
klass.add_handler 'get /v1/customers/(.*)/sources/(.*)', :retrieve_source
|
10
|
+
klass.add_handler 'delete /v1/customers/(.*)/sources/(.*)', :delete_source
|
11
|
+
klass.add_handler 'post /v1/customers/(.*)/sources/(.*)', :update_source
|
12
|
+
end
|
13
|
+
|
14
|
+
def create_source(route, method_url, params, headers)
|
15
|
+
route =~ method_url
|
16
|
+
add_source_to(:customer, $1, params, customers)
|
17
|
+
end
|
18
|
+
|
19
|
+
def retrieve_sources(route, method_url, params, headers)
|
20
|
+
route =~ method_url
|
21
|
+
retrieve_object_cards(:customer, $1, customers)
|
22
|
+
end
|
23
|
+
|
24
|
+
def retrieve_source(route, method_url, params, headers)
|
25
|
+
route =~ method_url
|
26
|
+
customer = assert_existence :customer, $1, customers[$1]
|
27
|
+
|
28
|
+
assert_existence :card, $2, get_card(customer, $2)
|
29
|
+
end
|
30
|
+
|
31
|
+
def delete_source(route, method_url, params, headers)
|
32
|
+
route =~ method_url
|
33
|
+
delete_card_from(:customer, $1, $2, customers)
|
34
|
+
end
|
35
|
+
|
36
|
+
def update_source(route, method_url, params, headers)
|
37
|
+
route =~ method_url
|
38
|
+
customer = assert_existence :customer, $1, customers[$1]
|
39
|
+
|
40
|
+
card = assert_existence :card, $2, get_card(customer, $2)
|
41
|
+
card.merge!(params)
|
42
|
+
card
|
43
|
+
end
|
44
|
+
|
45
|
+
def verify_source(route, method_url, params, headers)
|
46
|
+
route =~ method_url
|
47
|
+
customer = assert_existence :customer, $1, customers[$1]
|
48
|
+
|
49
|
+
bank_account = assert_existence :bank_account, $2, verify_bank_account(customer, $2)
|
50
|
+
bank_account
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module StripeMock
|
2
|
+
module RequestHandlers
|
3
|
+
module SubscriptionItems
|
4
|
+
|
5
|
+
def SubscriptionItems.included(klass)
|
6
|
+
klass.add_handler 'get /v1/subscription_items', :retrieve_subscription_items
|
7
|
+
klass.add_handler 'post /v1/subscription_items/([^/]*)', :update_subscription_item
|
8
|
+
klass.add_handler 'post /v1/subscription_items', :create_subscription_items
|
9
|
+
end
|
10
|
+
|
11
|
+
def retrieve_subscription_items(route, method_url, params, headers)
|
12
|
+
route =~ method_url
|
13
|
+
|
14
|
+
require_param(:subscription) unless params[:subscription]
|
15
|
+
|
16
|
+
Data.mock_list_object(subscriptions_items, params)
|
17
|
+
end
|
18
|
+
|
19
|
+
def create_subscription_items(route, method_url, params, headers)
|
20
|
+
params[:id] ||= new_id('si')
|
21
|
+
|
22
|
+
require_param(:subscription) unless params[:subscription]
|
23
|
+
require_param(:plan) unless params[:plan]
|
24
|
+
|
25
|
+
subscriptions_items[params[:id]] = Data.mock_subscription_item(params.merge(plan: plans[params[:plan]]))
|
26
|
+
end
|
27
|
+
|
28
|
+
def update_subscription_item(route, method_url, params, headers)
|
29
|
+
route =~ method_url
|
30
|
+
|
31
|
+
subscription_item = assert_existence :subscription_item, $1, subscriptions_items[$1]
|
32
|
+
subscription_item.merge!(params.merge(plan: plans[params[:plan]]))
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|