M-Pesa icon

M-Pesa

Process and verify M-Pesa payments

Overview

This node integrates with the M-Pesa payment system, allowing users to either initiate an STK Push payment request or query the status of a specific M-Pesa transaction. It is useful for automating mobile money payments and tracking their statuses within workflows.

A common scenario includes:

  • Initiating a payment request to a customer’s phone number (STK Push).
  • Querying the status of a previously initiated transaction by its transaction ID to confirm success or failure.

For example, a business can use this node to charge customers via M-Pesa and then verify if the payment was completed successfully by querying the transaction status.

Properties

Name Meaning
Transaction ID The unique M-Pesa transaction identifier to query (e.g., NEF61H8J60).
Result URL The URL where M-Pesa will send the transaction status result notification.
Queue Timeout URL The URL where M-Pesa will send timeout notifications if the transaction status query times out.

Output

The node outputs JSON data containing the response from the M-Pesa API for each input item processed.

  • For Query Transaction operation, the output JSON includes fields indicating whether the query was successful (success: true) along with detailed transaction status information returned by M-Pesa.
  • If an error occurs, the output JSON contains success: false and an error message describing the issue.

No binary data output is produced by this node.

Dependencies

  • Requires an active M-Pesa API credential with necessary keys and secrets configured in n8n.
  • Needs valid credentials including consumer key, consumer secret, business shortcode, passkey, initiator name, and initiator password for querying transactions.
  • Uses Safaricom’s OAuth 2.0 token endpoint to obtain access tokens.
  • Connects to either the M-Pesa production or sandbox API endpoints depending on the environment setting in credentials.

Troubleshooting

  • Missing Initiator Credentials: Querying transaction status requires both initiator name and password. If missing, the node throws an error instructing to add these credentials.
  • Invalid Access Token: If the access token expires or is invalid, the node automatically retries obtaining a new token once before failing.
  • API Errors: Errors returned by the M-Pesa API are surfaced clearly in the node output. Common issues include invalid transaction IDs or network problems.
  • Timeouts: If the transaction status query times out, M-Pesa sends a notification to the provided queue timeout URL; ensure this URL is reachable and correctly configured.

Links and References

Discussion