Skip to content

Commit

Permalink
Merge pull request #745 from stripe/remi-add-payment-methods
Browse files Browse the repository at this point in the history
Add support for the PaymentMethod resource
  • Loading branch information
remi-stripe authored Mar 18, 2019
2 parents 7b9b7b6 + f93c007 commit 39d66c0
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
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

0 comments on commit 39d66c0

Please sign in to comment.