From c066c9c5f8f4d8a04be7634813d4084f87918d98 Mon Sep 17 00:00:00 2001 From: Alexander Thiemann Date: Wed, 4 Apr 2018 11:13:59 -0700 Subject: [PATCH] flexible billing primitives and tests --- .rubocop_todo.yml | 4 ++-- lib/stripe.rb | 1 + lib/stripe/usage_record.rb | 12 ++++++++++ lib/stripe/util.rb | 1 + test/stripe/plan_test.rb | 40 ++++++++++++++++++++++++++++++++ test/stripe/usage_record_test.rb | 26 +++++++++++++++++++++ 6 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 lib/stripe/usage_record.rb create mode 100644 test/stripe/usage_record_test.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index e490e55b1..51a6cede2 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -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. diff --git a/lib/stripe.rb b/lib/stripe.rb index d31dc0c16..bef52289a 100644 --- a/lib/stripe.rb +++ b/lib/stripe.rb @@ -76,6 +76,7 @@ require "stripe/token" require "stripe/topup" require "stripe/transfer" +require "stripe/usage_record" # OAuth require "stripe/oauth" diff --git a/lib/stripe/usage_record.rb b/lib/stripe/usage_record.rb new file mode 100644 index 000000000..255001d54 --- /dev/null +++ b/lib/stripe/usage_record.rb @@ -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 diff --git a/lib/stripe/util.rb b/lib/stripe/util.rb index ae9d3b176..c735e3a4d 100644 --- a/lib/stripe/util.rb +++ b/lib/stripe/util.rb @@ -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 diff --git a/test/stripe/plan_test.rb b/test/stripe/plan_test.rb index db774efab..c22adf9d1 100644 --- a/test/stripe/plan_test.rb +++ b/test/stripe/plan_test.rb @@ -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" diff --git a/test/stripe/usage_record_test.rb b/test/stripe/usage_record_test.rb new file mode 100644 index 000000000..b2742fc22 --- /dev/null +++ b/test/stripe/usage_record_test.rb @@ -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