diff options
| author | Shulhan <ms@kilabit.info> | 2022-10-31 17:16:49 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2022-10-31 17:16:49 +0700 |
| commit | d131935fdfda4a9055670e8ab9a35d6a6c80172d (patch) | |
| tree | 1f5541ab3044ed3252556904790493e0967d1f20 | |
| parent | 07092174c8cdfd7206a319da999c0112c36cb7ef (diff) | |
| download | duitku-d131935fdfda4a9055670e8ab9a35d6a6c80172d.tar.xz | |
all: implement HTTP API checkBalance for disbursement
The DisbursementCheckBalance return the current and effective user's
balance.
| -rw-r--r-- | balance.go | 17 | ||||
| -rw-r--r-- | client.go | 32 | ||||
| -rw-r--r-- | client_test.go | 34 | ||||
| -rw-r--r-- | testdata/disbursement_checkbalance_test.txt | 14 |
4 files changed, 96 insertions, 1 deletions
diff --git a/balance.go b/balance.go new file mode 100644 index 0000000..e589889 --- /dev/null +++ b/balance.go @@ -0,0 +1,17 @@ +package duitku + +import "github.com/shuLhan/share/lib/math/big" + +// Balance contains the current user balances. +type Balance struct { + response + + // Current balance before settlement. + Current *big.Rat `json:"balance"` + + // Effective Balance that can be used for disbursement. + Effective *big.Rat `json:"effectiveBalance"` + + Email string `json:"email"` + UserID int64 `json:"userId"` +} @@ -14,7 +14,8 @@ import ( ) const ( - PathDisbursementListBank = `/disbursement/listBank` + PathDisbursementListBank = `/disbursement/listBank` + PathDisbursementCheckBalance = `/disbursement/checkBalance` ) type Client struct { @@ -39,6 +40,35 @@ func NewClient(opts ClientOptions) (cl *Client, err error) { return cl, nil } +// DisbursementCheckBalance get the current balances. +func (cl *Client) DisbursementCheckBalance() (bal *Balance, err error) { + var ( + logp = `DisbursementCheckBalance` + req = createRequest(cl.opts) + + httpRes *http.Response + resBody []byte + ) + + httpRes, resBody, err = cl.PostJSON(PathDisbursementCheckBalance, nil, req) + if err != nil { + return nil, fmt.Errorf(`%s: %w`, logp, err) + } + if httpRes.StatusCode >= 500 { + return nil, fmt.Errorf(`%s: %s`, logp, httpRes.Status) + } + + err = json.Unmarshal(resBody, &bal) + if err != nil { + return nil, fmt.Errorf(`%s: %s`, logp, err) + } + if bal.Code != resCodeSuccess { + return nil, fmt.Errorf(`%s: %s: %s`, logp, bal.Code, bal.Desc) + } + + return bal, nil +} + // DisbursementListBank fetch list of banks for disbursement. func (cl *Client) DisbursementListBank() (banks []Bank, err error) { var ( diff --git a/client_test.go b/client_test.go index 1daa196..95f61a6 100644 --- a/client_test.go +++ b/client_test.go @@ -10,6 +10,40 @@ import ( "github.com/shuLhan/share/lib/test" ) +func TestClient_DisbursementCheckBalance(t *testing.T) { + var ( + tdata *test.Data + balance *Balance + err error + exp []byte + got []byte + ) + + tdata, err = test.LoadData(`testdata/disbursement_checkbalance_test.txt`) + if err != nil { + t.Fatal(err) + } + + balance, err = testClient.DisbursementCheckBalance() + if err != nil { + t.Fatal(err) + } + + // Set the value to zero, since their value may be different on each + // run. + balance.Current.Scan(0) + balance.Effective.Scan(0) + + got, err = json.MarshalIndent(balance, ``, ` `) + if err != nil { + t.Fatal(err) + } + + exp = tdata.Output[`response.json`] + + test.Assert(t, `DisbursementCheckBalance`, string(exp), string(got)) +} + func TestClient_DisbursementListBank(t *testing.T) { var ( tdata *test.Data diff --git a/testdata/disbursement_checkbalance_test.txt b/testdata/disbursement_checkbalance_test.txt new file mode 100644 index 0000000..817995a --- /dev/null +++ b/testdata/disbursement_checkbalance_test.txt @@ -0,0 +1,14 @@ +## SPDX-FileCopyrightText: 2022 M. Shulhan <ms@kilabit.info> +## SPDX-License-Identifier: GPL-3.0-or-later + +Test data for HTTP API disbursement checkBalance. + +<<< response.json +{ + "responseCode": "00", + "responseDesc": "Success", + "balance": "0", + "effectiveBalance": "0", + "email": "test@chakratechnology.com", + "userId": 3551 +} |
