aboutsummaryrefslogtreecommitdiff
path: root/payment_method.go
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2023-01-31 19:18:47 +0700
committerShulhan <ms@kilabit.info>2023-01-31 19:22:52 +0700
commit2bf2b28ca70013e74442c464b8e1367bb40a36f1 (patch)
treee7965d1840b005837524701bf903c45d7134f605 /payment_method.go
parent31bac8aba1b26f9dd482b0784711b7695f43dbb0 (diff)
downloadduitku-2bf2b28ca70013e74442c464b8e1367bb40a36f1.tar.xz
all: implement API to get active payment method for merchant
The MerchantPaymentMethod method return list of payment methods enabled by merchant (your project).
Diffstat (limited to 'payment_method.go')
-rw-r--r--payment_method.go64
1 files changed, 64 insertions, 0 deletions
diff --git a/payment_method.go b/payment_method.go
new file mode 100644
index 0000000..b5e95fa
--- /dev/null
+++ b/payment_method.go
@@ -0,0 +1,64 @@
+// SPDX-FileCopyrightText: 2023 M. Shulhan <ms@kilabit.info>
+// SPDX-License-Identifier: GPL-3.0-or-later
+
+package duitku
+
+import (
+ "crypto/sha256"
+ "encoding/hex"
+ "fmt"
+ "time"
+)
+
+const (
+ // PaymentMethodDatetimeLayout define the date and time format for
+ // PaymentMethod.DateTime.
+ PaymentMethodDatetimeLayout = time.DateTime
+)
+
+// PaymentMethod contains request for client MerchantPaymentMethod.
+type PaymentMethod struct {
+ // [REQ] Merchant code from Duitku.
+ // Set by calling sign from ClientOptions.
+ MerchantCode string `json:"merchantcode"`
+
+ // [REQ] Format: yyyy-MM-dd HH:mm:ss
+ // If empty, it will be set during sign using current date time.
+ DateTime string `json:"datetime"`
+
+ // [REQ] Formula: Sha256(merchantcode + paymentAmount + datetime +
+ // apiKey).
+ // The value of hash is stored as lowercase hexadecimal.
+ Signature string `json:"signature"`
+
+ // [REQ] Transaction amount. No decimal code (.) and no decimal digit.
+ Amount int64 `json:"amount"`
+}
+
+// SetDateTime set the field DateTime using t.
+func (req *PaymentMethod) SetDateTime(t time.Time) {
+ req.DateTime = t.Format(PaymentMethodDatetimeLayout)
+}
+
+// Sign the request.
+// Set the MerchantCode and DateTime if its empty, and then generate the
+// Signature.
+func (req *PaymentMethod) Sign(opts ClientOptions) {
+ var (
+ plain string
+ hash [sha256.Size]byte
+ )
+
+ if len(req.MerchantCode) == 0 {
+ req.MerchantCode = opts.MerchantCode
+ }
+
+ if len(req.DateTime) == 0 {
+ req.SetDateTime(time.Now())
+ }
+
+ plain = fmt.Sprintf(`%s%d%s%s`, req.MerchantCode, req.Amount, req.DateTime, opts.MerchantApiKey)
+ hash = sha256.Sum256([]byte(plain))
+
+ req.Signature = hex.EncodeToString(hash[:])
+}