Skip to content

Commit

Permalink
Add support for the PaymentMethod resource
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-stripe committed Mar 18, 2019
1 parent df8c141 commit f93c007
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ sudo: false
env:
global:
# If changing this number, please also change it in `test/test_helper.rb`.
- STRIPE_MOCK_VERSION=0.47.0
- STRIPE_MOCK_VERSION=0.49.0

cache:
directories:
Expand Down
1 change: 1 addition & 0 deletions lib/stripe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
require "stripe/order"
require "stripe/order_return"
require "stripe/payment_intent"
require "stripe/payment_method"
require "stripe/payout"
require "stripe/person"
require "stripe/plan"
Expand Down
23 changes: 23 additions & 0 deletions lib/stripe/payment_method.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# frozen_string_literal: true

module Stripe
class PaymentMethod < APIResource
extend Stripe::APIOperations::Create
include Stripe::APIOperations::Save
extend Stripe::APIOperations::List

OBJECT_NAME = "payment_method".freeze

def attach(params = {}, opts = {})
url = resource_url + "/attach"
resp, opts = request(:post, url, params, opts)
initialize_from(resp.data, opts)
end

def detach(params = {}, opts = {})
url = resource_url + "/detach"
resp, opts = request(:post, url, params, opts)
initialize_from(resp.data, opts)
end
end
end
1 change: 1 addition & 0 deletions lib/stripe/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def self.object_classes # rubocop:disable Metrics/MethodLength
Order::OBJECT_NAME => Order,
OrderReturn::OBJECT_NAME => OrderReturn,
PaymentIntent::OBJECT_NAME => PaymentIntent,
PaymentMethod::OBJECT_NAME => PaymentMethod,
Payout::OBJECT_NAME => Payout,
Person::OBJECT_NAME => Person,
Plan::OBJECT_NAME => Plan,
Expand Down
66 changes: 66 additions & 0 deletions test/stripe/payment_method_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# frozen_string_literal: true

require ::File.expand_path("../../test_helper", __FILE__)

module Stripe
class PaymentMethodTest < Test::Unit::TestCase
should "be listable" do
payment_methods = Stripe::PaymentMethod.list(
customer: "cus_123",
type: "card"
)
assert_requested :get, "#{Stripe.api_base}/v1/payment_methods?customer=cus_123&type=card"
assert payment_methods.data.is_a?(Array)
assert payment_methods.first.is_a?(Stripe::PaymentMethod)
end

should "be retrievable" do
payment_method = Stripe::PaymentMethod.retrieve("pm_123")
assert_requested :get, "#{Stripe.api_base}/v1/payment_methods/pm_123"
assert payment_method.is_a?(Stripe::PaymentMethod)
end

should "be creatable" do
payment_method = Stripe::PaymentMethod.create(
type: "card"
)
assert_requested :post, "#{Stripe.api_base}/v1/payment_methods"
assert payment_method.is_a?(Stripe::PaymentMethod)
end

should "be saveable" do
payment_method = Stripe::PaymentMethod.retrieve("pm_123")
payment_method.metadata["key"] = "value"
payment_method.save
assert_requested :post, "#{Stripe.api_base}/v1/payment_methods/#{payment_method.id}"
end

should "be updateable" do
payment_method = Stripe::PaymentMethod.update("pm_123", metadata: { key: "value" })
assert_requested :post, "#{Stripe.api_base}/v1/payment_methods/pm_123"
assert payment_method.is_a?(Stripe::PaymentMethod)
end

context "#attach" do
should "attach payment_method" do
payment_method = Stripe::PaymentMethod.construct_from(id: "pm_123", object: "payment_method")
payment_method = payment_method.attach(
customer: "cus_123"
)

assert_requested :post, "#{Stripe.api_base}/v1/payment_methods/pm_123/attach"
assert payment_method.is_a?(Stripe::PaymentMethod)
end
end

context "#detach" do
should "detach payment_method" do
payment_method = Stripe::PaymentMethod.construct_from(id: "pm_123", object: "payment_method")
payment_method = payment_method.detach

assert_requested :post, "#{Stripe.api_base}/v1/payment_methods/pm_123/detach"
assert payment_method.is_a?(Stripe::PaymentMethod)
end
end
end
end
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
require ::File.expand_path("../stripe_mock", __FILE__)

# If changing this number, please also change it in `.travis.yml`.
MOCK_MINIMUM_VERSION = "0.47.0".freeze
MOCK_MINIMUM_VERSION = "0.49.0".freeze
MOCK_PORT = Stripe::StripeMock.start

# Disable all real network connections except those that are outgoing to
Expand Down

0 comments on commit f93c007

Please sign in to comment.