Class: CandyCheck::PlayStore::SubscriptionPurchases::SubscriptionPurchase

Inherits:
Object
  • Object
show all
Includes:
Utils::AttributeReader
Defined in:
lib/candy_check/play_store/subscription_purchases/subscription_purchase.rb

Overview

Describes a successfully validated subscription

Constant Summary collapse

PAYMENT_PENDING =

The payment of the subscription is pending (paymentState)

0
PAYMENT_RECEIVED =

The payment of the subscript is received (paymentState)

1
PAYMENT_CANCELED =

The subscription was canceled by the user (cancelReason)

0
PAYMENT_FAILED =

The payment failed during processing (cancelReason)

1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subscription_purchase) ⇒ SubscriptionPurchase

Initializes a new instance which bases on a JSON result from Google’s servers

Parameters:

  • subscription_purchase (Google::Apis::AndroidpublisherV3::SubscriptionPurchase)


23
24
25
# File 'lib/candy_check/play_store/subscription_purchases/subscription_purchase.rb', line 23

def initialize(subscription_purchase)
  @subscription_purchase = subscription_purchase
end

Instance Attribute Details

#subscription_purchaseGoogle::Apis::AndroidpublisherV3::SubscriptionPurchase (readonly)

Returns the raw subscription purchase from google-api-client.

Returns:

  • (Google::Apis::AndroidpublisherV3::SubscriptionPurchase)

    the raw subscription purchase from google-api-client



9
10
11
# File 'lib/candy_check/play_store/subscription_purchases/subscription_purchase.rb', line 9

def subscription_purchase
  @subscription_purchase
end

Instance Method Details

#auto_renewing?bool

Get the auto renewal status as given by Google

Returns:

  • (bool)

    true if renewing automatically, false otherwise



74
75
76
# File 'lib/candy_check/play_store/subscription_purchases/subscription_purchase.rb', line 74

def auto_renewing?
  @subscription_purchase.auto_renewing
end

#cancel_reasonInteger

Get the cancel reason, as given by Google

Returns:

  • (Integer)


93
94
95
# File 'lib/candy_check/play_store/subscription_purchases/subscription_purchase.rb', line 93

def cancel_reason
  @subscription_purchase.cancel_reason
end

#canceled_atDateTime

Get cancellation time in UTC

Returns:

  • (DateTime)


148
149
150
# File 'lib/candy_check/play_store/subscription_purchases/subscription_purchase.rb', line 148

def canceled_at
  Time.at(user_cancellation_time_millis / 1000).utc.to_datetime if user_cancellation_time_millis
end

#canceled_by_user?bool

see if this the user has canceled its subscription

Returns:

  • (bool)


62
63
64
# File 'lib/candy_check/play_store/subscription_purchases/subscription_purchase.rb', line 62

def canceled_by_user?
  cancel_reason == PAYMENT_CANCELED
end

#developer_payloadString

Get developer-specified supplemental information about the order

Returns:

  • (String)


105
106
107
# File 'lib/candy_check/play_store/subscription_purchases/subscription_purchase.rb', line 105

def developer_payload
  @subscription_purchase.developer_payload
end

#expired?bool

Check if the expiration date is passed

Returns:

  • (bool)


29
30
31
# File 'lib/candy_check/play_store/subscription_purchases/subscription_purchase.rb', line 29

def expired?
  overdue_days > 0
end

#expires_atDateTime

Get expiration time in UTC

Returns:

  • (DateTime)


142
143
144
# File 'lib/candy_check/play_store/subscription_purchases/subscription_purchase.rb', line 142

def expires_at
  Time.at(expiry_time_millis / 1000).utc.to_datetime
end

#expiry_time_millisInteger

Get expiry time for subscription in milliseconds since Epoch

Returns:

  • (Integer)


123
124
125
# File 'lib/candy_check/play_store/subscription_purchases/subscription_purchase.rb', line 123

def expiry_time_millis
  @subscription_purchase.expiry_time_millis
end

#kindString

Get the kind of subscription as stored in the android publisher service

Returns:

  • (String)


99
100
101
# File 'lib/candy_check/play_store/subscription_purchases/subscription_purchase.rb', line 99

def kind
  @subscription_purchase.kind
end

#overdue_daysInteger

Get number of overdue days. If this is negative, it is not overdue.

Returns:

  • (Integer)


68
69
70
# File 'lib/candy_check/play_store/subscription_purchases/subscription_purchase.rb', line 68

def overdue_days
  (Time.now.utc.to_date - expires_at.to_date).to_i
end

#payment_failed?bool

see if payment has failed according to Google

Returns:

  • (bool)


56
57
58
# File 'lib/candy_check/play_store/subscription_purchases/subscription_purchase.rb', line 56

def payment_failed?
  cancel_reason == PAYMENT_FAILED
end

#payment_pending?bool

see if payment is pending

Returns:

  • (bool)


50
51
52
# File 'lib/candy_check/play_store/subscription_purchases/subscription_purchase.rb', line 50

def payment_pending?
  payment_state == PAYMENT_PENDING
end

#payment_received?bool

see if payment is ok

Returns:

  • (bool)


44
45
46
# File 'lib/candy_check/play_store/subscription_purchases/subscription_purchase.rb', line 44

def payment_received?
  payment_state == PAYMENT_RECEIVED
end

#payment_stateInteger

Get the payment state as given by Google

Returns:

  • (Integer)


80
81
82
# File 'lib/candy_check/play_store/subscription_purchases/subscription_purchase.rb', line 80

def payment_state
  @subscription_purchase.payment_state
end

#price_amount_microsInteger

Get the price amount for the subscription in micros in the payed currency

Returns:

  • (Integer)


87
88
89
# File 'lib/candy_check/play_store/subscription_purchases/subscription_purchase.rb', line 87

def price_amount_micros
  @subscription_purchase.price_amount_micros
end

#price_currency_codeString

Get the currency code in ISO 4217 format, e.g. “GBP” for British pounds

Returns:

  • (String)


111
112
113
# File 'lib/candy_check/play_store/subscription_purchases/subscription_purchase.rb', line 111

def price_currency_code
  @subscription_purchase.price_currency_code
end

#start_time_millisInteger

Get start time for subscription in milliseconds since Epoch

Returns:

  • (Integer)


117
118
119
# File 'lib/candy_check/play_store/subscription_purchases/subscription_purchase.rb', line 117

def start_time_millis
  @subscription_purchase.start_time_millis
end

#starts_atDateTime

Get start time in UTC

Returns:

  • (DateTime)


136
137
138
# File 'lib/candy_check/play_store/subscription_purchases/subscription_purchase.rb', line 136

def starts_at
  Time.at(start_time_millis / 1000).utc.to_datetime
end

#trial?bool

Check if in trial. This is actually not given by Google, but we assume that it is a trial going on if the paid amount is 0 and renewal is activated.

Returns:

  • (bool)


37
38
39
40
# File 'lib/candy_check/play_store/subscription_purchases/subscription_purchase.rb', line 37

def trial?
  price_is_zero = price_amount_micros == 0
  price_is_zero && payment_received?
end

#user_cancellation_time_millisInteger

Get cancellation time for subscription in milliseconds since Epoch. Only present if cancelReason is 0.

Returns:

  • (Integer)


130
131
132
# File 'lib/candy_check/play_store/subscription_purchases/subscription_purchase.rb', line 130

def user_cancellation_time_millis
  @subscription_purchase.user_cancellation_time_millis if canceled_by_user?
end