stripe-rails 1.9.1 → 1.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/ruby.yml +3 -2
- data/Changelog.md +4 -0
- data/Gemfile +1 -1
- data/README.md +19 -2
- data/gemfiles/rails4.gemfile +1 -0
- data/lib/generators/templates/products.rb +2 -2
- data/lib/stripe/billing_tier.rb +34 -0
- data/lib/stripe/plans.rb +33 -5
- data/lib/stripe/products.rb +2 -0
- data/lib/stripe/rails.rb +1 -0
- data/lib/stripe/rails/version.rb +1 -1
- data/test/dummy/config/stripe/plans.rb +21 -0
- data/test/plan_builder_spec.rb +77 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 95873fe1e4c135546d576191494250facd47172be4b3a91f7325ff69c3ec5723
|
4
|
+
data.tar.gz: a16c59f3cdf162b62103a89332fb749a4f2c44ff45665576e4c00cc7f0637d16
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb222107648581b675d6a1348d4e7773ed93f0f4326dd6a708a66753d08fcad6a5e2a131375d397c48480570772532d0ec3ccf242739ada5d08417c18a9fb9d7
|
7
|
+
data.tar.gz: dab6fd8cae0e0a23dca3281b63f2734c3252dff6f9b3bd3dc431e75ab425a7ef1418087234493b950f2e0d303777a03f24911a539815890422a525cab0418998
|
data/.github/workflows/ruby.yml
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
name: Ruby
|
2
2
|
|
3
|
-
on: [push]
|
3
|
+
on: [push, pull_request]
|
4
4
|
|
5
5
|
jobs:
|
6
6
|
build:
|
@@ -8,7 +8,7 @@ jobs:
|
|
8
8
|
|
9
9
|
strategy:
|
10
10
|
matrix:
|
11
|
-
ruby: [2.
|
11
|
+
ruby: [2.4.x, 2.5.x, 2.6.x]
|
12
12
|
gemfile: [Gemfile, gemfiles/rails4.gemfile]
|
13
13
|
|
14
14
|
steps:
|
@@ -28,6 +28,7 @@ jobs:
|
|
28
28
|
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
|
29
29
|
RUBY_VERSION: ${{ matrix.ruby }}
|
30
30
|
run: |
|
31
|
+
gem uninstall bundler
|
31
32
|
gem install bundler -v 1.17.3
|
32
33
|
bundle install --jobs 4 --retry 3
|
33
34
|
bundle exec rake
|
data/Changelog.md
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Stripe::Rails: A Rails Engine for use with [stripe.com](https://stripe.com)
|
2
|
-
[](https://badge.fury.io/rb/stripe-rails)
|
3
|
+
[](https://travis-ci.org/tansengming/stripe-rails)
|
4
4
|
[](https://codeclimate.com/github/tansengming/stripe-rails)
|
5
5
|
[](https://codeclimate.com/github/tansengming/stripe-rails/coverage)
|
6
6
|
[](#)
|
@@ -188,6 +188,23 @@ Stripe.plan :bronze do |plan|
|
|
188
188
|
plan.product_id = 'prod_XXXXXXXXXXXXXX'
|
189
189
|
plan.amount = 999 # $9.99
|
190
190
|
plan.interval = 'month'
|
191
|
+
|
192
|
+
# Use graduated pricing tiers
|
193
|
+
# ref: https://stripe.com/docs/api/plans/object#plan_object-tiers
|
194
|
+
plan.tiers = [
|
195
|
+
{
|
196
|
+
unit_amount: 1500,
|
197
|
+
up_to: 10
|
198
|
+
},
|
199
|
+
{
|
200
|
+
unit_amount: 1000,
|
201
|
+
up_to: 'inf'
|
202
|
+
}
|
203
|
+
]
|
204
|
+
plan.tiers_mode = 'graduated'
|
205
|
+
|
206
|
+
# set the usage type to 'metered'
|
207
|
+
plan.usage_type = 'metered'
|
191
208
|
end
|
192
209
|
```
|
193
210
|
|
data/gemfiles/rails4.gemfile
CHANGED
@@ -11,9 +11,9 @@
|
|
11
11
|
# product.type = 'service'
|
12
12
|
# end
|
13
13
|
|
14
|
-
# Once you have your
|
14
|
+
# Once you have your products defined, you can run
|
15
15
|
#
|
16
16
|
# rake stripe:prepare
|
17
17
|
#
|
18
|
-
# This will export any new
|
18
|
+
# This will export any new products to stripe.com so that you can
|
19
19
|
# begin using them in your API calls.
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module Stripe
|
2
|
+
module Plans
|
3
|
+
class BillingTier
|
4
|
+
include ActiveModel::Validations
|
5
|
+
|
6
|
+
validates_presence_of :up_to
|
7
|
+
validates_presence_of :flat_amount, if: ->(tier) { tier.unit_amount.nil? },
|
8
|
+
message: 'one of `flat_amount` or `unit_amount` must be specified!'
|
9
|
+
validates_presence_of :unit_amount, if: ->(tier) { tier.flat_amount.nil? },
|
10
|
+
message: 'one of `flat_amount` or `unit_amount` must be specified!'
|
11
|
+
validates_absence_of :flat_amount, if: ->(tier) { tier.unit_amount.present? },
|
12
|
+
message: 'only one of `flat_amount` or `unit_amount` should be specified!'
|
13
|
+
validates_absence_of :unit_amount, if: ->(tier) { tier.flat_amount.present? },
|
14
|
+
message: 'only one of `flat_amount` or `unit_amount` should be specified!'
|
15
|
+
|
16
|
+
attr_accessor :up_to, :flat_amount, :unit_amount
|
17
|
+
|
18
|
+
def initialize(attrs)
|
19
|
+
@up_to = attrs[:up_to]
|
20
|
+
@flat_amount = attrs[:flat_amount]
|
21
|
+
@unit_amount = attrs[:unit_amount]
|
22
|
+
end
|
23
|
+
|
24
|
+
def to_h
|
25
|
+
{
|
26
|
+
up_to: up_to,
|
27
|
+
flat_amount: flat_amount,
|
28
|
+
unit_amount: unit_amount
|
29
|
+
}.compact
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/lib/stripe/plans.rb
CHANGED
@@ -12,16 +12,20 @@ module Stripe
|
|
12
12
|
:interval,
|
13
13
|
:interval_count,
|
14
14
|
:metadata,
|
15
|
-
:name,
|
15
|
+
:name,
|
16
16
|
:nickname,
|
17
17
|
:product_id,
|
18
18
|
:statement_descriptor,
|
19
|
+
:tiers,
|
19
20
|
:tiers_mode,
|
21
|
+
:transform_usage,
|
20
22
|
:trial_period_days,
|
21
23
|
:usage_type
|
22
24
|
|
23
|
-
validates_presence_of :id, :
|
24
|
-
|
25
|
+
validates_presence_of :id, :currency
|
26
|
+
validates_presence_of :amount, unless: ->(p) { p.billing_scheme == 'tiered' }
|
27
|
+
validates_absence_of :transform_usage, if: ->(p) { p.billing_scheme == 'tiered' }
|
28
|
+
validates_presence_of :tiers_mode, if: ->(p) { p.billing_scheme == 'tiered' }
|
25
29
|
validates_inclusion_of :interval,
|
26
30
|
in: %w(day week month year),
|
27
31
|
message: "'%{value}' is not one of 'day', 'week', 'month' or 'year'"
|
@@ -38,6 +42,11 @@ module Stripe
|
|
38
42
|
validate :aggregate_usage_must_be_metered, if: ->(p) { p.aggregate_usage.present? }
|
39
43
|
validate :valid_constant_name, unless: ->(p) { p.constant_name.nil? }
|
40
44
|
|
45
|
+
# validations for when using tiered billing
|
46
|
+
validate :tiers_must_be_array, if: ->(p) { p.tiers.present? }
|
47
|
+
validate :billing_scheme_must_be_tiered, if: ->(p) { p.tiers.present? }
|
48
|
+
validate :validate_tiers, if: ->(p) { p.billing_scheme == 'tiered' }
|
49
|
+
|
41
50
|
def initialize(*args)
|
42
51
|
super(*args)
|
43
52
|
@currency = 'usd'
|
@@ -54,6 +63,22 @@ module Stripe
|
|
54
63
|
errors.add(:base, 'must have a product_id or a name') unless (@product_id.present? ^ @name.present?)
|
55
64
|
end
|
56
65
|
|
66
|
+
def billing_scheme_must_be_tiered
|
67
|
+
errors.add(:billing_scheme, 'must be set to `tiered` when specifying `tiers`') unless billing_scheme == 'tiered'
|
68
|
+
end
|
69
|
+
|
70
|
+
def tiers_must_be_array
|
71
|
+
errors.add(:tiers, 'must be an Array') unless tiers.is_a?(Array)
|
72
|
+
end
|
73
|
+
|
74
|
+
def billing_tiers
|
75
|
+
@billing_tiers = tiers.map { |t| Stripe::Plans::BillingTier.new(t) } if tiers
|
76
|
+
end
|
77
|
+
|
78
|
+
def validate_tiers
|
79
|
+
billing_tiers.all?(&:valid?)
|
80
|
+
end
|
81
|
+
|
57
82
|
module ConstTester; end
|
58
83
|
def valid_constant_name
|
59
84
|
ConstTester.const_set(constant_name.to_s.upcase, constant_name)
|
@@ -82,8 +107,11 @@ module Stripe
|
|
82
107
|
usage_type: usage_type,
|
83
108
|
aggregate_usage: aggregate_usage,
|
84
109
|
billing_scheme: billing_scheme,
|
85
|
-
nickname: nickname
|
86
|
-
|
110
|
+
nickname: nickname,
|
111
|
+
tiers: tiers ? tiers.map(&:to_h) : nil,
|
112
|
+
tiers_mode: tiers_mode,
|
113
|
+
transform_usage: transform_usage
|
114
|
+
}.compact
|
87
115
|
end
|
88
116
|
|
89
117
|
def product_options
|
data/lib/stripe/products.rb
CHANGED
@@ -11,6 +11,7 @@ module Stripe
|
|
11
11
|
:caption,
|
12
12
|
:metadata,
|
13
13
|
:shippable,
|
14
|
+
:unit_label,
|
14
15
|
:url,
|
15
16
|
:statement_descriptor
|
16
17
|
|
@@ -42,6 +43,7 @@ module Stripe
|
|
42
43
|
caption: caption,
|
43
44
|
metadata: metadata,
|
44
45
|
shippable: shippable,
|
46
|
+
unit_label: unit_label,
|
45
47
|
url: url,
|
46
48
|
statement_descriptor: statement_descriptor
|
47
49
|
}
|
data/lib/stripe/rails.rb
CHANGED
data/lib/stripe/rails/version.rb
CHANGED
@@ -26,3 +26,24 @@ Stripe.plan :metered do |plan|
|
|
26
26
|
plan.aggregate_usage = 'max'
|
27
27
|
plan.billing_scheme = 'per_unit'
|
28
28
|
end
|
29
|
+
|
30
|
+
Stripe.plan :tiered do |plan|
|
31
|
+
plan.name = 'Tiered'
|
32
|
+
plan.aggregate_usage = 'max'
|
33
|
+
plan.billing_scheme = 'tiered'
|
34
|
+
# interval must be either 'day', 'week', 'month' or 'year'
|
35
|
+
plan.interval = 'month'
|
36
|
+
plan.interval_count = 1
|
37
|
+
plan.tiers = [
|
38
|
+
{
|
39
|
+
unit_amount: 1500,
|
40
|
+
up_to: 10
|
41
|
+
},
|
42
|
+
{
|
43
|
+
unit_amount: 1000,
|
44
|
+
up_to: 'inf'
|
45
|
+
}
|
46
|
+
]
|
47
|
+
plan.tiers_mode = 'graduated'
|
48
|
+
plan.usage_type = 'metered'
|
49
|
+
end
|
data/test/plan_builder_spec.rb
CHANGED
@@ -275,6 +275,82 @@ describe 'building plans' do
|
|
275
275
|
Stripe::Plans::METERED.put!
|
276
276
|
end
|
277
277
|
|
278
|
+
it 'creates a tiered plan' do
|
279
|
+
Stripe::Plan.expects(:create).with(
|
280
|
+
:id => :tiered,
|
281
|
+
:currency => 'usd',
|
282
|
+
:product => {
|
283
|
+
:name => 'Tiered',
|
284
|
+
:statement_descriptor => nil,
|
285
|
+
},
|
286
|
+
:interval => 'month',
|
287
|
+
:interval_count => 1,
|
288
|
+
:trial_period_days => 0,
|
289
|
+
:usage_type => 'metered',
|
290
|
+
:aggregate_usage => 'max',
|
291
|
+
:billing_scheme => 'tiered',
|
292
|
+
:tiers => [
|
293
|
+
{
|
294
|
+
:unit_amount => 1500,
|
295
|
+
:up_to => 10
|
296
|
+
},
|
297
|
+
{
|
298
|
+
:unit_amount => 1000,
|
299
|
+
:up_to => 'inf'
|
300
|
+
}
|
301
|
+
],
|
302
|
+
:tiers_mode => 'graduated'
|
303
|
+
)
|
304
|
+
plan = Stripe::Plans::TIERED
|
305
|
+
Stripe::Plans::TIERED.put!
|
306
|
+
end
|
307
|
+
|
308
|
+
describe 'when passed invalid arguments for tiered pricing' do
|
309
|
+
it 'raises a Stripe::InvalidConfigurationError when billing tiers are invalid' do
|
310
|
+
lambda {
|
311
|
+
Stripe.plan "Bad Tiers".to_sym do |plan|
|
312
|
+
plan.name = 'Acme as a service BAD TIERS'
|
313
|
+
plan.constant_name = 'BAD_TIERS'
|
314
|
+
plan.interval = 'month'
|
315
|
+
plan.interval_count = 1
|
316
|
+
plan.trial_period_days = 30
|
317
|
+
plan.usage_type = 'metered'
|
318
|
+
plan.tiers_mode = 'graduated'
|
319
|
+
plan.billing_scheme = 'per_unit'
|
320
|
+
plan.aggregate_usage = 'sum'
|
321
|
+
plan.tiers = [
|
322
|
+
{
|
323
|
+
unit_amount: 1500,
|
324
|
+
up_to: 10
|
325
|
+
},
|
326
|
+
{
|
327
|
+
unit_amount: 1000,
|
328
|
+
}
|
329
|
+
]
|
330
|
+
end
|
331
|
+
}.must_raise Stripe::InvalidConfigurationError
|
332
|
+
end
|
333
|
+
|
334
|
+
it 'raises a Stripe::InvalidConfigurationError when billing tiers is not an array' do
|
335
|
+
lambda {
|
336
|
+
Stripe.plan "Bad Tiers".to_sym do |plan|
|
337
|
+
plan.name = 'Acme as a service BAD TIERS'
|
338
|
+
plan.constant_name = 'BAD_TIERS'
|
339
|
+
plan.interval = 'month'
|
340
|
+
plan.interval_count = 1
|
341
|
+
plan.trial_period_days = 30
|
342
|
+
plan.usage_type = 'metered'
|
343
|
+
plan.tiers_mode = 'graduated'
|
344
|
+
plan.billing_scheme = 'per_unit'
|
345
|
+
plan.aggregate_usage = 'sum'
|
346
|
+
plan.tiers = {
|
347
|
+
unit_amount: 1500,
|
348
|
+
up_to: 10
|
349
|
+
}
|
350
|
+
end
|
351
|
+
}.must_raise Stripe::InvalidConfigurationError
|
352
|
+
end
|
353
|
+
end
|
278
354
|
|
279
355
|
describe 'when using a product id' do
|
280
356
|
before do
|
@@ -346,7 +422,7 @@ describe 'building plans' do
|
|
346
422
|
proc {Stripe.plan(:bad) {}}.must_raise Stripe::InvalidConfigurationError
|
347
423
|
end
|
348
424
|
end
|
349
|
-
|
425
|
+
|
350
426
|
describe 'with custom constant name' do
|
351
427
|
before do
|
352
428
|
Stripe.plan "Primo Plan".to_sym do |plan|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: stripe-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.10.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Charles Lowell
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2020-03-31 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rails
|
@@ -93,6 +93,7 @@ files:
|
|
93
93
|
- lib/generators/templates/plans.rb
|
94
94
|
- lib/generators/templates/products.rb
|
95
95
|
- lib/stripe-rails.rb
|
96
|
+
- lib/stripe/billing_tier.rb
|
96
97
|
- lib/stripe/callbacks.rb
|
97
98
|
- lib/stripe/callbacks/builder.rb
|
98
99
|
- lib/stripe/configuration_builder.rb
|