-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpayment.go
175 lines (156 loc) · 5.18 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
171
172
173
174
175
package gocardless
import (
"encoding/json"
"fmt"
"time"
)
const (
paymentEndpoint = "payments"
)
type (
// Payment objects represent payments from a customer to a creditor, taken against a Direct Debit payment.
Payment struct {
// ID is a unique identifier, beginning with "PM".
ID string `json:"id,omitempty"`
// Amount in pence (GBP), cents (AUD/EUR), öre (SEK), or øre (DKK).
// e.g 1000 is 10 GBP in pence
Amount int `json:"amount"`
// AmountRefunded is amount refunded in pence/cents/öre/øre.
AmountRefunded int `json:"amount_refunded,omitempty"`
// ChargeDate A future date on which the payment should be collected.
// If not specified, the payment will be collected as soon as possible
ChargeDate *Date `json:"charge_date,omitempty"`
// CreatedAt is a fixed timestamp, recording when the payment was created.
CreatedAt *time.Time `json:"created_at,omitempty"`
// Currency currency code, defaults to national currency of country_code
Currency string `json:"currency"`
// Description A human-readable description of the payment
Description string `json:"description,omitempty"`
// Metadata is a key-value store of custom data. Up to 3 keys are permitted, with key names up to 50
// characters and values up to 500 characters.
Metadata map[string]string `json:"metadata,omitempty"`
// Reference An optional payment reference that will appear on your customer’s bank statement
Reference string `json:"reference,omitempty"`
// Status status of payment.
Status string `json:"status,omitempty"`
// Links to cusomer and payment
Links paymentLinks `json:"links"`
// AppFee The amount to be deducted from the payment as the OAuth app’s fee, in pence/cents/öre/øre
AppFee int `json:"app_fee,omitempty"`
}
paymentLinks struct {
CreditorID string `json:"creditor,omitempty"`
PayoutID string `json:"payout,omitempty"`
SubscriptionID string `json:"subscription,omitempty"`
MandateID string `json:"mandate,omitempty"`
}
// paymentWrapper is a utility struct used to wrap and unwrap the JSON request being passed to the remote API
paymentWrapper struct {
Payment *Payment `json:"payments"`
}
// PaymentListResponse a List response of Payment instances
PaymentListResponse struct {
Payments []*Payment `json:"payments"`
Meta Meta `json:"meta,omitempty"`
}
)
func (p *Payment) String() string {
bs, _ := json.Marshal(p)
return string(bs)
}
// NewPayment instantiate new payment object
func NewPayment(amount int, currency, mandateID string) *Payment {
return &Payment{
Amount: amount,
Currency: currency,
Links: paymentLinks{MandateID: mandateID},
}
}
// AddMetadata adds new metadata item to payment object
func (p *Payment) AddMetadata(key, value string) {
p.Metadata[key] = value
}
// CreatePayment creates a new payment object.
//
// Relative endpoint: POST /payments
func (c *Client) CreatePayment(payment *Payment) error {
paymentReq := &paymentWrapper{payment}
err := c.post(paymentEndpoint, paymentReq, paymentReq)
if err != nil {
return err
}
return err
}
// GetPayments returns a cursor-paginated list of your payments.
//
// Relative endpoint: GET /payments
func (c *Client) GetPayments() (*PaymentListResponse, error) {
list := &PaymentListResponse{}
err := c.get(paymentEndpoint, list)
if err != nil {
return nil, err
}
return list, err
}
// GetPayment retrieves the details of an existing payment.
//
// Relative endpoint: GET /payments/PM123
func (c *Client) GetPayment(id string) (*Payment, error) {
wrapper := &paymentWrapper{}
err := c.get(fmt.Sprintf(`%s/%s`, paymentEndpoint, id), wrapper)
if err != nil {
return nil, err
}
return wrapper.Payment, err
}
// UpdatePayment Updates a payment object. Supports all of the fields supported when creating a payment.
//
// Relative endpoint: PUT /payments/PM123
func (c *Client) UpdatePayment(payment *Payment) error {
// allows only metadata
paymentMeta := map[string]interface{}{
"payments": map[string]interface{}{
"metadata": payment.Metadata,
},
}
paymentReq := &paymentWrapper{payment}
err := c.put(fmt.Sprintf(`%s/%s`, paymentEndpoint, payment.ID), paymentMeta, paymentReq)
if err != nil {
return err
}
return err
}
// CancelPayment immediately cancels a payment and all associated cancellable payments.
//
// Relative endpoint: POST /payments/PM123/actions/cancel
func (c *Client) CancelPayment(payment *Payment) error {
// allows only metadata
pMeta := map[string]interface{}{
"payments": map[string]interface{}{
"metadata": payment.Metadata,
},
}
wrapper := &paymentWrapper{payment}
err := c.post(fmt.Sprintf(`%s/%s/actions/cancel`, paymentEndpoint, payment.ID), pMeta, wrapper)
if err != nil {
return err
}
return err
}
// RetryPayment Retries a failed payment if the underlying mandate is active.
//
// Relative endpoint: POST /payments/PM123/actions/retry
func (c *Client) RetryPayment(payment *Payment) error {
// allows only metadata
pMeta := map[string]interface{}{
"payments": map[string]interface{}{
"metadata": payment.Metadata,
},
}
wrapper := &paymentWrapper{payment}
err := c.post(fmt.Sprintf(`%s/%s/actions/retry`, paymentEndpoint, payment.ID), pMeta, wrapper)
if err != nil {
return err
}
return err
}