mirror of
https://github.com/hanzoai/commerce.git
synced 2026-10-30 15:08:42 +00:00
Multi-tenant commerce and billing: orders, subscriptions, invoices, plans, usage metering and a credit ledger. One Go binary. MIT OR Apache-2.0. Claude-Session: https://claude.ai/code/session_01SAtaDFm8VDiWrhYHNNtxMw
76 lines
1.6 KiB
Go
76 lines
1.6 KiB
Go
package webhook
|
|
|
|
import (
|
|
"github.com/hanzoai/commerce/datastore"
|
|
"github.com/hanzoai/commerce/models/mixin"
|
|
"github.com/hanzoai/commerce/util/json"
|
|
"github.com/hanzoai/orm"
|
|
)
|
|
|
|
type Events map[string]bool
|
|
|
|
func init() { orm.Register[Webhook]("webhook") }
|
|
|
|
type Webhook struct {
|
|
mixin.Model[Webhook]
|
|
|
|
// Name
|
|
Name string `json:"name"`
|
|
|
|
// Endpoint webhook should deliver events to.
|
|
Url string `json:"url"`
|
|
|
|
// Whether to use Live or Test data.
|
|
Live bool `json:"live"`
|
|
|
|
// Whether to send all events or selectively using Events.
|
|
All bool `json:"all"`
|
|
|
|
// Random token to check against
|
|
AccessToken string `json:"accessToken"`
|
|
|
|
// Events to selectively send.
|
|
Events Events `json:"events" datastore:"-" orm:"default:{}"`
|
|
Events_ string `json:"-" datastore:",noindex"`
|
|
|
|
// Whether this webhook is enabled or not.
|
|
Enabled bool `json:"enabled"`
|
|
}
|
|
|
|
func (s *Webhook) Load(ps []datastore.Property) (err error) {
|
|
// Ensure we're initialized
|
|
if s.Events == nil {
|
|
s.Events = make(Events)
|
|
}
|
|
|
|
// Load supported properties
|
|
if err = datastore.LoadStruct(s, ps); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Deserialize from datastore
|
|
if len(s.Events_) > 0 {
|
|
err = json.DecodeBytes([]byte(s.Events_), &s.Events)
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
func (s *Webhook) Save() (ps []datastore.Property, err error) {
|
|
// Serialize unsupported properties
|
|
s.Events_ = string(json.EncodeBytes(&s.Events))
|
|
|
|
// Save properties
|
|
return datastore.SaveStruct(s)
|
|
}
|
|
|
|
func New(db *datastore.Datastore) *Webhook {
|
|
w := new(Webhook)
|
|
w.Init(db)
|
|
return w
|
|
}
|
|
|
|
func Query(db *datastore.Datastore) datastore.Query {
|
|
return db.Query("webhook")
|
|
}
|