diff options
Diffstat (limited to 'rtol_transfer.go')
| -rw-r--r-- | rtol_transfer.go | 92 |
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 +} |
