Skip to content

Commit

Permalink
Merge pull request #20 from Golang-Tanzania/dev
Browse files Browse the repository at this point in the history
Refactor to use enums for sandbox & production env
  • Loading branch information
Hopertz authored Feb 4, 2024
2 parents c866f78 + 69875c6 commit 38ae218
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 37 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ import (
```go
// NewClient returns new Client struct
client, err := mpesa.NewClient("your-api-key", "sandbox", 24)
client, err := mpesa.NewClient("your-api-key", mpesa.Sandbox, 24)
// "your-api-key" obtained at https://openapiportal.m-pesa.com
// sandbox is environment type can either be "sandbox" or "production"
// sandbox is environment type can either be mpesa.Sandbox or mpesa.Production
// 24 represent hours, its session lifetime before we request another session it can be found in the
// https://openapiportal.m-pesa.com/applications where you set for your application,
Expand Down Expand Up @@ -89,7 +89,7 @@ import (
func main() {
client, err := mpesa.NewClient("your-api-key", "sandbox", 24)
client, err := mpesa.NewClient("your-api-key", mpesa.Sandbox, 24)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -130,7 +130,7 @@ import (
func main() {
client, err := mpesa.NewClient("your-api-key", "sandbox", 24)
client, err := mpesa.NewClient("your-api-key", mpesa.Sandbox, 24)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -174,7 +174,7 @@ import (
func main() {
client, err := mpesa.NewClient("your-api-key", "sandbox", 24)
client, err := mpesa.NewClient("your-api-key", mpesa.Sandbox, 24)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -217,7 +217,7 @@ import (
func main() {
client, err := mpesa.NewClient("your-api-key", "sandbox", 24)
client, err := mpesa.NewClient("your-api-key", mpesa.Sandbox, 24)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -257,7 +257,7 @@ import (
func main() {
client, err := mpesa.NewClient("your-api-key", "sandbox", 24)
client, err := mpesa.NewClient("your-api-key", mpesa.Sandbox, 24)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -296,7 +296,7 @@ import (
func main() {
client, err := mpesa.NewClient("your-api-key", "sandbox", 24)
client, err := mpesa.NewClient("your-api-key", mpesa.Sandbox, 24)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -336,7 +336,7 @@ import (
func main() {
client, err := mpesa.NewClient("your-api-key", "sandbox", 24)
client, err := mpesa.NewClient("your-api-key", mpesa.Sandbox, 24)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -381,7 +381,7 @@ import (
func main() {
client, err := mpesa.NewClient("your-api-key", "sandbox", 24)
client, err := mpesa.NewClient("your-api-key", mpesa.Sandbox, 24)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -427,7 +427,7 @@ import (
func main() {
client, err := mpesa.NewClient("your-api-key", "sandbox", 24)
client, err := mpesa.NewClient("your-api-key", mpesa.Sandbox, 24)
if err != nil {
panic(err)
}
Expand Down Expand Up @@ -470,7 +470,7 @@ import (
func main() {
client, err := mpesa.NewClient("your-api-key", "sandbox", 24)
client, err := mpesa.NewClient("your-api-key", mpesa.Sandbox, 24)
if err != nil {
panic(err)
}
Expand Down
10 changes: 6 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ import (
"net/url"
"reflect"
"time"


)

// NewClient returns new Client struct
func NewClient(api_key string, envtype string, sessionLife int32) (*Client, error) {
if api_key == "" || envtype == "" {
return nil, errors.New("api Key, environment are required to create a Client")
func NewClient(api_key string, envtype Env, sessionLife int32) (*Client, error) {
if api_key == "" {
return nil, errors.New("api Key is to create a Client")
}

var keys *Keys
Expand Down Expand Up @@ -100,7 +102,7 @@ func (c *Client) makeUrl(endpoint string) string {

var url string

if c.Environment == "production" {
if c.Environment == Production {
url = fmt.Sprintf("https://%v:%v%v%v/", Address, Port, ProdEndpoint, endpoint)
} else {
url = fmt.Sprintf("https://%v:%v%v%v/", Address, Port, SandboxEndpoint, endpoint)
Expand Down
47 changes: 26 additions & 21 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,35 +29,40 @@ import (
)

const (
Address = "openapi.m-pesa.com"
Ssl = true
Port = 443
Sandbox = "sandbox"
Production = "production"
ProdEndpoint = "/openapi/ipg/v2/vodacomTZN/"
SandboxEndpoint = "/sandbox/ipg/v2/vodacomTZN/"
SessionEndPath = "getSession"
C2BPaymentPath = "c2bPayment/singleStage"
B2BPaymentPath = "b2bPayment"
B2CPaymentPath = "b2cPayment"
ReversalPath = "reversal"
QueryTxStatusPath = "queryTransactionStatus"
DirectDebitPath = "directDebitCreation"
DebitDBPaymentPath = "directDebitPayment"
QueryBeneficialPath = "queryBeneficiaryName"
QueryDirectDBPath = "queryDirectDebit"
CancelDirectDBPath = "directDebitCancel"
SandboxPublicKey = "MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEArv9yxA69XQKBo24BaF/D+fvlqmGdYjqLQ5WtNBb5tquqGvAvG3WMFETVUSow/LizQalxj2ElMVrUmzu5mGGkxK08bWEXF7a1DEvtVJs6nppIlFJc2SnrU14AOrIrB28ogm58JjAl5BOQawOXD5dfSk7MaAA82pVHoIqEu0FxA8BOKU+RGTihRU+ptw1j4bsAJYiPbSX6i71gfPvwHPYamM0bfI4CmlsUUR3KvCG24rB6FNPcRBhM3jDuv8ae2kC33w9hEq8qNB55uw51vK7hyXoAa+U7IqP1y6nBdlN25gkxEA8yrsl1678cspeXr+3ciRyqoRgj9RD/ONbJhhxFvt1cLBh+qwK2eqISfBb06eRnNeC71oBokDm3zyCnkOtMDGl7IvnMfZfEPFCfg5QgJVk1msPpRvQxmEsrX9MQRyFVzgy2CWNIb7c+jPapyrNwoUbANlN8adU1m6yOuoX7F49x+OjiG2se0EJ6nafeKUXw/+hiJZvELUYgzKUtMAZVTNZfT8jjb58j8GVtuS+6TM2AutbejaCV84ZK58E2CRJqhmjQibEUO6KPdD7oTlEkFy52Y1uOOBXgYpqMzufNPmfdqqqSM4dU70PO8ogyKGiLAIxCetMjjm6FCMEA3Kc8K0Ig7/XtFm9By6VxTJK1Mg36TlHaZKP6VzVLXMtesJECAwEAAQ=="
OpenapiPublicKey = "MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAietPTdEyyoV/wvxRjS5pSn3ZBQH9hnVtQC9SFLgM9IkomEX9Vu9fBg2MzWSSqkQlaYIGFGH3d69Q5NOWkRo+Y8p5a61sc9hZ+ItAiEL9KIbZzhnMwi12jUYCTff0bVTsTGSNUePQ2V42sToOIKCeBpUtwWKhhW3CSpK7S1iJhS9H22/BT/pk21Jd8btwMLUHfVD95iXbHNM8u6vFaYuHczx966T7gpa9RGGXRtiOr3ScJq1515tzOSOsHTPHLTun59nxxJiEjKoI4Lb9h6IlauvcGAQHp5q6/2XmxuqZdGzh39uLac8tMSmY3vC3fiHYC3iMyTb7eXqATIhDUOf9mOSbgZMS19iiVZvz8igDl950IMcelJwcj0qCLoufLE5y8ud5WIw47OCVkD7tcAEPmVWlCQ744SIM5afw+Jg50T1SEtu3q3GiL0UQ6KTLDyDEt5BL9HWXAIXsjFdPDpX1jtxZavVQV+Jd7FXhuPQuDbh12liTROREdzatYWRnrhzeOJ5Se9xeXLvYSj8DmAI4iFf2cVtWCzj/02uK4+iIGXlX7lHP1W+tycLS7Pe2RdtC2+oz5RSSqb5jI4+3iEY/vZjSMBVk69pCDzZy4ZE8LBgyEvSabJ/cddwWmShcRS+21XvGQ1uXYLv0FCTEHHobCfmn2y8bJBb/Hct53BaojWUCAwEAAQ=="
Address = "openapi.m-pesa.com"
Ssl = true
Port = 443
ProdEndpoint = "/openapi/ipg/v2/vodacomTZN/"
SandboxEndpoint = "/sandbox/ipg/v2/vodacomTZN/"
SessionEndPath = "getSession"
C2BPaymentPath = "c2bPayment/singleStage"
B2BPaymentPath = "b2bPayment"
B2CPaymentPath = "b2cPayment"
ReversalPath = "reversal"
QueryTxStatusPath = "queryTransactionStatus"
DirectDebitPath = "directDebitCreation"
DebitDBPaymentPath = "directDebitPayment"
QueryBeneficialPath = "queryBeneficiaryName"
QueryDirectDBPath = "queryDirectDebit"
CancelDirectDBPath = "directDebitCancel"
SandboxPublicKey = "MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEArv9yxA69XQKBo24BaF/D+fvlqmGdYjqLQ5WtNBb5tquqGvAvG3WMFETVUSow/LizQalxj2ElMVrUmzu5mGGkxK08bWEXF7a1DEvtVJs6nppIlFJc2SnrU14AOrIrB28ogm58JjAl5BOQawOXD5dfSk7MaAA82pVHoIqEu0FxA8BOKU+RGTihRU+ptw1j4bsAJYiPbSX6i71gfPvwHPYamM0bfI4CmlsUUR3KvCG24rB6FNPcRBhM3jDuv8ae2kC33w9hEq8qNB55uw51vK7hyXoAa+U7IqP1y6nBdlN25gkxEA8yrsl1678cspeXr+3ciRyqoRgj9RD/ONbJhhxFvt1cLBh+qwK2eqISfBb06eRnNeC71oBokDm3zyCnkOtMDGl7IvnMfZfEPFCfg5QgJVk1msPpRvQxmEsrX9MQRyFVzgy2CWNIb7c+jPapyrNwoUbANlN8adU1m6yOuoX7F49x+OjiG2se0EJ6nafeKUXw/+hiJZvELUYgzKUtMAZVTNZfT8jjb58j8GVtuS+6TM2AutbejaCV84ZK58E2CRJqhmjQibEUO6KPdD7oTlEkFy52Y1uOOBXgYpqMzufNPmfdqqqSM4dU70PO8ogyKGiLAIxCetMjjm6FCMEA3Kc8K0Ig7/XtFm9By6VxTJK1Mg36TlHaZKP6VzVLXMtesJECAwEAAQ=="
OpenapiPublicKey = "MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAietPTdEyyoV/wvxRjS5pSn3ZBQH9hnVtQC9SFLgM9IkomEX9Vu9fBg2MzWSSqkQlaYIGFGH3d69Q5NOWkRo+Y8p5a61sc9hZ+ItAiEL9KIbZzhnMwi12jUYCTff0bVTsTGSNUePQ2V42sToOIKCeBpUtwWKhhW3CSpK7S1iJhS9H22/BT/pk21Jd8btwMLUHfVD95iXbHNM8u6vFaYuHczx966T7gpa9RGGXRtiOr3ScJq1515tzOSOsHTPHLTun59nxxJiEjKoI4Lb9h6IlauvcGAQHp5q6/2XmxuqZdGzh39uLac8tMSmY3vC3fiHYC3iMyTb7eXqATIhDUOf9mOSbgZMS19iiVZvz8igDl950IMcelJwcj0qCLoufLE5y8ud5WIw47OCVkD7tcAEPmVWlCQ744SIM5afw+Jg50T1SEtu3q3GiL0UQ6KTLDyDEt5BL9HWXAIXsjFdPDpX1jtxZavVQV+Jd7FXhuPQuDbh12liTROREdzatYWRnrhzeOJ5Se9xeXLvYSj8DmAI4iFf2cVtWCzj/02uK4+iIGXlX7lHP1W+tycLS7Pe2RdtC2+oz5RSSqb5jI4+3iEY/vZjSMBVk69pCDzZy4ZE8LBgyEvSabJ/cddwWmShcRS+21XvGQ1uXYLv0FCTEHHobCfmn2y8bJBb/Hct53BaojWUCAwEAAQ=="
ReqNewSessionKeyBeforeExpiresIn = time.Duration(60) * time.Second
)

type Env int

const (
Sandbox Env = iota
Production
)

type (
Client struct {
mu sync.Mutex
Client *http.Client
Keys *Keys
Environment string
Environment Env
SessionKey string
ExpiresAt time.Time
SessionLife int32
Expand Down

0 comments on commit 38ae218

Please sign in to comment.