Skip to content

Instantly share code, notes, and snippets.

@jumaallan
Created July 6, 2020 13:30
Show Gist options
  • Save jumaallan/dd71cb2f11d63b3a083c74d34f3cfd29 to your computer and use it in GitHub Desktop.
Save jumaallan/dd71cb2f11d63b3a083c74d34f3cfd29 to your computer and use it in GitHub Desktop.
Go MPESA C2B, B2C and B2B Callbacks
// struct
type B2BCallBack struct {
Result struct {
ResultType int `json:"ResultType"`
ResultCode int `json:"ResultCode"`
ResultDesc string `json:"ResultDesc"`
OriginatorConversationID string `json:"OriginatorConversationID"`
ConversationID string `json:"ConversationID"`
TransactionID string `json:"TransactionID"`
ResultParameters struct {
ResultParameter []struct {
Key string `json:"Key"`
Value interface{} `json:"Value,omitempty"`
} `json:"ResultParameter"`
} `json:"ResultParameters"`
ReferenceData struct {
ReferenceItem []struct {
Key string `json:"Key"`
Value interface{} `json:"Value,omitempty"`
} `json:"ReferenceItem"`
} `json:"ReferenceData"`
} `json:"Result"`
}
// sample code
var payload model.B2BCallBack
if err := c.ShouldBindJSON(&payload); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
result := payload.Result
var ReceiverPartyPublicName string
var TransCompletedTime string
for _, param := range result.ResultParameters.ResultParameter {
key := param.Key
if key == "ReceiverPartyPublicName" {
value := param.Value
if value != nil {
ReceiverPartyPublicName = value.(string)
}
} else if key == "TransCompletedTime" {
value := param.Value
if value != nil {
TransCompletedTime = strconv.FormatFloat(value.(float64), 'f', 2, 64)
}
}
}
if ReceiverPartyPublicName == "" || TransCompletedTime == "" {
fmt.Println("params are null")
}
//BankName Struct
type BankName struct {
BankName string
}
//Split the two, and trim spaces
bankName := new(BankName)
parts := strings.Split(ReceiverPartyPublicName, "-")
name := strings.TrimSpace(parts[1])
bankName.BankName = name
// struct
type B2CCallBack struct {
Result struct {
ResultType int `json:"ResultType"`
ResultCode int `json:"ResultCode"`
ResultDesc string `json:"ResultDesc"`
OriginatorConversationID string `json:"OriginatorConversationID"`
ConversationID string `json:"ConversationID"`
TransactionID string `json:"TransactionID"`
ResultParameters struct {
ResultParameter []struct {
Key string `json:"Key"`
Value interface{} `json:"Value"`
} `json:"ResultParameter"`
} `json:"ResultParameters"`
ReferenceData struct {
ReferenceItem struct {
Key string `json:"Key"`
Value string `json:"Value"`
} `json:"ReferenceItem"`
} `json:"ReferenceData"`
} `json:"Result"`
}
// sample code
var payload model.B2CCallBack
if err := c.ShouldBindJSON(&payload); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
result := payload.Result
var ReceiverPartyPublicName string
var B2CRecipientIsRegisteredCustomer string
var TransactionCompletedDateTime string
for _, param := range result.ResultParameters.ResultParameter {
key := param.Key
if key == "ReceiverPartyPublicName" {
value := param.Value
if value != nil {
ReceiverPartyPublicName = value.(string)
}
} else if key == "TransactionCompletedDateTime" {
value := param.Value
if value != nil {
TransactionCompletedDateTime = value.(string)
}
} else if key == "B2CRecipientIsRegisteredCustomer" {
value := param.Value
if value != nil {
B2CRecipientIsRegisteredCustomer = value.(string)
}
}
}
if ReceiverPartyPublicName == "" || B2CRecipientIsRegisteredCustomer == "" || TransactionCompletedDateTime == "" {
fmt.Println("params are null")
}
//ReceiverName Struct
type ReceiverName struct {
ReceiverName string
}
//Split the tow, and trim spaces
receiverName := new(ReceiverName)
parts := strings.Split(ReceiverPartyPublicName, "-")
name := strings.TrimSpace(parts[1])
receiverName.ReceiverName = name
// struct
type C2BCallBack struct {
TransactionType string `json:"TransactionType"`
TransID string `json:"TransID"`
TransTime string `json:"TransTime"`
TransAmount string `json:"TransAmount"`
BusinessShortCode string `json:"BusinessShortCode"`
BillRefNumber string `json:"BillRefNumber"`
InvoiceNumber string `json:"InvoiceNumber"`
OrgAccountBalance string `json:"OrgAccountBalance"`
ThirdPartyTransID string `json:"ThirdPartyTransID"`
MSISDN string `json:"MSISDN"`
FirstName string `json:"FirstName"`
MiddleName string `json:"MiddleName"`
LastName string `json:"LastName"`
}
// sample code
var payload model.C2BCallBack
if err := c.ShouldBindJSON(&payload); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment