Skip to content

Commit

Permalink
Merge pull request #634 from stripe/alexander/flexible-billing
Browse files Browse the repository at this point in the history
Flexible/Metered Billing API support
  • Loading branch information
brandur-stripe authored Apr 11, 2018
2 parents 0b54bf7 + c066c9c commit 9594875
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ Metrics/LineLength:
# Offense count: 32
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 45
Max: 46

# Offense count: 1
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 305
Max: 306

# Offense count: 6
# Configuration parameters: CountKeywordArgs.
Expand Down
1 change: 1 addition & 0 deletions lib/stripe.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
require "stripe/token"
require "stripe/topup"
require "stripe/transfer"
require "stripe/usage_record"

# OAuth
require "stripe/oauth"
Expand Down
12 changes: 12 additions & 0 deletions lib/stripe/usage_record.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Stripe
class UsageRecord < APIResource
def self.create(params = {}, opts = {})
raise(ArgumentError, "Params must have a subscription_item key") unless params.key?(:subscription_item)
req_params = params.clone.delete_if { |key, _value| key == :subscription_item }
resp, opts = request(:post, "/v1/subscription_items/#{params[:subscription_item]}/usage_records", req_params, opts)
Util.convert_to_stripe_object(resp.data, opts)
end

OBJECT_NAME = "usage_record".freeze
end
end
1 change: 1 addition & 0 deletions lib/stripe/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def self.object_classes
Token::OBJECT_NAME => Token,
Topup::OBJECT_NAME => Topup,
Transfer::OBJECT_NAME => Transfer,
UsageRecord::OBJECT_NAME => UsageRecord,
}
end

Expand Down
40 changes: 40 additions & 0 deletions test/stripe/plan_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,46 @@ class PlanTest < Test::Unit::TestCase
assert plan.is_a?(Stripe::Plan)
end

should "be creatable with metered configuration" do
plan = Stripe::Plan.create(
amount: 5000,
interval: "month",
name: "Sapphire elite",
currency: "usd",
id: "sapphire-elite",
usage_type: "metered"
)
assert_requested :post, "#{Stripe.api_base}/v1/plans"
assert plan.is_a?(Stripe::Plan)
end

should "be creatable with tiered configuration" do
plan = Stripe::Plan.create(
interval: "month",
name: "Sapphire elite",
currency: "usd",
id: "sapphire-elite",
billing_scheme: "tiered",
tiers_mode: "volume",
tiers: [{ up_to: 100, amount: 1000 }, { up_to: "inf", amount: 2000 }]
)
assert_requested :post, "#{Stripe.api_base}/v1/plans"
assert plan.is_a?(Stripe::Plan)
end

should "be creatable with transform_usage" do
plan = Stripe::Plan.create(
interval: "month",
name: "Sapphire elite",
currency: "usd",
id: "sapphire-elite",
amount: 5000,
transform_usage: { round: "up", divide_by: 50 }
)
assert_requested :post, "#{Stripe.api_base}/v1/plans"
assert plan.is_a?(Stripe::Plan)
end

should "be saveable" do
plan = Stripe::Plan.retrieve("sapphire-elite")
plan.metadata["key"] = "value"
Expand Down
26 changes: 26 additions & 0 deletions test/stripe/usage_record_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
require File.expand_path("../../test_helper", __FILE__)

module Stripe
class UsageRecordTest < Test::Unit::TestCase
should "be creatable" do
usage_record = Stripe::UsageRecord.create(
quantity: 5000,
subscription_item: "si_abc",
timestamp: Time.now.to_i,
action: "increment"
)
assert_requested :post, "#{Stripe.api_base}/v1/subscription_items/si_abc/usage_records"
assert usage_record.is_a?(Stripe::UsageRecord)
end

should "raise when subscription_item is missing" do
assert_raise ArgumentError do
Stripe::UsageRecord.create(
quantity: 5000,
timestamp: Time.now.to_i,
action: "increment"
)
end
end
end
end

0 comments on commit 9594875

Please sign in to comment.