-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
318 lines (274 loc) · 9.86 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package sveawebpay
import (
"bytes"
"crypto/sha512"
"encoding/base64"
"encoding/hex"
"encoding/xml"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"time"
)
//URLTest is the api url used for tests
const URLTest = "https://webpaypaymentgatewaytest.svea.com/webpay/rest/"
//URLProd is the api url used in prod
const URLProd = "https://webpaypaymentgateway.svea.com/webpay/rest/"
//Client holds the necessary credentials to make requests to the svea apis
type Client struct {
merchantID string
secret string
Test bool
}
//NewClient creates a new client used for calls to the svea payment gateway api
func NewClient(merchantID string, secret string) Client {
return Client{
merchantID: merchantID,
secret: secret,
}
}
//post makes a http post request to the specified url with the specified body and
//unmarshals the response from xml to the specified response interface
func (c *Client) post(method string, body interface{}, dst interface{}) error {
//Marshal xml body
b, err := xml.Marshal(body)
if err != nil {
return fmt.Errorf("package error: failed to marshal xml body: %v", err.Error())
}
b = []byte("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + string(b))
//Create the mac
h := sha512.New()
if _, err := h.Write([]byte(base64.StdEncoding.EncodeToString(b) + c.secret)); err != nil {
return fmt.Errorf("package error: failed to hash mac: %v", err.Error())
}
mac := hex.EncodeToString(h.Sum(nil))
//Create the post form
form := url.Values{}
form.Add("merchantid", c.merchantID)
form.Add("message", base64.StdEncoding.EncodeToString(b))
form.Add("mac", mac)
//Determine the URL to use
URL := URLProd
if c.Test {
URL = URLTest
}
//Do the request
resp, err := http.DefaultClient.PostForm(URL+method, form)
if err != nil {
return fmt.Errorf("package error: request failed: %v", err.Error())
}
defer resp.Body.Close()
//Read the response body
buf := new(bytes.Buffer)
if i, err := buf.ReadFrom(resp.Body); err != nil {
return fmt.Errorf("package error: failed to read response body (%v bytes read): %v", i, err.Error())
}
//Unmarshal the response body
var x responseBody
if err := xml.Unmarshal(buf.Bytes(), &x); err != nil {
return fmt.Errorf("package error: failed to unmarshal xml response body: %v", err.Error())
}
//Decode the base64 response message
d, err := base64.StdEncoding.DecodeString(x.Message)
if err != nil {
return fmt.Errorf("package error: failed to decode base64 response message: %v", err.Error())
}
if dst != nil {
//Unmarshal the xml response message
if err := xml.Unmarshal(d, dst); err != nil {
return fmt.Errorf("package error: failed to unmarshal xml response message: %v", err.Error())
}
}
//Unmarshal the xml response to get the status code
var r response
if err := xml.Unmarshal(d, &r); err != nil {
return fmt.Errorf("package error: failed to unmarshal xml response message status code: %v", err.Error())
}
//Check the status code for error
err = CodeToErr(r.StatusCode)
if err != ErrUnknown {
return err
}
return nil
}
//PreparePayment calls the api to prepare a payment and if successful
//gets a url that can be visited to complete the payment. If an error occurs
//ahead of the request, status code -1 and a non nil error is returned.
//Otherwise a nil error is returned along with the statuscode returned by the api.
//Always check the error and status code before using the prepared payment.
func (c *Client) PreparePayment(order Order) (*PreparedPayment, error) {
//Make the post request to the api
var resp preparedPaymentResponse
if err := c.post("preparepayment", order, &resp); err != nil {
return nil, err
}
resp.PreparedPayment.test = c.Test
return &resp.PreparedPayment, nil
}
//DecodePaymentResponseBody decodes the response body of a call to the svea api.
//It can for example be used in the callback of a prepared payment.
func (c *Client) DecodePaymentResponseBody(r io.Reader) (*Transaction, error) {
//Read the response body
buf := new(bytes.Buffer)
if i, err := buf.ReadFrom(r); err != nil {
return nil, fmt.Errorf("package error: failed to read response body (%v bytes read): %v", i, err.Error())
}
//Unmarshal the response body
var rb responseBody
if err := xml.Unmarshal(buf.Bytes(), &rb); err != nil {
return nil, fmt.Errorf("package error: failed to unmarshal xml response body: %v", err.Error())
}
//Decode the base64 response message
d, err := base64.StdEncoding.DecodeString(rb.Message)
if err != nil {
return nil, fmt.Errorf("package error: failed to decode base64 response message: %v", err.Error())
}
//Unmarshal the xml response message
var p paymentResponse
if err := xml.Unmarshal(d, &p); err != nil {
return nil, fmt.Errorf("package error: failed to unmarshal xml response message: %v", err.Error())
}
return &p.Transaction, nil
}
//Recur calls the api to create a new transaction for an existing subscription
func (c *Client) Recur(order RecurOrder) (Transaction, error) {
//Make the post request to the api
var resp paymentResponse
if err := c.post("recur", order, &resp); err != nil {
return Transaction{}, err
}
return resp.Transaction, nil
}
//CancelRecurSubscription calls the api to cancel an existing recur subscription so that
//no further recurs can be made on it
func (c *Client) CancelRecurSubscription(subscriptionID int) error {
//Define the request body
req := struct {
XMLName xml.Name `xml:"cancelrecursubscription"`
SubscriptionID int `xml:"subscriptionid"`
}{
SubscriptionID: subscriptionID,
}
//Make the post request to the api
var resp paymentResponse
if err := c.post("cancelrecursubscription", req, &resp); err != nil {
return err
}
return nil
}
//Credit calls the api to return money to the customer. Only transactions that have reached
//the status SUCCESS can be credited
func (c *Client) Credit(transactionID int, amount float64) error {
//Define the request body
req := struct {
XMLName xml.Name `xml:"credit"`
TransactionID int `xml:"transactionid"`
AmountToCredit float64 `xml:"amounttocredit"`
}{
TransactionID: transactionID,
AmountToCredit: amount,
}
//Make the post request to the api
if err := c.post("credit", req, nil); err != nil {
return err
}
return nil
}
//Annul calls the api to cancel a payment before it has been captured. It can only be
//performed on card, invoice, or payment plan transactions having the status AUTHORIZED or CONFIRMED
func (c *Client) Annul(transactionID int) error {
//Define the request body
req := struct {
XMLName xml.Name `xml:"annul"`
TransactionID int `xml:"transactionid"`
}{
TransactionID: transactionID,
}
//Make the post request to the api
if err := c.post("annul", req, nil); err != nil {
return err
}
return nil
}
//Confirm calls the api to confirm a transaction. It is intended for card transactions.
//It can only be performed on card transactions having the status AUTHORIZED. This will
//result in a CONFIRMED transaction that will be captured (settled) on the given capture
//date. Confirm is mainly used by merchants who are configured to confirm their transactions
//themselves. Otherwise the transactions are confirmed automatically.
func (c *Client) Confirm(transactionID int, captureDate time.Time) error {
//Define the request body
req := struct {
XMLName xml.Name `xml:"confirm"`
TransactionID int `xml:"transactionid"`
CaptureDate string `xml:"capturedate"`
}{
TransactionID: transactionID,
CaptureDate: captureDate.Format("2006-01-02"),
}
//Make the post request to the api
if err := c.post("confirm", req, nil); err != nil {
return err
}
return nil
}
//LowerAmount calls the api to lower the amount on a transaction before it has been captured.
//It is intended for card transactions having the status AUTHORIZED or CONFIRMED. It can be
//used e.g. if the merchant is unable to deliver all of the items that was ordered by the customer,
//and wants to lower the total amount to pay. If the `amountToLower` is equal to the authorized
//amount, the transaction status will change to annulled.
func (c *Client) LowerAmount(transactionID int, amountToLower float64) error {
//Define the request body
req := struct {
XMLName xml.Name `xml:"loweramount"`
TransactionID int `xml:"transactionid"`
AmountToLower float64 `xml:"amounttolower"`
}{
TransactionID: transactionID,
AmountToLower: amountToLower,
}
//Make the post request to the api
if err := c.post("loweramount", req, nil); err != nil {
return err
}
return nil
}
//GetTransaction calls the api to get information about a specific order with either the specified
//transaction-id or customer reference number. Both does not need to be provided. If `transactionID <= 0`,
//`customerRefNo` will be used. If `customerRefNo` is an empty string as well a package error will
//be returned. If both are set `transactionID` will be used.
//
//**Please only use this when needed. Repetitive polling is not allowed.**
func (c *Client) GetTransaction(transactionID int, customerRefNo string) (*Transaction, error) {
var req interface{}
var method string
//Define the request body
if transactionID <= 0 {
//Transaction id is not provided, use the customer reference number
method = "querycustomerrefno"
req = struct {
XMLName xml.Name `xml:"loweramount"`
CustomerRefNo string `xml:"customerrefno"`
}{
CustomerRefNo: customerRefNo,
}
} else if len(customerRefNo) > 0 {
//Use the customer reference number if the transaction id is not provided
method = "querytransactionid"
req = struct {
XMLName xml.Name `xml:"loweramount"`
TransactionID int `xml:"transactionid"`
}{
TransactionID: transactionID,
}
} else {
return nil, errors.New("package error: neither a transaction id or customer reference number is provided")
}
//Make the post request to the api
var resp paymentResponse
if err := c.post(method, req, &resp); err != nil {
return nil, err
}
return &resp.Transaction, nil
}