-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpesa.go
89 lines (85 loc) · 2.92 KB
/
mpesa.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
package mpesa
import (
"github.com/elirehema/mpesa/datas"
"github.com/elirehema/mpesa/sandbox"
"github.com/joho/godotenv"
)
const (
SESSION = "/getSession/"
C2BPAYMENT = "/c2bPayment/singleStage/"
B2CPAYMENT = "/b2cPayment/singleStage/"
B2BPAYMENT = "/b2bPayment/singleStage/"
REVERSAL = "/reversal"
QUERY_BENEFICIARY_NAME = "/queryBeneficiaryName/"
QUERY_TRANSACTION_STATUS = "/queryTransactionStatus/"
)
func RefreshSessionKey() {
godotenv.Load("session.env")
result := datas.SessionResponseData{}
_, err := sandbox.MpesaClient.R().SetResult(&result).Get(SESSION)
if err != nil {
print(err.Error())
} else {
env, _ := godotenv.Unmarshal("API_SESSION_KEY=" + result.SessionId)
godotenv.Write(env, "./session.env")
print(&result)
}
}
func CustomerToBusiness(request datas.RequestCustomerToBusiness) datas.ResponseData {
result := datas.ResponseData{}
_, err := sandbox.MpesaClient.R().SetResult(&result).SetBody(&request).Post(C2BPAYMENT)
if err != nil {
print(err.Error())
}
return result
}
func BusinessToCustomer(request datas.RequestBusinessToCustomer) datas.ResponseData {
result := datas.ResponseData{}
_, err := sandbox.MpesaClient.R().SetResult(&result).SetBody(&request).Post(B2CPAYMENT)
if err != nil {
print(err.Error())
}
return result
}
func BusinessToBusiness(request datas.RequestBusinessToBusiness) datas.ResponseData {
result := datas.ResponseData{}
_, err := sandbox.MpesaClient.R().SetResult(&result).SetBody(&request).Post(B2BPAYMENT)
if err != nil {
print(err.Error())
}
return result
}
func ReverseTransaction(request datas.RequestReversalTransaction) datas.ResponseData {
result := datas.ResponseData{}
_, err := sandbox.MpesaClient.R().SetResult(&result).SetBody(&request).Post(REVERSAL)
if err != nil {
print(err.Error())
}
return result
}
func QueryBeneficiaryName(request datas.RequestQueryBeneficiaryName) datas.ResponseData {
result := datas.ResponseData{}
_, err := sandbox.MpesaClient.R().SetResult(&result).SetBody(&request).SetQueryParams(map[string]string{
"input_CustomerMSISDN": request.Msisdn,
"input_Country": request.Country,
"input_ServiceProviderCode": request.ServiceProviderCode,
"input_KYCQueryType": request.KYCQueryType,
}).Post(QUERY_BENEFICIARY_NAME)
if err != nil {
print(err.Error())
}
return result
}
func QueryTransactionStatus(request datas.RequestQueryTransactionStatus) datas.ResponseData {
result := datas.ResponseData{}
_, err := sandbox.MpesaClient.R().SetResult(&result).SetBody(&request).SetQueryParams(map[string]string{
"input_QueryReference": request.QueryReference,
"input_Country": request.Country,
"input_ServiceProviderCode": request.ServiceProviderCode,
"input_ThirdPartyConversationID": request.ConversationId,
}).Get(QUERY_TRANSACTION_STATUS)
if err != nil {
print(err.Error())
}
return result
}