-
Notifications
You must be signed in to change notification settings - Fork 6
/
payments.go
104 lines (86 loc) · 3.19 KB
/
payments.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
package invoiced
import (
"encoding/json"
"strconv"
)
type PaymentRequest struct {
Amount *float64 `json:"amount,omitempty"`
AppliedTo []*PaymentItemRequest `json:"applied_to,omitempty"`
Currency *string `json:"currency,omitempty"`
Customer *int64 `json:"-"`
Date *int64 `json:"date,omitempty"`
Method *string `json:"method,omitempty"`
Notes *string `json:"notes,omitempty"`
Metadata map[string]interface{} `json:"metadata"`
Reference *string `json:"reference,omitempty"`
Source *string `json:"source,omitempty"`
Voided *bool `json:"voided,omitempty"`
}
type PaymentItemRequest struct {
Amount *float64 `json:"amount,omitempty"`
CreditNote *int64 `json:"credit_note,omitempty"`
DocumentType *string `json:"document_type,omitempty"`
Estimate *int64 `json:"estimate,omitempty"`
Invoice *int64 `json:"invoice,omitempty"`
Type *string `json:"type,omitempty"`
}
type Payment struct {
Amount float64 `json:"amount"`
AppliedTo []PaymentItem `json:"applied_to"`
Balance float64 `json:"balance"`
Charge *Charge `json:"charge"`
CreatedAt int64 `json:"created_at"`
Currency string `json:"currency"`
Customer int64 `json:"-"`
CustomerFull *Customer `json:"-"`
CustomerRaw json.RawMessage `json:"customer"`
Date int64 `json:"date"`
Id int64 `json:"id"`
Matched bool `json:"matched"`
Metadata map[string]interface{} `json:"metadata"`
Method string `json:"method"`
Notes string `json:"notes"`
Object string `json:"object"`
PdfUrl string `json:"pdf_url"`
Reference string `json:"reference"`
Source string `json:"source"`
Status string `json:"status"`
UpdatedAt int64 `json:"updated_at"`
Voided bool `json:"voided"`
}
type PaymentItem struct {
Amount float64 `json:"amount"`
CreditNote int64 `json:"credit_note"`
DocumentType string `json:"document_type"`
Estimate int64 `json:"estimate"`
Invoice int64 `json:"invoice"`
Type string `json:"type"`
}
type Payments []*Payment
func (i *Payment) UnmarshalJSON(data []byte) error {
type payment2 Payment
if err := json.Unmarshal(data, (*payment2)(i)); err != nil {
return err
}
rj := i.CustomerRaw
i.Customer, _ = strconv.ParseInt(string(rj), 10, 64)
customer := new(Customer)
err := json.Unmarshal(rj, customer)
if err == nil {
i.CustomerFull = customer
i.Customer = customer.Id
}
return nil
}
func (i *Payment) MarshalJSON() ([]byte, error) {
type payment2 Payment
i2 := (*payment2)(i)
if i2.Customer > 0 {
i2.CustomerRaw = []byte(strconv.FormatInt(i2.Customer, 10))
}
return json.Marshal(i2)
}
func (i *Payment) String() string {
b, _ := json.MarshalIndent(i, "", " ")
return string(b)
}