-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
90 lines (85 loc) · 2.38 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
package goly
import (
"errors"
"net/url"
)
func (c *Client) CreateRequest(data url.Values) (*Request, error) {
var response Request
re := url.Values{}
for k, v := range data {
if len(v) > 0 {
re.Set(k, v[0])
} else {
re.Set(k, "")
}
}
re.Set("vendor_token", c.identity.PublicVendorToken)
err := c.request("/request/do.json", re, &response)
if err != nil {
return nil, err
}
if response.Error != "0" {
return nil, errors.New(response.Message)
}
return &response, nil
}
func (c *Client) GetRequestState(id string) (*RequestState, error) {
var res RequestState
var re = url.Values{}
re.Set("request_id", id)
re.Set("vendor_token", c.identity.PublicVendorToken)
err := c.request("/request/state.json", re, &res)
if err != nil {
return nil, err
}
return &res, nil
}
func (c *Client) GetRequestStateByOrderRef(orderRef string) (*RequestState, error) {
var res RequestState
var re = url.Values{}
re.Set("order_ref", orderRef)
re.Set("vendor_token", c.identity.PublicVendorToken)
err := c.request("/request/state.json", re, &res)
if err != nil {
return nil, err
}
return &res, nil
}
func (c *Client) GetRequestStateByRequestUuid(requestUuid string) (*RequestState, error) {
var res RequestState
var re = url.Values{}
re.Set("request_uuid", requestUuid)
re.Set("vendor_token", c.identity.PublicVendorToken)
err := c.request("/request/state.json", re, &res)
if err != nil {
return nil, err
}
return &res, nil
}
func (c *Client) BuildSignature(data *map[string]string) (string, error) {
if len(c.identity.PrivateVendorToken) == 0 {
return "", errors.New("no private vendor token specified")
}
return buildSignature(data, c.identity.PrivateVendorToken), nil
}
func (c *Client) GetB2CBalance() (*map[string]float64, error) {
var res struct {
Error int `json:"error,omitempty"`
Balance map[string]float64 `json:"balance,omitempty"`
}
var re = url.Values{}
re.Set("vendor_token", c.identity.PublicVendorToken)
signature, err := c.BuildSignature(&map[string]string{"vendor_token": c.identity.PublicVendorToken})
re.Set("signature", signature)
err = c.request("/business/b2cbalance.json", re, &res)
if err != nil {
return nil, err
}
if res.Error != 0 {
return nil, errors.New("unable to retrieve b2c balance")
}
if nil == res.Balance || len(res.Balance) == 0 {
return nil, errors.New("unable to retrieve b2c balance")
}
return &res.Balance, nil
}