-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefund.go
88 lines (69 loc) · 1.81 KB
/
refund.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
package yandex
import (
"encoding/json"
"log"
"github.com/google/go-querystring/query"
)
type Source struct {
AccountId string `json:"account_id"`
Amount *Amount `json:"amount"`
}
type Refund struct {
Id string `json:"id"`
PaymentId string `json:"payment_id"`
Status string `json:"status"`
CreatedAt string `json:"created_at"`
Amount *Amount `json:"amount"`
Description string `json:"description,omitempty"`
Sources []*Source `json:"sources,omitempty"`
}
type RefundRequest struct {
PaymentId string `json:"payment_id"`
Amount *Amount `json:"amount"`
Description string `json:"description,omitempty"`
Receipt *Receipt `json:"receipt,omitempty"`
Sources []*Source `json:"sources,omitempty"`
}
func (y *Yandex) CreateRefund(idempKey string, req *RefundRequest) (*Refund, error) {
q, err := query.Values(req)
if err != nil {
log.Printf("Failed creating query: %v\n", err)
return nil, err
}
r := &HttpRequest{
Method: "POST",
Path: "/refunds",
ShopId: y.ShopId,
SecretKey: y.SecretKey,
IdempotenceKey: idempKey,
Data: q,
}
bytes, err := r.SendRequest()
if err != nil {
return nil, err
}
res := &Refund{}
if err := json.Unmarshal(bytes, res); err != nil {
log.Printf("Failed unmarshaling bytes to struct: %v\n", err)
return nil, err
}
return res, nil
}
func (y *Yandex) GetRefundInfo(id string) (*Refund, error) {
r := &HttpRequest{
Method: "GET",
Path: "/refunds/" + id,
ShopId: y.ShopId,
SecretKey: y.SecretKey,
}
bytes, err := r.SendRequest()
if err != nil {
return nil, err
}
res := &Refund{}
if err := json.Unmarshal(bytes, res); err != nil {
log.Printf("Failed unmarshaling bytes to struct: %v\n", err)
return nil, err
}
return res, nil
}