From a993e892b33d166a44e0991e2fa6556ed5d2dc81 Mon Sep 17 00:00:00 2001 From: Remi Jannel Date: Thu, 27 Jun 2019 12:52:06 -0700 Subject: [PATCH] Add support for the `SetupIntent` resource and APIs --- .travis.yml | 2 +- lib/stripe/object_types.rb | 1 + lib/stripe/resources.rb | 1 + lib/stripe/resources/customer.rb | 5 +- lib/stripe/resources/setup_intent.rb | 24 ++++++++ test/stripe/payment_intent_test.rb | 4 +- test/stripe/setup_intent_test.rb | 84 ++++++++++++++++++++++++++++ test/test_helper.rb | 2 +- 8 files changed, 116 insertions(+), 7 deletions(-) create mode 100644 lib/stripe/resources/setup_intent.rb create mode 100644 test/stripe/setup_intent_test.rb diff --git a/.travis.yml b/.travis.yml index 8f6c18b30..ff92f6629 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,7 +18,7 @@ sudo: false env: global: # If changing this number, please also change it in `test/test_helper.rb`. - - STRIPE_MOCK_VERSION=0.58.0 + - STRIPE_MOCK_VERSION=0.60.0 cache: directories: diff --git a/lib/stripe/object_types.rb b/lib/stripe/object_types.rb index 612d37df7..b8d5ed167 100644 --- a/lib/stripe/object_types.rb +++ b/lib/stripe/object_types.rb @@ -69,6 +69,7 @@ def self.object_names_to_classes Reversal::OBJECT_NAME => Reversal, Review::OBJECT_NAME => Review, SKU::OBJECT_NAME => SKU, + SetupIntent::OBJECT_NAME => SetupIntent, Sigma::ScheduledQueryRun::OBJECT_NAME => Sigma::ScheduledQueryRun, Source::OBJECT_NAME => Source, SourceTransaction::OBJECT_NAME => SourceTransaction, diff --git a/lib/stripe/resources.rb b/lib/stripe/resources.rb index 8a56534d7..975415f6b 100644 --- a/lib/stripe/resources.rb +++ b/lib/stripe/resources.rb @@ -56,6 +56,7 @@ require "stripe/resources/reporting/report_type" require "stripe/resources/reversal" require "stripe/resources/review" +require "stripe/resources/setup_intent" require "stripe/resources/sigma/scheduled_query_run" require "stripe/resources/sku" require "stripe/resources/source" diff --git a/lib/stripe/resources/customer.rb b/lib/stripe/resources/customer.rb index 94d20b4ce..02baef6d9 100644 --- a/lib/stripe/resources/customer.rb +++ b/lib/stripe/resources/customer.rb @@ -10,6 +10,8 @@ class Customer < APIResource OBJECT_NAME = "customer".freeze + nested_resource_class_methods :balance_transaction, + operations: %i[create retrieve update list] nested_resource_class_methods :tax_id, operations: %i[create retrieve delete list] @@ -19,9 +21,6 @@ class Customer < APIResource nested_resource_class_methods :source, operations: %i[create retrieve update delete list] - nested_resource_class_methods :balance_transaction, - operations: %i[create retrieve update list] - # The API request for deleting a card or bank account and for detaching a # source object are the same. class << self diff --git a/lib/stripe/resources/setup_intent.rb b/lib/stripe/resources/setup_intent.rb new file mode 100644 index 000000000..542e74736 --- /dev/null +++ b/lib/stripe/resources/setup_intent.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +module Stripe + class SetupIntent < APIResource + extend Stripe::APIOperations::Create + extend Stripe::APIOperations::List + include Stripe::APIOperations::Save + + OBJECT_NAME = "setup_intent".freeze + + custom_method :cancel, http_verb: :post + custom_method :confirm, http_verb: :post + + def cancel(params = {}, opts = {}) + resp, opts = request(:post, resource_url + "/cancel", params, opts) + Util.convert_to_stripe_object(resp.data, opts) + end + + def confirm(params = {}, opts = {}) + resp, opts = request(:post, resource_url + "/confirm", params, opts) + Util.convert_to_stripe_object(resp.data, opts) + end + end +end diff --git a/test/stripe/payment_intent_test.rb b/test/stripe/payment_intent_test.rb index 5d4bf8ede..ef17b7857 100644 --- a/test/stripe/payment_intent_test.rb +++ b/test/stripe/payment_intent_test.rb @@ -3,9 +3,9 @@ require ::File.expand_path("../test_helper", __dir__) module Stripe - TEST_RESOURCE_ID = "pi_123".freeze - class PaymentIntentTest < Test::Unit::TestCase + TEST_RESOURCE_ID = "pi_123".freeze + should "be listable" do payment_intents = Stripe::PaymentIntent.list assert_requested :get, "#{Stripe.api_base}/v1/payment_intents" diff --git a/test/stripe/setup_intent_test.rb b/test/stripe/setup_intent_test.rb new file mode 100644 index 000000000..84044157f --- /dev/null +++ b/test/stripe/setup_intent_test.rb @@ -0,0 +1,84 @@ +# frozen_string_literal: true + +require ::File.expand_path("../test_helper", __dir__) + +module Stripe + class SetupIntentTest < Test::Unit::TestCase + TEST_RESOURCE_ID = "seti_123".freeze + + should "be listable" do + setup_intents = Stripe::SetupIntent.list + assert_requested :get, "#{Stripe.api_base}/v1/setup_intents" + assert setup_intents.data.is_a?(Array) + assert setup_intents.data[0].is_a?(Stripe::SetupIntent) + end + + should "be retrievable" do + setup_intent = Stripe::SetupIntent.retrieve("seti_123") + assert_requested :get, "#{Stripe.api_base}/v1/setup_intents/seti_123" + assert setup_intent.is_a?(Stripe::SetupIntent) + end + + should "be creatable" do + setup_intent = Stripe::SetupIntent.create( + payment_method_types: ["card"] + ) + assert_requested :post, "#{Stripe.api_base}/v1/setup_intents" + assert setup_intent.is_a?(Stripe::SetupIntent) + end + + should "be saveable" do + setup_intent = Stripe::SetupIntent.construct_from(id: "seti_123", object: "setup_intent", metadata: {}) + setup_intent.metadata["key"] = "value" + setup_intent.save + assert_requested :post, "#{Stripe.api_base}/v1/setup_intents/#{setup_intent.id}" + end + + should "be updateable" do + setup_intent = Stripe::SetupIntent.update("seti_123", metadata: { foo: "bar" }) + + assert_requested :post, "#{Stripe.api_base}/v1/setup_intents/seti_123" + assert setup_intent.is_a?(Stripe::SetupIntent) + end + + context "#cancel" do + should "cancel a setup_intent" do + setup_intent = Stripe::SetupIntent.construct_from(id: "seti_123", object: "setup_intent") + setup_intent = setup_intent.cancel + + assert_requested :post, "#{Stripe.api_base}/v1/setup_intents/seti_123/cancel" + assert setup_intent.is_a?(Stripe::SetupIntent) + end + end + + context ".cancel" do + should "cancel a setup_intent" do + setup_intent = Stripe::SetupIntent.cancel("seti_123") + + assert_requested :post, "#{Stripe.api_base}/v1/setup_intents/seti_123/cancel" + assert setup_intent.is_a?(Stripe::SetupIntent) + end + end + + context "#confirm" do + should "confirm a setup_intent" do + setup_intent = Stripe::SetupIntent.construct_from(id: "seti_123", object: "setup_intent") + setup_intent = setup_intent.confirm( + payment_method: "pm_123" + ) + + assert_requested :post, "#{Stripe.api_base}/v1/setup_intents/seti_123/confirm" + assert setup_intent.is_a?(Stripe::SetupIntent) + end + end + + context ".confirm" do + should "confirm a setup_intent" do + setup_intent = Stripe::SetupIntent.confirm("seti_123", payment_method: "pm_123") + + assert_requested :post, "#{Stripe.api_base}/v1/setup_intents/seti_123/confirm" + assert setup_intent.is_a?(Stripe::SetupIntent) + end + end + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index c9a0ca03a..a3d22a20e 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -17,7 +17,7 @@ require ::File.expand_path("stripe_mock", __dir__) # If changing this number, please also change it in `.travis.yml`. -MOCK_MINIMUM_VERSION = "0.58.0".freeze +MOCK_MINIMUM_VERSION = "0.60.0".freeze MOCK_PORT = Stripe::StripeMock.start # Disable all real network connections except those that are outgoing to