Skip to content

Commit

Permalink
Add support for CustomerBalanceTransaction resource and APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
remi-stripe committed Jun 16, 2019
1 parent b667280 commit abef09e
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 2 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.57.0
- STRIPE_MOCK_VERSION=0.58.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 @@ -30,6 +30,7 @@ def self.object_names_to_classes
Coupon::OBJECT_NAME => Coupon,
CreditNote::OBJECT_NAME => CreditNote,
Customer::OBJECT_NAME => Customer,
CustomerBalanceTransaction::OBJECT_NAME => CustomerBalanceTransaction,
Discount::OBJECT_NAME => Discount,
Dispute::OBJECT_NAME => Dispute,
EphemeralKey::OBJECT_NAME => EphemeralKey,
Expand Down
1 change: 1 addition & 0 deletions lib/stripe/resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
require "stripe/resources/coupon"
require "stripe/resources/credit_note"
require "stripe/resources/customer"
require "stripe/resources/customer_balance_transaction"
require "stripe/resources/discount"
require "stripe/resources/dispute"
require "stripe/resources/ephemeral_key"
Expand Down
3 changes: 3 additions & 0 deletions lib/stripe/resources/customer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ 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
30 changes: 30 additions & 0 deletions lib/stripe/resources/customer_balance_transaction.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

module Stripe
class CustomerBalanceTransaction < APIResource
extend Stripe::APIOperations::List
include Stripe::APIOperations::Save

OBJECT_NAME = "customer_balance_transaction".freeze

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

def self.retrieve(_id, _opts = {})
raise NotImplementedError,
"Customer Balance Transactions cannot be retrieved without a customer ID. " \
"Retrieve a Customer Balance Transaction using Customer.retrieve_balance_transaction('cus_123', 'cbtxn_123')"
end

def self.update(_id, _params = nil, _opts = nil)
raise NotImplementedError,
"Customer Balance Transactions cannot be retrieved without a customer ID. " \
"Update a Customer Balance Transaction using Customer.update_balance_transaction('cus_123', 'cbtxn_123', params)"
end
end
end
37 changes: 37 additions & 0 deletions test/stripe/customer_balance_transaction_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# frozen_string_literal: true

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

module Stripe
class CustomerBalanceTransactionTest < Test::Unit::TestCase
context "#resource_url" do
should "return a resource URL" do
transaction = Stripe::CustomerBalanceTransaction.construct_from(
id: "cbtxn_123",
customer: "cus_123"
)
assert_equal "/v1/customers/cus_123/balance_transactions/cbtxn_123",
transaction.resource_url
end

should "raise without a customer" do
transaction = Stripe::CustomerBalanceTransaction.construct_from(id: "cbtxn_123")
assert_raises NotImplementedError do
transaction.resource_url
end
end
end

should "raise on #retrieve" do
assert_raises NotImplementedError do
Stripe::CustomerBalanceTransaction.retrieve("cbtxn_123")
end
end

should "raise on #update" do
assert_raises NotImplementedError do
Stripe::CustomerBalanceTransaction.update("cbtxn_123")
end
end
end
end
43 changes: 43 additions & 0 deletions test/stripe/customer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -222,5 +222,48 @@ class CustomerTest < Test::Unit::TestCase
assert sources.data.is_a?(Array)
end
end

context "#create_balance_transaction" do
should "create a customer balance transaction" do
Stripe::Customer.create_balance_transaction(
"cus_123",
amount: 1234,
currency: "usd"
)
assert_requested :post, "#{Stripe.api_base}/v1/customers/cus_123/balance_transactions"
end
end

context "#retrieve_balance_transaction" do
should "retrieve a customer balance transaction" do
Stripe::Customer.retrieve_balance_transaction(
"cus_123",
"cbtxn_123"
)
assert_requested :get, "#{Stripe.api_base}/v1/customers/cus_123/balance_transactions/cbtxn_123"
end
end

context "#update_balance_transaction" do
should "update a customer balance transaction" do
Stripe::Customer.update_balance_transaction(
"cus_123",
"cbtxn_123",
description: "new"
)
assert_requested :post, "#{Stripe.api_base}/v1/customers/cus_123/balance_transactions/cbtxn_123"
end
end

context "#list_balance_transactions" do
should "list the customer balance transactions" do
sources = Stripe::Customer.list_balance_transactions(
"cus_123"
)
assert_requested :get, "#{Stripe.api_base}/v1/customers/cus_123/balance_transactions"
assert sources.is_a?(Stripe::ListObject)
assert sources.data.is_a?(Array)
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.57.0".freeze
MOCK_MINIMUM_VERSION = "0.58.0".freeze
MOCK_PORT = Stripe::StripeMock.start

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

0 comments on commit abef09e

Please sign in to comment.