-
Notifications
You must be signed in to change notification settings - Fork 1
/
payment.go
140 lines (119 loc) · 4.74 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
package gocardless
import (
"encoding/json"
"fmt"
"net/url"
"log"
"net/http"
"os"
)
type PaymentService service
type Payment struct {
Amount int64 `json:"amount, omitempty"`
AmountRefunded int64 `json:"amount_refunded,omitempty"`
ChargeDate string `json:"charge_date,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
Description string `json:"description,omitempty"`
ID string `json:"id,omitempty"`
Reference string `json:"reference,omitempty"`
Status Status `json:"status,omitempty"`
Currency Currency `json:"currency,omitempty"`
Links map[string]string `json:"links,omitempty"`
Metadata map[string]string `json:"metadata,omitempty"`
}
type Currency struct {
Currency string `json:"currency,omitempty"`
}
type PaymentListRequest struct {
CreatedAt CreatedAt `json:"created_at,omitempty"`
Limit int `json:"limit,omitempty"`
Before string `json:"before,omitempty"`
After string `json:"after,omitempty"`
Creditor string `json:"creditor,omitempty"`
Customer string `json:"customer,omitempty"`
Status string `json:"status,omitempty"`
Currency string `json:"currency,omitempty"`
Mandate string `json:"mandate,omitempty"`
Subscription string `json:"subscription,omitempty"`
}
type PaymentList struct {
Meta ListMeta
Values []Payment `json:"data"`
}
type PaymentCreateRequest struct {
Metadata map[string]string `json:"metadata,omitempty"`
Reference string `json:"reference,omitempty"`
Scheme string `json:"scheme,omitempty"`
AccountNumber string `json:"account_number,omitempty"`
Links []string `json:"links,omitempty"`
}
type PaymentCancelRequest struct {
Identity string `json:"identity,omitempty"`
}
// Create creates a new payment
func (s *PaymentService) CreatePayment(paymentReq *PaymentCreateRequest) (*Payment, error) {
u := fmt.Sprintf("/payments")
payment := &Payment{}
err := s.client.Call("POST", u, paymentReq, payment)
return payment, err
}
// List returns a list of payments
func (s *PaymentService) ListPayments(req *PaymentListRequest) (*PaymentList, error) {
reqd, err := http.NewRequest("GET", "/payments", nil)
if err != nil {
log.Print(err)
os.Exit(1)
}
params := reqd.URL.Query()
if req.After != "" { params.Add("after", req.After) }
if req.Before != "" { params.Add("before", req.Before) }
if req.CreatedAt.Gt != "" { params.Add("created_at[gt]", req.CreatedAt.Gt) }
if req.CreatedAt.Gte != "" { params.Add("created_at[gte]", req.CreatedAt.Gte) }
if req.CreatedAt.Lt != "" { params.Add("created_at[lt]", req.CreatedAt.Lt) }
if req.CreatedAt.Lte != "" { params.Add("created_at[lte]", req.CreatedAt.Lte) }
if req.Limit > 0 { params.Add("limit", string(req.Limit)) }
if req.Mandate != "" { params.Add("mandate", req.Mandate) }
if req.Status != "" { params.Add("status", req.Status) }
if req.Currency != "" {params.Add("currency", req.Currency)}
if req.Customer != "" {params.Add("customer", req.Customer)}
if req.Creditor != "" {params.Add("creditor", req.Creditor)}
if req.Subscription != "" {params.Add("subscription", req.Subscription)}
reqd.URL.RawQuery = params.Encode()
path := reqd.URL.String()
payments := &PaymentList{}
err = s.client.Call("GET", path, nil, payments)
return payments, err
}
func (s *PaymentService) GetPayment(id string) (*Payment, error) {
u := fmt.Sprintf("/payments/%s", id)
payment := &Payment{}
err := s.client.Call("GET", u, nil, payment)
return payment, err
}
func (s *PaymentService) UpdatePayment(updatedPayment *Payment, metadata map[string]string) (*Payment, error) {
params := url.Values{}
metadataString, _ := json.Marshal(metadata)
params.Add("metadata", string(metadataString))
u := fmt.Sprintf("/payments/%s", updatedPayment.ID)
payment := &Payment{}
err := s.client.Call("PUT", u, params, payment)
return payment, err
}
func (s *PaymentService) CancelPayment(paymentToCancel *Payment, metadata map[string]string) (*Response, error) {
params := url.Values{}
metadataString, _ := json.Marshal(metadata)
params.Add("metadata", string(metadataString))
u := fmt.Sprintf("/payments/%s/actions/cancel", paymentToCancel.ID)
resp := &Response{}
err := s.client.Call("POST", u, params, resp)
return resp, err
}
func (s *MandateService) RetryPayment(payment *Payment, metadata map[string]string) (*Response, error) {
params := url.Values{}
metadataString, _ := json.Marshal(metadata)
params.Add("metadata", string(metadataString))
u := fmt.Sprintf("/payments/%s/actions/retry", payment.ID)
resp := &Response{}
err := s.client.Call("POST", u, params, resp)
return resp, err
}