Class: Monza::StatusResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/monza/status_response.rb

Defined Under Namespace

Modules: Type

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ StatusResponse

Returns a new instance of StatusResponse.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/monza/status_response.rb', line 63

def initialize(attributes)
  @original_json_response = attributes

  @auto_renew_product_id = attributes['auto_renew_product_id']
  @auto_renew_status = attributes['auto_renew_status'].to_bool
  if attributes['auto_renew_status_change_date']
    @auto_renew_status_change_date = DateTime.parse(attributes['auto_renew_status_change_date'])
  end
  if attributes['auto_renew_status_change_date_ms']
    @auto_renew_status_change_date_ms = Time.zone.at(attributes['auto_renew_status_change_date_ms'].to_i / 1000)
  end
  if attributes['auto_renew_status_change_date_pst']
    @auto_renew_status_change_date_pst = DateTime.parse(attributes['auto_renew_status_change_date_pst'].gsub("America/Los_Angeles", "PST"))
  end
  
  @environment = attributes['environment']
  @expiration_intent = attributes['expiration_intent']

  @latest_receipt = attributes.dig('unified_receipt', 'latest_receipt')
  @notification_type = attributes['notification_type']

  @password = attributes['password'] if attributes['password']

  latest_receipt_info = []
  case attributes.dig('unified_receipt', 'latest_receipt_info')
  when Array
    attributes.dig('unified_receipt', 'latest_receipt_info').each do |transaction_receipt_attributes|
      latest_receipt_info << transaction_receipt_attributes
    end
  when Hash
    latest_receipt_info << attributes.dig('unified_receipt', 'latest_receipt_info')
  end
  @renewal_info = []
  attributes.dig('unified_receipt', 'pending_renewal_info')&.each do |renewal_info_attributes|
    renewal_info << RenewalInfo.new(renewal_info_attributes)
  end

  latest_receipt_info = latest_receipt_info.first

  @bundle_id = latest_receipt_info['bid']
  @bvrs = latest_receipt_info['bvrs'].to_i
  @item_id = latest_receipt_info['item_id'].to_i
  @product_id = latest_receipt_info['product_id']
  @transaction_id = latest_receipt_info['transaction_id']
  @original_transaction_id = latest_receipt_info['original_transaction_id']
  @purchase_date = DateTime.parse(latest_receipt_info['purchase_date']) if latest_receipt_info['purchase_date']
  @purchase_date_ms = Time.zone.at(latest_receipt_info['purchase_date_ms'].to_i / 1000)
  if latest_receipt_info['purchase_date_pst']
    @purchase_date_pst = date_for_pacific_time(latest_receipt_info['purchase_date_pst'])
  end
  if latest_receipt_info['original_purchase_date']
    @original_purchase_date = DateTime.parse(latest_receipt_info['original_purchase_date'])
  end
  @original_purchase_date_ms = Time.zone.at(latest_receipt_info['original_purchase_date_ms'].to_i / 1000)
  if latest_receipt_info['original_purchase_date_pst']
    @original_purchase_date_pst = date_for_pacific_time(latest_receipt_info['original_purchase_date_pst'])
  end
  @web_order_line_item_id = latest_receipt_info['web_order_line_item_id']
  @quantity = latest_receipt_info['quantity'].to_i
  
  @unique_identifier = latest_receipt_info['unique_identifier']
  @unique_vendor_identifier = latest_receipt_info['unique_vendor_identifier']
  
  # Here we coerce the field names to match what the receipt verify response returns
  if latest_receipt_info['expires_date_formatted']
    @expires_date = DateTime.parse(latest_receipt_info['expires_date_formatted'])
  end
  if latest_receipt_info['expires_date']
    @expires_date_ms = if latest_receipt_info['expires_date'].size == 13
                         Time.zone.at(latest_receipt_info['expires_date'].to_i / 1000)
                       else
                         DateTime.parse(latest_receipt_info['expires_date'])
                       end
  end
  if latest_receipt_info['expires_date_formatted_pst']
    @expires_date_pst = date_for_pacific_time(latest_receipt_info['expires_date_formatted_pst'] || latest_receipt_info['expires_date_pst'])
  end
  if latest_receipt_info['is_in_intro_offer_period']
    @is_in_intro_offer_period = latest_receipt_info['is_in_intro_offer_period'].to_bool
  end
  if latest_receipt_info['is_trial_period']
    @is_trial_period = latest_receipt_info['is_trial_period'].to_bool
  end
  if latest_receipt_info['cancellation_date']
    @cancellation_date = DateTime.parse(latest_receipt_info['cancellation_date'])
  end

  @latest_receipt_info = []
  case attributes.dig('unified_receipt', 'latest_receipt_info')
  when Array
    attributes.dig('unified_receipt', 'latest_receipt_info').each do |transaction_receipt_attributes|
      @latest_receipt_info << TransactionReceipt.new(transaction_receipt_attributes)
    end
  when Hash
    @latest_receipt_info << TransactionReceipt.new(attributes.dig('unified_receipt', 'latest_receipt_info'))
  end
  @renewal_info = []
  if attributes.dig('unified_receipt', 'pending_renewal_info')
    attributes.dig('unified_receipt', 'pending_renewal_info').each do |renewal_info_attributes|
      @renewal_info << RenewalInfo.new(renewal_info_attributes)
    end
  end
end

Instance Attribute Details

#auto_renew_product_idObject (readonly)

Returns the value of attribute auto_renew_product_id.



23
24
25
# File 'lib/monza/status_response.rb', line 23

def auto_renew_product_id
  @auto_renew_product_id
end

#auto_renew_statusObject (readonly)

Returns the value of attribute auto_renew_status.



24
25
26
# File 'lib/monza/status_response.rb', line 24

def auto_renew_status
  @auto_renew_status
end

#auto_renew_status_change_dateObject (readonly)

Returns the value of attribute auto_renew_status_change_date.



25
26
27
# File 'lib/monza/status_response.rb', line 25

def auto_renew_status_change_date
  @auto_renew_status_change_date
end

#auto_renew_status_change_date_msObject (readonly)

Returns the value of attribute auto_renew_status_change_date_ms.



26
27
28
# File 'lib/monza/status_response.rb', line 26

def auto_renew_status_change_date_ms
  @auto_renew_status_change_date_ms
end

#auto_renew_status_change_date_pstObject (readonly)

Returns the value of attribute auto_renew_status_change_date_pst.



27
28
29
# File 'lib/monza/status_response.rb', line 27

def auto_renew_status_change_date_pst
  @auto_renew_status_change_date_pst
end

#bundle_idObject (readonly)

Returns the value of attribute bundle_id.



38
39
40
# File 'lib/monza/status_response.rb', line 38

def bundle_id
  @bundle_id
end

#bvrsObject (readonly)

Returns the value of attribute bvrs.



39
40
41
# File 'lib/monza/status_response.rb', line 39

def bvrs
  @bvrs
end

#cancellation_dateObject (readonly)

Returns the value of attribute cancellation_date.



59
60
61
# File 'lib/monza/status_response.rb', line 59

def cancellation_date
  @cancellation_date
end

#environmentObject (readonly)

Returns the value of attribute environment.



28
29
30
# File 'lib/monza/status_response.rb', line 28

def environment
  @environment
end

#expiration_intentObject (readonly)

Returns the value of attribute expiration_intent.



29
30
31
# File 'lib/monza/status_response.rb', line 29

def expiration_intent
  @expiration_intent
end

#expires_dateObject (readonly)

Returns the value of attribute expires_date.



54
55
56
# File 'lib/monza/status_response.rb', line 54

def expires_date
  @expires_date
end

#expires_date_msObject (readonly)

Returns the value of attribute expires_date_ms.



55
56
57
# File 'lib/monza/status_response.rb', line 55

def expires_date_ms
  @expires_date_ms
end

#expires_date_pstObject (readonly)

Returns the value of attribute expires_date_pst.



56
57
58
# File 'lib/monza/status_response.rb', line 56

def expires_date_pst
  @expires_date_pst
end

#is_in_intro_offer_periodObject (readonly)

Returns the value of attribute is_in_intro_offer_period.



57
58
59
# File 'lib/monza/status_response.rb', line 57

def is_in_intro_offer_period
  @is_in_intro_offer_period
end

#is_trial_periodObject (readonly)

Returns the value of attribute is_trial_period.



58
59
60
# File 'lib/monza/status_response.rb', line 58

def is_trial_period
  @is_trial_period
end

#item_idObject (readonly)

Returns the value of attribute item_id.



40
41
42
# File 'lib/monza/status_response.rb', line 40

def item_id
  @item_id
end

#latest_receiptObject (readonly)

Returns the value of attribute latest_receipt.



31
32
33
# File 'lib/monza/status_response.rb', line 31

def latest_receipt
  @latest_receipt
end

#latest_receipt_infoObject (readonly)

Returns the value of attribute latest_receipt_info.



36
37
38
# File 'lib/monza/status_response.rb', line 36

def latest_receipt_info
  @latest_receipt_info
end

#notification_typeObject (readonly)

Returns the value of attribute notification_type.



33
34
35
# File 'lib/monza/status_response.rb', line 33

def notification_type
  @notification_type
end

#original_json_responseObject (readonly)

Returns the value of attribute original_json_response.



61
62
63
# File 'lib/monza/status_response.rb', line 61

def original_json_response
  @original_json_response
end

#original_purchase_dateObject (readonly)

Returns the value of attribute original_purchase_date.



47
48
49
# File 'lib/monza/status_response.rb', line 47

def original_purchase_date
  @original_purchase_date
end

#original_purchase_date_msObject (readonly)

Returns the value of attribute original_purchase_date_ms.



48
49
50
# File 'lib/monza/status_response.rb', line 48

def original_purchase_date_ms
  @original_purchase_date_ms
end

#original_purchase_date_pstObject (readonly)

Returns the value of attribute original_purchase_date_pst.



49
50
51
# File 'lib/monza/status_response.rb', line 49

def original_purchase_date_pst
  @original_purchase_date_pst
end

#original_transaction_idObject (readonly)

Returns the value of attribute original_transaction_id.



43
44
45
# File 'lib/monza/status_response.rb', line 43

def original_transaction_id
  @original_transaction_id
end

#passwordObject (readonly)

Returns the value of attribute password.



34
35
36
# File 'lib/monza/status_response.rb', line 34

def password
  @password
end

#product_idObject (readonly)

Returns the value of attribute product_id.



41
42
43
# File 'lib/monza/status_response.rb', line 41

def product_id
  @product_id
end

#purchase_dateObject (readonly)

Returns the value of attribute purchase_date.



44
45
46
# File 'lib/monza/status_response.rb', line 44

def purchase_date
  @purchase_date
end

#purchase_date_msObject (readonly)

Returns the value of attribute purchase_date_ms.



45
46
47
# File 'lib/monza/status_response.rb', line 45

def purchase_date_ms
  @purchase_date_ms
end

#purchase_date_pstObject (readonly)

Returns the value of attribute purchase_date_pst.



46
47
48
# File 'lib/monza/status_response.rb', line 46

def purchase_date_pst
  @purchase_date_pst
end

#quantityObject (readonly)

Returns the value of attribute quantity.



51
52
53
# File 'lib/monza/status_response.rb', line 51

def quantity
  @quantity
end

#renewal_infoObject (readonly)

Returns the value of attribute renewal_info.



37
38
39
# File 'lib/monza/status_response.rb', line 37

def renewal_info
  @renewal_info
end

#transaction_idObject (readonly)

Returns the value of attribute transaction_id.



42
43
44
# File 'lib/monza/status_response.rb', line 42

def transaction_id
  @transaction_id
end

#unique_identifierObject (readonly)

Returns the value of attribute unique_identifier.



52
53
54
# File 'lib/monza/status_response.rb', line 52

def unique_identifier
  @unique_identifier
end

#unique_vendor_identifierObject (readonly)

Returns the value of attribute unique_vendor_identifier.



53
54
55
# File 'lib/monza/status_response.rb', line 53

def unique_vendor_identifier
  @unique_vendor_identifier
end

#web_order_line_item_idObject (readonly)

Returns the value of attribute web_order_line_item_id.



50
51
52
# File 'lib/monza/status_response.rb', line 50

def web_order_line_item_id
  @web_order_line_item_id
end

Instance Method Details

#cancel?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/monza/status_response.rb', line 173

def cancel?
  notification_type == Type::CANCEL
end

#date_for_pacific_time(pt) ⇒ Object



167
168
169
170
171
# File 'lib/monza/status_response.rb', line 167

def date_for_pacific_time pt
  # The field is labelled "PST" by apple, but the "America/Los_Angelus" time zone is actually Pacific Time, 
  # which is different, because it observes DST.
  ActiveSupport::TimeZone["Pacific Time (US & Canada)"].parse(pt).to_datetime
end

#did_change_renewal_pref?Boolean

Returns:

  • (Boolean)


177
178
179
# File 'lib/monza/status_response.rb', line 177

def did_change_renewal_pref?
  notification_type == Type::DID_CHANGE_RENEWAL_PREF
end

#did_change_renewal_status?Boolean

Returns:

  • (Boolean)


181
182
183
# File 'lib/monza/status_response.rb', line 181

def did_change_renewal_status?
  notification_type == Type::DID_CHANGE_RENEWAL_STATUS
end

#did_fail_to_renew?Boolean

Returns:

  • (Boolean)


185
186
187
# File 'lib/monza/status_response.rb', line 185

def did_fail_to_renew?
  notification_type == Type::DID_FAIL_TO_RENEW
end

#did_recover?Boolean

Returns:

  • (Boolean)


189
190
191
# File 'lib/monza/status_response.rb', line 189

def did_recover?
  notification_type == Type::DID_RECOVER
end

#initial_buy?Boolean

Returns:

  • (Boolean)


193
194
195
# File 'lib/monza/status_response.rb', line 193

def initial_buy?
  notification_type == Type::INITIAL_BUY
end

#interactive_renewal?Boolean

Returns:

  • (Boolean)


197
198
199
# File 'lib/monza/status_response.rb', line 197

def interactive_renewal?
  notification_type == Type::INTERACTIVE_RENEWAL
end

#refund?Boolean

Returns:

  • (Boolean)


205
206
207
# File 'lib/monza/status_response.rb', line 205

def refund?
  notification_type == Type::REFUND
end

#renewal?Boolean

Returns:

  • (Boolean)


201
202
203
# File 'lib/monza/status_response.rb', line 201

def renewal?
  notification_type == Type::RENEWAL
end