/
order.go
189 lines (152 loc) · 4.63 KB
/
order.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package models
import (
"encoding/json"
"fmt"
"time"
"github.com/jinzhu/gorm"
"github.com/netlify/gocommerce/calculator"
"github.com/pborman/uuid"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
// PendingState is the pending state of an Order
const PendingState = "pending"
// PaidState is the paid state of an Order
const PaidState = "paid"
// ShippedState is the shipped state of an Order
const ShippedState = "shipped"
// FailedState is the failed state of an Order
const FailedState = "failed"
// NumberType | StringType | BoolType are the different types supported in custom data for orders
const (
NumberType = iota
StringType
BoolType
)
// Order model
type Order struct {
InstanceID string `json:"-"`
ID string `json:"id"`
InvoiceNumber int64 `json:"invoice_number,omitempty"`
IP string `json:"ip"`
User *User `json:"user,omitempty"`
UserID string `json:"user_id,omitempty"`
SessionID string `json:"-"`
Email string `json:"email"`
LineItems []*LineItem `json:"line_items"`
Downloads []Download `json:"downloads"`
Currency string `json:"currency"`
Taxes uint64 `json:"taxes"`
Shipping uint64 `json:"shipping"`
SubTotal uint64 `json:"subtotal"`
Discount uint64 `json:"discount"`
Total uint64 `json:"total"`
PaymentState string `json:"payment_state"`
FulfillmentState string `json:"fulfillment_state"`
State string `json:"state"`
PaymentProcessor string `json:"payment_processor"`
Transactions []*Transaction `json:"transactions"`
Notes []*OrderNote `json:"notes"`
ShippingAddress Address `json:"shipping_address" gorm:"ForeignKey:ShippingAddressID"`
ShippingAddressID string `json:"shipping_address_id"`
BillingAddress Address `json:"billing_address" gorm:"ForeignKey:BillingAddressID"`
BillingAddressID string `json:"billing_address_id"`
VATNumber string `json:"vatnumber"`
MetaData map[string]interface{} `sql:"-" json:"meta"`
RawMetaData string `json:"-" sql:"type:text"`
CouponCode string `json:"coupon_code,omitempty"`
Coupon *Coupon `json:"coupon,omitempty" sql:"-"`
RawCoupon string `json:"-" sql:"type:text"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
DeletedAt *time.Time `json:"-" sql:"index:idx_orders_deleted_at"`
}
// TableName returns the database table name for the Order model.
func (Order) TableName() string {
return tableName("orders")
}
// AfterFind database callback.
func (o *Order) AfterFind() error {
if o.RawMetaData != "" {
err := json.Unmarshal([]byte(o.RawMetaData), &o.MetaData)
if err != nil {
return err
}
}
if o.RawCoupon != "" {
o.Coupon = &Coupon{}
err := json.Unmarshal([]byte(o.RawCoupon), &o.Coupon)
if err != nil {
return err
}
}
return nil
}
// BeforeSave database callback.
func (o *Order) BeforeSave() error {
if o.MetaData != nil {
data, err := json.Marshal(o.MetaData)
if err != nil {
return err
}
o.RawMetaData = string(data)
}
if o.Coupon != nil {
data, err := json.Marshal(o.Coupon)
if err != nil {
return err
}
o.RawCoupon = string(data)
}
return nil
}
// NewOrder creates a new pending Order.
func NewOrder(instanceID, sessionID, email, currency string) *Order {
order := &Order{
InstanceID: instanceID,
ID: uuid.NewRandom().String(),
SessionID: sessionID,
Email: email,
Currency: currency,
}
order.PaymentState = PendingState
order.FulfillmentState = PendingState
order.State = PendingState
return order
}
// CalculateTotal calculates the total price of an Order.
func (o *Order) CalculateTotal(settings *calculator.Settings, claims map[string]interface{}, log logrus.FieldLogger) {
items := make([]calculator.Item, len(o.LineItems))
for i, item := range o.LineItems {
items[i] = item
}
params := calculator.PriceParameters{o.ShippingAddress.Country, o.Currency, o.Coupon, items}
price := calculator.CalculatePrice(settings, claims, params, log)
o.SubTotal = price.Subtotal
o.Taxes = price.Taxes
o.Discount = price.Discount
if price.Total > 0 {
o.Total = uint64(price.Total)
}
}
func (o *Order) BeforeDelete(tx *gorm.DB) error {
cascadeModels := map[string]interface{}{
"line item": &[]LineItem{},
}
for name, cm := range cascadeModels {
if err := cascadeDelete(tx, "order_id = ?", o.ID, name, cm); err != nil {
return err
}
}
delModels := map[string]interface{}{
"event": Event{},
"transaction": Transaction{},
"download": Download{},
}
for name, dm := range delModels {
if result := tx.Delete(dm, "order_id = ?", o.ID); result.Error != nil {
return errors.Wrap(result.Error, fmt.Sprintf("Error deleting %s records", name))
}
}
return nil
}