Giter Club home page Giter Club logo

safaricom-daraja-sdk's Introduction

Daraja SDK

npm version

A JavaScript SDK for easy integration with the Safaricom Daraja API, facilitating seamless mobile payment and USSD services integration.

Installation

You can install the daraja-api-sdk via npm:

npm install daraja-api-sdk

Usage

const { Transaction } = require('daraja-api-sdk')

// Initialize SDK with your credentials
const transaction = new Transaction({
  customerKey: 'yourCustomerKey',
  customerKeySecret: 'yourCustomerKeySecret',
})

// Example usage: Initiating STK push
// Change these values with your actual details...This is just for demo
const options = {
  BusinessShortCode: '174379',
  passKey: 'Pass key can be found in the your safaricom developer dashboard'
  TransactionType: 'CustomerPayBillOnline',
  Amount: '1',
  PartyA: 'number sending money',
  PartyB: '174379',
  PhoneNumber: 'The Mobile Number to receive the STK Pin Prompt',
  CallBackURL:
    'It is the endpoint to which the results will be sent by M-Pesa API Party A completes or cancel the transaction',
  AccountReference: 'Test',
  TransactionDesc: 'Test',
}

// Using async to optimize the code to Send the transaction request
const sendTransaction = async () => {
  try {
    const result = await transaction.sendTransaction(options); 
    console.log('STK push initiated:', result);
  } catch (error) {
    console.error('Error initiating STK push:', error.message);
  }
};

// Call the function to initiate the transaction
sendTransaction();

If everything is correct you should get back an object:

{
  "status": 200,
  "MerchantRequestID": "29115-34620561-1",
  "CheckoutRequestID": "ws_CO_191220191020363925",
  "ResponseCode": "0",
  "ResponseDescription": "Success. Request accepted for processing",
  "CustomerMessage": "Success. Request accepted for processing"
}

If you get an error you should get back an object with status code , error message and a possible explanation of the error.

Documentation

For detailed documentation, API reference, and examples, visit the Github respository.

safaricom-daraja-sdk's People

Contributors

addyxx-h4ck1 avatar dav-oss avatar

Stargazers

Kramer  avatar Alfie avatar Medrine Mulindi avatar Miss Kanyi avatar martin nkemakolam avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.