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)


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

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



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

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



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

def auto_renewing?
  @subscription_purchase.auto_renewing
end

#cancel_reasonInteger

Get the cancel reason, as given by Google

Returns:

  • (Integer)


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

def cancel_reason
  @subscription_purchase.cancel_reason
end

#canceled_atDateTime

Get cancellation time in UTC

Returns:

  • (DateTime)


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

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)


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

def canceled_by_user?
  cancel_reason == PAYMENT_CANCELED
end

#developer_payloadString

Get developer-specified supplemental information about the order

Returns:

  • (String)


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

def developer_payload
  @subscription_purchase.developer_payload
end

#expired?bool

Check if the expiration date is passed

Returns:

  • (bool)


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

def expired?
  overdue_days > 0
end

#expires_atDateTime

Get expiration time in UTC

Returns:

  • (DateTime)


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

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)


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

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)


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

def kind
  @subscription_purchase.kind
end

#overdue_daysInteger

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

Returns:

  • (Integer)


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

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)


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

def payment_failed?
  cancel_reason == PAYMENT_FAILED
end

#payment_pending?bool

see if payment is pending

Returns:

  • (bool)


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

def payment_pending?
  payment_state == PAYMENT_PENDING
end

#payment_received?bool

see if payment is ok

Returns:

  • (bool)


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

def payment_received?
  payment_state == PAYMENT_RECEIVED
end

#payment_stateInteger

Get the payment state as given by Google

Returns:

  • (Integer)


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

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)


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

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)


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

def price_currency_code
  @subscription_purchase.price_currency_code
end

#start_time_millisInteger

Get start time for subscription in milliseconds since Epoch

Returns:

  • (Integer)


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

def start_time_millis
  @subscription_purchase.start_time_millis
end

#starts_atDateTime

Get start time in UTC

Returns:

  • (DateTime)


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

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)


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

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)


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

def user_cancellation_time_millis
  @subscription_purchase.user_cancellation_time_millis if canceled_by_user?
end