-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevent.go
More file actions
67 lines (63 loc) · 2.25 KB
/
event.go
File metadata and controls
67 lines (63 loc) · 2.25 KB
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
package gintonic
import (
"encoding/json"
"errors"
"github.com/YianAndCode/gintonic/lmdata"
)
const (
Event_OrderCreated string = "order_created"
Event_OrderRefunded string = "order_refunded"
Event_SubscriptionCreated string = "subscription_created"
Event_SubscriptionUpdated string = "subscription_updated"
Event_SubscriptionCancelled string = "subscription_cancelled"
Event_SubscriptionResumed string = "subscription_resumed"
Event_SubscriptionExpired string = "subscription_expired"
Event_SubscriptionPaused string = "subscription_paused"
Event_SubscriptionUnpaused string = "subscription_unpaused"
Event_SubscriptionPaymentSuccess string = "subscription_payment_success"
Event_SubscriptionPaymentFailed string = "subscription_payment_failed"
Event_SubscriptionPaymentRecovered string = "subscription_payment_recovered"
Event_LicenseKeyCreated string = "license_key_created"
Event_LicenseKeyUpdated string = "license_key_updated"
)
func ParseEventPayload(eventName string, payload []byte) (any, error) {
switch eventName {
case Event_OrderCreated:
fallthrough
case Event_OrderRefunded:
payloadObj := lmdata.OrderPayload{}
err := json.Unmarshal(payload, &payloadObj)
return payloadObj, err
case Event_SubscriptionCreated:
fallthrough
case Event_SubscriptionUpdated:
fallthrough
case Event_SubscriptionCancelled:
fallthrough
case Event_SubscriptionResumed:
fallthrough
case Event_SubscriptionExpired:
fallthrough
case Event_SubscriptionPaused:
fallthrough
case Event_SubscriptionUnpaused:
payloadObj := lmdata.SubscriptionPayload{}
err := json.Unmarshal(payload, &payloadObj)
return payloadObj, err
case Event_SubscriptionPaymentSuccess:
fallthrough
case Event_SubscriptionPaymentFailed:
fallthrough
case Event_SubscriptionPaymentRecovered:
payloadObj := lmdata.SubscriptionInvoicePayload{}
err := json.Unmarshal(payload, &payloadObj)
return payloadObj, err
case Event_LicenseKeyCreated:
fallthrough
case Event_LicenseKeyUpdated:
payloadObj := lmdata.LicenseKeyPayload{}
err := json.Unmarshal(payload, &payloadObj)
return payloadObj, err
}
return nil, errors.New("invalid event:" + string(eventName))
}