M-Pesa C2B Payment

Processar pagamentos de cliente para negócio via M-Pesa

M-Pesa C2B Payment

AI-generated

Overview

This node processes Customer to Business (C2B) payments via the M-Pesa mobile money platform. It enables users to initiate payment transactions where customers pay a business directly using their mobile phones. Typical use cases include e-commerce checkouts, bill payments, and service fees collection where businesses want to receive payments through M-Pesa.

For example, an online store can use this node to charge customers by specifying the transaction reference, customer phone number, amount, and other details. The node handles validation of inputs and communicates with the M-Pesa API to execute the payment.

Properties

Name Meaning
Transaction Reference Reference for the transaction (e.g., card number, invoice reference).
Customer MSISDN Customer's phone number in the format 258XXXXXXXXX (country code + number).
Amount The monetary value of the transaction (must be a positive number).
Third Party Reference Unique reference from the third-party system identifying the transaction.
Service Provider Code The shortcode or code of the service provider handling the payment.
Auto Generate Reference Boolean flag indicating whether to auto-generate the third party reference if not provided.

Output

The node outputs a JSON object per input item containing:

  • The full response from the M-Pesa API call.
  • The original request data sent to the API.
  • A success boolean indicating if the payment was successful (true if response code is "INS-0").
  • A timestamp of when the response was received.

If the node encounters errors and "Continue On Fail" is enabled, it outputs an error message instead of stopping execution.

No binary data output is produced by this node.

Dependencies

  • Requires an API key credential for authenticating with the M-Pesa API.
  • Uses an internal helper utility to interact with the M-Pesa API endpoints.
  • The node expects the service provider code to be either provided as input or retrieved from credentials.
  • Proper network access to the M-Pesa API endpoint /ipg/v1x/c2bPayment/singleStage/ is necessary.

Troubleshooting

  • Invalid MSISDN Format: If the customer phone number is not in the required format (258XXXXXXXXX), the node throws an error. Ensure the phone number includes the country code and has no extra characters.
  • Invalid Amount: The amount must be a positive number. Non-numeric or negative values will cause an error.
  • Reference Too Long: The third party reference cannot exceed 20 characters. Shorten the reference or enable auto-generation.
  • API Errors: If the M-Pesa API returns an error, the node will throw an exception unless "Continue On Fail" is enabled, in which case the error message is returned in the output.
  • Missing Credentials: The node requires valid API credentials configured in n8n; missing or invalid credentials will prevent execution.

Links and References

Discussion