-
Notifications
You must be signed in to change notification settings - Fork 0
/
redeem_code.exchange.go
77 lines (65 loc) · 2.25 KB
/
redeem_code.exchange.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
package ezpay
import (
"context"
"encoding/json"
"errors"
"fmt"
log "github.com/sirupsen/logrus"
)
const (
APIRedeemCodeExchange = "redeem_code.exchange"
)
// RedeemCodeExchangeRequest redeem_code.exchange
type RedeemCodeExchangeRequest struct {
CustomerId string `json:"customer_id"` // 艺爪系统用户ID
Value string `json:"value"` // 兑换码
}
// RedeemCodeExchangeResult redeem_code.exchange
type RedeemCodeExchangeResult struct {
ProjectId string `json:"project_id"` // 项目ID
Value string `json:"value"` // 兑换码
Product *Product `json:"product"` // 兑换商品信息
}
type Product struct {
Id string `json:"id"` // 商品ID
Name string `json:"name"` // 商品名称
}
type RedeemCodeExchangeResultError struct {
ErrorType RedeemCodeExchangeResultErrorType `json:"error"` // 错误信息
Message string `json:"message"` // 错误信息
}
func (e *RedeemCodeExchangeResultError) Error() string {
return fmt.Sprintf("%v:%s", e.ErrorType, e.Message)
}
type RedeemCodeExchangeResultErrorType string
const (
ErrorUnknown RedeemCodeExchangeResultErrorType = "Unknown"
ErrorRedeemCodeAlreadyUsed RedeemCodeExchangeResultErrorType = "RedeemCodeAlreadyUsed"
ErrorRedeemCodeExhausted RedeemCodeExchangeResultErrorType = "RedeemCodeExhausted"
ErrorRedeemCodeExpired RedeemCodeExchangeResultErrorType = "RedeemCodeExpired"
ErrorRedeemCodeInvalid RedeemCodeExchangeResultErrorType = "RedeemCodeInvalid"
)
func (c *Client) RedeemCodeExchange(ctx context.Context, request *RedeemCodeExchangeRequest) (*RedeemCodeExchangeResult, error) {
resp, err := c.Call(ctx, APIRedeemCodeExchange, request)
if err != nil && !errors.Is(err, ErrFailedResponse) {
return nil, err
}
respBody, err := json.Marshal(resp)
if err != nil {
return nil, err
}
if errors.Is(err, ErrFailedResponse) {
errResult := &RedeemCodeExchangeResultError{}
if err := json.Unmarshal(respBody, errResult); err != nil {
log.Errorf("Unmarshal Error, err=%+v", err)
return nil, err
}
return nil, errResult
}
result := &RedeemCodeExchangeResult{}
if err := json.Unmarshal(respBody, result); err != nil {
log.Errorf("Unmarshal Error, err=%+v", err)
return nil, err
}
return result, nil
}