Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
24 changes: 24 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,29 @@ 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 = payment_methods[payment_method_id]

if payment_method && payment_method[:customer] == customer_id
return payment_method
end

# If not found or not attached correctly, attempt to find in the customer's legacy sources.
if customer[:sources] && customer[:sources][:data]
source = customer[:sources][:data].detect { |src| src[:id] == payment_method_id }
if source && source[:customer] == customer_id
return source
end
end

raise Stripe::InvalidRequestError.new("Payment method #{payment_method_id} is not attached to customer #{customer_id}", :customer, http_status: 404)
end
end
end
end
34 changes: 34 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,38 @@ 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

it "retrives a stripe customers payment method with a default card" do
customer = Stripe::Customer.create({
email: 'johnny@appleseed.com',
source: gen_card_tk,
description: "a description"
})

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

end