-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
157 lines (119 loc) · 3.28 KB
/
client.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
package sofortpay
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"github.com/google/uuid"
)
const sofortPayBaseUrl = "https://api.sofort-pay.com"
// Please note that sofortpay is a Payment Initiation Service,
// which means that your client can initiate a payment with their bank account via this payment method.
//
// https://manage.sofort-pay.com/merchant/documentation/payments/api/payment
type Client interface {
InitializePayment(ctx context.Context, p *InitializePayment) (*Transaction, error)
GetPayment(ctx context.Context, uuid uuid.UUID) (*Transaction, error)
DeletePayment(ctx context.Context, uuid uuid.UUID) error
}
// SofortPay HTTP Client constructor (public)
func NewSofortPayClient(httpClient *http.Client, apiKey APIKey) (Client, error) {
baseUrl, err := url.Parse(sofortPayBaseUrl)
if err != nil {
return nil, err
}
if err := apiKey.Valid(); err != nil {
return nil, err
}
return newClient(httpClient, baseUrl, apiKey), nil
}
// SofortPay HTTP Client
type client struct {
baseURL *url.URL
apiKey APIKey
httpClient *http.Client
}
// SofortPay HTTP Client constructor
func newClient(httpClient *http.Client, baseUrl *url.URL, apiKey APIKey) *client {
return &client{
baseURL: baseUrl,
apiKey: apiKey,
httpClient: httpClient,
}
}
// Initialize a payment
func (rcv *client) InitializePayment(ctx context.Context, p *InitializePayment) (*Transaction, error) {
payload, err := prepareInitializePaymentPayload(p)
if err != nil {
return nil, err
}
r, err := rcv.newRequest(ctx, "POST", "/api/v1/payments", payload)
if err != nil {
return nil, err
}
res, err := rcv.httpClient.Do(r)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusAccepted {
return nil, prepareError(res)
}
return prepareTransaction(res)
}
// Return Payment Transaction Object
func (rcv *client) GetPayment(ctx context.Context, uuid uuid.UUID) (*Transaction, error) {
r, err := rcv.newRequest(ctx, "GET", fmt.Sprintf("/api/v1/payments/%s", uuid), nil)
if err != nil {
return nil, err
}
res, err := rcv.httpClient.Do(r)
if err != nil {
return nil, err
}
if res.StatusCode != http.StatusOK {
return nil, prepareError(res)
}
return prepareTransaction(res)
}
// Delete a Payment Transaction Object
func (rcv *client) DeletePayment(ctx context.Context, uuid uuid.UUID) error {
r, err := rcv.newRequest(ctx, "DELETE", fmt.Sprintf("/api/v1/payments/%s", uuid), nil)
if err != nil {
return err
}
res, err := rcv.httpClient.Do(r)
if err != nil {
return err
}
if res.StatusCode != http.StatusNoContent {
return prepareError(res)
}
return nil
}
// Prepare request
func (rcv *client) newRequest(ctx context.Context, method string, path string, body interface{}) (*http.Request, error) {
u := rcv.baseURL.ResolveReference(&url.URL{Path: path})
var buf io.ReadWriter
if body != nil {
buf = new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(body); err != nil {
return nil, err
}
}
r, err := http.NewRequest(method, u.String(), buf)
if err != nil {
return nil, err
}
if ctx != nil {
r = r.WithContext(ctx)
}
if body != nil {
r.Header.Set("Content-Type", "application/json")
}
r.Header.Set("Accept", "application/json")
r.Header.Set("Authorization", string(rcv.apiKey))
return r, nil
}