/NodeJsDaraja

A simple REST API service built with javascript, express and mongodb showing how to use daraja API by safaricom to intergrate payments for eccommerce app. It utilises the Express STK push to prompt the customer to authenticate the request by the input of their Mpesa pin

Primary LanguageJavaScript

runme
id version
01HGZNS58YACW06V3H4MDWQS3S
v2.0

M-Pesa Stk Push Backend

This backend code is designed to facilitate M-Pesa Stk Push transactions. It includes functionality for generating OAuth tokens, initiating Stk Push transactions, and handling callbacks for transaction status updates.

Prerequisites

Before running the backend code, make sure you have the following set up:

  • Node.js installed
  • npm (Node Package Manager) installed
  • MongoDB or another database (For storing transaction data)

Installation

  1. Clone the repository:
git clone https://github.com/Domains18/NodejsDaraja
  1. Navigate to the project directory:
cd /path/to/your/backend
  1. Install dependencies:
npm install
  1. Set up environment variables:

Create a .env file in the project root and configure the following variables:

CONSUMER_KEY=your_consumer_key
CONSUMER_SECRET=your_consumer_secret
MPESA_SHORTCODE=your_mpesa_shortcode
MPESA_PASSKEY=your_mpesa_passkey
MONGODB_URI=your_mongodb_uri
  1. Start the server:
npm start

The backend server should now be running.

Endpoints

1. Generate OAuth Token and PushStk

  • Endpoint: /api/stkpush
  • Method: POST
  • Description: Generates an OAuth token required for M-Pesa API requests and innitiates a pushstk request.

3. Callback for Transaction Status

  • Endpoint: /api/callback

  • Method: POST

  • Description: Handles callbacks from M-Pesa to update transaction status.

  • If you do not have postman or want to use a frontend, click here FrontEnd

Additional Information

  • This backend code uses Axios for making HTTP requests.
  • Transaction data is stored in a MongoDB database.
  • Callbacks from M-Pesa are handled to update the transaction status.

Feel free to customize and extend the backend based on your specific requirements.

License

This project is licensed under the MIT License.