-
Notifications
You must be signed in to change notification settings - Fork 37
/
payment.go
170 lines (135 loc) · 4.16 KB
/
payment.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
package quickbooks
import (
"errors"
"strconv"
)
type Payment struct {
SyncToken string `json:",omitempty"`
Domain string `json:"domain,omitempty"`
DepositToAccountRef ReferenceType `json:",omitempty"`
UnappliedAmt float64 `json:",omitempty"`
TxnDate Date `json:",omitempty"`
TotalAmt float64 `json:",omitempty"`
ProcessPayment bool `json:",omitempty"`
Line []PaymentLine `json:",omitempty"`
CustomerRef ReferenceType `json:",omitempty"`
Id string `json:",omitempty"`
MetaData MetaData `json:",omitempty"`
}
type PaymentLine struct {
Amount float64 `json:",omitempty"`
LinkedTxn []LinkedTxn `json:",omitempty"`
}
// CreatePayment creates the given payment within QuickBooks.
func (c *Client) CreatePayment(payment *Payment) (*Payment, error) {
var resp struct {
Payment Payment
Time Date
}
if err := c.post("payment", payment, &resp, nil); err != nil {
return nil, err
}
return &resp.Payment, nil
}
// DeletePayment deletes the given payment from QuickBooks.
func (c *Client) DeletePayment(payment *Payment) error {
if payment.Id == "" || payment.SyncToken == "" {
return errors.New("missing id/sync token")
}
return c.post("payment", payment, nil, map[string]string{"operation": "delete"})
}
// FindPayments gets the full list of Payments in the QuickBooks account.
func (c *Client) FindPayments() ([]Payment, error) {
var resp struct {
QueryResponse struct {
Payments []Payment `json:"Payment"`
MaxResults int
StartPosition int
TotalCount int
}
}
if err := c.query("SELECT COUNT(*) FROM Payment", &resp); err != nil {
return nil, err
}
if resp.QueryResponse.TotalCount == 0 {
return nil, errors.New("no payments could be found")
}
payments := make([]Payment, 0, resp.QueryResponse.TotalCount)
for i := 0; i < resp.QueryResponse.TotalCount; i += queryPageSize {
query := "SELECT * FROM Payment ORDERBY Id STARTPOSITION " + strconv.Itoa(i+1) + " MAXRESULTS " + strconv.Itoa(queryPageSize)
if err := c.query(query, &resp); err != nil {
return nil, err
}
if resp.QueryResponse.Payments == nil {
return nil, errors.New("no payments could be found")
}
payments = append(payments, resp.QueryResponse.Payments...)
}
return payments, nil
}
// FindPaymentById returns an payment with a given Id.
func (c *Client) FindPaymentById(id string) (*Payment, error) {
var resp struct {
Payment Payment
Time Date
}
if err := c.get("payment/"+id, &resp, nil); err != nil {
return nil, err
}
return &resp.Payment, nil
}
// QueryPayments accepts a SQL query and returns all payments found using it.
func (c *Client) QueryPayments(query string) ([]Payment, error) {
var resp struct {
QueryResponse struct {
Payments []Payment `json:"Payment"`
StartPosition int
MaxResults int
}
}
if err := c.query(query, &resp); err != nil {
return nil, err
}
if resp.QueryResponse.Payments == nil {
return nil, errors.New("could not find any payments")
}
return resp.QueryResponse.Payments, nil
}
// UpdatePayment updates the given payment in QuickBooks.
func (c *Client) UpdatePayment(payment *Payment) (*Payment, error) {
if payment.Id == "" {
return nil, errors.New("missing payment id")
}
existingPayment, err := c.FindPaymentById(payment.Id)
if err != nil {
return nil, err
}
payment.SyncToken = existingPayment.SyncToken
payload := struct {
*Payment
Sparse bool `json:"sparse"`
}{
Payment: payment,
Sparse: true,
}
var paymentData struct {
Payment Payment
Time Date
}
if err = c.post("payment", payload, &paymentData, nil); err != nil {
return nil, err
}
return &paymentData.Payment, err
}
// VoidPayment voids the given payment in QuickBooks.
func (c *Client) VoidPayment(payment Payment) error {
if payment.Id == "" {
return errors.New("missing payment id")
}
existingPayment, err := c.FindPaymentById(payment.Id)
if err != nil {
return err
}
payment.SyncToken = existingPayment.SyncToken
return c.post("payment", payment, nil, map[string]string{"operation": "update", "include": "void"})
}