Mpesa-Deno
A Deno module for M-Pesa Daraja API calls.
Ready to use:
Adding to your project
Import the module from Deno.land as follows;
import { Mpesa } from "https://deno.land/x/mpesa/mod.ts";
Requirements
You will need the following from Safaricom:
- Consumer Key.
- Consumer Secret.
- Security Credentials for Production/Sandbox environment.
- [Callback server with Mpesa apis whitelisted]
How to use
const credentials = {
clientKey: 'YOUR_CONSUMER_KEY_HERE',
clientSecret: 'YOUR_CONSUMER_SECRET_HERE',
securityCredential: 'YOUR_SECURITY_CREDENTIAL',
};
//Environment is a string and may be either `sandbox` or `production`
const environment = "sandbox"
const mpesa = new Mpesa(credentials, environment);
Methods
Register
The C2B Register URL API registers the 3rd party’s confirmation and validation URLs to M-Pesa ; which then maps these URLs to the 3rd party shortcode. Whenever M-Pesa receives a transaction on the shortcode, M-Pesa triggers a validation request against the validation URL and the 3rd party system responds to M-Pesa with a validation response (either a success or an error code). The response expected is the success code the 3rd party
M-Pesa completes or cancels the transaction depending on the validation response it receives from the 3rd party system. A confirmation request of the transaction is then sent by M-Pesa through the confirmation URL back to the 3rd party which then should respond with a success acknowledging the confirmation.
mpesa
.c2bregister({
ShortCode: "Short Code",
ConfirmationURL: "Confirmation URL",
ValidationURL: "Validation URL",
ResponseType: "Response Type",
})
.then((response) => {
//Do something with the response
//eg
console.log(response);
})
.catch((error) => {
//Do something with the error;
//eg
console.error(error);
});
- ShortCode - The short code of the organization.
- ResponseType - Default response type for timeout.
- ConfirmationURL- Confirmation URL for the client.
- ValidationURL - Validation URL for the client.
Lipa na mpesa online
Lipa na M-Pesa Online Payment API is used to initiate a M-Pesa transaction on behalf of a customer using STK Push. This is the same technique mySafaricom App uses whenever the app is used to make payments.
mpesa
.lipaNaMpesaOnline({
BusinessShortCode: 123456,
Amount: 1000 /* 1000 is an example amount */,
PartyA: "Party A",
PhoneNumber: "Phone Number",
CallBackURL: "CallBack URL",
AccountReference: "Account Reference",
passKey: "Lipa Na Mpesa Pass Key",
TransactionType: "Transaction Type" /* OPTIONAL */,
TransactionDesc: "Transaction Description" /* OPTIONAL */,
})
.then((response) => {
//Do something with the response
//eg
console.log(response);
})
.catch((error) => {
//Do something with the error;
//eg
console.error(error);
});
- BusinessShortCode - The organization shortcode used to receive the transaction.
- Amount - The amount to be transacted.
- PartyA - The MSISDN sending the funds.
- PartyB - The organization shortcode receiving the funds. Default is the BusinessShorCode.
- PhoneNumber - The MSISDN sending the funds.
- CallBackURL - The url to where responses from M-Pesa will be sent to.
- AccountReference - Used with M-Pesa PayBills.
- TransactionDesc - A description of the transaction.
- passKey - Lipa Na Mpesa Pass Key.
- Transaction Type - Default is
CustomerPayBillOnline
Lipa na mpesa online query
mpesa
.lipaNaMpesaQuery({
BusinessShortCode: 123456,
CheckoutRequestID: "Checkout Request ID",
passKey: "Lipa Na Mpesa Pass Key",
})
.then((response) => {
//Do something with the response
//eg
console.log(response);
})
.catch((error) => {
//Do something with the error;
//eg
console.error(error);
});
- BusinessShortCode - Business Short Code
- CheckoutRequestID - Checkout RequestID
- Lipa Na Mpesa Pass Key
Simulate
mpesa
.c2bsimulate({
ShortCode: 123456,
Amount: 1000 /* 1000 is an example amount */,
Msisdn: 254792123456,
CommandID: "Command ID" /* OPTIONAL */,
BillRefNumber: "Bill Reference Number" /* OPTIONAL */,
})
.then((response) => {
//Do something with the response
//eg
console.log(response);
})
.catch((error) => {
//Do something with the error;
//eg
console.error(error);
});
- ShortCode - 6 digit M-Pesa Till Number or PayBill Number
- CommandID - Unique command for each transaction type. Default is
CustomerPayBillOnline
- Amount - The amount been transacted.
- MSISDN - MSISDN (phone number) sending the transaction, start with country code without the plus(+) sign.
- BillRefNumber - Bill Reference Number (Optional).
Account Balance
The Account Balance API requests for the account balance of a shortcode.
mpesa
.accountBalance({
Initiator: "Initiator Name",
PartyA: "Party A",
IdentifierType: "Identifier Type",
QueueTimeOutURL: "Queue Timeout URL",
ResultURL: "Result URL",
CommandID: "Command ID" /* OPTIONAL */,
Remarks: "Remarks" /* OPTIONAL */,
})
.then((response) => {
//Do something with the response
//eg
console.log(response);
})
.catch((error) => {
//Do something with the error;
//eg
console.error(error);
});
- Initiator - This is the credential/username used to authenticate the transaction request.
- CommandID - A unique command passed to the M-Pesa system. Default is
AccountBalance
- PartyB - The shortcode of the organisation receiving the transaction.
- ReceiverIdentifierType - Type of the organisation receiving the transaction.
- Remarks - Comments that are sent along with the transaction.
- QueueTimeOutURL - The timeout end-point that receives a timeout message.
- ResultURL - The end-point that receives a successful transaction.
Transaction Status
Transaction Status API checks the status of a B2B, B2C and C2B APIs transactions.
mpesa
.transactionStatus({
Initiator: "Initiator",
TransactionID: "Transaction ID",
PartyA: "Party A",
IdentifierType: "Identifier Type",
ResultURL: "Result URL",
QueueTimeOutURL: "Queue Timeout URL",
CommandID: "Command ID" /* OPTIONAL */,
Remarks: "Remarks" /* OPTIONAL */,
Occasion: "Occasion" /* OPTIONAL */,
})
.then((response) => {
//Do something with the response
//eg
console.log(response);
})
.catch((error) => {
//Do something with the error;
//eg
console.error(error);
});
- Initiator - The name of Initiator to initiating the request.
- CommandID - Unique command for each transaction type, possible values are:
TransactionStatusQuery
. - TransactionID - Organization Receiving the funds.
- Party A - Organization /MSISDN sending the transaction.
- IdentifierType - Type of organization receiving the transaction.
- ResultURL - The path that stores information of transaction.
- QueueTimeOutURL - The path that stores information of time out transaction.
- Remarks - Comments that are sent along with the transaction.
- Occasion - Optional.
Reversal
Reverses a B2B, B2C or C2B M-Pesa transaction.
mpesa
.reversal({
Initiator: "Initiator",
TransactionID: "Transaction ID",
Amount: 1000 /* 1000 is an example amount */,
ReceiverParty: "Reciever Party",
ResultURL: "Result URL",
QueueTimeOutURL: "Queue Timeout URL",
CommandID: "Command ID" /* OPTIONAL */,
RecieverIdentifierType: 11 /* OPTIONAL */,
Remarks: "Remarks" /* OPTIONAL */,
Occasion: "Ocassion" /* OPTIONAL */,
})
.then((response) => {
//Do something with the response
//eg
console.log(response);
})
.catch((error) => {
//Do something with the error;
//eg
console.error(error);
});
- Initiator - This is the credential/username used to authenticate the transaction request.
- TransactionID - Organization Receiving the funds.
- Amount - The Amount To Be Reversed
- PartyA - Organization/MSISDN sending the transaction.
- RecieverIdentifierType - Type of organization receiving the transaction. Default is
11
- ResultURL - The path that stores information of transaction.
- QueueTimeOutURL - The path that stores information of time out transaction.
- Remarks - Comments that are sent along with the transaction.
- Occasion - Optional.
- Command ID - Default is
TransactionReversal
Name | Role |
---|---|
Newton Munene | Contributor |
Aubrey O | Contributor |