Skip to content

Commit

Permalink
Add support for the SetupIntent resource and APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-stripe committed Jun 27, 2019
1 parent 2cc7714 commit a993e89
Show file tree
Hide file tree
Showing 8 changed files with 116 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
1 change: 1 addition & 0 deletions lib/stripe/object_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions lib/stripe/resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 2 additions & 3 deletions lib/stripe/resources/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand All @@ -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
Expand Down
24 changes: 24 additions & 0 deletions lib/stripe/resources/setup_intent.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions test/stripe/payment_intent_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
84 changes: 84 additions & 0 deletions test/stripe/setup_intent_test.rb
Original file line number Diff line number Diff line change
@@ -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
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", __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
Expand Down

0 comments on commit a993e89

Please # to comment.