Skip to content

Commit

Permalink
add support for already encrypted passwords
Browse files Browse the repository at this point in the history
  • Loading branch information
jakhax committed Nov 1, 2020
1 parent cd47f57 commit 593ced8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ func b2CExample()(err error){
return
}
b2c := &mpesa.B2C{
EncryptPassword: true,
ShortCode:"123456",
InitiatorUserName:"testapi115",
InitiatorPassword:"Safaricom007@",
Expand Down Expand Up @@ -222,6 +223,7 @@ func transactionStatusExample()(err error){
return
}
transactionStatus := &mpesa.TransactionStatus{
EncryptPassword: true,
PhoneNumber:"254712345678",
InitiatorUserName:"testapi115",
InitiatorPassword:"Safaricom007@",
Expand Down
13 changes: 10 additions & 3 deletions mpesa/b2c.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type B2C struct {
TimeOutCallBackURL string
//optional defaults to ""
Remarks string
EncryptPassword bool
}

//OK validates B2C
Expand Down Expand Up @@ -120,10 +121,16 @@ func (s *Mpesa) B2C(b2c *B2C) (apiRes *APIRes, err error) {
return
}
//encrypt password
securityCredential, err := EncryptPassword(b2c.InitiatorPassword, s.Config.Environment)
if err != nil {
return
var securityCredential string
if(b2c.EncryptPassword){
securityCredential, err = EncryptPassword(b2c.InitiatorPassword, s.Config.Environment)
if err != nil {
return
}
}else{
securityCredential = b2c.InitiatorPassword
}

payload := &B2CPayload{
InitiatorName: b2c.InitiatorUserName,
SecurityCredential: securityCredential,
Expand Down
12 changes: 9 additions & 3 deletions mpesa/reversal.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type Reversal struct {
TimeOutCallBackURL string
ResultCallBackURL string
Remarks string
EncryptPassword bool
}

//OK validates
Expand Down Expand Up @@ -137,9 +138,14 @@ func (s *Mpesa) Reverse(r *Reversal) (apiRes *APIRes, err error) {
return
}
//encrypt password
securityCredential, err := EncryptPassword(r.InitiatorPassword, s.Config.Environment)
if err != nil {
return
var securityCredential string
if(r.EncryptPassword){
securityCredential, err = EncryptPassword(r.InitiatorPassword, s.Config.Environment)
if err != nil {
return
}
}else{
securityCredential = r.InitiatorPassword
}
amount := float32(math.Round(float64(r.Amount)*100) / 100)

Expand Down

0 comments on commit 593ced8

Please sign in to comment.