Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions lib/stripe_mock/request_handlers/customers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def Customers.included(klass)
klass.add_handler 'get /v1/customers', :list_customers
klass.add_handler 'get /v1/customers/search', :search_customers
klass.add_handler 'delete /v1/customers/([^/]*)/discount', :delete_customer_discount
klass.add_handler 'get /v1/customers/([^/]+)/payment_methods/([^/]+)', :retrieve_payment_method
end

def new_customer(route, method_url, params, headers)
Expand Down Expand Up @@ -160,6 +161,17 @@ def delete_customer_discount(route, method_url, params, headers)

cus
end

def retrieve_payment_method(route, method_url, params, headers)
stripe_account = headers && headers[:stripe_account] || Stripe.api_key

route =~ method_url
customer_id, payment_method_id = $1, $2
customer = assert_existence(:customer, customer_id, customers[stripe_account][customer_id])
payment_method = customer[:sources][:data].find { |source| source[:id] == payment_method_id }
assert_existence(:payment_method, payment_method_id, payment_method)
payment_method
end
end
end
end
14 changes: 11 additions & 3 deletions lib/stripe_mock/request_handlers/payment_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,19 @@ def attach_payment_method(route, method_url, params, headers)
allowed_params = [:customer]

id = method_url.match(route)[1]

assert_existence :customer, params[:customer], customers[stripe_account][params[:customer]]

customer = assert_existence :customer, params[:customer], customers[stripe_account][params[:customer]]
payment_method = assert_existence :payment_method, id, payment_methods[id]
payment_methods[id] = Util.rmerge(payment_method, params.select { |k, _v| allowed_params.include?(k) })

customer[:sources] ||= { data: [], total_count: 0 }

unless customer[:sources][:data].any? { |source| source[:id] == id }
customer[:sources][:data] << payment_methods[id]
customer[:sources][:total_count] += 1
end

payment_methods[id].clone
end

Expand Down
22 changes: 22 additions & 0 deletions spec/shared_stripe_examples/customer_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -535,4 +535,26 @@ def gen_card_tk
customer = Stripe::Customer.retrieve("test_customer_update")
expect(customer.discount).to be nil
end

it "retrieves a stripe customers payment method" do
payment_method = Stripe::PaymentMethod.create({
type: 'card',
card: {
number: '4242424242424242',
exp_month: 12,
exp_year: 2024,
cvc: 123
}
})

customer = Stripe::Customer.create({
email: 'johnny@appleseed.com',
description: "a description"
})

payment_method.attach(customer: customer.id)

retrieved_payment_method = Stripe::Customer.retrieve_payment_method(customer.id, payment_method.id)
expect(retrieved_payment_method.id).to eq(payment_method.id)
end
end