diff --git a/.travis.yml b/.travis.yml index f443fd659..b0ac92fdb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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: diff --git a/lib/stripe.rb b/lib/stripe.rb index 67d39075b..00a84c8af 100644 --- a/lib/stripe.rb +++ b/lib/stripe.rb @@ -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" diff --git a/lib/stripe/payment_method.rb b/lib/stripe/payment_method.rb new file mode 100644 index 000000000..a04791bb9 --- /dev/null +++ b/lib/stripe/payment_method.rb @@ -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 diff --git a/lib/stripe/util.rb b/lib/stripe/util.rb index a89ef9eac..d7fc71a36 100644 --- a/lib/stripe/util.rb +++ b/lib/stripe/util.rb @@ -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, diff --git a/test/stripe/payment_method_test.rb b/test/stripe/payment_method_test.rb new file mode 100644 index 000000000..d07303b69 --- /dev/null +++ b/test/stripe/payment_method_test.rb @@ -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 diff --git a/test/test_helper.rb b/test/test_helper.rb index 56f38b62a..e427dfd42 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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