-
Notifications
You must be signed in to change notification settings - Fork 12
/
request.go
executable file
·109 lines (91 loc) · 2.66 KB
/
request.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
package amazonpay
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/xml"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"path"
"sort"
"strings"
"time"
)
// APIError Amazon Pay API Error definition
type APIError struct {
XMLName xml.Name `xml:"ErrorResponse"`
Type string `xml:"Error>Type"`
Code string `xml:"Error>Code"`
Message string `xml:"Error>Message"`
}
func (apiError APIError) Error() string {
return apiError.Message
}
// Post post API info
func (amazonPay *AmazonPay) Post(params Params, response interface{}) error {
if _, ok := params.Get("AWSAccessKeyId"); !ok {
params.Set("AWSAccessKeyId", amazonPay.Config.AccessKey)
}
if _, ok := params.Get("SellerId"); !ok {
params.Set("SellerId", amazonPay.Config.MerchantID)
}
if _, ok := params.Get("SignatureMethod"); !ok {
params.Set("SignatureMethod", "HmacSHA256")
}
if _, ok := params.Get("SignatureVersion"); !ok {
params.Set("SignatureVersion", "2")
}
if _, ok := params.Get("Timestamp"); !ok {
params.Set("Timestamp", time.Now().UTC().Format("2006-01-02T15:04:05Z"))
}
params.Set("Signature", params.Sign())
if _, ok := params.Get("Version"); !ok {
params.Set("Version", "2013-01-01")
}
URL := url.URL{
Scheme: "https",
Host: amazonPay.Config.Endpoint,
Path: path.Join(amazonPay.Config.ModePath, amazonPay.Config.APIVersion),
}
resp, err := http.Post(URL.String(), "application/x-www-form-urlencoded", strings.NewReader(amazonPay.buildPostURL(params)))
var data []byte
if err == nil {
defer resp.Body.Close()
data, err = ioutil.ReadAll(resp.Body)
if resp.StatusCode == 200 {
if response != nil {
err = xml.Unmarshal([]byte(data), response)
}
} else {
var apiError APIError
if err = xml.Unmarshal([]byte(data), &apiError); err == nil {
return apiError
}
return errors.New("Get error: " + string(data))
}
}
return err
}
// buildPostURL build post URL
func (amazonPay *AmazonPay) buildPostURL(params Params) string {
apiParams := []string{}
for key, value := range params {
if str := fmt.Sprint(value); str != "" {
apiParams = append(apiParams, key+"="+url.QueryEscape(str))
}
}
sort.Strings(apiParams)
postURL := strings.Join(apiParams, "&")
postURL += "&Signature=" + amazonPay.Sign(strings.Join([]string{"POST", amazonPay.Config.Endpoint, fmt.Sprintf("/%v/%v", amazonPay.Config.ModePath, amazonPay.Config.APIVersion), postURL}, "\n"))
return postURL
}
// Sign sign messages
func (amazonPay *AmazonPay) Sign(message string) string {
key := []byte(amazonPay.Config.SecretKey)
h := hmac.New(sha256.New, key)
h.Write([]byte(message))
return url.QueryEscape(base64.StdEncoding.EncodeToString(h.Sum(nil)))
}