Skip to content

Commit

Permalink
Add support for TaxId resource and APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-stripe committed Apr 19, 2019
1 parent 5416f02 commit b5cd9e3
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 2 deletions.
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.52.0
- STRIPE_MOCK_VERSION=0.54.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 @@ -97,6 +97,7 @@
require "stripe/subscription_item"
require "stripe/subscription_schedule"
require "stripe/subscription_schedule_revision"
require "stripe/tax_id"
require "stripe/terminal/connection_token"
require "stripe/terminal/location"
require "stripe/terminal/reader"
Expand Down
3 changes: 3 additions & 0 deletions lib/stripe/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class Customer < APIResource
nested_resource_class_methods :source,
operations: %i[create retrieve update delete list]

nested_resource_class_methods :tax_id,
operations: %i[create retrieve delete list]

# The API request for deleting a card or bank account and for detaching a
# source object are the same.
class << self
Expand Down
22 changes: 22 additions & 0 deletions lib/stripe/tax_id.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module Stripe
class TaxId < APIResource
extend Stripe::APIOperations::List
include Stripe::APIOperations::Delete

OBJECT_NAME = "tax_id".freeze

def resource_url
if !respond_to?(:customer) || customer.nil?
raise NotImplementedError,
"Tax Ids cannot be accessed without a customer ID."
end
"#{Customer.resource_url}/#{CGI.escape(customer)}/tax_ids/#{CGI.escape(id)}"
end

def self.retrieve(_id, _opts = {})
raise NotImplementedError, "Tax Ids cannot be retrieved without a customer ID. Retrieve a tax id using Customer.retrieve_tax_id('tax_id')"
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 @@ -107,6 +107,7 @@ def self.object_classes # rubocop:disable Metrics/MethodLength
SubscriptionItem::OBJECT_NAME => SubscriptionItem,
SubscriptionSchedule::OBJECT_NAME => SubscriptionSchedule,
SubscriptionScheduleRevision::OBJECT_NAME => SubscriptionScheduleRevision,
TaxId::OBJECT_NAME => TaxId,
Terminal::ConnectionToken::OBJECT_NAME => Terminal::ConnectionToken,
Terminal::Location::OBJECT_NAME => Terminal::Location,
Terminal::Reader::OBJECT_NAME => Terminal::Reader,
Expand Down
42 changes: 42 additions & 0 deletions test/stripe/customer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,5 +180,47 @@ class CustomerTest < Test::Unit::TestCase
assert_equal true, c.source.save_with_parent
end
end

context "#create_tax_id" do
should "create a tax id" do
Stripe::Customer.create_tax_id(
"cus_123",
type: "eu_vat",
value: "11111"
)
assert_requested :post, "#{Stripe.api_base}/v1/customers/cus_123/tax_ids"
end
end

context "#retrieve_tax_id" do
should "retrieve a tax id" do
Stripe::Customer.retrieve_tax_id(
"cus_123",
"txi_123"
)
assert_requested :get, "#{Stripe.api_base}/v1/customers/cus_123/tax_ids/txi_123"
end
end

context "#delete_tax_id" do
should "delete a tax id" do
Stripe::Customer.delete_tax_id(
"cus_123",
"txi_123"
)
assert_requested :delete, "#{Stripe.api_base}/v1/customers/cus_123/tax_ids/txi_123"
end
end

context "#list_tax_ids" do
should "list the customer's tax ids" do
sources = Stripe::Customer.list_tax_ids(
"cus_123"
)
assert_requested :get, "#{Stripe.api_base}/v1/customers/cus_123/tax_ids"
assert sources.is_a?(Stripe::ListObject)
assert sources.data.is_a?(Array)
end
end
end
end
31 changes: 31 additions & 0 deletions test/stripe/tax_id_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

require ::File.expand_path("../../test_helper", __FILE__)

module Stripe
class TaxIdTest < Test::Unit::TestCase
context "#resource_url" do
should "return a resource URL" do
tax_id = Stripe::TaxId.construct_from(
id: "txi_123",
customer: "cus_123"
)
assert_equal "/v1/customers/cus_123/tax_ids/txi_123",
tax_id.resource_url
end

should "raise without a customer" do
tax_id = Stripe::TaxId.construct_from(id: "txi_123")
assert_raises NotImplementedError do
tax_id.resource_url
end
end
end

should "raise on #retrieve" do
assert_raises NotImplementedError do
Stripe::TaxId.retrieve("txi_123")
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.52.0".freeze
MOCK_MINIMUM_VERSION = "0.54.0".freeze
MOCK_PORT = Stripe::StripeMock.start

# Disable all real network connections except those that are outgoing to
Expand Down

0 comments on commit b5cd9e3

Please sign in to comment.