diff options
| author | Shulhan <ms@kilabit.info> | 2023-02-01 14:56:51 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2023-02-01 15:26:41 +0700 |
| commit | 78469f295acd5931d64c8af483c0261ff2eb6183 (patch) | |
| tree | ff055c4b6759ff4e40f8bc3175de1968cb21e8a5 /client.go | |
| parent | dd24a0248dc011fe43540b7a8f1487ed7d1e5d90 (diff) | |
| download | duitku-78469f295acd5931d64c8af483c0261ff2eb6183.tar.xz | |
all: implement API to check merchant transaction status
The MerchantTxStatus method check the payment status by its orderID and
payment method.
Reference: https://docs.duitku.com/api/en/#check-transaction
Diffstat (limited to 'client.go')
| -rw-r--r-- | client.go | 44 |
1 files changed, 42 insertions, 2 deletions
@@ -7,6 +7,7 @@ import ( "encoding/json" "fmt" "net/http" + "net/url" "sort" "strings" @@ -36,8 +37,9 @@ const ( PathTransferClearing = `/webapi/api/disbursement/transferclearing` PathTransferClearingSandbox = `/webapi/api/disbursement/transferclearingsandbox` // Used for testing. - PathMerchantPaymentMethod = `/webapi/api/merchant/paymentmethod/getpaymentmethod` - PathMerchantInquiry = `/webapi/api/merchant/v2/inquiry` + PathMerchantInquiry = `/webapi/api/merchant/v2/inquiry` + PathMerchantPaymentMethod = `/webapi/api/merchant/paymentmethod/getpaymentmethod` + PathMerchantTransactionStatus = `/webapi/api/merchant/transactionStatus` ) type Client struct { @@ -323,6 +325,44 @@ func (cl *Client) MerchantPaymentMethod(req *PaymentMethod) (resp *PaymentMethod return resp, nil } +// [MerchantTxStatus] get the status of payment from customer. +// +// [MerchantTxStatus]: https://docs.duitku.com/api/en/#check-transaction +func (cl *Client) MerchantTxStatus(orderID, paymentMethod string) (resp *TxStatusResponse, err error) { + var ( + logp = `MerchantTxStatus` + req = transactionStatus{ + OrderID: orderID, + } + + params url.Values + httpRes *http.Response + resBody []byte + ) + + req.sign(cl.opts, paymentMethod) + + params, err = libhttp.MarshalForm(req) + if err != nil { + return nil, fmt.Errorf(`%s: %w`, logp, err) + } + + httpRes, resBody, err = cl.PostForm(PathMerchantTransactionStatus, nil, params) + if err != nil { + return nil, fmt.Errorf(`%s: %w`, logp, err) + } + if httpRes.StatusCode >= 400 { + return nil, fmt.Errorf(`%s: %s: %s`, logp, httpRes.Status, resBody) + } + + err = json.Unmarshal(resBody, &resp) + if err != nil { + return nil, fmt.Errorf(`%s: %w`, logp, err) + } + + return resp, nil +} + // Options return the current client configuration. func (cl *Client) Options() (opts ClientOptions) { return cl.opts |
