Skip to content

mpesa-express-payment-template: A Node.js starter template for Safaricom's M-Pesa Express API integration. Includes pre-configured environment variables and example code for merchant-initiated payments using the STK Push API. Ideal for quickly adding M-Pesa payment functionality to your app.

License

Notifications You must be signed in to change notification settings

felixkamau/mpesa-express-payment-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Template code for M-pesa daraja-api


M-Pesa Express Merchant Initiated Online Payment API

This project provides a Node.js script to initiate M-Pesa Express payments using Safaricom's Daraja API. The script uses Axios for HTTP requests and dotenv for environment variable management.

Prerequisites

  • Node.js and npm installed.
  • An active Safaricom account with access to the Daraja API.
  • Environment variables set up for Safaricom API credentials.

Setup

  1. Clone the Repository

    git clone <repository_url>
    cd <repository_directory>
  2. Install Dependencies

    Make sure you have axios and dotenv installed:

    npm install axios dotenv
  3. Create Environment File

    Create a .env file in the root directory of your project and add the following environment variables:

    BASE_URL=https://sandbox.safaricom.co.ke
    CONSUMER_KEY=your_consumer_key
    CONSUMER_SECRET=your_consumer_secret
    SHORT_CODE=your_shortcode
    PASSKEY=your_passkey
    CALLBACK_URL=your_callback_url

    Replace the placeholder values with your actual Safaricom API credentials.

Code Overview

The script contains two main functions:

  1. getAccessToken

    Retrieves an access token using the Consumer Key and Consumer Secret.

  2. mpesaExpress

    Initiates a payment request to the M-Pesa API.

Example Usage

To use the mpesaExpress function, replace the phone number and amount in the example:

import { mpesaExpress } from './path_to_your_script';

mpesaExpress('+254708374149', 1)
  .then(response => console.log('Payment Response:', response))
  .catch(error => console.error('Payment Error:', error));

Running the Script

To run the script, use the following command:

node your_script.js

Error Handling

Ensure that your API credentials and environment variables are correct. Common issues might include:

  • Incorrect or expired API credentials.
  • Incorrect endpoint URLs.

Check the response from the API and consult the Safaricom Daraja API documentation for further troubleshooting.

License

This project is licensed under the MIT License - see the LICENSE file for details.


Feel free to adjust the details to better fit your project specifics!

About

mpesa-express-payment-template: A Node.js starter template for Safaricom's M-Pesa Express API integration. Includes pre-configured environment variables and example code for merchant-initiated payments using the STK Push API. Ideal for quickly adding M-Pesa payment functionality to your app.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published