aboutsummaryrefslogtreecommitdiff
path: root/client.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-11-04 13:49:28 +0700
committerShulhan <ms@kilabit.info>2022-11-04 19:01:07 +0700
commit0eef440900eda71b3a257707e49e0ad53be5353f (patch)
tree7682364bc2f6ac826afd27fe71980ede98160b73 /client.go
parent1846d3fc5d6a364986c073afccc66936ab3e469d (diff)
downloadduitku-0eef440900eda71b3a257707e49e0ad53be5353f.tar.xz
all: implement API for online transfer inquiry
The RtolInquiry method get the information of the name of the account owner of the transfer destination. After getting this information, customers can determine whether the purpose of such a transfer is in accordance with the intended or not. If appropriate, the customer can proceed to the transfer process. Ref: https://docs.duitku.com/disbursement/en/#transfer-online
Diffstat (limited to 'client.go')
-rw-r--r--client.go65
1 files changed, 65 insertions, 0 deletions
diff --git a/client.go b/client.go
index 1a531dd..8768a5b 100644
--- a/client.go
+++ b/client.go
@@ -8,7 +8,9 @@ import (
"fmt"
"net/http"
"sort"
+ "strconv"
"strings"
+ "time"
libhttp "github.com/shuLhan/share/lib/http"
)
@@ -16,6 +18,10 @@ import (
const (
PathDisbursementListBank = `/disbursement/listBank`
PathDisbursementCheckBalance = `/disbursement/checkBalance`
+
+ // Path for transfer online.
+ PathDisbursementInquiry = `/disbursement/inquiry`
+ PathDisbursementInquirySandbox = `/disbursement/inquirysandbox` // Used when server URL is sandbox (testing).
)
type Client struct {
@@ -27,11 +33,17 @@ type Client struct {
// NewClient create and initialize new Client.
func NewClient(opts ClientOptions) (cl *Client, err error) {
var (
+ logp = `NewClient`
httpcOpts = libhttp.ClientOptions{
ServerUrl: opts.ServerUrl,
}
)
+ err = opts.validate()
+ if err != nil {
+ return nil, fmt.Errorf(`%s: %w`, logp, err)
+ }
+
cl = &Client{
Client: libhttp.NewClient(&httpcOpts),
opts: opts,
@@ -117,3 +129,56 @@ func (cl *Client) DisbursementListBank() (banks []Bank, err error) {
return banks, nil
}
+
+// RtolInquiry get the information of the name of the account owner of the
+// transfer destination.
+//
+// After getting this information, customers can determine whether the purpose
+// of such a transfer is in accordance with the intended or not.
+// If appropriate, the customer can proceed to the transfer process.
+//
+// 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
+
+ resHttp *http.Response
+ resBody []byte
+ )
+
+ // Since the path is different in test environment, we check the host
+ // here to set it.
+ if cl.opts.host != hostLive {
+ 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)
+
+ resHttp, resBody, err = cl.PostJSON(path, nil, req)
+ if err != nil {
+ return nil, fmt.Errorf(`%s: %w`, logp, err)
+ }
+ if resHttp.StatusCode >= 500 {
+ return nil, fmt.Errorf(`%s: %s`, logp, resHttp.Status)
+ }
+
+ err = json.Unmarshal(resBody, &res)
+ if err != nil {
+ return nil, fmt.Errorf(`%s: %w`, logp, err)
+ }
+ if res.Code != resCodeSuccess {
+ return nil, fmt.Errorf(`%s: %s: %s`, logp, res.Code, res.Desc)
+ }
+
+ return res, nil
+}