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
126 lines
2.7 KiB
Go
126 lines
2.7 KiB
Go
package invoice
|
|
|
|
import (
|
|
"github.com/hanzoai/commerce/datastore"
|
|
"github.com/hanzoai/commerce/models/mixin"
|
|
"github.com/hanzoai/commerce/models/types/client"
|
|
"github.com/hanzoai/commerce/models/types/currency"
|
|
"github.com/hanzoai/commerce/util/json"
|
|
"github.com/hanzoai/commerce/util/val"
|
|
"github.com/hanzoai/orm"
|
|
|
|
. "github.com/hanzoai/commerce/types"
|
|
)
|
|
|
|
func init() { orm.Register[Invoice]("payment") }
|
|
|
|
var IgnoreFieldMismatch = datastore.IgnoreFieldMismatch
|
|
|
|
type Status string
|
|
|
|
const (
|
|
Cancelled Status = "cancelled"
|
|
Credit = "credit"
|
|
Disputed = "disputed"
|
|
Failed = "failed"
|
|
Fraudulent = "fraudulent"
|
|
Paid = "paid"
|
|
Refunded = "refunded"
|
|
Unpaid = "unpaid"
|
|
)
|
|
|
|
type Type string
|
|
|
|
const (
|
|
Stripe Type = "stripe"
|
|
Affirm = "affirm"
|
|
PayPal = "paypal"
|
|
)
|
|
|
|
type Invoice struct {
|
|
mixin.Model[Invoice]
|
|
|
|
// Immutable buyer data from time of payment, may or may not be associated
|
|
// with a user.
|
|
Buyer Buyer `json:"buyer"`
|
|
|
|
Type Type `json:"type"`
|
|
|
|
// Order this is associated with
|
|
OrderId string `json:"orderId,omitempty"`
|
|
|
|
Currency currency.Type `json:"currency"`
|
|
|
|
CampaignId string `json:"campaignId"`
|
|
|
|
Amount currency.Cents `json:"amount"`
|
|
AmountRefunded currency.Cents `json:"amountRefunded"`
|
|
Fee currency.Cents `json:"fee"`
|
|
|
|
AmountTransferred currency.Cents `json:"-"`
|
|
CurrencyTransferred currency.Type `json:"-"`
|
|
|
|
Description string `json:"description"`
|
|
Status Status `json:"status"`
|
|
|
|
// Client's browser, associated info
|
|
Client client.Client `json:"client"`
|
|
|
|
// Whether this payment has been captured or not
|
|
Captured bool `json:"captured"`
|
|
|
|
// Stripe livemode
|
|
Live bool `json:"live"`
|
|
|
|
// Internal testing flag
|
|
Test bool `json:"-"`
|
|
|
|
Metadata Map `json:"metadata" datastore:"-"`
|
|
Metadata_ string `json:"-" datastore:"-"`
|
|
}
|
|
|
|
func (p *Invoice) Defaults() {
|
|
p.Status = Unpaid
|
|
p.Metadata = make(Map)
|
|
}
|
|
|
|
func (p *Invoice) Load(ps []datastore.Property) (err error) {
|
|
// Ensure we're initialized
|
|
p.Defaults()
|
|
|
|
// Load supported properties
|
|
if err = IgnoreFieldMismatch(datastore.LoadStruct(p, ps)); err != nil {
|
|
return err
|
|
}
|
|
|
|
// Deserialize from datastore
|
|
if len(p.Metadata_) > 0 {
|
|
err = json.DecodeBytes([]byte(p.Metadata_), &p.Metadata)
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
func (p *Invoice) Save() (ps []datastore.Property, err error) {
|
|
// Serialize unsupported properties
|
|
p.Metadata_ = string(json.EncodeBytes(&p.Metadata))
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Save properties
|
|
return datastore.SaveStruct(p)
|
|
}
|
|
|
|
func (p *Invoice) Validator() *val.Validator {
|
|
return val.New()
|
|
}
|
|
|
|
func New(db *datastore.Datastore) *Invoice {
|
|
p := new(Invoice)
|
|
p.Init(db)
|
|
p.Defaults()
|
|
return p
|
|
}
|