aboutsummaryrefslogtreecommitdiff
path: root/clearing_callback.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-11-07 14:35:56 +0700
committerShulhan <ms@kilabit.info>2022-11-07 14:45:00 +0700
commit30f4a32cc8db54449c922f7f69410241d6a9d710 (patch)
tree53d713b6ef8276fc6b01b8057b38551b56786f05 /clearing_callback.go
parent1cf5da9119f2e23b7de5c2500c3cea521ee6e73e (diff)
downloadduitku-30f4a32cc8db54449c922f7f69410241d6a9d710.tar.xz
all: implement type for Clearing callback
The ClearingCallbackResponse contains fields that must be set in order to response from ClearingTransfer callback.
Diffstat (limited to 'clearing_callback.go')
-rw-r--r--clearing_callback.go78
1 files changed, 78 insertions, 0 deletions
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[:])
+}