aboutsummaryrefslogtreecommitdiff
path: root/rtol_transfer.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-11-04 19:21:35 +0700
committerShulhan <ms@kilabit.info>2022-11-04 19:23:41 +0700
commit84e72cd4f383db1bfbf8b04471911217538bd49d (patch)
tree9eb695ea02dee1562d9ed509df7aa266bd34e6a5 /rtol_transfer.go
parent0eef440900eda71b3a257707e49e0ad53be5353f (diff)
downloadduitku-84e72cd4f383db1bfbf8b04471911217538bd49d.tar.xz
all: implement API to do online transfer
The RtolTransfer do the actual transfer to customer's bank account using the request and response from RtolInquiry.
Diffstat (limited to 'rtol_transfer.go')
-rw-r--r--rtol_transfer.go92
1 files changed, 92 insertions, 0 deletions
diff --git a/rtol_transfer.go b/rtol_transfer.go
new file mode 100644
index 0000000..db6eabc
--- /dev/null
+++ b/rtol_transfer.go
@@ -0,0 +1,92 @@
+package duitku
+
+import (
+ "bytes"
+ "crypto/sha256"
+ "encoding/hex"
+ "fmt"
+ "strconv"
+ "time"
+)
+
+// rtolTransfer containts request to transfer amount from merchant to
+// customer's bank account, using the previous data obtained from the inquiry
+// process.
+//
+// The formula to generate Signature is SHA256(email + timestamp + bankCode +
+// bankAccount + accountName + custRefNumber + amountTransfer + purpose +
+// disburseId + secretKey).
+//
+// Ref: https://docs.duitku.com/disbursement/en/#online-transfer-transfer-request
+type rtolTransfer struct {
+ // Bank Account owner, obtained after getting a response from the
+ // inquiry process.
+ AccountName string `json:"accountName"`
+
+ // Destination Bank Code sent when inquiry process.
+ BankCode string `json:"bankCode"`
+
+ // Destination account number sent when inquiry procces.
+ BankAccount string `json:"bankAccount"`
+
+ // Customer reference number, obtained after getting a response from
+ // the inquiry process.
+ CustRefNumber string `json:"custRefNumber"`
+
+ // Description of transfer purpose.
+ Purpose string `json:"purpose"`
+
+ request
+
+ // Disbursement transfer amount.
+ Amount int64 `json:"amountTransfer"`
+
+ // Disbursement Id provided by Duitku, obtained after getting a
+ // response from the inquiry process.
+ DisburseID int64 `json:"disburseId"`
+}
+
+// newRtolTransfer create new rtolTransfer request from request and response
+// of RtolInquiry.
+func newRtolTransfer(inquiryReq *RtolInquiry, inquiryRes *RtolInquiryResponse) (req *rtolTransfer) {
+ req = &rtolTransfer{
+ Amount: inquiryReq.Amount,
+ Purpose: inquiryReq.Purpose,
+
+ AccountName: inquiryRes.AccountName,
+ BankCode: inquiryRes.BankCode,
+ BankAccount: inquiryRes.BankAccount,
+ CustRefNumber: inquiryRes.CustRefNumber,
+ DisburseID: inquiryRes.DisburseID,
+ }
+ return req
+}
+
+func (req *rtolTransfer) sign(opts ClientOptions) (err error) {
+ var (
+ logp = `sign`
+ now = time.Now()
+
+ bb bytes.Buffer
+ plainHash [sha256.Size]byte
+ )
+
+ req.UserID, err = strconv.ParseInt(opts.UserID, 10, 64)
+ if err != nil {
+ return fmt.Errorf(`%s: %s`, logp, err)
+ }
+
+ req.Email = opts.Email
+ req.Timestamp = now.UnixMilli()
+
+ fmt.Fprintf(&bb, `%s%d%s%s%s%s%d%s%d%s`, req.Email, req.Timestamp,
+ req.BankCode, req.BankAccount, req.AccountName,
+ req.CustRefNumber, req.Amount, req.Purpose,
+ req.DisburseID, opts.ApiKey)
+
+ plainHash = sha256.Sum256(bb.Bytes())
+
+ req.Signature = hex.EncodeToString(plainHash[:])
+
+ return nil
+}