From 548c92ff835de375e2b35add255bd4bf77a3e632 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Fri, 17 Mar 2023 13:57:34 +0700 Subject: all: change the MerchantTxStatus parameter to struct The idea is to allow the caller to know the Signature of request after call to MerchantTxStatus and possibly log the whole request. While at it, we change the method name to MerchantPaymentStatus and the type from transactionStatus to PaymentStatus, and from TxStatusResponse to PaymentStatusResponse. --- client.go | 15 ++++++--------- client_test.go | 14 +++++++++----- payment_status.go | 32 ++++++++++++++++++++++++++++++++ payment_status_response.go | 28 ++++++++++++++++++++++++++++ transaction_status.go | 32 -------------------------------- transaction_status_response.go | 28 ---------------------------- 6 files changed, 75 insertions(+), 74 deletions(-) create mode 100644 payment_status.go create mode 100644 payment_status_response.go delete mode 100644 transaction_status.go delete mode 100644 transaction_status_response.go diff --git a/client.go b/client.go index 68790b1..98ba946 100644 --- a/client.go +++ b/client.go @@ -322,24 +322,21 @@ func (cl *Client) MerchantPaymentMethod(req *PaymentMethod) (resp *PaymentMethod return resp, nil } -// [MerchantTxStatus] get the status of payment from customer. +// [MerchantPaymentStatus] 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) { +// [MerchantPaymentStatus]: https://docs.duitku.com/api/en/#check-transaction +func (cl *Client) MerchantPaymentStatus(req *PaymentStatus) (resp *PaymentStatusResponse, err error) { var ( - logp = `MerchantTxStatus` - req = transactionStatus{ - OrderID: orderID, - } + logp = `MerchantPaymentStatus` params url.Values httpRes *http.Response resBody []byte ) - req.sign(cl.opts, paymentMethod) + req.sign(cl.opts) - params, err = libhttp.MarshalForm(req) + params, err = libhttp.MarshalForm(*req) if err != nil { return nil, fmt.Errorf(`%s: %w`, logp, err) } diff --git a/client_test.go b/client_test.go index 109712d..e54a825 100644 --- a/client_test.go +++ b/client_test.go @@ -258,15 +258,19 @@ func TestClient_MerchantInquiry(t *testing.T) { // Test checking the transaction status. var ( - txResp *TxStatusResponse + paymentReq = &PaymentStatus{ + MerchantCode: req.PaymentMethod, + OrderID: req.MerchantOrderId, + } + paymentResp *PaymentStatusResponse ) - txResp, err = testClientMerchant.MerchantTxStatus(req.MerchantOrderId, req.PaymentMethod) + paymentResp, err = testClientMerchant.MerchantPaymentStatus(paymentReq) if err != nil { t.Fatal(err) } - got, err = json.MarshalIndent(txResp, ``, ` `) + got, err = json.MarshalIndent(paymentResp, ``, ` `) if err != nil { t.Fatal(err) } @@ -274,8 +278,8 @@ func TestClient_MerchantInquiry(t *testing.T) { tag = `tx_status_response.json` exp = tdata.Output[tag] exp = bytes.ReplaceAll(exp, []byte(`$ref`), []byte(resp.Reference)) - t.Logf(`MerchantTxStatus: response: %s`, got) - test.Assert(t, `MerchantTxStatus`, string(exp), string(got)) + t.Logf(`MerchantPaymentStatus: response: %s`, got) + test.Assert(t, `MerchantPaymentStatus`, string(exp), string(got)) } func TestClient_MerchantPaymentMethod(t *testing.T) { diff --git a/payment_status.go b/payment_status.go new file mode 100644 index 0000000..4eccd0c --- /dev/null +++ b/payment_status.go @@ -0,0 +1,32 @@ +// SPDX-FileCopyrightText: 2023 M. Shulhan +// SPDX-License-Identifier: GPL-3.0-or-later + +package duitku + +import ( + "crypto/md5" + "encoding/hex" + "fmt" +) + +type PaymentStatus struct { + MerchantCode string `form:"merchantCode"` + OrderID string `form:"merchantOrderId"` + + // Transaction identification code. + // Formula: md5(merchantCode + merchantOrderId + apiKey). + Signature string `form:"signature"` +} + +func (req *PaymentStatus) sign(opts ClientOptions) { + var merchant = opts.Merchant(req.MerchantCode) + + req.MerchantCode = merchant.Code + + var ( + plain = fmt.Sprintf(`%s%s%s`, req.MerchantCode, req.OrderID, merchant.ApiKey) + md5Plain = md5.Sum([]byte(plain)) + ) + + req.Signature = hex.EncodeToString(md5Plain[:]) +} diff --git a/payment_status_response.go b/payment_status_response.go new file mode 100644 index 0000000..c78dd3c --- /dev/null +++ b/payment_status_response.go @@ -0,0 +1,28 @@ +// SPDX-FileCopyrightText: 2023 M. Shulhan +// SPDX-License-Identifier: GPL-3.0-or-later + +package duitku + +// List of valid Code in PaymentStatusResponse. +const ( + PaymentStatusSuccess = `00` + PaymentStatusProcess = `01` + PaymentStatusFailed = `02` +) + +// PaymentStatusResponse contains response from checking merchant payment +// status. +type PaymentStatusResponse struct { + OrderID string `json:"merchantOrderId"` + Reference string `json:"reference"` + Amount string `json:"amount"` + + // Status code transaction. + // - 00 - Success + // - 01 - Process + // - 02 - Failed/Expired + Code string `json:"statusCode"` + + // Description that explain the status Code. + Message string `json:"statusMessage"` +} diff --git a/transaction_status.go b/transaction_status.go deleted file mode 100644 index 7ddae30..0000000 --- a/transaction_status.go +++ /dev/null @@ -1,32 +0,0 @@ -// SPDX-FileCopyrightText: 2023 M. Shulhan -// SPDX-License-Identifier: GPL-3.0-or-later - -package duitku - -import ( - "crypto/md5" - "encoding/hex" - "fmt" -) - -type transactionStatus struct { - MerchantCode string `form:"merchantCode"` - OrderID string `form:"merchantOrderId"` - - // Transaction identification code. - // Formula: md5(merchantCode + merchantOrderId + apiKey). - Signature string `form:"signature"` -} - -func (tx *transactionStatus) sign(opts ClientOptions, paymentMethod string) { - var merchant = opts.Merchant(paymentMethod) - - tx.MerchantCode = merchant.Code - - var ( - plain = fmt.Sprintf(`%s%s%s`, tx.MerchantCode, tx.OrderID, merchant.ApiKey) - md5Plain = md5.Sum([]byte(plain)) - ) - - tx.Signature = hex.EncodeToString(md5Plain[:]) -} diff --git a/transaction_status_response.go b/transaction_status_response.go deleted file mode 100644 index cba2fee..0000000 --- a/transaction_status_response.go +++ /dev/null @@ -1,28 +0,0 @@ -// SPDX-FileCopyrightText: 2023 M. Shulhan -// SPDX-License-Identifier: GPL-3.0-or-later - -package duitku - -// List of valid Code in TxStatusResponse. -const ( - MerchantTxStatusSuccess = `00` - MerchantTxStatusProcess = `01` - MerchantTxStatusFailed = `02` -) - -// TxStatusResponse contains response from checking merchant payment -// status. -type TxStatusResponse struct { - OrderID string `json:"merchantOrderId"` - Reference string `json:"reference"` - Amount string `json:"amount"` - - // Status code transaction. - // - 00 - Success - // - 01 - Process - // - 02 - Failed/Expired - Code string `json:"statusCode"` - - // Description that explain the status Code. - Message string `json:"statusMessage"` -} -- cgit v1.3