-
Notifications
You must be signed in to change notification settings - Fork 0
/
gift_cards.go
88 lines (69 loc) · 2.03 KB
/
gift_cards.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 tebexgo
import (
"encoding/json"
"fmt"
"log"
"github.com/itschip/tebexgo/internal"
)
func (s *Session) GetAllGiftCards() (*GiftCards, error) {
resp, err := internal.GetRequest(s.Secret, GiftCardsEndpoint)
if err != nil {
log.Println("Failed to get all gift cards")
return nil, err
}
var giftCards GiftCards
internal.UnmarshalResponse(resp, &giftCards)
return &giftCards, nil
}
func (s *Session) GetGiftCard(cardId string) (*GiftCard, error) {
endpoint := fmt.Sprintf("%s/%s", GiftCardsEndpoint, cardId)
resp, err := internal.GetRequest(s.Secret, endpoint)
if err != nil {
log.Println("Failed to get gift card")
return nil, err
}
var giftCard GiftCard
internal.UnmarshalResponse(resp, &giftCard)
return &giftCard, nil
}
func (s *Session) CreateGiftCard(cardObject *PutGiftCardObject) (*GiftCard, error) {
jsonBody, err := json.Marshal(&cardObject)
if err != nil {
log.Println("Failed to marshal gift card object")
return nil, err
}
resp, err := internal.PostRequest(s.Secret, GiftCardsEndpoint, jsonBody)
if err != nil {
log.Println("Failed to create gift card")
return nil, err
}
var giftCard GiftCard
internal.UnmarshalResponse(resp, &giftCard)
return &giftCard, nil
}
func (s *Session) DisableGiftCard(giftCardId string) (*GiftCard, error) {
endpoint := fmt.Sprintf("%s/%s", GiftCardsEndpoint, giftCardId)
resp, err := internal.DeleteRequest(s.Secret, endpoint, nil)
if err != nil {
log.Println("Failed to disable giftcard")
return nil, err
}
var giftCard GiftCard
err = internal.UnmarshalResponse(resp, &giftCard)
if err != nil {
log.Println("Failed to unmarshal response")
return nil, err
}
return &giftCard, nil
}
func (s *Session) TopUpGiftCard(giftCardId string) (*GiftCard, error) {
endpoint := fmt.Sprintf("%s/%s", GiftCardsEndpoint, giftCardId)
resp, err := internal.PutRequest(s.Secret, endpoint, nil)
if err != nil {
log.Println("Failed to create gift card")
return nil, err
}
var giftCard GiftCard
internal.UnmarshalResponse(resp, &giftCard)
return &giftCard, nil
}