stripe-rails 1.6.1 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4925781be88d86f0b3e1aa2811bd6498a03f68a0d85c0cac7f2135ebee3d4e14
4
- data.tar.gz: c3d71a7f8d057b3c5a4a715907842dbf44512ddd508a1baf81f0ee74b8348e4f
3
+ metadata.gz: f24ac8d0b91f140510d25e7c614a14e2f2574ea8f3b280c052691f088d5aa9bf
4
+ data.tar.gz: ce0d05dde985042654708e2701012dfd7c74d38f22c19df4a2cc703effa582b0
5
5
  SHA512:
6
- metadata.gz: c6fea8783eb4b7c18231c061317c48595c9d0504481d1766c0c584365dd86482738ae47a301c08eeca46535778f68d0a4d88c777a3fcdaae856f0a322e2a2f96
7
- data.tar.gz: 910d3aa144e182e9e0972eb363497fd0587ad59ceaee6576ec32507941e03799cad3b1013e16fdfeb1c6b870bab16d2e8fb970f86f0d570fe2c404b78e1012cd
6
+ metadata.gz: 6353b526babcf9e9e5a5a36282470167aa6a794242038ad78bfabcef3bba00ea76c4aefee7891de01d12f1d0dd52470c1e614f454c5e88e13bc63173258e58b3
7
+ data.tar.gz: 4b7045a4a6f1abbaafa970ad55c24b96ec31e8d4e1caa1b2ca41dc90346b10f21f3fadbc2cec102087c733f716d39eb79b83a6f2f401d22aeacff7ecfb1f65a8
data/Changelog.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 1.7.0 (2019-05-09)
2
+
3
+ * [New Feature] add support for Plan to use a constant name different from plan id thanks @alexagranov !
4
+ * Add checkout.session.completed webhook thanks @Nitrino !
5
+
1
6
  ## 1.6.1 (2019-03-04)
2
7
 
3
8
  * Add new invoice webhooks thanks @noahezekwugo !
data/README.md CHANGED
@@ -194,6 +194,25 @@ Stripe::Plans::SILVER # => 'silver: ACME Silver'
194
194
  Stripe::Plans::GOLD # => 'gold: ACME Gold'
195
195
  ```
196
196
 
197
+ If you have to support an existing plan with a Stripe plan id that can not
198
+ be used as a Ruby constant, provide the plan id as a symbol when
199
+ defining the plan, but provide the name for the constant to define with `constant_name`:
200
+
201
+ ```ruby
202
+ Stripe.plan "Silver-Plan".to_sym do |plan|
203
+ plan.constant_name = 'SILVER_PLAN'
204
+ end
205
+
206
+ Stripe::Plans::SILVER_PLAN # => will be defined
207
+ # Will map to plan :id => "Silver-Plan" on Stripe
208
+ ```
209
+
210
+ **Note** - If you're planning on running `rake stripe:prepare` to
211
+ create your subscription plans, Stripe will restrict plan ids to match
212
+ this regexp (`/\A[a-zA-Z0-9_\-]+\z/`) when created via API but still
213
+ allows creation of plan ids that don't follow this restriction when
214
+ manually created on stripe.com.
215
+
197
216
  Coupons are created in much the same way:
198
217
 
199
218
  ```ruby
@@ -25,6 +25,7 @@ module Stripe
25
25
  callback 'charge.dispute.funds_withdrawn'
26
26
  callback 'charge.dispute.updated'
27
27
  callback 'charge.refund.updated'
28
+ callback 'checkout.session.completed'
28
29
  callback 'coupon.created'
29
30
  callback 'coupon.deleted'
30
31
  callback 'coupon.updated'
@@ -68,8 +68,9 @@ module Stripe
68
68
  end
69
69
 
70
70
  def globalize!
71
- @stripe_configuration_class[@id.to_s] = self
72
- @stripe_configuration_class.const_set(@id.to_s.upcase, self)
71
+ id_to_use = @constant_name || @id
72
+ @stripe_configuration_class[id_to_use.to_s.downcase] = self
73
+ @stripe_configuration_class.const_set(id_to_use.to_s.upcase, self)
73
74
  end
74
75
 
75
76
  def put!
data/lib/stripe/plans.rb CHANGED
@@ -7,6 +7,7 @@ module Stripe
7
7
  :aggregate_usage,
8
8
  :amount,
9
9
  :billing_scheme,
10
+ :constant_name,
10
11
  :currency,
11
12
  :interval,
12
13
  :interval_count,
@@ -35,6 +36,7 @@ module Stripe
35
36
 
36
37
  validate :name_or_product_id
37
38
  validate :aggregate_usage_must_be_metered, if: ->(p) { p.aggregate_usage.present? }
39
+ validate :valid_constant_name, unless: ->(p) { p.constant_name.nil? }
38
40
 
39
41
  def initialize(*args)
40
42
  super(*args)
@@ -52,6 +54,14 @@ module Stripe
52
54
  errors.add(:base, 'must have a product_id or a name') unless (@product_id.present? ^ @name.present?)
53
55
  end
54
56
 
57
+ module ConstTester; end
58
+ def valid_constant_name
59
+ ConstTester.const_set(constant_name.to_s.upcase, constant_name)
60
+ ConstTester.send(:remove_const, constant_name.to_s.upcase.to_sym)
61
+ rescue NameError
62
+ errors.add(:constant_name, 'is not a valid Ruby constant name.')
63
+ end
64
+
55
65
  def create_options
56
66
  if CurrentApiVersion.after_switch_to_products_in_plans?
57
67
  default_create_options
@@ -1,5 +1,5 @@
1
1
  module Stripe
2
2
  module Rails
3
- VERSION = '1.6.1'
3
+ VERSION = '1.7.0'
4
4
  end
5
5
  end
@@ -4,6 +4,13 @@ Stripe.plan :gold do |plan|
4
4
  plan.interval = 'month'
5
5
  end
6
6
 
7
+ Stripe.plan "Solid Gold".to_sym do |plan|
8
+ plan.constant_name = 'SOLID_GOLD'
9
+ plan.name = 'Solid Gold'
10
+ plan.amount = 699
11
+ plan.interval = 'month'
12
+ end
13
+
7
14
  Stripe.plan :alternative_currency do |plan|
8
15
  plan.name = 'Alternative Currency'
9
16
  plan.amount = 699
@@ -346,4 +346,81 @@ describe 'building plans' do
346
346
  proc {Stripe.plan(:bad) {}}.must_raise Stripe::InvalidConfigurationError
347
347
  end
348
348
  end
349
+
350
+ describe 'with custom constant name' do
351
+ before do
352
+ Stripe.plan "Primo Plan".to_sym do |plan|
353
+ plan.name = 'Acme as a service PRIMO'
354
+ plan.constant_name = 'PRIMO_PLAN'
355
+ plan.amount = 699
356
+ plan.interval = 'month'
357
+ plan.interval_count = 3
358
+ plan.trial_period_days = 30
359
+ plan.metadata = {:number_of_awesome_things => 5}
360
+ plan.statement_descriptor = 'Acme Primo'
361
+ plan.active = true
362
+ plan.nickname = 'primo'
363
+ plan.usage_type = 'metered'
364
+ plan.billing_scheme = 'per_unit'
365
+ plan.aggregate_usage = 'sum'
366
+ plan.tiers_mode = 'graduated'
367
+ end
368
+ end
369
+
370
+ after { Stripe::Plans.send(:remove_const, :PRIMO_PLAN) }
371
+
372
+ it 'is accessible via upcased constant_name' do
373
+ Stripe::Plans::PRIMO_PLAN.wont_be_nil
374
+ end
375
+
376
+ it 'is accessible via collection' do
377
+ Stripe::Plans.all.must_include Stripe::Plans::PRIMO_PLAN
378
+ end
379
+
380
+ it 'is accessible via hash lookup (symbol/string agnostic)' do
381
+ Stripe::Plans[:primo_plan].must_equal Stripe::Plans::PRIMO_PLAN
382
+ Stripe::Plans['primo_plan'].must_equal Stripe::Plans::PRIMO_PLAN
383
+ end
384
+
385
+ describe 'constant name validation' do
386
+ it 'should be invalid when providing a constant name that can not be used for Ruby constant' do
387
+ lambda {
388
+ Stripe.plan "Primo Plan".to_sym do |plan|
389
+ plan.name = 'Acme as a service PRIMO'
390
+ plan.constant_name = 'PRIMO PLAN'
391
+ plan.amount = 999
392
+ plan.interval = 'month'
393
+ end
394
+ }.must_raise Stripe::InvalidConfigurationError
395
+ end
396
+ end
397
+
398
+ describe 'uploading' do
399
+ include FixtureLoader
400
+
401
+ describe 'when none exists on stripe.com' do
402
+ let(:headers) { load_request_fixture('stripe_plans_headers_2017.json') }
403
+ before do
404
+ Stripe::Plan.stubs(:retrieve).raises(Stripe::InvalidRequestError.new("not found", "id"))
405
+
406
+ stub_request(:get, "https://api.stripe.com/v1/plans").
407
+ with(headers: { 'Authorization'=>'Bearer XYZ',}).
408
+ to_return(status: 200, body: load_request_fixture('stripe_plans.json'), headers: JSON.parse(headers))
409
+ end
410
+
411
+ it 'creates the plan online' do
412
+ Stripe::Plan.expects(:create).with(
413
+ :id => "Solid Gold".to_sym,
414
+ :currency => 'usd',
415
+ :name => 'Solid Gold',
416
+ :amount => 699,
417
+ :interval => 'month',
418
+ :interval_count => 1,
419
+ :trial_period_days => 0
420
+ )
421
+ Stripe::Plans::SOLID_GOLD.put!
422
+ end
423
+ end
424
+ end
425
+ end
349
426
  end
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.6.1
4
+ version: 1.7.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: 2019-03-04 00:00:00.000000000 Z
13
+ date: 2019-05-09 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rails
@@ -184,8 +184,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
184
184
  - !ruby/object:Gem::Version
185
185
  version: '0'
186
186
  requirements: []
187
- rubyforge_project:
188
- rubygems_version: 2.7.7
187
+ rubygems_version: 3.0.3
189
188
  signing_key:
190
189
  specification_version: 4
191
190
  summary: A gem to integrate stripe into your rails app