From 30f4a32cc8db54449c922f7f69410241d6a9d710 Mon Sep 17 00:00:00 2001 From: Shulhan Date: Mon, 7 Nov 2022 14:35:56 +0700 Subject: all: implement type for Clearing callback The ClearingCallbackResponse contains fields that must be set in order to response from ClearingTransfer callback. --- clearing_callback.go | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++ clearing_inquiry.go | 6 ++-- client.go | 2 -- 3 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 clearing_callback.go diff --git a/clearing_callback.go b/clearing_callback.go new file mode 100644 index 0000000..721ce25 --- /dev/null +++ b/clearing_callback.go @@ -0,0 +1,78 @@ +package duitku + +import ( + "bytes" + "crypto/sha256" + "encoding/hex" + "fmt" +) + +// List of callback status code. +const ( + CallbackCodeSuccess = `00` + CallbackCodeFail = `01` +) + +// ClearingCallbackResponse contains fields that must be set in order to +// response from ClearingTransfer callback. +// +// The signature created using the following formula: +// +// SHA256(email + bankCode + bankAccount + accountName + +// custRefNumber + amountTransfer + disburseId + secretKey) +type ClearingCallbackResponse struct { + AccountName string `json:"accountName"` + BankAccount string `json:"bankAccount"` + BankCode string `json:"bankCode"` + CustRefNumber string `json:"custRefNumber"` + Email string `json:"email"` + + ErrorMsg string `json:"errorMessage"` + StatusCode string `json:"statusCode"` + StatusDesc string `json:"statusDesc"` + + Signature string `json:"signature"` + + Amount int64 `json:"amountTransfer"` + DisburseID int64 `json:"disburseId"` + UserID int64 `json:"userId"` +} + +// NewClearingCallbackResponse create ClearingCallbackResponse from +// Clearing Transfer response. +// +// The StatusCode is set to success initially. +func NewClearingCallbackResponse(transferRes ClearingTransferResponse) (cbres *ClearingCallbackResponse) { + cbres = &ClearingCallbackResponse{ + AccountName: transferRes.AccountName, + BankAccount: transferRes.BankAccount, + BankCode: transferRes.BankCode, + CustRefNumber: transferRes.CustRefNumber, + + StatusCode: CallbackCodeSuccess, + + Amount: transferRes.Amount.Int64(), + DisburseID: transferRes.DisburseID, + } + return cbres +} + +// Sign set the Signature SHA256 and convert to hex. +func (cbres *ClearingCallbackResponse) Sign(opts ClientOptions) { + var ( + bb bytes.Buffer + plainHash [sha256.Size]byte + ) + + bb.WriteString(cbres.Email) + bb.WriteString(cbres.BankCode) + bb.WriteString(cbres.BankAccount) + bb.WriteString(cbres.AccountName) + bb.WriteString(cbres.CustRefNumber) + fmt.Fprintf(&bb, `%d%d`, cbres.Amount, cbres.DisburseID) + bb.WriteString(opts.ApiKey) + + plainHash = sha256.Sum256(bb.Bytes()) + + cbres.Signature = hex.EncodeToString(plainHash[:]) +} diff --git a/clearing_inquiry.go b/clearing_inquiry.go index 8d3634e..5349272 100644 --- a/clearing_inquiry.go +++ b/clearing_inquiry.go @@ -12,10 +12,10 @@ import ( // // For Signature it use the following formula: // -// SHA256(email + timestamp + bankCode + type + bankAccount + amountTransfer -// + purpose + apiKey). +// SHA256(email + timestamp + bankCode + type + bankAccount + +// amountTransfer + purpose + apiKey) // -// [Clearing type]: // https://docs.duitku.com/disbursement/en/#clearing-inquiry-request +// [Clearing type]: https://docs.duitku.com/disbursement/en/#clearing-inquiry-request type ClearingInquiry struct { // 9 digits customer reference number. CustRefNumber string `json:"custRefNumber"` diff --git a/client.go b/client.go index d4d4d9f..b62ef22 100644 --- a/client.go +++ b/client.go @@ -117,8 +117,6 @@ func (cl *Client) ClearingInquiry(req ClearingInquiry) (res *ClearingInquiryResp return nil, fmt.Errorf(`%s: %s`, logp, httpRes.Status) } - fmt.Printf(`%s: resBody: %s\n`, logp, resBody) - err = json.Unmarshal(resBody, &res) if err != nil { return nil, fmt.Errorf(`%s: %w`, logp, err) -- cgit v1.3