Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for the Person resource #692

Merged
merged 1 commit into from
Oct 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.33.0
- STRIPE_MOCK_VERSION=0.35.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 @@ -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"
Expand Down
7 changes: 7 additions & 0 deletions lib/stripe/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
26 changes: 26 additions & 0 deletions lib/stripe/person.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions lib/stripe/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
70 changes: 70 additions & 0 deletions test/stripe/account_persons_operations_test.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions test/stripe/account_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 2 additions & 4 deletions test/stripe/customer_card_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 4 additions & 8 deletions test/stripe/customer_sources_operations_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 1 addition & 3 deletions test/stripe/order_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions test/stripe/person_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

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

module Stripe
class PersonTest < Test::Unit::TestCase
ob-stripe marked this conversation as resolved.
Show resolved Hide resolved
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
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down