monza 1.3.4 → 1.3.9

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dbed8295d16cca5672b895abfa7f90dc28ca5eabf6e36f05e4fcd5eadf01b98c
4
- data.tar.gz: f4a6cace88d1ce3664f4576e359cbb7406355015d29f31a2f91a30d9851097e7
3
+ metadata.gz: 352558d4ab4014083ece9624e7361abf178e7930b85f734eadc1b7bf7f2ae251
4
+ data.tar.gz: 59fbaee7463a1e76f28cca6ab8f8d677b6461249d7f5e84d6eb1fe75f21117ee
5
5
  SHA512:
6
- metadata.gz: cfe794f5ebf8af1880a65cfe8ca18711c6ed36e33f0a6c57f026a64892f3ace2ee499e73250bda1b9db7918e047461f51d34d9d244c3b53a92b60b1c320d16b5
7
- data.tar.gz: 7fc5075d7056c6c8b2287770c4115cdc380666970638d72fea0e2c59541091b6fbcdabf7800d2d4cb024d6f6a6801e5894d3d920fc3104edc8f11050d1727ff1
6
+ metadata.gz: ab12f4fa179c45495ada0bd05b6a706cdd94948aff4af9dd9f8e84c00adc19b49fec36b52483687989b92a2c25e679a5a7ce205d47d713ee22e15140dae4de30
7
+ data.tar.gz: 1911b4a5e8dea7cfd152394fcb92b58affc6a5a0c610f9b8a48ff70a073296889209de90d06725ab640b9aab074126e98d91089338c3a42e6412b7dbe7f70995
data/README.md CHANGED
@@ -41,11 +41,6 @@ You can also pass in `exclude_old_transactions` with value `true` as an option i
41
41
  # this checks if latest transaction receipt expiry_date is in today or the future
42
42
  response.is_subscription_active? # => true or false
43
43
 
44
- # Check if subscription is active
45
- # this checks if ANY transaction receipt expiry_date is in today or the future
46
- # this is helpful in case the most recent transaction was cancelled but the user still has a previous transaction or subscription that is active
47
- response.is_any_subscription_active? # => true or false
48
-
49
44
  # Returns the active subscription TransactionReceipt or nil
50
45
  response.latest_active_transaction_receipt # => TransactionReceipt instance
51
46
 
@@ -1,3 +1,4 @@
1
+ require "monza/bool_typecasting"
1
2
  require "monza/version"
2
3
  require "monza/client"
3
4
  require "monza/renewal_info"
@@ -5,5 +6,4 @@ require "monza/verification_response"
5
6
  require "monza/status_response"
6
7
  require "monza/receipt"
7
8
  require "monza/transaction_receipt"
8
- require "monza/bool_typecasting"
9
9
  require "monza/config"
@@ -1,35 +1,38 @@
1
- class String
2
- def to_bool
3
- return true if self == true || self =~ (/^(true|t|yes|y|1)$/i)
4
- return false if self == false || self.empty? || self =~ (/^(false|f|no|n|0)$/i)
5
- raise ArgumentError.new("invalid value for Boolean: \"#{self}\"")
1
+ module BoolTypecasting
2
+ refine String do
3
+ def to_bool
4
+ return true if self == true || self =~ (/^(true|t|yes|y|1)$/i)
5
+ return false if self == false || self.empty? || self =~ (/^(false|f|no|n|0)$/i)
6
+ raise ArgumentError.new("invalid value for Boolean: \"#{self}\"")
7
+ end
8
+ # if using Rails empty? can be changed for blank?
6
9
  end
7
- # if using Rails empty? can be changed for blank?
8
- end
9
10
 
10
- if RUBY_VERSION >= "2.4"
11
- integer_class = Object.const_get("Integer")
12
- else
13
- integer_class = Object.const_get("Fixnum")
14
- end
15
- integer_class::class_eval do
16
- def to_bool
17
- return true if self == 1
18
- return false if self == 0
19
- raise ArgumentError.new("invalid value for Boolean: \"#{self}\"")
11
+ if RUBY_VERSION >= "2.4"
12
+ integer_class = Object.const_get("Integer")
13
+ else
14
+ integer_class = Object.const_get("Fixnum")
20
15
  end
21
- end
22
16
 
23
- class TrueClass
24
- def to_i; 1; end
25
- def to_bool; self; end
26
- end
17
+ refine integer_class do
18
+ def to_bool
19
+ return true if self == 1
20
+ return false if self == 0
21
+ raise ArgumentError.new("invalid value for Boolean: \"#{self}\"")
22
+ end
23
+ end
27
24
 
28
- class FalseClass
29
- def to_i; 0; end
30
- def to_bool; self; end
31
- end
25
+ refine TrueClass do
26
+ def to_i; 1; end
27
+ def to_bool; self; end
28
+ end
32
29
 
33
- class NilClass
34
- def to_bool; false; end
30
+ refine FalseClass do
31
+ def to_i; 0; end
32
+ def to_bool; self; end
33
+ end
34
+
35
+ refine NilClass do
36
+ def to_bool; false; end
37
+ end
35
38
  end
@@ -1,5 +1,7 @@
1
1
  module Monza
2
2
  class RenewalInfo
3
+ using BoolTypecasting
4
+
3
5
  # Receipt Fields Documentation
4
6
  # https://developer.apple.com/library/ios/releasenotes/General/ValidateAppStoreReceipt/Chapters/ReceiptFields.html#//apple_ref/doc/uid/TP40010573-CH106-SW1
5
7
 
@@ -19,10 +21,15 @@ module Monza
19
21
  @auto_renew_product_id = attributes['auto_renew_product_id']
20
22
  @original_transaction_id = attributes['original_transaction_id']
21
23
 
22
- @grace_period_expires_date = DateTime.parse(attributes['grace_period_expires_date'])
23
- @grace_period_expires_date_ms = Time.zone.at(attributes['grace_period_expires_date_ms'].to_i / 1000)
24
- @grace_period_expires_date_pst = DateTime.parse(attributes['grace_period_expires_date_pst'].gsub("America/Los_Angeles","PST"))
25
-
24
+ if attributes['grace_period_expires_date']
25
+ @grace_period_expires_date = DateTime.parse(attributes['grace_period_expires_date'])
26
+ end
27
+ if attributes['grace_period_expires_date_ms']
28
+ @grace_period_expires_date_ms = Time.zone.at(attributes['grace_period_expires_date_ms'].to_i / 1000)
29
+ end
30
+ if attributes['grace_period_expires_date_pst']
31
+ @grace_period_expires_date_pst = DateTime.parse(attributes['grace_period_expires_date_pst'].gsub("America/Los_Angeles","PST"))
32
+ end
26
33
  if attributes['expiration_intent']
27
34
  @expiration_intent = attributes['expiration_intent']
28
35
  end
@@ -1,17 +1,40 @@
1
+ # frozen_string_literal: true
1
2
  require 'time'
2
3
 
3
4
  module Monza
4
5
  class StatusResponse
6
+ using BoolTypecasting
7
+
5
8
  # https://developer.apple.com/library/ios/releasenotes/General/ValidateAppStoreReceipt/Chapters/ValidateRemotely.html
9
+ # https://developer.apple.com/documentation/appstoreservernotifications/responsebody
10
+ module Type
11
+ CANCEL = 'CANCEL'
12
+ DID_CHANGE_RENEWAL_PREF = 'DID_CHANGE_RENEWAL_PREF'
13
+ DID_CHANGE_RENEWAL_STATUS = 'DID_CHANGE_RENEWAL_STATUS'
14
+ DID_FAIL_TO_RENEW = 'DID_FAIL_TO_RENEW'
15
+ DID_RECOVER = 'DID_RECOVER'
16
+ INITIAL_BUY = 'INITIAL_BUY'
17
+ INTERACTIVE_RENEWAL = 'INTERACTIVE_RENEWAL'
18
+ RENEWAL = 'RENEWAL'
19
+ REFUND = 'REFUND'
20
+ end
21
+
6
22
 
7
23
  attr_reader :auto_renew_product_id
8
24
  attr_reader :auto_renew_status
25
+ attr_reader :auto_renew_status_change_date
26
+ attr_reader :auto_renew_status_change_date_ms
27
+ attr_reader :auto_renew_status_change_date_pst
9
28
  attr_reader :environment
29
+ attr_reader :expiration_intent
30
+
10
31
  attr_reader :latest_receipt
11
32
 
12
33
  attr_reader :notification_type
13
34
  attr_reader :password
14
35
 
36
+ attr_reader :latest_receipt_info
37
+ attr_reader :renewal_info
15
38
  attr_reader :bundle_id
16
39
  attr_reader :bvrs
17
40
  attr_reader :item_id
@@ -42,8 +65,20 @@ module Monza
42
65
 
43
66
  @auto_renew_product_id = attributes['auto_renew_product_id']
44
67
  @auto_renew_status = attributes['auto_renew_status'].to_bool
68
+ if attributes['auto_renew_status_change_date']
69
+ @auto_renew_status_change_date = DateTime.parse(attributes['auto_renew_status_change_date'])
70
+ end
71
+ if attributes['auto_renew_status_change_date_ms']
72
+ @auto_renew_status_change_date_ms = Time.zone.at(attributes['auto_renew_status_change_date_ms'].to_i / 1000)
73
+ end
74
+ if attributes['auto_renew_status_change_date_pst']
75
+ @auto_renew_status_change_date_pst = DateTime.parse(attributes['auto_renew_status_change_date_pst'].gsub("America/Los_Angeles", "PST"))
76
+ end
77
+
45
78
  @environment = attributes['environment']
46
- @latest_receipt = attributes['latest_receipt']
79
+ @expiration_intent = attributes['expiration_intent']
80
+
81
+ @latest_receipt = attributes['latest_receipt'] || attributes['latest_expired_receipt']
47
82
  @notification_type = attributes['notification_type']
48
83
 
49
84
  if attributes['password']
@@ -95,6 +130,22 @@ module Monza
95
130
  if latest_receipt_info['cancellation_date']
96
131
  @cancellation_date = DateTime.parse(latest_receipt_info['cancellation_date'])
97
132
  end
133
+
134
+ @latest_receipt_info = []
135
+ case attributes.dig('unified_receipt', 'latest_receipt_info')
136
+ when Array
137
+ attributes.dig('unified_receipt', 'latest_receipt_info').each do |transaction_receipt_attributes|
138
+ @latest_receipt_info << TransactionReceipt.new(transaction_receipt_attributes)
139
+ end
140
+ when Hash
141
+ @latest_receipt_info << TransactionReceipt.new(attributes.dig('unified_receipt', 'latest_receipt_info'))
142
+ end
143
+ @renewal_info = []
144
+ if attributes.dig('unified_receipt', 'pending_renewal_info')
145
+ attributes.dig('unified_receipt', 'pending_renewal_info').each do |renewal_info_attributes|
146
+ @renewal_info << RenewalInfo.new(renewal_info_attributes)
147
+ end
148
+ end
98
149
  end
99
150
 
100
151
  def date_for_pacific_time pt
@@ -103,6 +154,42 @@ module Monza
103
154
  ActiveSupport::TimeZone["Pacific Time (US & Canada)"].parse(pt).to_datetime
104
155
  end
105
156
 
157
+ def cancel?
158
+ notification_type == Type::CANCEL
159
+ end
160
+
161
+ def did_change_renewal_pref?
162
+ notification_type == Type::DID_CHANGE_RENEWAL_PREF
163
+ end
164
+
165
+ def did_change_renewal_status?
166
+ notification_type == Type::DID_CHANGE_RENEWAL_STATUS
167
+ end
168
+
169
+ def did_fail_to_renew?
170
+ notification_type == Type::DID_FAIL_TO_RENEW
171
+ end
172
+
173
+ def did_recover?
174
+ notification_type == Type::DID_RECOVER
175
+ end
176
+
177
+ def initial_buy?
178
+ notification_type == Type::INITIAL_BUY
179
+ end
180
+
181
+ def interactive_renewal?
182
+ notification_type == Type::INTERACTIVE_RENEWAL
183
+ end
184
+
185
+ def renewal?
186
+ notification_type == Type::RENEWAL
187
+ end
188
+
189
+ def refund?
190
+ notification_type == Type::REFUND
191
+ end
192
+
106
193
  end # class
107
194
  end # module
108
195
 
@@ -3,6 +3,8 @@ require 'active_support/core_ext/time'
3
3
 
4
4
  module Monza
5
5
  class TransactionReceipt
6
+ using BoolTypecasting
7
+
6
8
  # Receipt Fields Documentation
7
9
  # https://developer.apple.com/library/ios/releasenotes/General/ValidateAppStoreReceipt/Chapters/ReceiptFields.html#//apple_ref/doc/uid/TP40010573-CH106-SW1
8
10
 
@@ -22,9 +24,15 @@ module Monza
22
24
  attr_reader :expires_date_ms
23
25
  attr_reader :expires_date_pst
24
26
  attr_reader :is_trial_period
27
+ attr_reader :cancellation_reason
25
28
  attr_reader :cancellation_date
29
+ attr_reader :cancellation_date_ms
30
+ attr_reader :cancellation_date_pst
31
+ attr_reader :is_in_intro_offer_period
32
+ attr_reader :original_attributes
26
33
 
27
34
  def initialize(attributes)
35
+ @original_attributes = attributes
28
36
  @quantity = attributes['quantity'].to_i
29
37
  @product_id = attributes['product_id']
30
38
  @transaction_id = attributes['transaction_id']
@@ -36,6 +44,10 @@ module Monza
36
44
  @original_purchase_date_ms = Time.zone.at(attributes['original_purchase_date_ms'].to_i / 1000)
37
45
  @original_purchase_date_pst = DateTime.parse(attributes['original_purchase_date_pst'].gsub("America/Los_Angeles","PST")) if attributes['original_purchase_date_pst']
38
46
  @web_order_line_item_id = attributes['web_order_line_item_id']
47
+ @cancellation_reason = attributes['cancellation_reason'] if attributes['cancellation_reason']
48
+ @cancellation_date = DateTime.parse(attributes['cancellation_date']) if attributes['cancellation_date']
49
+ @cancellation_date_ms = Time.zone.at(attributes['cancellation_date_ms'].to_i / 1000) if attributes['cancellation_date_ms']
50
+ @cancellation_date_pst = DateTime.parse(attributes['cancellation_date_pst'].gsub("America/Los_Angeles","PST")) if attributes['cancellation_date_pst']
39
51
 
40
52
  if attributes['expires_date']
41
53
  begin
@@ -57,8 +69,8 @@ module Monza
57
69
  if attributes['is_trial_period']
58
70
  @is_trial_period = attributes['is_trial_period'].to_bool
59
71
  end
60
- if attributes['cancellation_date']
61
- @cancellation_date = DateTime.parse(attributes['cancellation_date'])
72
+ if attributes['is_in_intro_offer_period']
73
+ @is_in_intro_offer_period = attributes['is_in_intro_offer_period'].to_bool
62
74
  end
63
75
  end # end initialize
64
76
 
@@ -37,30 +37,19 @@ module Monza
37
37
  end
38
38
 
39
39
  def is_subscription_active?
40
- if @latest_receipt_info.last
41
- if @latest_receipt_info.last.cancellation_date
42
- false
43
- else
44
- @latest_receipt_info.last.expires_date_ms >= Time.zone.now
45
- end
46
- else
47
- false
48
- end
40
+ latest_active_transaction_receipt.present?
49
41
  end
50
42
 
43
+ # Deprecated, only left here for backwards compatibility, please use is_subscription_active?
51
44
  def is_any_subscription_active?
52
- expires_dates_ms = @latest_receipt_info.reject(&:cancellation_date).map(&:expires_date_ms)
53
- latest_expires_date_ms = expires_dates_ms.max
54
-
55
- if latest_expires_date_ms
56
- latest_expires_date_ms >= Time.zone.now
57
- else
58
- false
59
- end
45
+ is_subscription_active?
60
46
  end
61
47
 
62
48
  def latest_active_transaction_receipt
63
- latest_active_sub = @latest_receipt_info.sort_by(&:expires_date_ms).last
49
+ latest_active_sub = @latest_receipt_info
50
+ .reject(&:cancellation_date)
51
+ .select(&:expires_date_ms)
52
+ .max_by(&:expires_date_ms)
64
53
 
65
54
  if latest_active_sub && latest_active_sub.expires_date_ms >= Time.zone.now
66
55
  return latest_active_sub
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Monza
2
- VERSION = "1.3.4"
4
+ VERSION = '1.3.9'
3
5
  end
@@ -30,7 +30,7 @@ Gem::Specification.new do |spec|
30
30
  spec.add_dependency "json"
31
31
  spec.add_dependency "activesupport"
32
32
 
33
- spec.add_development_dependency "bundler", "~> 1.10"
33
+ spec.add_development_dependency "bundler", "~> 2.1"
34
34
  spec.add_development_dependency "rake", "~> 10.0"
35
35
  spec.add_development_dependency "rspec"
36
36
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: monza
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.4
4
+ version: 1.3.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Garza
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-10-30 00:00:00.000000000 Z
11
+ date: 2020-10-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -44,14 +44,14 @@ dependencies:
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '1.10'
47
+ version: '2.1'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '1.10'
54
+ version: '2.1'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rake
57
57
  requirement: !ruby/object:Gem::Requirement