-
Notifications
You must be signed in to change notification settings - Fork 2
/
cielo.go
55 lines (46 loc) · 1.24 KB
/
cielo.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
// Package cielo is used to communicate with Cielo API
package cielo
import (
"bytes"
"encoding/json"
"net/http"
"github.com/satori/go.uuid"
)
// Ecommerce is the interface used to communicate with Cielo v3 API
type Ecommerce interface {
CreateSale(sale Sale) (Sale, error)
}
type cieloEcommerce struct {
merchant Merchant
env environment
}
func (c *cieloEcommerce) CreateSale(s Sale) (sale Sale, err error) {
body, err := json.Marshal(s)
if err != nil {
return
}
req, err := http.NewRequest(http.MethodPost, c.env.APIURL+"/1/sales", bytes.NewBuffer(body))
if err != nil {
return
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Accept-Encoding", "gzip")
req.Header.Add("Content-Type", "application/json")
req.Header.Add("User-Agent", "CieloEcommerce/3.0 Golang client")
req.Header.Add("MerchantId", c.merchant.ID)
req.Header.Add("MerchantKey", c.merchant.Key)
req.Header.Add("RequestId", uuid.NewV4().String())
response, err := http.DefaultClient.Do(req)
if err != nil {
return
}
err = json.NewDecoder(response.Body).Decode(&sale)
return
}
// NewEcommerce create a Cielo v3 API client
func NewEcommerce(m Merchant, env environment) Ecommerce {
return &cieloEcommerce{
merchant: m,
env: env,
}
}