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



113
114
115
# File 'lib/purchasekit/events.rb', line 113

def current_period_end
  parse_time(payload[:current_period_end])
end

#current_period_startObject

When the current billing period started



108
109
110
# File 'lib/purchasekit/events.rb', line 108

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)



118
119
120
# File 'lib/purchasekit/events.rb', line 118

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

#statusObject

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



103
104
105
# File 'lib/purchasekit/events.rb', line 103

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



98
99
100
# File 'lib/purchasekit/events.rb', line 98

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.



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

def success_path
  payload[:success_path]
end

#trial_ends_atObject

When the trial period ends (nil if no trial)



123
124
125
# File 'lib/purchasekit/events.rb', line 123

def trial_ends_at
  parse_time(payload[:trial_ends_at])
end