Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the PaymentMethod resource #745

Merged
merged 1 commit into from
Mar 18, 2019
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
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