forked from veritrans/go-midtrans
-
Notifications
You must be signed in to change notification settings - Fork 1
/
iris.go
138 lines (106 loc) · 3.84 KB
/
iris.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package midtrans
import (
"bytes"
"crypto/sha512"
"encoding/hex"
"encoding/json"
"io"
"strings"
)
// IrisGateway struct
type IrisGateway struct {
Client Client
}
// Call : base method to call Core API
func (gateway *IrisGateway) Call(method, path string, body io.Reader, v interface{}, key string) error {
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
path = gateway.Client.APIEnvType.IrisURL() + path
req, err := gateway.Client.NewRequest(method, path, body, key)
if err != nil {
return err
}
return gateway.Client.ExecuteRequest(req, v)
}
// CheckBalance : get current balance information
func (gateway *IrisGateway) CheckBalance() (map[string]interface{}, error) {
var resp map[string]interface{}
err := gateway.Call("GET", "api/v1/balance", nil, &resp, gateway.Client.ApproverKey)
if err != nil {
gateway.Client.Logger.Println("Error Check Balance: ", err)
return resp, err
}
return resp, nil
}
// CreateBeneficiaries : Perform transaction using ChargeReq
func (gateway *IrisGateway) CreateBeneficiaries(req *BeneficiariesReq) (map[string]interface{}, error) {
var resp map[string]interface{}
jsonReq, _ := json.Marshal(req)
err := gateway.Call("POST", "api/v1/beneficiaries", bytes.NewBuffer(jsonReq), &resp, gateway.Client.ApproverKey)
if err != nil {
gateway.Client.Logger.Println("Error create beneficiaries: ", err)
return resp, err
}
return resp, nil
}
// ValidateBankAccount : get order status using order ID
func (gateway *IrisGateway) ValidateBankAccount(bankName string, account string) (map[string]interface{}, error) {
var resp map[string]interface{}
err := gateway.Call("GET", "api/v1/account_validation?bank="+bankName+"&account="+account, nil, &resp, gateway.Client.ApproverKey)
if err != nil {
gateway.Client.Logger.Println("Error approving: ", err)
return resp, err
}
return resp, nil
}
// CreatePayouts : Create Payout with single or multiple payouts
func (gateway *IrisGateway) CreatePayouts(req *PayoutReq) (Payout, error) {
resp := Payout{}
jsonReq, _ := json.Marshal(req)
err := gateway.Call("POST", "api/v1/payouts", bytes.NewBuffer(jsonReq), &resp, gateway.Client.CreatorKey)
if err != nil {
gateway.Client.Logger.Println("Error create payouts: ", err)
return resp, err
}
return resp, nil
}
// ApprovePayouts : Approve Payout(s) with single or multiple payouts
func (gateway *IrisGateway) ApprovePayouts(req *ApprovePayoutReq) (map[string]interface{}, error) {
var resp map[string]interface{}
jsonReq, _ := json.Marshal(req)
err := gateway.Call("POST", "api/v1/payouts/approve", bytes.NewBuffer(jsonReq), &resp, gateway.Client.ApproverKey)
if err != nil {
gateway.Client.Logger.Println("Error approve payouts: ", err)
return resp, err
}
return resp, nil
}
// RejectPayouts : Reject Payout(s) with single or multiple payouts
func (gateway *IrisGateway) RejectPayouts(req *RejectPayoutReq) (map[string]interface{}, error) {
var resp map[string]interface{}
jsonReq, _ := json.Marshal(req)
err := gateway.Call("POST", "api/v1/payouts/reject", bytes.NewBuffer(jsonReq), &resp, gateway.Client.ApproverKey)
if err != nil {
gateway.Client.Logger.Println("Error reject payouts: ", err)
return resp, err
}
return resp, nil
}
// ValidateSignatureKey : Validate Iris Signature from Payout Notification
func (gateway *IrisGateway) ValidateSignatureKey(payload string, headerKey string) bool {
hasher := sha512.New()
hasher.Write([]byte(string(payload) + gateway.Client.MerchantKey))
signatureKey := hex.EncodeToString(hasher.Sum(nil))
if signatureKey == headerKey {
return true
}
return false
}
// GenerateSignatureKey : Generate Iris Signature key
func (gateway *IrisGateway) GenerateSignatureKey(payload string) string {
hasher := sha512.New()
hasher.Write([]byte(string(payload) + gateway.Client.MerchantKey))
signatureKey := hex.EncodeToString(hasher.Sum(nil))
return signatureKey
}