-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
130 lines (120 loc) · 3.82 KB
/
main.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
package Intouchpay
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"time"
)
// NewClient creates a new IntouchPay client
func NewClient(username, accountNumber, partnerPassword, callbackUrl string, sid int) *Client {
return &Client{
Username: username,
AccountNo: accountNumber,
PartnerPassword: partnerPassword,
CallbackURL: callbackUrl,
Sid: sid,
HTTPClient: &http.Client{},
}
}
// generatePassword calculates the SHA256 hash of the password string
func (c *Client) generatePassword(timestamp string) string {
passwordString := c.Username + c.AccountNo + c.PartnerPassword + timestamp
hash := sha256.New()
hash.Write([]byte(passwordString))
hashInBytes := hash.Sum(nil)
password := hex.EncodeToString(hashInBytes)
return password
}
// doRequest sends an HTTP request and handles the response
func (c *Client) doRequest(method, endpoint string, data interface{}) (*map[string]interface{}, error) {
var response *map[string]interface{}
body := new(bytes.Buffer)
_ = json.NewEncoder(body).Encode(data)
requestUrl := BaseUrl + endpoint
req, _ := http.NewRequest(method, requestUrl, body)
req.Header.Set("Content-Type", "application/json")
resp, err := c.HTTPClient.Do(req)
if err != nil {
return response, err
}
defer resp.Body.Close()
if errr := json.NewDecoder(resp.Body).Decode(&response); errr != nil {
return response, fmt.Errorf("IntouchPay API error: %d\n %s\n %v", resp.StatusCode, resp.Status, errr)
}
if resp.StatusCode != http.StatusOK {
return response, fmt.Errorf("IntouchPay API error: %d\n %s\n %v", resp.StatusCode, resp.Status, *response)
}
return response, nil
}
// RequestPayment initiates a payment request
func (c *Client) RequestPayment(params *RequestPaymentParams) (*RequestPaymentResponse, error) {
now := time.Now()
timestamp := now.Format("20060102150405")
password := c.generatePassword(timestamp)
body := RequestPaymentBody{
Username: c.Username,
Timestamp: timestamp,
Amount: params.Amount,
Password: password,
MobilePhone: params.MobilePhone,
RequestTransactionId: params.RequestTransactionId,
CallbackURL: c.CallbackURL,
}
var cResp *RequestPaymentResponse
resp, err := c.doRequest(http.MethodPost, RequestPaymentEndpoint, body)
if err != nil {
return cResp, err
}
respBytes, _ := json.Marshal(resp)
errr := json.Unmarshal(respBytes, &cResp)
if errr != nil {
return cResp, errr
}
return cResp, nil
}
// RequestDeposit initiates a deposit request
func (c *Client) RequestDeposit(params *RequestDepositParams) (*RequestDepositResponse, error) {
now := time.Now()
timestamp := now.Format("20060102150405")
password := c.generatePassword(timestamp)
var cResp *RequestDepositResponse
body := map[string]interface{}{
"username": c.Username,
"timestamp": timestamp,
"amount": params.Amount,
"withdrawcharge": params.WithdrawCharge,
"reason": params.Reason,
"sid": c.Sid,
"password": password,
"mobilephone": params.MobilePhone,
"requesttransactionid": params.RequestTransactionId,
}
resp, err := c.doRequest(http.MethodPost, RequestDepositEndpoint, body)
if err != nil {
return cResp, err
}
respBytes, _ := json.Marshal(resp)
errr := json.Unmarshal(respBytes, &cResp)
if errr != nil {
return cResp, errr
}
return cResp, nil
}
// GetBalance queries account balance
func (c *Client) GetBalance() (*BalanceResponse, error) {
timestamp := time.Now()
var cResp *BalanceResponse
resp, err := c.doRequest(http.MethodGet, GetBalanceEndpoint, timestamp)
if err != nil {
return cResp, err
}
respBytes, _ := json.Marshal(resp)
errr := json.Unmarshal(respBytes, &cResp)
if errr != nil {
return cResp, errr
}
return cResp, nil
}