Skip to content
This repository was archived by the owner on Feb 7, 2023. It is now read-only.

Commit 427007d

Browse files
authored
Merge pull request #3 from kitabisa/task/UPS-3845-change-dana-endpoint-for-get-detail-user
added dana inquiry user info function
2 parents 1c98a1a + e35f257 commit 427007d

File tree

4 files changed

+121
-7
lines changed

4 files changed

+121
-7
lines changed

client.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io/ioutil"
88
"moul.io/http2curl"
99
"net/http"
10+
"strings"
1011
"time"
1112
)
1213

@@ -40,8 +41,8 @@ func NewClient() Client {
4041
// 1: Errors only
4142
// 2: Errors + informational (default)
4243
// 3: Errors + informational + debug
43-
LogLevel: 2,
44-
Logger: logger,
44+
LogLevel: 2,
45+
Logger: logger,
4546
SignatureEnabled: true,
4647
}
4748
}
@@ -94,15 +95,23 @@ func (c *Client) ExecuteRequest(req *http.Request, v interface{}) error {
9495

9596
if v != nil && res.StatusCode == 200 {
9697
if err = json.Unmarshal(resBody, v); err != nil {
98+
c.Logger.Error("Failed unmarshal body: %v ", err)
9799
return err
98100
}
99101

102+
// Dana endpoint V1 doesn't return signature in response, so we don't need to verify signature again
103+
if strings.Contains(req.URL.String(), "/v1/") {
104+
c.Logger.Info("Req URL Contains Dana endpoint V1")
105+
return nil
106+
}
107+
100108
if c.SignatureEnabled {
101109
response := gjson.Get(string(resBody), "response")
102110
signature := gjson.Get(string(resBody), "signature")
103111

104112
err = verifySignature(response.String(), signature.String(), c.PublicKey)
105113
if err != nil {
114+
c.Logger.Error("verifySignature failed: %v ", err)
106115
return err
107116
}
108117
}
@@ -124,4 +133,3 @@ func (c *Client) Call(method, path string, header map[string]string, body io.Rea
124133
}
125134

126135
// ===================== END HTTP CLIENT ================================================
127-

core.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ const (
2323
USER_PROFILE_PATH = "alipayplus/member/query/queryUserProfile.htm"
2424
DANA_TIME_LAYOUT = "2006-01-02T15:04:05-07:00"
2525
CURRENCY_IDR = "IDR"
26+
INQUIRY_USER_INFO_PATH = "v1/customers/user/inquiryUserInfoByAccessToken.htm"
2627

2728
FUNCTION_CREATE_ORDER = "dana.acquiring.order.createOrder"
2829
FUNCTION_QUERY_ORDER = "dana.acquiring.order.query"
2930
FUNCTION_REFUND = "dana.acquiring.refund.refund"
3031
FUNCTION_APPLY_ACCESS_TOKEN = "dana.oauth.auth.applyToken"
3132
FUNCTION_USER_PROFILE = "dana.member.query.queryUserProfile"
33+
FUNCTION_INQUIRY_USER_INFO = "customers.openapi.user.inquiryUserInfoByAccessToken"
3234
)
3335

3436
// CoreGateway struct
@@ -155,6 +157,27 @@ func (gateway *CoreGateway) UserProfile(reqBody *UserProfileRequestData, accessT
155157
return
156158
}
157159

160+
func (gateway *CoreGateway) InquiryUserInfo(reqBody *InquiryUserInfoRequest, accessToken string) (res InquiryUserInfoResponse, err error) {
161+
var response interface{}
162+
response, err = gateway.requestToDanaV1(reqBody, accessToken, FUNCTION_INQUIRY_USER_INFO, INQUIRY_USER_INFO_PATH)
163+
if err != nil {
164+
return
165+
}
166+
167+
var responseByte []byte
168+
responseByte, err = json.Marshal(response)
169+
if err != nil {
170+
return
171+
}
172+
173+
err = json.Unmarshal(responseByte, &res)
174+
if err != nil {
175+
return
176+
}
177+
178+
return
179+
}
180+
158181
func (gateway *CoreGateway) requestToDana(reqBody interface{}, accessToken string, headerFunction string, path string) (res ResponseBody, err error) {
159182
now := time.Now()
160183

@@ -211,3 +234,61 @@ func (gateway *CoreGateway) requestToDana(reqBody interface{}, accessToken strin
211234

212235
return
213236
}
237+
238+
func (gateway *CoreGateway) requestToDanaV1(reqBody interface{}, accessToken string, headerFunction string, path string) (res interface{}, err error) {
239+
now := time.Now()
240+
241+
head := RequestHeader{}
242+
head.Version = gateway.Client.Version
243+
head.Function = headerFunction
244+
head.ClientID = gateway.Client.ClientId
245+
head.ReqTime = now.Format(DANA_TIME_LAYOUT)
246+
head.ClientSecret = gateway.Client.ClientSecret
247+
248+
if accessToken != "" {
249+
head.AccessToken = accessToken
250+
}
251+
252+
var id uuid.UUID
253+
id, err = uuid.NewUUID()
254+
if err != nil {
255+
return
256+
}
257+
258+
head.ReqMsgID = id.String()
259+
260+
req := Request{
261+
Head: head,
262+
Body: reqBody,
263+
}
264+
265+
sig, err := generateSignature(req, gateway.Client.PrivateKey)
266+
if err != nil {
267+
err = fmt.Errorf("failed to generate signature: %v", err)
268+
log.Println("generateSignature Failed: ", err)
269+
return
270+
}
271+
272+
reqJson, err := json.Marshal(reqBody)
273+
if err != nil {
274+
return
275+
}
276+
277+
log.Println("Dana request: ", string(reqJson))
278+
279+
headers := map[string]string{
280+
"Content-Type": "application/json",
281+
"Client-Id": gateway.Client.ClientId,
282+
"Request-Time": now.Format(DANA_TIME_LAYOUT),
283+
"Signature": sig,
284+
}
285+
286+
bodyReq := bytes.NewBuffer(reqJson)
287+
err = gateway.Call("POST", path, headers, bodyReq, &res)
288+
if err != nil {
289+
log.Println("Failed call dana endpoint: ", err)
290+
return
291+
}
292+
293+
return
294+
}

request.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,8 @@ type RequestApplyAccessToken struct {
170170
type UserProfileRequestData struct {
171171
UserResources []string `json:"userResources" valid:"required"`
172172
}
173+
174+
type InquiryUserInfoRequest struct {
175+
AccessToken string `json:"accessToken" valid:"required"`
176+
ExtendInfo string `json:"extendInfo,omitempty" valid:"optional"`
177+
}

responses.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ type RefundResponseData struct {
4949
}
5050

5151
type ResultInfo struct {
52-
ResultStatus string `json:"resultStatus" valid:"optional"`
53-
ResultCodeID string `json:"resultCodeId" valid:"optional"`
54-
ResultMsg string `json:"resultMsg" valid:"optional"`
55-
ResultCode string `json:"resultCode" valid:"optional"`
52+
ResultStatus string `json:"resultStatus" valid:"optional"`
53+
ResultCodeID string `json:"resultCodeId" valid:"optional"`
54+
ResultMsg string `json:"resultMsg" valid:"optional"`
55+
ResultCode string `json:"resultCode" valid:"optional"`
56+
ResultMessage string `json:"resultMessage" valid:"optional"`
5657
}
5758

5859
type PayFinishResponse struct {
@@ -142,3 +143,22 @@ type UserProfileResponseData struct {
142143
ResultInfo ResultInfo `json:"resultInfo" valid:"required"`
143144
UserResourceInfos []UserResourceInfos `json:"userResourceInfos" valid:"required"`
144145
}
146+
147+
type InquiryUserInfoResponse struct {
148+
Result ResultInfo `json:"result" valid:"required"`
149+
UserInfo ResultUserInfo `json:"userInfo" valid:"required"`
150+
}
151+
152+
type ResultUserInfo struct {
153+
UserContactInfoEmail string `json:"USER_CONTACTINFO_EMAIL" valid:"optional"`
154+
UserName string `json:"USER_NAME" valid:"optional"`
155+
UserAddress []UserAddress `json:"USER_ADDRESS" valid:"optional"`
156+
UserContactInfo string `json:"USER_CONTACTINFO" valid:"optional"`
157+
}
158+
159+
type UserAddress struct {
160+
Area string `json:"area" valid:"optional"`
161+
Province string `json:"province" valid:"optional"`
162+
City string `json:"city" valid:"optional"`
163+
Address1 string `json:"address1" valid:"optional"`
164+
}

0 commit comments

Comments
 (0)