-
Notifications
You must be signed in to change notification settings - Fork 6
/
event.go
101 lines (90 loc) · 3.92 KB
/
event.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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
package revcatgo
import (
"time"
"gopkg.in/guregu/null.v4"
)
// WebhookEvent represents a request body of RevenueCat webhook.
// https://docs.revenuecat.com/docs/webhooks
type WebhookEvent struct {
Event Event `json:"event"`
APIVersion string `json:"api_version"`
}
// Event represents an Event of RevenueCat webhook
type Event struct {
ID string `json:"id"`
Type eventType `json:"type"`
EventTimestampAt milliseconds `json:"event_timestamp_ms"`
AppUserID string `json:"app_user_id"`
Aliases []string `json:"aliases"`
OriginalAppUserID string `json:"original_app_user_id"`
ProductID string `json:"product_id"`
EntitlementIDs []string `json:"entitlement_ids"`
PeriodType periodType `json:"period_type"`
PurchasedAt milliseconds `json:"purchased_at_ms"`
GracePeriodExpirationAt milliseconds `json:"grace_period_expiration_at_ms"`
ExpirationAt milliseconds `json:"expiration_at_ms"`
AutoResumeAt milliseconds `json:"auto_resume_at_ms"`
Store store `json:"store"`
Environment environment `json:"environment"`
IsTrialConversion null.Bool `json:"is_trial_conversion"`
CancelReason cancelReason `json:"cancel_reason"`
ExpirationReason cancelReason `json:"expiration_reason"`
NewProductID string `json:"new_product_id"`
PresentedOfferingID string `json:"presented_offering_id"`
Price price `json:"price"`
Currency null.String `json:"currency"`
PriceInPurchasedCurrency float32 `json:"price_in_purchased_currency"`
TakeHomePercentage float32 `json:"takehome_percentage"`
CommissionPercentage float32 `json:"commission_percentage"`
SubscriberAttributes subscriberAttributes `json:"subscriber_attributes"`
TransactionID string `json:"transaction_id"`
OriginalTransactionID string `json:"original_transaction_id"`
IsFamilyShare bool `json:"is_family_share"`
TransferredFrom []string `json:"transferred_from"`
TransferredTo []string `json:"transferred_to"`
CountryCode string `json:"country_code"`
OfferCode string `json:"offer_code"`
}
// IsExpired checks whether a subscription is expired or not.
func (e *Event) IsExpired(grace time.Duration, base *time.Time) bool {
var b time.Time
if base == nil {
b = time.Now()
} else {
b = *base
}
return e.ExpirationAt.DateTime().Add(grace).Before(b.UTC())
}
// HasEntitlementID checks whether the id exists or not.
func (e *Event) HasEntitlementID(id string) bool {
return contains(e.EntitlementIDs, id)
}
// GetAllRelatedUserID returns a unique id list of AppUserID, OriginalAppUserID, and Aliases.
func (e *Event) GetAllRelatedUserID() []string {
m := make(map[string]bool)
var idList []string
if e.AppUserID != "" {
idList = append(idList, e.AppUserID)
}
if e.OriginalAppUserID != "" {
idList = append(idList, e.OriginalAppUserID)
}
if len(e.Aliases) > 0 {
idList = append(idList, e.Aliases...)
}
var uniqueIDList []string
for _, ele := range idList {
if !m[ele] {
m[ele] = true
uniqueIDList = append(uniqueIDList, ele)
}
}
return uniqueIDList
}
// SubscriberAttributes represents a map of SubscriberAttribute.
type subscriberAttributes map[string]subscriberAttribute
// SubscriberAttribute represents attributes of subscriber.
type subscriberAttribute struct {
Value string `json:"value"`
UpdatedAt milliseconds `json:"updated_at_ms"`
}