Parse M-Pesa STK Push callbacks - #54
Open
FrankCidney wants to merge 6 commits into
Open
FrankCidney wants to merge 6 commits into
FrankCidney wants to merge 6 commits into
Conversation
carsonak
requested changes
Aug 4, 2026
carsonak
left a comment
Collaborator
There was a problem hiding this comment.
Great work! Check out the changes I suggested, push the changes to this PR and mark the changes as resolved so that I can merge this PR.
| if err := json.Unmarshal(item.Value, &referenceValue); err != nil { | ||
| return fmt.Errorf("metadata item %q has invalid Value: %w", item.Name, err) | ||
| } | ||
| result.ReferenceData[item.Name] = referenceValue |
Collaborator
There was a problem hiding this comment.
Instead of decoding unknown fields, we should preserve the original json values as json.RawMessage in the STKPushCallbackResult.ReferenceData map. We can leave the decoding to the users, as they might have more information about the type of the values.
| if callback.CallbackMetadata == nil { | ||
| // Failed payments normally omit metadata because no transaction was | ||
| // completed. A successful result cannot satisfy the public contract | ||
| // without its amount, receipt number, and customer number. |
Collaborator
There was a problem hiding this comment.
"Successful callbacks will always have all the named fields field out". Mention this in the ParseSTKPushCallback function documentation or the STKPushCallbackResult struct documentation.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this solves
M-Pesa sends the final outcome of an STK Push payment as a nested callback whose payment details are stored in a name/value metadata list. Applications currently have to decode that provider-specific structure themselves and distinguish malformed payloads from valid unsuccessful payment outcomes.
This adds a focused parser that converts the callback into a structured Go result while preserving provider metadata.
What changed
ParseSTKPushCallbackandSTKPushCallbackResultin thempesapackage.Testing
go test ./mpesa -count=1go test ./...go test -race ./mpesa -count=1go vet ./...golangci-lint run ./...Related issue
Closes #18