This repository has been archived by the owner on Jan 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
payments.go
104 lines (86 loc) · 3.15 KB
/
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
package starling
import (
"context"
"net/http"
"path"
)
// LocalPayment represents a local payment
type LocalPayment struct {
Payment PaymentAmount `json:"payment"`
DestinationAccountUID string `json:"destinationAccountUid"`
Reference string `json:"reference"`
}
// ScheduledPayment represents a scheduled payment
type ScheduledPayment struct {
LocalPayment
Schedule RecurrenceRule `json:"recurrenceRule"`
}
// PaymentOrder is a single PaymentOrder
type PaymentOrder struct {
UID string `json:"paymentOrderId"`
Currency string `json:"currency"`
Amount float64 `json:"amount"`
Reference string `json:"reference"`
ReceivingContactAccountUID string `json:"receivingContactAccountId"`
RecipientName string `json:"recipientName"`
Immediate bool `json:"immediate"`
RecurrenceRule RecurrenceRule `json:"recurrenceRule"`
StartDate string `json:"startDate"`
NextDate string `json:"nextDate"`
CancelledAt string `json:"cancelledAt"`
PaymentType string `json:"paymentType"`
MandateUID string `json:"mandateId"`
}
// PaymentOrders is a list of PaymentOrders
type paymentOrders struct {
PaymentOrders []PaymentOrder `json:"paymentOrders"`
}
// HALPaymentOrders is a HAL wrapper around the Transactions type.
type halPaymentOrders struct {
Embedded *paymentOrders `json:"_embedded"`
}
// PaymentAmount represents the currency and amount of a payment
type PaymentAmount struct {
Currency string `json:"currency"`
Amount float64 `json:"amount"`
}
// MakeLocalPayment creates a local payment.
func (c *Client) MakeLocalPayment(ctx context.Context, p LocalPayment) (*http.Response, error) {
req, err := c.NewRequest("POST", "/api/v1/payments/local", p)
if err != nil {
return nil, err
}
resp, err := c.Do(ctx, req, nil)
return resp, err
}
// CreateScheduledPayment creates a scheduled payment. It returns the UID for the scheduled payment.
func (c *Client) CreateScheduledPayment(ctx context.Context, p ScheduledPayment) (string, *http.Response, error) {
req, err := c.NewRequest("POST", "/api/v1/payments/scheduled", p)
if err != nil {
return "", nil, err
}
resp, err := c.Do(ctx, req, nil)
if err != nil {
return "", resp, err
}
loc := resp.Header.Get("Location")
uid := path.Base(loc)
return uid, resp, err
}
// ScheduledPayments retrieves a list of all the payment orders on the customer account. These may be
// orders for previous immediate payments or scheduled payment orders for future or on-going payments.
func (c *Client) ScheduledPayments(ctx context.Context) ([]PaymentOrder, *http.Response, error) {
req, err := c.NewRequest("GET", "/api/v1/payments/scheduled", nil)
if err != nil {
return nil, nil, err
}
hPO := new(halPaymentOrders)
resp, err := c.Do(ctx, req, &hPO)
if hPO == nil {
return nil, resp, err
}
if hPO.Embedded == nil {
return nil, resp, err
}
return hPO.Embedded.PaymentOrders, resp, err
}