Class: PurchaseKit::Events::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/purchasekit/events.rb

Overview

Represents a subscription event from Apple or Google.

Events are normalized by the PurchaseKit SaaS before being sent to your application, so you get a consistent interface regardless of which store the purchase came from.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type:, payload:) ⇒ Event

Returns a new instance of Event.



63
64
65
66
# File 'lib/purchasekit/events.rb', line 63

def initialize(type:, payload:)
  @type = type.to_sym
  @payload = payload.is_a?(Hash) ? payload.with_indifferent_access : payload
end

Instance Attribute Details

#payloadObject (readonly)

Returns the value of attribute payload.



61
62
63
# File 'lib/purchasekit/events.rb', line 61

def payload
  @payload
end

#typeObject (readonly)

Returns the value of attribute type.



61
62
63
# File 'lib/purchasekit/events.rb', line 61

def type
  @type
end

Instance Method Details

#current_period_endObject

When the current billing period ends



120
121
122
# File 'lib/purchasekit/events.rb', line 120

def current_period_end
  parse_time(payload[:current_period_end])
end

#current_period_startObject

When the current billing period started



115
116
117
# File 'lib/purchasekit/events.rb', line 115

def current_period_start
  parse_time(payload[:current_period_start])
end

#customer_idObject

The customer ID you passed when creating the purchase intent. Use this to look up the user in your database.



76
77
78
# File 'lib/purchasekit/events.rb', line 76

def customer_id
  payload[:customer_id]
end

#ends_atObject

When the subscription will end (for canceled subscriptions)



125
126
127
# File 'lib/purchasekit/events.rb', line 125

def ends_at
  parse_time(payload[:ends_at])
end

#event_idObject

Unique identifier for this event. Use for idempotency checks. Store processed event_ids to prevent duplicate processing.



70
71
72
# File 'lib/purchasekit/events.rb', line 70

def event_id
  payload[:event_id]
end

#google_base_plan_idObject

The Google Play base plan ID (e.g., “annual”, “monthly”). Only present when the purchase used Google’s umbrella subscription with multiple base plans. Nil for Apple and for flat Google products.



100
101
102
# File 'lib/purchasekit/events.rb', line 100

def google_base_plan_id
  payload[:google_base_plan_id]
end

#statusObject

Current status: “active”, “canceled”, “expired”, etc.



110
111
112
# File 'lib/purchasekit/events.rb', line 110

def status
  payload[:status]
end

#storeObject

Which store the purchase came from: “apple” or “google”



88
89
90
# File 'lib/purchasekit/events.rb', line 88

def store
  payload[:store]
end

#store_product_idObject

The store-specific product ID (e.g., “com.example.pro.annual”)



93
94
95
# File 'lib/purchasekit/events.rb', line 93

def store_product_id
  payload[:store_product_id]
end

#subscription_idObject

The unique subscription ID from the store. Apple: originalTransactionId Google: purchaseToken



83
84
85
# File 'lib/purchasekit/events.rb', line 83

def subscription_id
  payload[:subscription_id]
end

#subscription_nameObject

The subscription name you configured in PurchaseKit



105
106
107
# File 'lib/purchasekit/events.rb', line 105

def subscription_name
  payload[:subscription_name]
end

#success_pathObject

The success path you passed when creating the purchase intent. Use this for redirecting after purchase completion.



136
137
138
# File 'lib/purchasekit/events.rb', line 136

def success_path
  payload[:success_path]
end

#trial_ends_atObject

When the trial period ends (nil if no trial)



130
131
132
# File 'lib/purchasekit/events.rb', line 130

def trial_ends_at
  parse_time(payload[:trial_ends_at])
end