forked from linode/linodego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccount_payments.go
115 lines (97 loc) · 2.74 KB
/
account_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
105
106
107
108
109
110
111
112
113
114
115
package linodego
import (
"context"
"encoding/json"
"fmt"
"time"
)
// Payment represents a Payment object
type Payment struct {
// The unique ID of the Payment
ID int `json:"id"`
// The amount, in US dollars, of the Payment.
USD json.Number `json:"usd,Number"`
// When the Payment was made.
DateStr string `json:"date"`
Date *time.Time `json:"-"`
}
// PaymentCreateOptions fields are those accepted by CreatePayment
type PaymentCreateOptions struct {
// CVV (Card Verification Value) of the credit card to be used for the Payment
CVV string `json:"cvv,omitempty"`
// The amount, in US dollars, of the Payment
USD json.Number `json:"usd,Number"`
}
// GetCreateOptions converts a Payment to PaymentCreateOptions for use in CreatePayment
func (i Payment) GetCreateOptions() (o PaymentCreateOptions) {
o.USD = i.USD
return
}
// PaymentsPagedResponse represents a paginated Payment API response
type PaymentsPagedResponse struct {
*PageOptions
Data []Payment `json:"data"`
}
// endpoint gets the endpoint URL for Payment
func (PaymentsPagedResponse) endpoint(c *Client) string {
endpoint, err := c.Payments.Endpoint()
if err != nil {
panic(err)
}
return endpoint
}
// appendData appends Payments when processing paginated Payment responses
func (resp *PaymentsPagedResponse) appendData(r *PaymentsPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
}
// ListPayments lists Payments
func (c *Client) ListPayments(ctx context.Context, opts *ListOptions) ([]Payment, error) {
response := PaymentsPagedResponse{}
err := c.listHelper(ctx, &response, opts)
for i := range response.Data {
response.Data[i].fixDates()
}
if err != nil {
return nil, err
}
return response.Data, nil
}
// fixDates converts JSON timestamps to Go time.Time values
func (i *Payment) fixDates() *Payment {
i.Date, _ = parseDates(i.DateStr)
return i
}
// GetPayment gets the payment with the provided ID
func (c *Client) GetPayment(ctx context.Context, id int) (*Payment, error) {
e, err := c.Payments.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, id)
r, err := coupleAPIErrors(c.R(ctx).SetResult(&Payment{}).Get(e))
if err != nil {
return nil, err
}
return r.Result().(*Payment).fixDates(), nil
}
// CreatePayment creates a Payment
func (c *Client) CreatePayment(ctx context.Context, createOpts PaymentCreateOptions) (*Payment, error) {
var body string
e, err := c.Payments.Endpoint()
if err != nil {
return nil, err
}
req := c.R(ctx).SetResult(&Payment{})
if bodyData, err := json.Marshal(createOpts); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
r, err := coupleAPIErrors(req.
SetBody(body).
Post(e))
if err != nil {
return nil, err
}
return r.Result().(*Payment).fixDates(), nil
}