-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbilling.go
220 lines (174 loc) · 5.89 KB
/
billing.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package whmcsgo
import (
"encoding/json"
"errors"
"fmt"
)
// BillingService handles communication with the billableitem related
// methods of the WHMCS API.
//
// WHMCS API docs: http://docs.whmcs.com/API
type BillingService struct {
client *Client
}
// BillingItem represents a
type BillingItem struct {
ClientID *string `json:"clientid"`
Description *string `json:"description"`
Hours *string `json:"hours"`
Amount *string `json:"amount"`
InvoiceAction *string `json:"invoiceaction"`
}
func (r BillingItem) String() string {
return Stringify(r)
}
// AddBillableItem a new billable item.
//
// WHMCS API docs: https://developers.whmcs.com/api-reference/addbillableitem/
func (s *BillingService) AddBillableItem(parms map[string]string) (*BillingItem, *Response, error) {
a := new(BillingItem)
resp, err := do(s.client, Params{parms: parms, u: "AddBillableItem"}, a)
if err != nil {
return nil, resp, err
}
return a, resp, err
}
// InvoicesReply object from WHMCS
type InvoicesReply struct {
Invoices struct {
Invoice []Invoice `json:"invoice"`
} `json:"invoices"`
Numreturned int `json:"numreturned"`
Result string `json:"result"`
Startnumber int `json:"startnumber"`
Totalresults int `json:"totalresults"`
}
// Invoices from WHCMS
type Invoices struct {
Invoice []Invoice `json:"invoice"`
}
// Invoice from WHCMS
type Invoice struct {
Companyname string `json:"companyname"`
UserID int `json:"userid"`
CreatedAt WHCMSdate `json:"created_at"`
Credit string `json:"credit"`
Currencycode string `json:"currencycode"`
Currencyprefix string `json:"currencyprefix"`
Currencysuffix string `json:"currencysuffix"`
Date WHCMSdate `json:"date"`
DateCancelled WHCMSdate `json:"date_cancelled"`
DateRefunded WHCMSdate `json:"date_refunded"`
Datepaid WHCMSdate `json:"datepaid"`
Duedate WHCMSdate `json:"duedate"`
Firstname string `json:"firstname"`
ID int `json:"id"`
Invoicenum string `json:"invoicenum"`
LastCaptureAttempt WHCMSdate `json:"last_capture_attempt"`
Lastname string `json:"lastname"`
Notes string `json:"notes"`
Paymentmethod string `json:"paymentmethod"`
Paymethodid interface{} `json:"paymethodid"`
Status string `json:"status"`
Subtotal string `json:"subtotal"`
Tax string `json:"tax"`
Tax2 string `json:"tax2"`
Taxrate string `json:"taxrate"`
Taxrate2 string `json:"taxrate2"`
Total string `json:"total"`
UpdatedAt WHCMSdate `json:"updated_at"`
}
func (i Invoices) String() string {
return Stringify(i)
}
/*
GetLastInvoice retrieve the task invoice for a given status
Request Parameters
userid
Find invoices for a specific client id - Requited
status
Find invoices for a specific status. Standard Invoice statuses plus Overdue - Requited
*/
func (s *BillingService) GetLastInvoice(userid int, status string) (Invoice, error) {
invoices := new(InvoicesReply)
uid := fmt.Sprintf("%d", userid)
parms := map[string]string{"status": status, "userid": uid, "limitnum": "1", "orderby": "date", "order": "desc"}
resp, err := do(s.client, Params{u: "GetInvoices", parms: parms}, invoices)
if err != nil {
return Invoice{}, err
}
err = json.Unmarshal([]byte(resp.Body), &invoices)
if err != nil {
return Invoice{}, fmt.Errorf("Can't unmarshal invoices: %w", err)
}
if invoices.Numreturned > 0 {
return invoices.Invoices.Invoice[0], nil
}
return Invoice{}, errors.New("No invoice found")
// return invoices.Invoices.Invoice[0], err
}
/*
GetInvoices retrieve a list of invoices.
WHMCS API docs
https://developers.whmcs.com/api-reference/getinvoices/
Request Parameters
limitstart
The offset for the returned invoice data (default: 0) - Optional
limitnum
The number of records to return (default: 25) - Optional
userid
Find invoices for a specific client id - Optional
status
Find invoices for a specific status. Standard Invoice statuses plus Overdue - Optional
orderby
The field to sort results by. Accepted values are: id, invoicenumber, date, duedate, total, status - Optional
order
Order sort attribute. Accepted values are: asc or desc - Optional
*/
func (s *BillingService) GetInvoices(parms map[string]string) ([]Invoice, *Response, error) {
invoices := new(InvoicesReply)
resp, err := do(s.client, Params{parms: parms, u: "GetInvoices"}, invoices)
if err != nil {
return nil, resp, err
}
err = json.Unmarshal([]byte(resp.Body), &invoices)
if err != nil {
return nil, nil, fmt.Errorf("unable to unmarshal invoices: %w", err)
}
var r []Invoice
for _, i := range invoices.Invoices.Invoice {
r = append(r, i)
}
return r, resp, err
}
// CaptureResult from a payment capture attempt
// Possible error condition responses include:
// Invoice Not Found or Not Unpaid
// Payment Attempt Failed
type CaptureResult struct {
Result string `json:"result"`
Message string `json:"message"`
}
/*
CapturePayment attempt to capture a payment on an unpaid Invoice
WHMCS API docs: https://developers.whmcs.com/api-reference/capturepayment/
Request Parameters
invoiceid
The ID of the pending order Required
cvv
The CVV Number for the card being attempted Optional
*/
func (s *BillingService) CapturePayment(invoice int) (*CaptureResult, *Response, error) {
if invoice < 1 {
return nil, nil, errors.New("Invoice ID required to attempt payment capture")
}
result := new(CaptureResult)
invoiceid := fmt.Sprintf("%d", invoice)
parms := map[string]string{"invoiceid": invoiceid}
resp, err := do(s.client, Params{parms: parms, u: "CapturePayment"}, result)
if err != nil {
return nil, resp, err
}
err = json.Unmarshal([]byte(resp.Body), &result)
return result, resp, err
}