-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmdk.go
120 lines (103 loc) · 3.01 KB
/
mdk.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
116
117
118
119
120
package veritrans
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
)
// CardRequest is a struct of the request parameters of the veritrans MDK token api
type CardRequest struct {
CardNumber string `json:"card_number"`
CardExpire string `json:"card_expire"`
SecurityCode string `json:"security_code"`
CardHolderName string `json:"cardholder_name,omitempty"`
TokenAPIKey string `json:"token_api_key"`
Lang string `json:"lang"`
}
// CardResponse represents a response of the veritrans MDK token api
type CardResponse struct {
Token string `json:"token"`
TokenExpireDate string `json:"token_expire_date"`
ReqCardNumber string `json:"req_card_number"`
Status string `json:"status"`
Code string `json:"code"`
Message string `json:"message"`
}
// ClientCardInfo indicates the card Information of the client
type ClientCardInfo struct {
CardNumber string `json:"card_number"`
CardExpire string `json:"card_expire"`
SecurityCode string `json:"security_code"`
CardHolderName string `json:"cardholder_name,omitempty"`
}
// MDKConfig is a configuration of the MDK service
type MDKConfig struct {
APIURL string
APIToken string
}
// MDKService handles the several veritrans APIs for MDK payment
type MDKService struct {
Config MDKConfig
}
// NewMDKService initializes a MDK service
func NewMDKService(config MDKConfig) *MDKService {
newService := &MDKService{Config: config}
return newService
}
// ExecuteCardRequest process the requests
func (mdk *MDKService) ExecuteCardRequest(cardRequest *CardRequest) (*CardResponse, error) {
cardReqJSON, err := json.Marshal(cardRequest)
if err != nil {
return nil, err
}
parsedURL, err := url.ParseRequestURI(mdk.Config.APIURL)
if err != nil {
return nil, err
}
httpClient := &http.Client{}
body := bytes.NewBuffer(cardReqJSON)
req, err := http.NewRequest("POST", parsedURL.String(), body)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("Accept", "application/json")
res, err := httpClient.Do(req)
if err != nil {
return nil, err
}
resBody, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}
var cardResponse CardResponse
err = json.Unmarshal(resBody, &cardResponse)
if err != nil {
return nil, err
}
return &cardResponse, nil
}
// GetCardToken gets a card token
func (mdk *MDKService) GetCardToken(cardInfo *ClientCardInfo) (string, error) {
if cardInfo == nil {
return "", errors.New("no card information")
}
cardRequest := CardRequest{
CardNumber: cardInfo.CardNumber,
CardExpire: cardInfo.CardExpire,
CardHolderName: cardInfo.CardHolderName,
SecurityCode: cardInfo.SecurityCode,
TokenAPIKey: mdk.Config.APIToken,
Lang: "ja",
}
cardResponse, err := mdk.ExecuteCardRequest(&cardRequest)
if err != nil {
return "", err
}
if cardResponse.Status == "success" {
return cardResponse.Token, nil
}
return "", errors.New(cardResponse.Message)
}