compensated 0.1.0.pre12 → 0.1.0.pre13

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: bd372eb0e25b2a4556c00570678711625b4bc2c0fbc03a39895eecf41773dded
4
- data.tar.gz: c284ee4d7dabd693c24f4249dfa8c377541e8f96eee6d4177bd12e7f8da776af
3
+ metadata.gz: 1ffaeea7f175182813c34e67a329003fd5d59830fad22ba2fdee2a0431e8d471
4
+ data.tar.gz: 8afb60789b60155b3b905e92235f861583fdda79507d3d2e2c1c61c4e10fa71b
5
5
  SHA512:
6
- metadata.gz: 932ec9c9431d968d7c27ec007abad733e6993d5028f08740cb4d785d5811d8ee0db2a831f79a89b10bce8b3708cefb10f73cf86c8b22fdabe2b270f648da7531
7
- data.tar.gz: bbb5ac4795f1fdfee765a9d1417e3af6c5236b15bc51a5cc9b9fb24dd9a87be85c1085ffa55413bf396f9d639d516a74663a3b570e3c21ed54dda7bd45e1f9c5
6
+ metadata.gz: ee192c911a7d01c525f99ff4bb5dd9b894de9346fead72f0bdb7699dceec262ab24cd78625063de80321310bab284785a995379d6a70959603be23a36a0af7d9
7
+ data.tar.gz: df66cefcd2e238b646bf68f1814dc9519ddea22e9a5c0d8f50822112f640fd9bde12303ef47d308cacbd0e135c3aab9870c4435e7e20b16f3960038cde57e0e7
@@ -1,7 +1,8 @@
1
1
  require "compensated/event_parser"
2
2
  module Compensated
3
3
  module Stripe
4
- SUPPORTED_EVENTS = %w[charge.succeeded invoice.payment_succeeded invoice.payment_failed customer.subscription.deleted]
4
+ SUPPORTED_EVENTS = %w[charge.succeeded invoice.payment_succeeded invoice.payment_failed
5
+ customer.subscription.updated customer.subscription.deleted]
5
6
  class EventParser < Compensated::EventParser
6
7
  def parses?(request)
7
8
  return false if request.nil? || request.empty?
@@ -45,13 +46,16 @@ module Compensated
45
46
  end
46
47
 
47
48
  private def products(data)
48
- if invoice?(data)
49
- data[:data][:object][:lines][:data].map do |line|
50
- value = product(line, data)
51
- next if value.nil? || value.empty?
52
- value
53
- end
49
+ key = (invoice?(data) ? :lines : :items)
50
+ return [] unless data[:data][:object][key]
51
+ items = data[:data][:object][key][:data]
52
+
53
+ items.map do |line|
54
+ value = product(line, data)
55
+ next if value.nil? || value.empty?
56
+ value
54
57
  end
58
+
55
59
  end
56
60
 
57
61
  private def product(line, data)
@@ -61,14 +65,43 @@ module Compensated
61
65
  purchased: purchased(data),
62
66
  description: line[:description],
63
67
  quantity: line[:quantity],
64
- expiration: Time.at(line[:period][:end]),
65
- subscription: subscription(line),
68
+ # TODO: Deprecate `expiration` as it now lives on subscription!
69
+ # BUT people are using `expiration` so let's figure out
70
+ # a way to safely remove it.
71
+ expiration: period_end(line, data),
72
+ subscription: subscription(line, data),
66
73
  plan: plan(line)
67
74
  }.compact
68
75
  end
69
76
 
70
- private def subscription(line)
71
- { id: line[:subscription] }.compact
77
+ private def period_start(line, data)
78
+ return nil unless line[:period] || data[:data][:object][:current_period_start]
79
+ timestamp = line[:period] ? line[:period][:start] : data[:data][:object][:current_period_start]
80
+ Time.at(timestamp)
81
+ end
82
+ private def period_end(line, data)
83
+ return nil unless line[:period] || data[:data][:object][:current_period_end]
84
+ timestamp = line[:period] ? line[:period][:end] : data[:data][:object][:current_period_end]
85
+ Time.at(timestamp)
86
+ end
87
+
88
+ private def subscription(line, data)
89
+ {
90
+ id: line[:subscription],
91
+ period: {
92
+ start: period_start(line, data),
93
+ end: period_end(line, data),
94
+ }.compact,
95
+ status: subscription_status(data)
96
+ }.compact
97
+ end
98
+
99
+ private def subscription_status(data)
100
+ return :ended unless data[:data][:object][:ended_at].nil?
101
+ return :canceled unless data[:data][:object][:canceled_at].nil?
102
+ return :active if data[:data][:object][:status] == "paid"
103
+
104
+ data[:data][:object][:status].to_sym
72
105
  end
73
106
 
74
107
  private def plan(line)
@@ -90,7 +123,11 @@ module Compensated
90
123
  end
91
124
 
92
125
  private def purchased(data)
93
- string = data[:data][:object][:status_transitions][:paid_at]
126
+ string = if data[:data][:object][:created]
127
+ data[:data][:object][:created]
128
+ else
129
+ data[:data][:object][:status_transitions][0][:paid_at]
130
+ end
94
131
  return nil if string.nil?
95
132
  Time.at(string)
96
133
  end
@@ -1,3 +1,3 @@
1
1
  module Compensated
2
- VERSION = "0.1.0.pre12"
2
+ VERSION = "0.1.0.pre13"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: compensated
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.pre12
4
+ version: 0.1.0.pre13
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zee
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-02-12 00:00:00.000000000 Z
11
+ date: 2020-04-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -115,8 +115,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
115
115
  - !ruby/object:Gem::Version
116
116
  version: 1.3.1
117
117
  requirements: []
118
- rubyforge_project:
119
- rubygems_version: 2.7.6.2
118
+ rubygems_version: 3.0.3
120
119
  signing_key:
121
120
  specification_version: 4
122
121
  summary: Provide value (In Ruby!). Get paid.