diff options
| author | Shulhan <ms@kilabit.info> | 2022-11-07 12:00:35 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2022-11-07 12:35:46 +0700 |
| commit | eeacdd3063b6fc718fe513517808cb9fde72217b (patch) | |
| tree | c8b4962bb807601530e8a7a6894ed68e4a34a0d9 | |
| parent | eb3c3fc6b20b77c26a571395f584efdce8cfb4f7 (diff) | |
| download | duitku-eeacdd3063b6fc718fe513517808cb9fde72217b.tar.xz | |
all: changes the UserID in ClientOptions from string to int64
The type in request is always be an int.
| -rw-r--r-- | clearing_inquiry.go | 2 | ||||
| -rw-r--r-- | clearing_inquiry_response.go | 4 | ||||
| -rw-r--r-- | client.go | 19 | ||||
| -rw-r--r-- | client_options.go | 14 | ||||
| -rw-r--r-- | duitku_test.go | 2 | ||||
| -rw-r--r-- | request.go | 3 | ||||
| -rw-r--r-- | rtol_inquiry.go | 9 | ||||
| -rw-r--r-- | rtol_transfer.go | 14 |
8 files changed, 20 insertions, 47 deletions
diff --git a/clearing_inquiry.go b/clearing_inquiry.go index 6bc7112..8d3634e 100644 --- a/clearing_inquiry.go +++ b/clearing_inquiry.go @@ -27,7 +27,7 @@ type ClearingInquiry struct { } func (inq *ClearingInquiry) sign(opts ClientOptions) { - inq.UserID = opts.userID + inq.UserID = opts.UserID inq.Email = opts.Email inq.Timestamp = time.Now().UnixMilli() diff --git a/clearing_inquiry_response.go b/clearing_inquiry_response.go index 0fa6d98..f67023b 100644 --- a/clearing_inquiry_response.go +++ b/clearing_inquiry_response.go @@ -5,7 +5,7 @@ package duitku // // [Clearing Inquiry request]: https://docs.duitku.com/disbursement/en/#clearing-inquiry-request type ClearingInquiryResponse struct { - RtolInquiryResponse - Type string `json:"type"` + + RtolInquiryResponse } @@ -8,9 +8,7 @@ import ( "fmt" "net/http" "sort" - "strconv" "strings" - "time" libhttp "github.com/shuLhan/share/lib/http" ) @@ -191,7 +189,6 @@ func (cl *Client) ListBank() (banks []Bank, err error) { // Ref: https://docs.duitku.com/disbursement/en/#transfer-online func (cl *Client) RtolInquiry(req RtolInquiry) (res *RtolInquiryResponse, err error) { var ( - now = time.Now() logp = `RtolInquiry` path = PathDisbursementInquiry @@ -205,15 +202,7 @@ func (cl *Client) RtolInquiry(req RtolInquiry) (res *RtolInquiryResponse, err er path = PathDisbursementInquirySandbox } - req.UserID, err = strconv.ParseInt(cl.opts.UserID, 10, 64) - if err != nil { - return nil, fmt.Errorf(`%s: %s`, logp, err) - } - - req.Email = cl.opts.Email - req.Timestamp = now.UnixMilli() - - req.sign(cl.opts.ApiKey) + req.sign(cl.opts) resHttp, resBody, err = cl.PostJSON(path, nil, req) if err != nil { @@ -258,11 +247,7 @@ func (cl *Client) RtolTransfer(inquiryReq RtolInquiry, inquiryRes RtolInquiryRes } req = newRtolTransfer(&inquiryReq, &inquiryRes) - - err = req.sign(cl.opts) - if err != nil { - return nil, fmt.Errorf(`%s: %w`, logp, err) - } + req.sign(cl.opts) resHttp, resBody, err = cl.PostJSON(path, nil, req) if err != nil { diff --git a/client_options.go b/client_options.go index 4915b9c..211eea6 100644 --- a/client_options.go +++ b/client_options.go @@ -6,21 +6,18 @@ package duitku import ( "fmt" "net/url" - "strconv" ) // ClientOptions configuration for HTTP client. type ClientOptions struct { ServerUrl string - UserID string Email string ApiKey string // The hostname extracted from ServerUrl. host string - // The UserID converted to int64. - userID int64 + UserID int64 } // validate each field values. @@ -35,14 +32,9 @@ func (opts *ClientOptions) validate() (err error) { } opts.host = urlServer.Host - if len(opts.UserID) == 0 { - return fmt.Errorf(`invalid or empty UserID: %s`, opts.UserID) + if opts.UserID <= 0 { + return fmt.Errorf(`invalid or empty UserID: %d`, opts.UserID) } - opts.userID, err = strconv.ParseInt(opts.UserID, 10, 64) - if err != nil { - return fmt.Errorf(`invalid or empty UserID: %s`, opts.UserID) - } - if len(opts.Email) == 0 { return fmt.Errorf(`invalid or empty Email: %s`, opts.Email) } diff --git a/duitku_test.go b/duitku_test.go index 472fc7c..129e97f 100644 --- a/duitku_test.go +++ b/duitku_test.go @@ -18,7 +18,7 @@ func TestMain(m *testing.M) { clOpts = ClientOptions{ ServerUrl: ServerUrlSandbox, Email: `test@chakratechnology.com`, - UserID: `3551`, + UserID: 3551, ApiKey: `de56f832487bc1ce1de5ff2cfacf8d9486c61da69df6fd61d5537b6b7d6d354d`, } @@ -7,7 +7,6 @@ import ( "crypto/sha256" "encoding/hex" "fmt" - "strconv" "time" ) @@ -27,7 +26,7 @@ type Request struct { } func CreateRequest(opts ClientOptions) (req Request) { - req.UserID, _ = strconv.ParseInt(opts.UserID, 10, 64) + req.UserID = opts.UserID req.Email = opts.Email req.Timestamp = time.Now().UnixMilli() diff --git a/rtol_inquiry.go b/rtol_inquiry.go index 17cd824..8b6fc4e 100644 --- a/rtol_inquiry.go +++ b/rtol_inquiry.go @@ -4,6 +4,7 @@ import ( "crypto/sha256" "encoding/hex" "fmt" + "time" ) // RtolInquiry contains request to initiate transfer from merchant to @@ -35,11 +36,15 @@ type RtolInquiry struct { Amount int64 `json:"amountTransfer"` } -func (inq *RtolInquiry) sign(apiKey string) { +func (inq *RtolInquiry) sign(opts ClientOptions) { + inq.UserID = opts.UserID + inq.Email = opts.Email + inq.Timestamp = time.Now().UnixMilli() + var ( plain = fmt.Sprintf(`%s%d%s%s%d%s%s`, inq.Email, inq.Timestamp, inq.BankCode, inq.BankAccount, - inq.Amount, inq.Purpose, apiKey) + inq.Amount, inq.Purpose, opts.ApiKey) plainHash [sha256.Size]byte = sha256.Sum256([]byte(plain)) ) inq.Signature = hex.EncodeToString(plainHash[:]) diff --git a/rtol_transfer.go b/rtol_transfer.go index 75b22b6..e87515c 100644 --- a/rtol_transfer.go +++ b/rtol_transfer.go @@ -5,7 +5,6 @@ import ( "crypto/sha256" "encoding/hex" "fmt" - "strconv" "time" ) @@ -62,20 +61,15 @@ func newRtolTransfer(inquiryReq *RtolInquiry, inquiryRes *RtolInquiryResponse) ( return req } -func (req *rtolTransfer) sign(opts ClientOptions) (err error) { +func (req *rtolTransfer) sign(opts ClientOptions) { var ( - logp = `sign` - now = time.Now() + now = time.Now() bb bytes.Buffer plainHash [sha256.Size]byte ) - req.UserID, err = strconv.ParseInt(opts.UserID, 10, 64) - if err != nil { - return fmt.Errorf(`%s: %s`, logp, err) - } - + req.UserID = opts.UserID req.Email = opts.Email req.Timestamp = now.UnixMilli() @@ -87,6 +81,4 @@ func (req *rtolTransfer) sign(opts ClientOptions) (err error) { plainHash = sha256.Sum256(bb.Bytes()) req.Signature = hex.EncodeToString(plainHash[:]) - - return nil } |
