-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpayment.go
114 lines (109 loc) · 2.73 KB
/
payment.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
package mercadopago
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
)
// CreatePayment Creates a payment
// @param preference
// @return json
func (mp *MP) CreatePayment(payment *Payment) (*Payment, error) {
res := &Payment{}
uri := fmt.Sprintf("/v1/payments")
// Call POST method
r, err := mp.post(uri, payment, 2)
if err != nil {
return nil, err
}
// Check response status
if r.StatusCode != 200 && r.StatusCode != 201 {
return nil, fmt.Errorf("Bad status received in HTTP response: %v", r.Status)
}
// Read response Body and unmarshall content
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
if err = json.Unmarshal(body, res); err != nil {
return nil, err
}
return res, nil
}
// GetPayment Get a payment by ID
// @param id
// @return json
func (mp *MP) GetPayment(id string) (*Payment, error) {
res := &Payment{}
uri := fmt.Sprintf("/v1/payments/%v", id)
// Call GET method
r, err := mp.jget(uri, nil, 2)
if err != nil {
return nil, err
}
// Check response status
if r.StatusCode != 200 {
return nil, fmt.Errorf("Bad status received in HTTP response: %v", r.Status)
}
// Read response Body and unmarshall content
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
if err = json.Unmarshal(body, res); err != nil {
return nil, err
}
return res, nil
}
// GetPaymentsByRef Get a payment by External Reference
// @param id
// @return json
func (mp *MP) GetPaymentsByRef(externalReference string) (*PaymentSearch, error) {
res := &PaymentSearch{}
uri := fmt.Sprintf("/v1/payments/search")
data := &url.Values{}
data.Add("external_reference", externalReference)
// Call GET method
r, err := mp.get(uri, data, 2)
if err != nil {
return nil, err
}
// Check response status
if r.StatusCode != 200 {
return nil, fmt.Errorf("Bad status received in HTTP response: %v", r.Status)
}
// Read response Body and unmarshall content
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
if err = json.Unmarshal(body, res); err != nil {
return nil, err
}
return res, nil
}
// PaymentsSearch Search for payments using a filter set
// @param filters in url.Values object
// @return json
func (mp *MP) PaymentsSearch(filters *url.Values) (*PaymentSearch, error) {
res := &PaymentSearch{}
uri := fmt.Sprintf("/v1/payments/search")
// Call GET method
r, err := mp.get(uri, filters, 2)
if err != nil {
return nil, err
}
// Check response status
if r.StatusCode != 200 {
return nil, fmt.Errorf("Bad status received in HTTP response: %v", r.Status)
}
// Read response Body and unmarshall content
body, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
if err = json.Unmarshal(body, res); err != nil {
return nil, err
}
return res, nil
}