-
Notifications
You must be signed in to change notification settings - Fork 0
/
gocielo.go
108 lines (90 loc) · 2.35 KB
/
gocielo.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
package gocielo
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
const apiCieloSandboxPrefix = "https://apisandbox.cieloecommerce.cielo.com.br"
const apiCieloProductionPrefix = "https://api.cieloecommerce.cielo.com.br"
func NewPayment(amount, installments int64, softDescriptor, provider string) *Payment {
return &Payment{
Amount: amount,
Installments: installments,
SoftDescriptor: softDescriptor,
Provider: provider,
}
}
func NewCreditCard(number, holder, expirationDate, brand, securityCode string, saveCard bool) *CreditCard {
return &CreditCard{
CardNumber: number,
Holder: holder,
ExpirationDate: expirationDate,
SaveCard: saveCard,
Brand: brand,
SecurityCode: securityCode,
}
}
func NewMerchant(merchantID, merchantKey string) *Merchant {
return &Merchant{
Id: merchantID,
Key: merchantKey,
}
}
func ExecuteCreditCardPayment(sandbox bool, merchant *Merchant, customerName string, card *CreditCard, payment *Payment, orderID string) (*Order, error) {
data, err := json.Marshal(
&Order{
MerchantOrderID: orderID,
Customer: &Customer{
Name: customerName,
},
Payment: &Payment{
Type: "CreditCard",
Amount: payment.Amount,
Installments: payment.Installments,
SoftDescriptor: payment.SoftDescriptor,
CreditCard: &CreditCard{
CardNumber: card.CardNumber,
Holder: card.Holder,
ExpirationDate: card.ExpirationDate,
SecurityCode: card.SecurityCode,
Brand: card.Brand,
},
Country: "BRA",
Currency: "BRL",
Provider: payment.Provider,
}})
if err != nil {
return nil, err
}
var prefix string
if sandbox == true {
prefix = apiCieloSandboxPrefix
} else {
prefix = apiCieloProductionPrefix
}
url := fmt.Sprintf("%s/1/sales/", prefix)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(data))
if err != nil {
return nil, err
}
req.Header.Add("MerchantId", merchant.Id)
req.Header.Add("MerchantKey", merchant.Key)
req.Header.Add("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return nil, err
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var order *Order
err = json.Unmarshal(body, &order)
if err != nil {
return nil, err
}
return order, nil
}