-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathstatic_wallet.go
115 lines (92 loc) · 2.87 KB
/
static_wallet.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
115
package cryptomus
import (
"encoding/json"
"errors"
)
const (
createStaticWalletEndpoint = "/wallet"
generateStaticWalletQRCodeEndpoint = "/wallet/qr"
blockWalletAddressEndpoint = "/wallet/block-address"
)
type StaticWalletRequest struct {
Currency string `json:"currency"`
Network string `json:"network"`
OrderId string `json:"order_id"`
*StaticWalletRequestOptions
}
type StaticWalletRequestOptions struct {
UrlCallback string `json:"url_callback,omitempty"`
FromReferralCode string `json:"from_referral_code,omitempty"`
}
type StaticWalletResponse struct {
OrderId string `json:"order_id"`
WalletUUID string `json:"wallet_uuid"`
UUID string `json:"uuid"`
Address string `json:"address"`
Network string `json:"network"`
Currency string `json:"currency"`
Url string `json:"url"`
}
type staticWalletRawResponse struct {
Result *StaticWalletResponse `json:"result"`
State int8 `json:"state"`
}
type staticWalletQRCodeRawResponse struct {
Result struct {
Image string `json:"image"`
} `json:"result"`
State int8 `json:"state"`
}
type BlockAddressRequest struct {
WalletUUID string `json:"uuid,omitempty"`
OrderId string `json:"order_id,omitempty"`
IsForceRefund bool `json:"is_force_refund,omitempty"`
}
type BlockAddressResponse struct {
WalletUUID string `json:"uuid"`
Status string `json:"status"`
}
type blockAddressRawResponse struct {
Result *BlockAddressResponse
State int8
}
func (c *Cryptomus) CreateStaticWallet(staticWalletReq *StaticWalletRequest) (*StaticWalletResponse, error) {
res, err := c.fetch("POST", createStaticWalletEndpoint, staticWalletReq)
if err != nil {
return nil, err
}
defer res.Body.Close()
response := &staticWalletRawResponse{}
if err = json.NewDecoder(res.Body).Decode(response); err != nil {
return nil, err
}
return response.Result, nil
}
func (c *Cryptomus) GenerateStaticWalletQRCode(walletUUID string) (string, error) {
payload := map[string]any{"wallet_address_uuid": walletUUID}
res, err := c.fetch("POST", generateStaticWalletQRCodeEndpoint, payload)
if err != nil {
return "", err
}
defer res.Body.Close()
response := &staticWalletQRCodeRawResponse{}
if err = json.NewDecoder(res.Body).Decode(response); err != nil {
return "", err
}
return response.Result.Image, nil
}
func (c *Cryptomus) BlockAddress(blockAddressReq *BlockAddressRequest) (*BlockAddressResponse, error) {
if blockAddressReq.WalletUUID == "" || blockAddressReq.OrderId == "" {
return nil, errors.New("you should pass one of required values [WalletUUID, OrderId]")
}
res, err := c.fetch("POST", blockWalletAddressEndpoint, blockAddressReq)
if err != nil {
return nil, err
}
defer res.Body.Close()
response := &blockAddressRawResponse{}
if err = json.NewDecoder(res.Body).Decode(response); err != nil {
return nil, err
}
return response.Result, nil
}