All Projects → edwinmugendi → sapamapay

edwinmugendi / sapamapay

Licence: other
New Safaricom MPESA (Daraja) API

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to sapamapay

safaricom-daraja-nodejs
Standalone M-Pesa payments service using the new Daraja Payment API by Safaricom.
Stars: ✭ 31 (+0%)
Mutual labels:  mpesa, mpesa-api, safaricom
mpesa-rest-api
mpesa rest api converts the mpesa api to a RESTful API that is easy for developers to use instead of the current SOAP web service provided by mpesa.
Stars: ✭ 24 (-22.58%)
Mutual labels:  mpesa, mpesa-api, safaricom
Chowder
Chowder for Android M-Pesa payments.
Stars: ✭ 31 (+0%)
Mutual labels:  mpesa-api, safaricom
mpesa-dart
A dart wrapper around mpesa daraja api
Stars: ✭ 19 (-38.71%)
Mutual labels:  mpesa, mpesa-api
mpesa-rust
An abstraction/wrapper on top of Safaricom's M-PESA daraja api in Rust
Stars: ✭ 17 (-45.16%)
Mutual labels:  mpesa, safaricom
python-daraja
Python Wrapper for interacting with the MPESA Daraja API. More Features to be implemented
Stars: ✭ 20 (-35.48%)
Mutual labels:  mpesa, mpesa-api
php-pesa
Open payment integration toolkit for PHP
Stars: ✭ 26 (-16.13%)
Mutual labels:  mpesa, mpesa-api
tz-mpesa-ussd-push
Vodacom Tanzania USSD Push API Client
Stars: ✭ 18 (-41.94%)
Mutual labels:  mpesa
JustJava-Android
JustJava is a mock food ordering and delivery application for a coffee shop.
Stars: ✭ 59 (+90.32%)
Mutual labels:  mpesa
ex pesa
Payment Library For Most Public Payment API's in Kenya and hopefully Africa. Let us get this moneybag
Stars: ✭ 19 (-38.71%)
Mutual labels:  mpesa
pesa-js
Pesa SDK
Stars: ✭ 19 (-38.71%)
Mutual labels:  mpesa
loopy
Loopy is an unofficial cli client for CBA Loop for the lazy bones
Stars: ✭ 23 (-25.81%)
Mutual labels:  mpesa
mobile-money
Mobile Money Payments API in Kenya (MPESA, EQUITY/EQUITEL,T-CASH). Supports B2B, B2C and C2B
Stars: ✭ 17 (-45.16%)
Mutual labels:  mpesa-api

Build Status License Development Version Monthly Installs StyleCI

SapamaPay API

This library is an API wrapper to the following Safaricom MPESA API's

##Installation

###Requirements

PHP >=4.0.2

Add edwinmugendi/sapamapay to composer.json.

"edwinmugendi/sapamapay": "master"

Run composer update to pull down the latest version.

Or run

composer require edwinmugendi/sapamapay

Without composer. Download the source code and require_once the autoload.php

require_once __DIR__ . '/../vendor/autoload.php';

##Testing Update the $api variable to the API you want to run.

<?php

require_once __DIR__ . '/../vendor/autoload.php'; // Autoload files using Composer autoload

use Edwinmugendi\Sapamapay\MpesaApi;

$mpesa_api = new MpesaApi();
$configs = array(
    'AccessToken' => 'ACCESSTOKEN',
    'Environment' => 'sandbox',
    'Content-Type' => 'application/json',
    'Verbose' => 'true',
);

$api = 'generate_token';

if ($api == 'stk_push') {
    $parameters = array(
        'BusinessShortCode' => '603013',
        'Password' => 'TkNZpjhQ',
        'Timestamp' => '20171010101010',
        'TransactionType' => 'TransactionType',
        'Amount' => '10',
        'PartyA' => '254708374149',
        'PartyB' => '603013',
        'PhoneNumber' => '254708374149',
        'CallBackURL' => 'https://url',
        'AccountReference' => '1232',
        'TransactionDesc' => 'TESTING',
    );
} else if ($api == 'stk_query') {
    $parameters = array(
        'BusinessShortCode' => '603013',
        'Password' => 'TkNZpjhQ',
        'Timestamp' => '20171010101010',
        'CheckoutRequestID' => 'ws_co_123456789',
    );
} else if ($api == 'account_balance') {
    $parameters = array(
        'CommandID' => 'AccountBalance',
        'PartyA' => '603013',
        'IdentifierType' => '4',
        'Remarks' => 'Remarks',
        'Initiator' => 'apiop41',
        'SecurityCredential' => 'TkNZpjhQ',
        'QueueTimeOutURL' => 'https://url',
        'ResultURL' => 'https://url',
    );
} else if ($api == 'b2b_payment_request') {
    $parameters = array(
        'CommandID' => 'BusinessPayBill',
        'Amount' => '10',
        'PartyA' => '603013',
        'SenderIdentifierType' => '4',
        'PartyB' => '600000',
        'RecieverIdentifierType' => '4',
        'Remarks' => 'Remarks',
        'Initiator' => 'apiop41',
        'SecurityCredential' => 'TkNZpjhQ',
        'QueueTimeOutURL' => 'https://url',
        'ResultURL' => 'https://url',
        'AccountReference' => '12',
    );
} else if ($api == 'b2c_payment_request') {
    $parameters = array(
        'InitiatorName' => 'apiop41',
        'SecurityCredential' => 'TkNZpjhQ',
        'CommandID' => 'SalaryPayment',
        'Amount' => '10',
        'PartyA' => '603013',
        'PartyB' => '254708374149',
        'Remarks' => 'Remarks',
        'QueueTimeOutURL' => 'https://url',
        'ResultURL' => 'https://url',
        'Occasion' => '12',
    );
} else if ($api == 'reversal') {
    $parameters = array(
        'CommandID' => 'TransactionReversal',
        'ReceiverParty' => '254708374149',
        'RecieverIdentifierType' => '1',
        'Remarks' => 'remarks',
        'Initiator' => 'apiop41',
        'SecurityCredential' => 'TkNZpjhQ',
        'QueueTimeOutURL' => 'https://url',
        'ResultURL' => 'https://url',
        'TransactionID' => '11211',
        'Occasion' => '12',
        'Amount' => '10',
    );
} else if ($api == 'transaction_status_request') {
    $parameters = array(
        'CommandID' => 'TransactionStatusQuery',
        'PartyA' => '254708374149',
        'IdentifierType' => '603013',
        'Remarks' => 'remarks',
        'Initiator' => 'apiop41',
        'SecurityCredential' => 'TkNZpjhQ',
        'QueueTimeOutURL' => 'https://url',
        'ResultURL' => 'https://url',
        'TransactionID' => '11211',
        'Occasion' => '12',
    );
} else if ($api == 'c2b_register_url') {
    $parameters = array(
        'ValidationURL' => 'https://url',
        'ConfirmationURL' => 'https://url',
        'ResponseType' => 'Completed',
        'ShortCode' => '603013',
    );
} else if ($api == 'c2b_simulate') {

    $parameters = array(
        'CommandID' => 'CustomerPayBillOnline',
        'Amount' => '100',
        'Msisdn' => '254708374149',
        'BillRefNumber' => 'TESTING',
        'ShortCode' => '603013',
    );
} else if ($api == 'generate_token') {
    $parameters = array(
        'ConsumerKey' => 'CONSUMER_KEY',
        'ConsumerSecret' => 'CONSUMER_SECRET',
    );
}//E# if statement

$response = $mpesa_api->call($api, $configs, $parameters);
echo 'JSON response: <p>';
echo json_encode($response);
echo '<p>Response var_dump:<p>';
var_dump($response);

##Authentication First call the generate_token to get the access token After getting the access token, set it in the AccessToken index in the $configs to make other calls.

##Configurations The $configs parameters has the following indices

  • AccessToken - The access token. Get the access to ken by running calling the `generate_token' API
  • Environment - Can be sandbox (when testing your app) or live (when your app is in production)
  • Content-Type - Should always be application/json
  • Verbose - (Optional) for easy debugging, set this index to run your code in verbose mode ie echo and var dump parameters
  • Url - (Optional), this overrides the endpoint. By default we use https://sandbox.safaricom.co.ke/ and https://api.safaricom.co.ke/ for sandbox and live respecitvely. Don't forget the forward slush as the end(/)
$configs = array(
    'AccessToken' => 'ACCESSTOKEN',
    'Environment' => 'sandbox',
    'Content-Type' => 'application/json',
    'Verbose' => 'true', //THIS
);

##Response The response has the following indices

  • Environment - live or sandbox
  • Name - The name of the API called
  • HttpVerb - get or post
  • HttpStatusCode - HTTP status code
  • HttpStatusMessage - HTTP status message
  • Message - Custom Message
  • Response - Response array
  • Endpoint - URL called
  • Parameters - Parameters passed to the URL
  • ExpectedResponse - Expected Reponse Parameters as documents in the API

Sample Json

{"Environment":"sandbox","Name":"Generate Token","HttpVerb":"get","HttpStatusCode":"200","HttpStatusMessage":"Success","Message":"Success","Response":{"access_token":"YdiXeOksM3G9WVgl7jR1pCtT2Ckt","expires_in":"3599"},"Endpoint":"https:\/\/sandbox.safaricom.co.ke\/oauth\/v1\/generate","Parameters":{"ConsumerKey":"Li2dKUeKhlX6Gw0Fpkbq6LEBndlpOuxZ","ConsumerSecret":"hX3Yyd0BGMBiYaln"},"ExpectedResponse":{"Expiry":{"name":"Token expiry time in seconds.","type":"Integer","sample_value":"3599"},"Access_Token":{"name":"Access token to access other APIs","type":"Alpha-Numeric","sample_value":"O22vJy6rnN2nRAnOPqZ8dkyGxmXG"}}}

Sample PHP Var dump

array (size=10)
  'Environment' => string 'sandbox' (length=7)
  'Name' => string 'Generate Token' (length=14)
  'HttpVerb' => string 'get' (length=3)
  'HttpStatusCode' => string '200' (length=3)
  'HttpStatusMessage' => string 'Success' (length=7)
  'Message' => string 'Success' (length=7)
  'Response' => 
    array (size=2)
      'access_token' => string 'YdiXeOksM3G9WVgl7jR1pCtT2Ckt' (length=28)
      'expires_in' => string '3599' (length=4)
  'Endpoint' => string 'https://sandbox.safaricom.co.ke/oauth/v1/generate' (length=49)
  'Parameters' => 
    array (size=2)
      'ConsumerKey' => string 'Li2dKUeKhlX6Gw0Fpkbq6LEBndlpOuxZ' (length=32)
      'ConsumerSecret' => string 'hX3Yyd0BGMBiYaln' (length=16)
  'ExpectedResponse' => 
    array (size=2)
      'Expiry' => 
        array (size=3)
          'name' => string 'Token expiry time in seconds.' (length=29)
          'type' => string 'Integer' (length=7)
          'sample_value' => string '3599' (length=4)
      'Access_Token' => 
        array (size=3)
          'name' => string 'Access token to access other APIs' (length=33)
          'type' => string 'Alpha-Numeric' (length=13)
          'sample_value' => string 'O22vJy6rnN2nRAnOPqZ8dkyGxmXG' (length=28)

##Help For API integration assistance, bugs or assistance, kindly reach me on [email protected]

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].