diff --git a/.travis.yml b/.travis.yml index 2c1e57ad6..c964bc91d 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.33.0 + - STRIPE_MOCK_VERSION=0.35.0 cache: directories: diff --git a/lib/stripe.rb b/lib/stripe.rb index 5acc38520..6050273c2 100644 --- a/lib/stripe.rb +++ b/lib/stripe.rb @@ -72,6 +72,7 @@ require "stripe/order_return" require "stripe/payment_intent" require "stripe/payout" +require "stripe/person" require "stripe/plan" require "stripe/product" require "stripe/recipient" diff --git a/lib/stripe/account.rb b/lib/stripe/account.rb index 000a06100..8ea718cf0 100644 --- a/lib/stripe/account.rb +++ b/lib/stripe/account.rb @@ -15,6 +15,8 @@ class Account < APIResource nested_resource_class_methods :external_account, operations: %i[create retrieve update delete list] nested_resource_class_methods :login_link, operations: %i[create] + nested_resource_class_methods :person, + operations: %i[create retrieve update delete list] # This method is deprecated. Please use `#external_account=` instead. save_nested_resource :bank_account @@ -43,6 +45,11 @@ def self.retrieve(id = ARGUMENT_NOT_PROVIDED, opts = {}) super(id, opts) end + def persons(params = {}, opts = {}) + resp, opts = request(:get, resource_url + "/persons", params, Util.normalize_opts(opts)) + Util.convert_to_stripe_object(resp.data, opts) + end + def reject(params = {}, opts = {}) opts = Util.normalize_opts(opts) resp, opts = request(:post, resource_url + "/reject", params, opts) diff --git a/lib/stripe/person.rb b/lib/stripe/person.rb new file mode 100644 index 000000000..0edde4450 --- /dev/null +++ b/lib/stripe/person.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +module Stripe + class Person < APIResource + extend Stripe::APIOperations::List + include Stripe::APIOperations::Save + + OBJECT_NAME = "person".freeze + + def resource_url + if !respond_to?(:account) || account.nil? + raise NotImplementedError, + "Persons cannot be accessed without an account ID." + end + "#{Account.resource_url}/#{CGI.escape(account)}/persons/#{CGI.escape(id)}" + end + + def self.retrieve(_id, _opts = {}) + raise NotImplementedError, "Persons cannot be retrieved without an account ID. Retrieve a person using account.persons.retrieve('person_id')" + end + + def self.update(_id, _params = nil, _opts = nil) + raise NotImplementedError, "Persons cannot be updated without an account ID. Update a person using `p = account.persons.retrieve('person_id'); p.save`" + end + end +end diff --git a/lib/stripe/util.rb b/lib/stripe/util.rb index 233535f31..9d1400f9c 100644 --- a/lib/stripe/util.rb +++ b/lib/stripe/util.rb @@ -82,6 +82,7 @@ def self.object_classes # rubocop:disable Metrics/MethodLength OrderReturn::OBJECT_NAME => OrderReturn, PaymentIntent::OBJECT_NAME => PaymentIntent, Payout::OBJECT_NAME => Payout, + Person::OBJECT_NAME => Person, Plan::OBJECT_NAME => Plan, Product::OBJECT_NAME => Product, Recipient::OBJECT_NAME => Recipient, diff --git a/test/stripe/account_persons_operations_test.rb b/test/stripe/account_persons_operations_test.rb new file mode 100644 index 000000000..e805f6eb4 --- /dev/null +++ b/test/stripe/account_persons_operations_test.rb @@ -0,0 +1,70 @@ +# frozen_string_literal: true + +require ::File.expand_path("../../test_helper", __FILE__) + +module Stripe + class AccountPersonsOperationsTest < Test::Unit::TestCase + setup do + @account_id = "acct_123" + @person_id = "person_123" + end + + context "#create_person" do + should "create a person" do + person = Stripe::Account.create_person( + @account_id, + first_name: "John", + last_name: "Doe" + ) + assert_requested :post, "#{Stripe.api_base}/v1/accounts/#{@account_id}/persons" + assert person.is_a?(Stripe::Person) + end + end + + context "#retrieve_person" do + should "retrieve a person" do + person = Stripe::Account.retrieve_person( + @account_id, + @person_id + ) + assert_requested :get, "#{Stripe.api_base}/v1/accounts/#{@account_id}/persons/#{@person_id}" + assert person.is_a?(Stripe::Person) + end + end + + context "#update_person" do + should "update a person" do + person = Stripe::Account.update_person( + @account_id, + @person_id, + first_name: "John" + ) + assert_requested :post, "#{Stripe.api_base}/v1/accounts/#{@account_id}/persons/#{@person_id}" + assert person.is_a?(Stripe::Person) + end + end + + context "#delete_person" do + should "delete an person" do + person = Stripe::Account.delete_person( + @account_id, + @person_id + ) + assert_requested :delete, "#{Stripe.api_base}/v1/accounts/#{@account_id}/persons/#{@person_id}" + assert person.deleted + assert_equal @person_id, person.id + end + end + + context "#list_persons" do + should "list the account's external accounts" do + persons = Stripe::Account.list_persons( + @account_id + ) + assert_requested :get, "#{Stripe.api_base}/v1/accounts/#{@account_id}/persons" + assert persons.is_a?(Stripe::ListObject) + assert persons.data.is_a?(Array) + end + end + end +end diff --git a/test/stripe/account_test.rb b/test/stripe/account_test.rb index a3fae36f6..1206873bc 100644 --- a/test/stripe/account_test.rb +++ b/test/stripe/account_test.rb @@ -61,6 +61,14 @@ class AccountTest < Test::Unit::TestCase assert account.is_a?(Stripe::Account) end + should "be able to list Persons" do + account = Stripe::Account.retrieve("acct_123") + persons = account.persons + assert_requested :get, "#{Stripe.api_base}/v1/accounts/acct_123/persons" + assert persons.data.is_a?(Array) + assert persons.data[0].is_a?(Stripe::Person) + end + context "#bank_account=" do should "warn that #bank_account= is deprecated" do old_stderr = $stderr diff --git a/test/stripe/customer_card_test.rb b/test/stripe/customer_card_test.rb index c099db329..75b4b7007 100644 --- a/test/stripe/customer_card_test.rb +++ b/test/stripe/customer_card_test.rb @@ -17,19 +17,17 @@ class CustomerCardTest < Test::Unit::TestCase end should "be creatable" do - card = @customer.sources.create( + @customer.sources.create( source: "tok_123" ) assert_requested :post, "#{Stripe.api_base}/v1/customers/#{@customer.id}/sources" - assert card.is_a?(Stripe::BankAccount) end should "be deletable" do card = Stripe::Card.construct_from(customer: @customer.id, id: "card_123") - card = card.delete + card.delete assert_requested :delete, "#{Stripe.api_base}/v1/customers/#{@customer.id}/sources/card_123" - assert card.is_a?(Stripe::Card) end should "be saveable" do diff --git a/test/stripe/customer_sources_operations_test.rb b/test/stripe/customer_sources_operations_test.rb index dddc6152b..25b7ef587 100644 --- a/test/stripe/customer_sources_operations_test.rb +++ b/test/stripe/customer_sources_operations_test.rb @@ -11,46 +11,42 @@ class CustomerSourcesOperationsTest < Test::Unit::TestCase context "#create_source" do should "create a source" do - source = Stripe::Customer.create_source( + Stripe::Customer.create_source( @customer_id, source: "tok_123" ) assert_requested :post, "#{Stripe.api_base}/v1/customers/#{@customer_id}/sources" - assert source.is_a?(Stripe::BankAccount) end end context "#retrieve_source" do should "retrieve a source" do - source = Stripe::Customer.retrieve_source( + Stripe::Customer.retrieve_source( @customer_id, @source_id ) assert_requested :get, "#{Stripe.api_base}/v1/customers/#{@customer_id}/sources/#{@source_id}" - assert source.is_a?(Stripe::BankAccount) end end context "#update_source" do should "update a source" do - source = Stripe::Customer.update_source( + Stripe::Customer.update_source( @customer_id, @source_id, metadata: { foo: "bar" } ) assert_requested :post, "#{Stripe.api_base}/v1/customers/#{@customer_id}/sources/#{@source_id}" - assert source.is_a?(Stripe::Card) end end context "#delete_source" do should "delete a source" do - source = Stripe::Customer.delete_source( + Stripe::Customer.delete_source( @customer_id, @source_id ) assert_requested :delete, "#{Stripe.api_base}/v1/customers/#{@customer_id}/sources/#{@source_id}" - assert source.is_a?(Stripe::BankAccount) end end diff --git a/test/stripe/order_test.rb b/test/stripe/order_test.rb index 97c72c406..ffcda1d46 100644 --- a/test/stripe/order_test.rb +++ b/test/stripe/order_test.rb @@ -49,9 +49,7 @@ class OrderTest < Test::Unit::TestCase context "#return_order" do should "return an order" do order = Stripe::Order.retrieve("or_123") - order = order.return_order(items: [ - { parent: "sku_123" }, - ]) + order = order.return_order({}) assert order.is_a?(Stripe::OrderReturn) end end diff --git a/test/stripe/person_test.rb b/test/stripe/person_test.rb new file mode 100644 index 000000000..f90571bc9 --- /dev/null +++ b/test/stripe/person_test.rb @@ -0,0 +1,46 @@ +# frozen_string_literal: true + +require ::File.expand_path("../../test_helper", __FILE__) + +module Stripe + class PersonTest < Test::Unit::TestCase + context "#resource_url" do + should "return a resource URL" do + person = Stripe::Person.construct_from( + id: "person_123", + account: "acct_123" + ) + assert_equal "/v1/accounts/acct_123/persons/person_123", + person.resource_url + end + + should "raise without an account" do + person = Stripe::Person.construct_from(id: "person_123") + assert_raises NotImplementedError do + person.resource_url + end + end + end + + should "raise on #retrieve" do + assert_raises NotImplementedError do + Stripe::Person.retrieve("person_123") + end + end + + should "raise on #update" do + assert_raises NotImplementedError do + Stripe::Person.update("person_123", {}) + end + end + + should "be saveable" do + account = Stripe::Account.retrieve("acct_123") + person = account.persons.retrieve("person_123") + person.first_name = "John" + person.save + assert_requested :post, + "#{Stripe.api_base}/v1/accounts/#{person.account}/persons/#{person.id}" + end + end +end diff --git a/test/test_helper.rb b/test/test_helper.rb index 0812a7cf1..1b803611a 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -16,7 +16,7 @@ require ::File.expand_path("../test_data", __FILE__) # If changing this number, please also change it in `.travis.yml`. -MOCK_MINIMUM_VERSION = "0.33.0".freeze +MOCK_MINIMUM_VERSION = "0.35.0".freeze MOCK_PORT = ENV["STRIPE_MOCK_PORT"] || 12_111 # Disable all real network connections except those that are outgoing to