├── .gitignore ├── .DS_Store ├── bun.lockb ├── src ├── .DS_Store ├── index.ts ├── types.ts ├── certs │ ├── sandbox │ │ └── cert.cer │ └── live │ │ └── cert.cer ├── service.ts ├── billing.ts ├── useMpesa.ts └── mpesa.ts ├── .vscode └── settings.json ├── jsr.json ├── dist ├── types.js.map ├── types.js ├── index.d.ts ├── index.js.map ├── index.js ├── types.d.ts ├── service.d.ts ├── useMpesa.d.ts ├── service.js.map ├── billing.js.map ├── billing.d.ts ├── billing.js ├── service.js ├── mpesa.d.ts ├── useMpesa.js.map ├── mpesa.js.map ├── useMpesa.js └── mpesa.js ├── nodemon.json ├── package.json ├── tsconfig.json ├── examples ├── compose.ts └── index.ts ├── README.md ├── tsconfig.tsbuildinfo └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | examples/*.js 3 | .env 4 | -------------------------------------------------------------------------------- /.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/osenco/mpesats/HEAD/.DS_Store -------------------------------------------------------------------------------- /bun.lockb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/osenco/mpesats/HEAD/bun.lockb -------------------------------------------------------------------------------- /src/.DS_Store: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/osenco/mpesats/HEAD/src/.DS_Store -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "editor.tabCompletion": "on", 3 | "diffEditor.codeLens": true 4 | } -------------------------------------------------------------------------------- /jsr.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@osenco/mpesa", 3 | "version": "0.1.1", 4 | "exports": "./src/index.ts" 5 | } -------------------------------------------------------------------------------- /dist/types.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":""} -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | export * from './mpesa' 2 | export * from './billing' 3 | export * from './types' 4 | export * from './useMpesa' -------------------------------------------------------------------------------- /dist/types.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | Object.defineProperty(exports, "__esModule", { value: true }); 3 | //# sourceMappingURL=types.js.map -------------------------------------------------------------------------------- /dist/index.d.ts: -------------------------------------------------------------------------------- 1 | export * from './mpesa'; 2 | export * from './billing'; 3 | export * from './types'; 4 | export * from './useMpesa'; 5 | -------------------------------------------------------------------------------- /nodemon.json: -------------------------------------------------------------------------------- 1 | { 2 | "watch": ["src"], 3 | "ext": "ts", 4 | "ignore": [], 5 | "exec": "npm run build && node ./dist/test.js" 6 | } 7 | -------------------------------------------------------------------------------- /dist/index.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAuB;AACvB,4CAAyB;AACzB,0CAAuB;AACvB,6CAA0B"} -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@osenco/mpesa", 3 | "version": "0.1.1", 4 | "description": "M-Pesa API typescript SDK", 5 | "main": "dist/index.js", 6 | "scripts": { 7 | "build": "tsc", 8 | "deploy": "npm publish --access public" 9 | }, 10 | "repository": { 11 | "type": "git", 12 | "url": "https://github.com/osenco/mpesats.git" 13 | }, 14 | "homepage": "https://github.com/osenco/mpesats", 15 | "author": "Osen Concepts ", 16 | "license": "MIT", 17 | "dependencies": { 18 | "axios": "^1.7.2", 19 | "date-fns": "^3.6.0" 20 | }, 21 | "devDependencies": { 22 | "@types/node": "^20.12.12", 23 | "typescript": "^5.4.5" 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "http://json.schemastore.org/tsconfig", 3 | "compilerOptions": { 4 | "outDir": "dist", 5 | "rootDir": "src", 6 | 7 | "emitDecoratorMetadata": true, 8 | "experimentalDecorators": true, 9 | "noImplicitAny": true, 10 | "strictNullChecks": true, 11 | "resolveJsonModule": true, 12 | "skipLibCheck": true, 13 | 14 | "incremental": true, 15 | 16 | "lib": ["es2018", "esnext.asynciterable"], 17 | "module": "commonjs", 18 | "esModuleInterop": true, 19 | "moduleResolution": "node", 20 | "target": "es2017", 21 | "sourceMap": true, 22 | "declaration": true 23 | }, 24 | "include": ["src", "mytest.ts"], 25 | "exclude": [ 26 | "**/node_modules/**", 27 | "**/dist/**", 28 | "**/*.d.ts" 29 | ] 30 | } 31 | -------------------------------------------------------------------------------- /src/types.ts: -------------------------------------------------------------------------------- 1 | export type MpesaConfig = { 2 | env: "sandbox" | "live"; 3 | type: number; 4 | shortcode: number; 5 | store: number | null; 6 | key: string; 7 | secret: string; 8 | username: string; 9 | password: string; 10 | passkey: string; 11 | }; 12 | 13 | export type MpesaResponse = { data: any; error: any }; 14 | 15 | export type MpesaSTKResponse = { 16 | MerchantRequestID: string | null; 17 | CheckoutRequestID: string | null; 18 | ResponseCode: number | null; 19 | ResponseDescription: string | null; 20 | CustomerMessage: string | null; 21 | errorCode?: number | null; 22 | errorMessage?: string | null; 23 | }; 24 | 25 | export type ResponseType = "Completed" | "Cancelled"; 26 | 27 | export type B2BCommands = 28 | | "BusinessPayBill" 29 | | "BusinessBuyGoods" 30 | | "DisburseFundsToBusiness" 31 | | "BusinessToBusinessTransfer" 32 | | "MerchantToMerchantTransfer"; 33 | 34 | export type B2CCommands = 35 | | "BusinessPayment" 36 | | "SalaryPayment" 37 | | "PromotionPayment"; 38 | -------------------------------------------------------------------------------- /dist/index.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { 3 | if (k2 === undefined) k2 = k; 4 | var desc = Object.getOwnPropertyDescriptor(m, k); 5 | if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { 6 | desc = { enumerable: true, get: function() { return m[k]; } }; 7 | } 8 | Object.defineProperty(o, k2, desc); 9 | }) : (function(o, m, k, k2) { 10 | if (k2 === undefined) k2 = k; 11 | o[k2] = m[k]; 12 | })); 13 | var __exportStar = (this && this.__exportStar) || function(m, exports) { 14 | for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); 15 | }; 16 | Object.defineProperty(exports, "__esModule", { value: true }); 17 | __exportStar(require("./mpesa"), exports); 18 | __exportStar(require("./billing"), exports); 19 | __exportStar(require("./types"), exports); 20 | __exportStar(require("./useMpesa"), exports); 21 | //# sourceMappingURL=index.js.map -------------------------------------------------------------------------------- /dist/types.d.ts: -------------------------------------------------------------------------------- 1 | export declare type MpesaConfig = { 2 | env: "sandbox" | "live"; 3 | type: number; 4 | shortcode: number; 5 | store: number | null; 6 | key: string; 7 | secret: string; 8 | username: string; 9 | password: string; 10 | passkey: string; 11 | validationUrl: string; 12 | confirmationUrl: string; 13 | callbackUrl: string; 14 | timeoutUrl?: string; 15 | resultUrl?: string; 16 | billingUrl?: string; 17 | }; 18 | export declare type MpesaResponse = { 19 | data: any; 20 | error: any; 21 | }; 22 | export declare type MpesaSTKResponse = { 23 | MerchantRequestID: string | null; 24 | CheckoutRequestID: string | null; 25 | ResponseCode: number | null; 26 | ResponseDescription: string | null; 27 | CustomerMessage: string | null; 28 | errorCode: number | null; 29 | errorMessage: string | null; 30 | }; 31 | export declare type ResponseType = "Completed" | "Cancelled"; 32 | export declare type B2BCommands = "BusinessPayBill" | "BusinessBuyGoods" | "DisburseFundsToBusiness" | "BusinessToBusinessTransfer" | "MerchantToMerchantTransfer"; 33 | export declare type B2CCommands = "BusinessPayment" | "SalaryPayment" | "PromotionPayment"; 34 | -------------------------------------------------------------------------------- /dist/service.d.ts: -------------------------------------------------------------------------------- 1 | import { AxiosResponse } from "axios"; 2 | import { MpesaConfig } from "./types"; 3 | export declare class Service { 4 | private http; 5 | token: string | null; 6 | /** 7 | * @param object config Configuration options 8 | */ 9 | config: MpesaConfig; 10 | /** 11 | * Setup global configuration for classes 12 | * @param Array configs Formatted configuration options 13 | * 14 | * @return void 15 | */ 16 | constructor(configs: MpesaConfig); 17 | /** 18 | * Fetch Token To Authenticate Requests 19 | * 20 | * @return string Access token 21 | */ 22 | authenticate(token?: string | null): any; 23 | generateSecurityCredential(): Promise; 24 | /** 25 | * Perform a GET request to the M-PESA Daraja API 26 | * @param endpoint Daraja API URL Endpoint 27 | * @param credentials Formated Auth credentials 28 | * 29 | * @return string/bool 30 | */ 31 | get(endpoint: string): Promise>; 32 | /** 33 | * Perform a POST request to the M-PESA Daraja API 34 | * @param endpoint Daraja API URL Endpoint 35 | * @param Array data Formated array of data to send 36 | * 37 | * @return string/bool 38 | */ 39 | post(endpoint: string, payload: any): Promise; 40 | } 41 | -------------------------------------------------------------------------------- /examples/compose.ts: -------------------------------------------------------------------------------- 1 | import { useMpesa } from "../dist"; 2 | 3 | const { stkPush, billManager } = useMpesa({ 4 | env: "sandbox", 5 | type: 4, 6 | shortcode: 174379, 7 | store: 174379, 8 | key: "9v38Dtu5u2BpsITPmLcXNWGMsjZRWSTG", 9 | secret: "bclwIPkcRqw61yUt", 10 | username: "apitest", 11 | password: "", 12 | passkey: "bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919", 13 | validationUrl: "https://shop.osen.co.ke/lipwa/validate", 14 | confirmationUrl: "https://shop.osen.co.ke/lipwa/confirm", 15 | callbackUrl: "https://shop.osen.co.ke/lipwa/reconcile", 16 | timeoutUrl: "https://shop.osen.co.ke/lipwa/timeout", 17 | resultUrl: "https://shop.osen.co.ke/lipwa/results", 18 | }); 19 | 20 | try { 21 | stkPush(254115911300, 10) 22 | .then(({ error, data }) => { 23 | if (data) { 24 | const { 25 | MerchantRequestID, 26 | CheckoutRequestID, 27 | ResponseCode, 28 | ResponseDescription, 29 | CustomerMessage, 30 | } = data; 31 | console.log(MerchantRequestID); 32 | } 33 | 34 | if (error) { 35 | const { errorCode, errorMessage } = error; 36 | console.log(errorCode, errorMessage); 37 | } 38 | }) 39 | .catch((e: any) => { 40 | console.log(e); 41 | }); 42 | } catch (error) { 43 | console.log(error); 44 | } 45 | 46 | billManager() 47 | .onboard( 48 | "hi@osen.co.ke", 49 | 254705459494, 50 | "https://osen.co.ke/wp-content/uploads/2019/11/logo.png", 51 | 1 52 | ) 53 | .then(({ error, data }) => {}) 54 | .catch((e) => {}); 55 | -------------------------------------------------------------------------------- /dist/useMpesa.d.ts: -------------------------------------------------------------------------------- 1 | import { BillManager } from "./billing"; 2 | import { MpesaResponse, B2BCommands, B2CCommands, MpesaConfig, ResponseType } from "./types"; 3 | export declare const useMpesa: (configs: MpesaConfig, token?: string | null) => { 4 | billManager: () => BillManager; 5 | stkPush: (phone: string | number, amount: number, reference?: string | number, description?: string, remark?: string) => Promise; 6 | registerUrls: (response_type?: ResponseType) => Promise; 7 | simulateC2B: (phone: string | number, amount?: number, reference?: string | number, command?: string) => Promise<{ 8 | data: any; 9 | error: null; 10 | } | { 11 | data: null; 12 | error: any; 13 | } | undefined>; 14 | sendB2B: (receiver: string | number, receiver_type: string | number, amount: number, command?: B2BCommands, reference?: string | number, remarks?: string) => Promise; 15 | sendB2C: (phone: string | number, amount?: number, command?: B2CCommands, remarks?: string, occassion?: string) => Promise; 16 | checkBalance: (command: string, remarks?: string) => Promise; 17 | checkStatus: (transaction: string, command?: string, remarks?: string, occasion?: string) => Promise; 18 | reverseTransaction: (transaction: string, amount: number, receiver: number, receiver_type?: number, remarks?: string, occasion?: string) => Promise; 19 | validateTransaction: (ok: boolean) => { 20 | ResultCode: number; 21 | ResultDesc: string; 22 | }; 23 | confirmTransaction: (ok: boolean, data: any, callback: Function) => { 24 | ResultCode: number; 25 | ResultDesc: string; 26 | }; 27 | reconcileTransaction: (ok: boolean) => { 28 | ResultCode: number; 29 | ResultDesc: string; 30 | }; 31 | processResults: (ok: boolean) => { 32 | ResultCode: number; 33 | ResultDesc: string; 34 | }; 35 | processTimeout: (callback: CallableFunction, ok: boolean) => { 36 | ResultCode: number; 37 | ResultDesc: string; 38 | }; 39 | }; 40 | -------------------------------------------------------------------------------- /src/certs/sandbox/cert.cer: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIGKzCCBROgAwIBAgIQDL7NH8cxSdUpl0ihH0A1wTANBgkqhkiG9w0BAQsFADBN 3 | MQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGlnaUNlcnQgSW5jMScwJQYDVQQDEx5E 4 | aWdpQ2VydCBTSEEyIFNlY3VyZSBTZXJ2ZXIgQ0EwHhcNMTgwODI3MDAwMDAwWhcN 5 | MTkwNDA0MTIwMDAwWjBuMQswCQYDVQQGEwJLRTEQMA4GA1UEBxMHTmFpcm9iaTEW 6 | MBQGA1UEChMNU2FmYXJpY29tIFBMQzETMBEGA1UECxMKRGlnaXRhbCBJVDEgMB4G 7 | A1UEAxMXc2FuZGJveC5zYWZhcmljb20uY28ua2UwggEiMA0GCSqGSIb3DQEBAQUA 8 | A4IBDwAwggEKAoIBAQC78yeC/wLoZY6TJeqc4g/9eAKIpeCwEsjX09pD8ZxAGXqT 9 | Oi7ssdIGJBPmJZNeEVyf8ocFhisCuLngJ9Z5e/AvH52PhrEFmVu2D03zSf4C+rhZ 10 | ndEKP6G79pUAb/bemOliU9zM8xYYkpCRzPWUzk6zSDarg0ZDLw5FrtZj/VJ9YEDL 11 | WGgAfwExEgSN3wjyUlJ2UwI3wqQXLka0VNFWoZxUH5j436gbSWRIL6NJUmrq8V8S 12 | aTEPz3eJHj3NOToDu245c7VKdF/KExyZjRjD2p5I+Aip80TXzKlZj6DjMb3DlfXF 13 | Hsnu0+1uJE701mvKX7BiscxKr8tCRphL63as4dqvAgMBAAGjggLkMIIC4DAfBgNV 14 | HSMEGDAWgBQPgGEcgjFh1S8o541GOLQs4cbZ4jAdBgNVHQ4EFgQUzZmY7ZORLw9w 15 | qRbAQN5m9lJ28qMwIgYDVR0RBBswGYIXc2FuZGJveC5zYWZhcmljb20uY28ua2Uw 16 | DgYDVR0PAQH/BAQDAgWgMB0GA1UdJQQWMBQGCCsGAQUF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cf+g8F/MvCCVdNAiVMdz6UpCscf6BRPHNZ5ifvChGh7aUKjrVLLuF4Ls 33 | HE05qm6HNyV5eTa6wvcbc4ewguN1UDZvPWetSyfBk10Wbpor4znQ4TJ3Y9uCvsJH 34 | 41ldblDvZZ2z4kB2UYQ7iBkPlJSxSOaFgW/GGDXq49sz/995xzhVITHxh2SdLkI= 35 | -----END CERTIFICATE----- 36 | -------------------------------------------------------------------------------- /dist/service.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"service.js","sourceRoot":"","sources":["../src/service.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,kDAA4D;AAE5D,qDAAuC;AACvC,+CAAiC;AACjC,uCAAyB;AACzB,2CAA6B;AAE7B,MAAa,OAAO;IAwBhB;;;;;OAKG;IACH,YAAY,OAAoB;QA1BhC;;WAEG;QACI,WAAM,GAAgB;YACzB,GAAG,EAAE,SAAS;YACd,IAAI,EAAE,CAAC;YACP,SAAS,EAAE,MAAM;YACjB,KAAK,EAAE,MAAM;YACb,GAAG,EAAE,kCAAkC;YACvC,MAAM,EAAE,kBAAkB;YAC1B,QAAQ,EAAE,SAAS;YACnB,QAAQ,EAAE,EAAE;YACZ,OAAO,EAAE,kEAAkE;YAC3E,aAAa,EAAE,iBAAiB;YAChC,eAAe,EAAE,gBAAgB;YACjC,WAAW,EAAE,kBAAkB;YAC/B,UAAU,EAAE,gBAAgB;YAC5B,SAAS,EAAE,gBAAgB;YAC3B,UAAU,EAAE,gBAAgB;SAC/B,CAAC;QAQE,IAAI,CAAC,MAAM,GAAG,OAAO,CAAC;QAEtB,IAAI,CAAC,IAAI,GAAG,eAAK,CAAC,MAAM,CAAC;YACrB,OAAO,EACH,IAAI,CAAC,MAAM,CAAC,GAAG,KAAK,MAAM;gBACtB,CAAC,CAAC,6BAA6B;gBAC/B,CAAC,CAAC,iCAAiC;YAC3C,eAAe,EAAE,IAAI;SACxB,CAAC,CAAC;QAEH,IAAI,CAAC,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I,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,EAAE;YACjC,OAAO,EAAE;gBACL,aAAa,EAAE,IAAI;aACtB;SACJ,CAAC,CAAC;IACP,CAAC;IAED;;;;;;OAMG;IACI,KAAK,CAAC,IAAI,CAAC,QAAgB,EAAE,OAAY;QAC5C,OAAO,IAAI,CAAC,IAAI;aACX,IAAI,CAAC,QAAQ,EAAE,OAAO,EAAE;YACrB,OAAO,EAAE;gBACL,aAAa,EAAE,SAAS,GAAG,IAAI,CAAC,KAAK;aACxC;SACJ,CAAC;aACD,IAAI,CAAC,CAAC,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC,IAAI,CAAC;aACxB,KAAK,CAAC,CAAC,CAAM,EAAE,EAAE;YACd,IAAI,CAAC,CAAC,QAAQ,CAAC,IAAI,EAAE;gBACjB,OAAO,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC;aAC1B;iBAAM;gBACH,OAAO,EAAE,SAAS,EAAE,GAAG,EAAE,YAAY,EAAE,CAAC,CAAC,OAAO,EAAE,CAAC;aACtD;QACL,CAAC,CAAC,CAAC;IACX,CAAC;CACJ;AAzID,0BAyIC"} -------------------------------------------------------------------------------- /src/certs/live/cert.cer: -------------------------------------------------------------------------------- 1 | -----BEGIN CERTIFICATE----- 2 | MIIGkzCCBXugAwIBAgIKXfBp5gAAAD+hNjANBgkqhkiG9w0BAQsFADBbMRMwEQYK 3 | CZImiZPyLGQBGRYDbmV0MRkwFwYKCZImiZPyLGQBGRYJc2FmYXJpY29tMSkwJwYD 4 | VQQDEyBTYWZhcmljb20gSW50ZXJuYWwgSXNzdWluZyBDQSAwMjAeFw0xNzA0MjUx 5 | NjA3MjRaFw0xODAzMjExMzIwMTNaMIGNMQswCQYDVQQGEwJLRTEQMA4GA1UECBMH 6 | TmFpcm9iaTEQMA4GA1UEBxMHTmFpcm9iaTEaMBgGA1UEChMRU2FmYXJpY29tIExp 7 | bWl0ZWQxEzARBgNVBAsTClRlY2hub2xvZ3kx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BggrBgEFBQcwAoaBvGxkYXA6Ly8vQ049U2FmYXJpY29tJTIwSW50 24 | ZXJuYWwlMjBJc3N1aW5nJTIwQ0ElMjAwMixDTj1BSUEsQ049UHVibGljJTIwS2V5 25 | JTIwU2VydmljZXMsQ049U2VydmljZXMsQ049Q29uZmlndXJhdGlvbixEQz1zYWZh 26 | cmljb20sREM9bmV0P2NBQ2VydGlmaWNhdGU/YmFzZT9vYmplY3RDbGFzcz1jZXJ0 27 | aWZpY2F0aW9uQXV0aG9yaXR5MCsGCCsGAQUFBzABhh9odHRwOi8vY3JsLnNhZmFy 28 | aWNvbS5jby5rZS9vY3NwMAsGA1UdDwQEAwIFoDA9BgkrBgEEAYI3FQcEMDAuBiYr 29 | BgEEAYI3FQiHz4xWhMLEA4XphTaE3tENhqCICGeGwcdsg7m5awIBZAIBDDAdBgNV 30 | HSUEFjAUBggrBgEFBQcDAgYIKwYBBQUHAwEwJwYJKwYBBAGCNxUKBBowGDAKBggr 31 | BgEFBQcDAjAKBggrBgEFBQcDATANBgkqhkiG9w0BAQsFAAOCAQEAC/hWx7KTwSYr 32 | x2SOyyHNLTRmCnCJmqxA/Q+IzpW1mGtw4Sb/8jdsoWrDiYLxoKGkgkvmQmB2J3zU 33 | ngzJIM2EeU921vbjLqX9sLWStZbNC2Udk5HEecdpe1AN/ltIoE09ntglUNINyCmf 34 | zChs2maF0Rd/y5hGnMM9bX9ub0sqrkzL3ihfmv4vkXNxYR8k246ZZ8tjQEVsKehE 35 | dqAmj8WYkYdWIHQlkKFP9ba0RJv7aBKb8/KP+qZ5hJip0I5Ey6JJ3wlEWRWUYUKh 36 | gYoPHrJ92ToadnFCCpOlLKWc0xVxANofy6fqreOVboPO0qTAYpoXakmgeRNLUiar 37 | 0ah6M/q/KA== 38 | -----END CERTIFICATE----- 39 | -------------------------------------------------------------------------------- /dist/billing.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"billing.js","sourceRoot":"","sources":["../src/billing.ts"],"names":[],"mappings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E,CAAC;SACvC;IACF,CAAC;IAEM,KAAK,CAAC,aAAa,CAAC,OAS1B;QACA,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,CACvC,uCAAuC,EACvC,OAAO,CACP,CAAC;QAEF,IAAI,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,GAAG,EAAE;YACpC,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;SACvC;aAAM;YACN,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC;SACvC;IACF,CAAC;IAEM,KAAK,CAAC,aAAa,CAAC,iBAAyB;QACnD,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,CACvC,8CAA8C,EAC9C,EAAE,iBAAiB,EAAE,CACrB,CAAC;QAEF,IAAI,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,GAAG,EAAE;YACpC,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;SACvC;aAAM;YACN,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC;SACvC;IACF,CAAC;IAEM,KAAK,CAAC,cAAc,CAAC,QAAyC;QACpE,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,IAAI,CACvC,4CAA4C,EAC5C,QAAQ,CACR,CAAC;QAEF,IAAI,MAAM,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,GAAG,EAAE;YACpC,OAAO,EAAE,IAAI,EAAE,QAAQ,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;SACvC;aAAM;YACN,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,KAAK,EAAE,QAAQ,EAAE,CAAC;SACvC;IACF,CAAC;CACD;AAlMD,kCAkMC"} -------------------------------------------------------------------------------- /examples/index.ts: -------------------------------------------------------------------------------- 1 | import { Mpesa } from "../dist"; 2 | 3 | const mpesa = new Mpesa({ 4 | env: "sandbox", 5 | type: 4, 6 | shortcode: 174379, 7 | store: 174379, 8 | key: "9v38Dtu5u2BpsITPmLcXNWGMsjZRWSTG", 9 | secret: "bclwIPkcRqw61yUt", 10 | username: "apitest", 11 | password: "", 12 | passkey: "bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919", 13 | validationUrl: "https://shop.osen.co.ke/lipwa/validate", 14 | confirmationUrl: "https://shop.osen.co.ke/lipwa/confirm", 15 | callbackUrl: "https://shop.osen.co.ke/lipwa/reconcile", 16 | timeoutUrl: "https://shop.osen.co.ke/lipwa/timeout", 17 | resultUrl: "https://shop.osen.co.ke/lipwa/results", 18 | }); 19 | 20 | try { 21 | mpesa 22 | .stkPush("0115911300", 10) 23 | .then(({ error, data }) => { 24 | if (data) { 25 | const { 26 | MerchantRequestID, 27 | CheckoutRequestID, 28 | ResponseCode, 29 | ResponseDescription, 30 | CustomerMessage, 31 | } = data; 32 | console.log(MerchantRequestID); 33 | } 34 | 35 | if (error) { 36 | const { errorCode, errorMessage } = error; 37 | console.log(errorCode, errorMessage); 38 | } 39 | }) 40 | .catch((e: any) => { 41 | console.log(e); 42 | }); 43 | } catch (error) { 44 | console.log(error); 45 | } 46 | 47 | async () => { 48 | const { 49 | error: { errorCode, errorMessage }, 50 | data: { 51 | MerchantRequestID, 52 | CheckoutRequestID, 53 | ResponseCode, 54 | ResponseDescription, 55 | CustomerMessage, 56 | }, 57 | } = await mpesa.stkPush( 58 | 254705459494, 59 | 10, 60 | "ACCOUNT", 61 | "Transaction Description", 62 | "Remark" 63 | ); 64 | }; 65 | 66 | mpesa.registerUrls().then(({ error, data }) => { 67 | if (data) { 68 | const { ResponseCode, ResponseDescription } = data; 69 | console.log(ResponseDescription); 70 | } 71 | 72 | if (error) { 73 | const { errorCode, errorMessage } = error; 74 | console.log(errorCode, errorMessage); 75 | } 76 | }); 77 | 78 | mpesa.sendB2C(254705459494, 10).then(({ error, data }) => { 79 | if (data) { 80 | const { 81 | ConversationID, 82 | OriginatorConversationID, 83 | ResponseCode, 84 | ResponseDescription, 85 | } = data; 86 | console.log(OriginatorConversationID); 87 | } 88 | 89 | if (error) { 90 | const { errorCode, errorMessage } = error; 91 | console.log(errorCode, errorMessage); 92 | } 93 | }); 94 | 95 | mpesa 96 | .billing() 97 | .onboard( 98 | "hi@osen.co.ke", 99 | 254705459494, 100 | "https://osen.co.ke/wp-content/uploads/2019/11/logo.png", 101 | 1 102 | ) 103 | .then(({ error, data }) => {}) 104 | .catch((e) => {}); 105 | 106 | const invoice = mpesa 107 | .billing() 108 | .sendInvoice({ 109 | externalReference: "", 110 | billedFullName: "", 111 | billedPhoneNumber: "", 112 | billedPeriod: "", 113 | invoiceName: "", 114 | dueDate: "", 115 | accountReference: "", 116 | amount: "", 117 | }) 118 | .then(({ error, data }) => {}) 119 | .catch((e) => {}); 120 | 121 | mpesa 122 | .generateQR(100, "Osen Concepts", 254700900499, "AC6G9GB", "PB") 123 | .then(({ error, data: { QRCode } }) => { 124 | if (QRCode) { 125 | const imgSrc = `data:image/png;base64, ${QRCode}`; 126 | } else { 127 | console.log(error); 128 | } 129 | }); 130 | -------------------------------------------------------------------------------- /dist/billing.d.ts: -------------------------------------------------------------------------------- 1 | import { Mpesa } from "./mpesa"; 2 | import { MpesaResponse } from "./types"; 3 | export declare class BillManager extends Mpesa { 4 | /** 5 | * @param email Official contact email address for the organization signing up to bill manager. It will appear in features sent to the customer such as invoices and payment receipts for customers to reach out to you as a business. 6 | * @param officialContact Official contact phone number for the organization signing up to bill manager. It will appear in features sent to the customer such as invoices and payment receipts for customers to reach out to you as a business. 7 | * @param sendReminders Enable or disable SMS payment reminders for invoices sent. A payment reminder is sent 7 days before the due date, 3 days before the due date and on the day the payment is due.(0 - Disable Reminders 1- Enable Reminders) 8 | * @param logo File with your organization logo. File| Required .png, .jpg file, Base64 9 | */ 10 | onboard(email: string, officialContact: string | number, logo: string, sendReminders?: number): Promise; 11 | modify(email: string, officialContact: string | number, sendReminders?: number): Promise; 12 | sendInvoice(invoice: { 13 | externalReference: string; 14 | billedFullName: string; 15 | billedPhoneNumber: string; 16 | billedPeriod: string; 17 | invoiceName: string; 18 | dueDate: string; 19 | accountReference: string; 20 | amount: string; 21 | }): Promise; 22 | sendInvoices(invoices: { 23 | externalReference: string; 24 | billedFullName: string; 25 | billedPhoneNumber: string; 26 | billedPeriod: string; 27 | invoiceName: string; 28 | dueDate: string; 29 | accountReference: string; 30 | amount: string; 31 | }[]): Promise; 32 | reconcile(invoice: { 33 | paymentDate: string; 34 | paidAmount: string; 35 | accountReference: string; 36 | transactionId: string; 37 | phoneNumber: string; 38 | fullName: string; 39 | invoiceName: string; 40 | externalReference: string; 41 | }, callback: Function): Promise; 42 | acknowledge(invoice: { 43 | transactionId: string; 44 | paidAmount: string; 45 | phoneNumber: string; 46 | paymentDate: string; 47 | fullName: string; 48 | invoiceName: string; 49 | accountReference: string; 50 | externalName: string; 51 | }): Promise; 52 | changeInvoice(invoice: { 53 | paymentDate: string; 54 | paidAmount: string; 55 | accountReference: string; 56 | transactionId: string; 57 | phoneNumber: string; 58 | fullName: string; 59 | invoiceName: string; 60 | externalReference: string; 61 | }): Promise<{ 62 | data: any; 63 | error: null; 64 | } | { 65 | data: null; 66 | error: any; 67 | }>; 68 | cancelInvoice(externalReference: string): Promise<{ 69 | data: any; 70 | error: null; 71 | } | { 72 | data: null; 73 | error: any; 74 | }>; 75 | cancelInvoices(invoices: [{ 76 | externalReference: string; 77 | }]): Promise<{ 78 | data: any; 79 | error: null; 80 | } | { 81 | data: null; 82 | error: any; 83 | }>; 84 | } 85 | -------------------------------------------------------------------------------- /src/service.ts: -------------------------------------------------------------------------------- 1 | import axios, { AxiosInstance, AxiosResponse } from "axios"; 2 | import { MpesaConfig } from "./types"; 3 | import * as constants from "constants"; 4 | import * as crypto from "crypto"; 5 | import * as fs from "fs"; 6 | import * as path from "path"; 7 | 8 | export class Service { 9 | private http: AxiosInstance; 10 | public token: string | null; 11 | 12 | /** 13 | * @param object config Configuration options 14 | */ 15 | public config: MpesaConfig = { 16 | env: "sandbox", 17 | type: 4, 18 | shortcode: 174379, 19 | store: 174379, 20 | key: "9v38Dtu5u2BpsITPmLcXNWGMsjZRWSTG", 21 | secret: "bclwIPkcRqw61yUt", 22 | username: "apitest", 23 | password: "", 24 | passkey: "bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919" 25 | }; 26 | /** 27 | * Setup global configuration for classes 28 | * @param Array configs Formatted configuration options 29 | * 30 | * @return void 31 | */ 32 | constructor(configs: MpesaConfig) { 33 | this.config = configs; 34 | 35 | this.http = axios.create({ 36 | baseURL: 37 | this.config.env === "live" 38 | ? "https://api.safaricom.co.ke" 39 | : "https://sandbox.safaricom.co.ke", 40 | withCredentials: true, 41 | }); 42 | 43 | this.http.defaults.headers.common = { 44 | Accept: "application/json", 45 | "Content-Type": "application/json", 46 | }; 47 | } 48 | 49 | /** 50 | * Fetch Token To Authenticate Requests 51 | * 52 | * @return string Access token 53 | */ 54 | 55 | public authenticate(token: string | null = null) { 56 | if (!this.token && token) { 57 | this.token = token; 58 | } else { 59 | try { 60 | this.get( 61 | "oauth/v1/generate?grant_type=client_credentials" 62 | ).then(({ data }: AxiosResponse) => { 63 | 64 | this.token = data?.access_token; 65 | }) 66 | } catch (error) { 67 | return error; 68 | } 69 | } 70 | 71 | return this 72 | } 73 | 74 | public async generateSecurityCredential() { 75 | return crypto 76 | .publicEncrypt( 77 | { 78 | key: fs.readFileSync( 79 | path.join( 80 | __dirname, 81 | "certs", 82 | this.config.env, 83 | "cert.cer" 84 | ), 85 | "utf8" 86 | ), 87 | padding: constants.RSA_PKCS1_PADDING, 88 | }, 89 | 90 | Buffer.from(this.config.password) 91 | ) 92 | .toString("base64"); 93 | } 94 | 95 | /** 96 | * Perform a GET request to the M-PESA Daraja API 97 | * @param endpoint Daraja API URL Endpoint 98 | * @param credentials Formated Auth credentials 99 | * 100 | * @return string/bool 101 | */ 102 | public async get(endpoint: string) { 103 | const auth = 104 | "Basic " + 105 | Buffer.from(`${this.config.key}:${this.config.secret}`).toString( 106 | "base64" 107 | ); 108 | 109 | return await this.http.get(endpoint, { 110 | headers: { 111 | Authorization: auth, 112 | }, 113 | }); 114 | } 115 | 116 | /** 117 | * Perform a POST request to the M-PESA Daraja API 118 | * @param endpoint Daraja API URL Endpoint 119 | * @param Array data Formated array of data to send 120 | * 121 | * @return string/bool 122 | */ 123 | public async post(endpoint: string, payload: any): Promise { 124 | return this.http 125 | .post(endpoint, payload, { 126 | headers: { 127 | Authorization: "Bearer " + this.token, 128 | }, 129 | }) 130 | .then(({ data }) => data) 131 | .catch((e: any) => { 132 | if (e.response.data) { 133 | return e.response.data; 134 | } else { 135 | return { errorCode: 500, errorMessage: e.message }; 136 | } 137 | }); 138 | } 139 | } 140 | -------------------------------------------------------------------------------- /dist/billing.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | Object.defineProperty(exports, "__esModule", { value: true }); 3 | exports.BillManager = void 0; 4 | const mpesa_1 = require("./mpesa"); 5 | class BillManager extends mpesa_1.Mpesa { 6 | /** 7 | * @param email Official contact email address for the organization signing up to bill manager. It will appear in features sent to the customer such as invoices and payment receipts for customers to reach out to you as a business. 8 | * @param officialContact Official contact phone number for the organization signing up to bill manager. It will appear in features sent to the customer such as invoices and payment receipts for customers to reach out to you as a business. 9 | * @param sendReminders Enable or disable SMS payment reminders for invoices sent. A payment reminder is sent 7 days before the due date, 3 days before the due date and on the day the payment is due.(0 - Disable Reminders 1- Enable Reminders) 10 | * @param logo File with your organization logo. File| Required .png, .jpg file, Base64 11 | */ 12 | async onboard(email, officialContact, logo, sendReminders = 1) { 13 | const response = await this.service.post("v1/billmanager-invoice/optin", { 14 | shortcode: this.config.shortcode, 15 | logo, 16 | email, 17 | officialContact, 18 | sendReminders, 19 | callbackUrl: this.config.billingUrl, 20 | }); 21 | if (response.errorCode) { 22 | return { data: null, error: response }; 23 | } 24 | else { 25 | return { data: response, error: null }; 26 | } 27 | } 28 | async modify(email, officialContact, sendReminders = 1) { 29 | const response = await this.service.post("v1/billmanager-invoice/change-optin-details", { 30 | shortcode: this.config.shortcode, 31 | email, 32 | officialContact, 33 | sendReminders, 34 | callbackUrl: this.config.billingUrl, 35 | }); 36 | if (response.errorCode) { 37 | return { data: null, error: response }; 38 | } 39 | else { 40 | return { data: response, error: null }; 41 | } 42 | } 43 | async sendInvoice(invoice) { 44 | const response = await this.service.post("v1/billmanager-invoice/single-invoicing", invoice); 45 | if (Number(response.rescode) == 200) { 46 | return { data: response, error: null }; 47 | } 48 | else { 49 | return { data: null, error: response }; 50 | } 51 | } 52 | async sendInvoices(invoices) { 53 | const response = await this.service.post("v1/billmanager-invoice/bulk-invoicing", invoices); 54 | if (Number(response.rescode) == 200) { 55 | return { data: response, error: null }; 56 | } 57 | else { 58 | return { data: null, error: response }; 59 | } 60 | } 61 | async reconcile(invoice, callback) { 62 | return callback(invoice) 63 | ? { 64 | resmsg: "Success", 65 | rescode: "200", 66 | } 67 | : { 68 | resmsg: "Failed", 69 | rescode: "400", 70 | }; 71 | } 72 | async acknowledge(invoice) { 73 | const response = await this.service.post("v1/billmanager-invoice/reconciliation", invoice); 74 | if (Number(response.rescode) == 200) { 75 | return { data: response, error: null }; 76 | } 77 | else { 78 | return { data: null, error: response }; 79 | } 80 | } 81 | async changeInvoice(invoice) { 82 | const response = await this.service.post("v1/billmanager-invoice/change-invoice", invoice); 83 | if (Number(response.rescode) == 200) { 84 | return { data: response, error: null }; 85 | } 86 | else { 87 | return { data: null, error: response }; 88 | } 89 | } 90 | async cancelInvoice(externalReference) { 91 | const response = await this.service.post("v1/billmanager-invoice/cancel-single-invoice", { externalReference }); 92 | if (Number(response.rescode) == 200) { 93 | return { data: response, error: null }; 94 | } 95 | else { 96 | return { data: null, error: response }; 97 | } 98 | } 99 | async cancelInvoices(invoices) { 100 | const response = await this.service.post("v1/billmanager-invoice/cancel-bulk-invoice", invoices); 101 | if (Number(response.rescode) == 200) { 102 | return { data: response, error: null }; 103 | } 104 | else { 105 | return { data: null, error: response }; 106 | } 107 | } 108 | } 109 | exports.BillManager = BillManager; 110 | //# sourceMappingURL=billing.js.map -------------------------------------------------------------------------------- /dist/service.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { 3 | if (k2 === undefined) k2 = k; 4 | var desc = Object.getOwnPropertyDescriptor(m, k); 5 | if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { 6 | desc = { enumerable: true, get: function() { return m[k]; } }; 7 | } 8 | Object.defineProperty(o, k2, desc); 9 | }) : (function(o, m, k, k2) { 10 | if (k2 === undefined) k2 = k; 11 | o[k2] = m[k]; 12 | })); 13 | var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { 14 | Object.defineProperty(o, "default", { enumerable: true, value: v }); 15 | }) : function(o, v) { 16 | o["default"] = v; 17 | }); 18 | var __importStar = (this && this.__importStar) || function (mod) { 19 | if (mod && mod.__esModule) return mod; 20 | var result = {}; 21 | if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); 22 | __setModuleDefault(result, mod); 23 | return result; 24 | }; 25 | var __importDefault = (this && this.__importDefault) || function (mod) { 26 | return (mod && mod.__esModule) ? mod : { "default": mod }; 27 | }; 28 | Object.defineProperty(exports, "__esModule", { value: true }); 29 | exports.Service = void 0; 30 | const axios_1 = __importDefault(require("axios")); 31 | const constants = __importStar(require("constants")); 32 | const crypto = __importStar(require("crypto")); 33 | const fs = __importStar(require("fs")); 34 | const path = __importStar(require("path")); 35 | class Service { 36 | /** 37 | * Setup global configuration for classes 38 | * @param Array configs Formatted configuration options 39 | * 40 | * @return void 41 | */ 42 | constructor(configs) { 43 | /** 44 | * @param object config Configuration options 45 | */ 46 | this.config = { 47 | env: "sandbox", 48 | type: 4, 49 | shortcode: 174379, 50 | store: 174379, 51 | key: "9v38Dtu5u2BpsITPmLcXNWGMsjZRWSTG", 52 | secret: "bclwIPkcRqw61yUt", 53 | username: "apitest", 54 | password: "", 55 | passkey: "bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919", 56 | validationUrl: "/lipwa/validate", 57 | confirmationUrl: "/lipwa/confirm", 58 | callbackUrl: "/lipwa/reconcile", 59 | timeoutUrl: "/lipwa/timeout", 60 | resultUrl: "/lipwa/results", 61 | billingUrl: "/lipwa/billing", 62 | }; 63 | this.config = configs; 64 | this.http = axios_1.default.create({ 65 | baseURL: this.config.env === "live" 66 | ? "https://api.safaricom.co.ke" 67 | : "https://sandbox.safaricom.co.ke", 68 | withCredentials: true, 69 | }); 70 | this.http.defaults.headers.common = { 71 | Accept: "application/json", 72 | "Content-Type": "application/json", 73 | }; 74 | } 75 | /** 76 | * Fetch Token To Authenticate Requests 77 | * 78 | * @return string Access token 79 | */ 80 | authenticate(token = null) { 81 | if (!this.token && token) { 82 | this.token = token; 83 | } 84 | else { 85 | try { 86 | this.get("oauth/v1/generate?grant_type=client_credentials").then(({ data }) => { 87 | this.token = data === null || data === void 0 ? void 0 : data.access_token; 88 | }); 89 | } 90 | catch (error) { 91 | return error; 92 | } 93 | } 94 | return this; 95 | } 96 | async generateSecurityCredential() { 97 | return crypto 98 | .publicEncrypt({ 99 | key: fs.readFileSync(path.join(__dirname, "certs", this.config.env, "cert.cer"), "utf8"), 100 | padding: constants.RSA_PKCS1_PADDING, 101 | }, Buffer.from(this.config.password)) 102 | .toString("base64"); 103 | } 104 | /** 105 | * Perform a GET request to the M-PESA Daraja API 106 | * @param endpoint Daraja API URL Endpoint 107 | * @param credentials Formated Auth credentials 108 | * 109 | * @return string/bool 110 | */ 111 | async get(endpoint) { 112 | const auth = "Basic " + 113 | Buffer.from(`${this.config.key}:${this.config.secret}`).toString("base64"); 114 | return await this.http.get(endpoint, { 115 | headers: { 116 | Authorization: auth, 117 | }, 118 | }); 119 | } 120 | /** 121 | * Perform a POST request to the M-PESA Daraja API 122 | * @param endpoint Daraja API URL Endpoint 123 | * @param Array data Formated array of data to send 124 | * 125 | * @return string/bool 126 | */ 127 | async post(endpoint, payload) { 128 | return this.http 129 | .post(endpoint, payload, { 130 | headers: { 131 | Authorization: "Bearer " + this.token, 132 | }, 133 | }) 134 | .then(({ data }) => data) 135 | .catch((e) => { 136 | if (e.response.data) { 137 | return e.response.data; 138 | } 139 | else { 140 | return { errorCode: 500, errorMessage: e.message }; 141 | } 142 | }); 143 | } 144 | } 145 | exports.Service = Service; 146 | //# sourceMappingURL=service.js.map -------------------------------------------------------------------------------- /src/billing.ts: -------------------------------------------------------------------------------- 1 | import { Mpesa } from "./mpesa"; 2 | import { MpesaResponse } from "./types"; 3 | 4 | export class BillManager extends Mpesa { 5 | /** 6 | * @param email Official contact email address for the organization signing up to bill manager. It will appear in features sent to the customer such as invoices and payment receipts for customers to reach out to you as a business. 7 | * @param officialContact Official contact phone number for the organization signing up to bill manager. It will appear in features sent to the customer such as invoices and payment receipts for customers to reach out to you as a business. 8 | * @param sendReminders Enable or disable SMS payment reminders for invoices sent. A payment reminder is sent 7 days before the due date, 3 days before the due date and on the day the payment is due.(0 - Disable Reminders 1- Enable Reminders) 9 | * @param logo File with your organization logo. File| Required .png, .jpg file, Base64 10 | */ 11 | public async onboard( 12 | email: string, 13 | officialContact: string | number, 14 | logo: string, 15 | billingUrl: string, 16 | sendReminders = 1 17 | ): Promise { 18 | const response = await this.service.post( 19 | "v1/billmanager-invoice/optin", 20 | { 21 | shortcode: this.config.shortcode, 22 | logo, 23 | email, 24 | officialContact, 25 | sendReminders, 26 | callbackUrl: billingUrl, 27 | } 28 | ); 29 | 30 | if (response.errorCode) { 31 | return { data: null, error: response }; 32 | } else { 33 | return { data: response, error: null }; 34 | } 35 | } 36 | 37 | public async modify( 38 | email: string, 39 | officialContact: string | number, 40 | billingUrl: string, 41 | sendReminders = 1 42 | ): Promise { 43 | const response = await this.service.post( 44 | "v1/billmanager-invoice/change-optin-details", 45 | { 46 | shortcode: this.config.shortcode, 47 | email, 48 | officialContact, 49 | sendReminders, 50 | callbackUrl: billingUrl, 51 | } 52 | ); 53 | 54 | if (response.errorCode) { 55 | return { data: null, error: response }; 56 | } else { 57 | return { data: response, error: null }; 58 | } 59 | } 60 | 61 | public async sendInvoice(invoice: { 62 | externalReference: string; 63 | billedFullName: string; 64 | billedPhoneNumber: string; 65 | billedPeriod: string; 66 | invoiceName: string; 67 | dueDate: string; 68 | accountReference: string; 69 | amount: string; 70 | }): Promise { 71 | const response = await this.service.post( 72 | "v1/billmanager-invoice/single-invoicing", 73 | invoice 74 | ); 75 | 76 | if (Number(response.rescode) == 200) { 77 | return { data: response, error: null }; 78 | } else { 79 | return { data: null, error: response }; 80 | } 81 | } 82 | 83 | public async sendInvoices( 84 | invoices: { 85 | externalReference: string; 86 | billedFullName: string; 87 | billedPhoneNumber: string; 88 | billedPeriod: string; 89 | invoiceName: string; 90 | dueDate: string; 91 | accountReference: string; 92 | amount: string; 93 | }[] 94 | ): Promise { 95 | const response = await this.service.post( 96 | "v1/billmanager-invoice/bulk-invoicing", 97 | invoices 98 | ); 99 | 100 | if (Number(response.rescode) == 200) { 101 | return { data: response, error: null }; 102 | } else { 103 | return { data: null, error: response }; 104 | } 105 | } 106 | 107 | public async reconcile( 108 | invoice: { 109 | paymentDate: string; 110 | paidAmount: string; 111 | accountReference: string; 112 | transactionId: string; 113 | phoneNumber: string; 114 | fullName: string; 115 | invoiceName: string; 116 | externalReference: string; 117 | }, 118 | callback: Function 119 | ): Promise { 120 | return callback(invoice) 121 | ? { 122 | resmsg: "Success", 123 | rescode: "200", 124 | } 125 | : { 126 | resmsg: "Failed", 127 | rescode: "400", 128 | }; 129 | } 130 | 131 | public async acknowledge(invoice: { 132 | transactionId: string; 133 | paidAmount: string; 134 | phoneNumber: string; 135 | paymentDate: string; 136 | fullName: string; 137 | invoiceName: string; 138 | accountReference: string; 139 | externalName: string; 140 | }): Promise { 141 | const response = await this.service.post( 142 | "v1/billmanager-invoice/reconciliation", 143 | invoice 144 | ); 145 | 146 | if (Number(response.rescode) == 200) { 147 | return { data: response, error: null }; 148 | } else { 149 | return { data: null, error: response }; 150 | } 151 | } 152 | 153 | public async changeInvoice(invoice: { 154 | paymentDate: string; 155 | paidAmount: string; 156 | accountReference: string; 157 | transactionId: string; 158 | phoneNumber: string; 159 | fullName: string; 160 | invoiceName: string; 161 | externalReference: string; 162 | }) { 163 | const response = await this.service.post( 164 | "v1/billmanager-invoice/change-invoice", 165 | invoice 166 | ); 167 | 168 | if (Number(response.rescode) == 200) { 169 | return { data: response, error: null }; 170 | } else { 171 | return { data: null, error: response }; 172 | } 173 | } 174 | 175 | public async cancelInvoice(externalReference: string) { 176 | const response = await this.service.post( 177 | "v1/billmanager-invoice/cancel-single-invoice", 178 | { externalReference } 179 | ); 180 | 181 | if (Number(response.rescode) == 200) { 182 | return { data: response, error: null }; 183 | } else { 184 | return { data: null, error: response }; 185 | } 186 | } 187 | 188 | public async cancelInvoices(invoices: [{ externalReference: string }]) { 189 | const response = await this.service.post( 190 | "v1/billmanager-invoice/cancel-bulk-invoice", 191 | invoices 192 | ); 193 | 194 | if (Number(response.rescode) == 200) { 195 | return { data: response, error: null }; 196 | } else { 197 | return { data: null, error: response }; 198 | } 199 | } 200 | } 201 | -------------------------------------------------------------------------------- /dist/mpesa.d.ts: -------------------------------------------------------------------------------- 1 | import { BillManager } from "./billing"; 2 | import { Service } from "./service"; 3 | import { MpesaResponse, B2BCommands, B2CCommands, MpesaConfig, ResponseType } from "./types"; 4 | export declare class Mpesa { 5 | protected service: Service; 6 | /** 7 | * @param object config Configuration options 8 | */ 9 | config: MpesaConfig; 10 | ref: string; 11 | /** 12 | * Setup global configuration for classes 13 | * @param Array configs Formatted configuration options 14 | * 15 | * @return void 16 | */ 17 | constructor(configs: MpesaConfig); 18 | billing(): BillManager; 19 | /** 20 | * @param phone The MSISDN sending the funds. 21 | * @param amount The amount to be transacted. 22 | * @param reference Used with M-Pesa PayBills. 23 | * @param description A description of the transaction. 24 | * @param remark Remarks 25 | * 26 | * @return Promise Response 27 | */ 28 | stkPush(phone: string | number, amount: number, reference?: string | number, description?: string, remark?: string): Promise; 29 | registerUrls(response_type?: ResponseType): Promise; 30 | /** 31 | * Simulates a C2B request 32 | * 33 | * @param phone Receiving party phone 34 | * @param amount Amount to transfer 35 | * @param command Command ID 36 | * @param reference 37 | * @param callback Defined function or closure to process data and return true/false 38 | * 39 | * @return Promise 40 | */ 41 | simulateC2B(phone: string | number, amount?: number, reference?: string | number, command?: string): Promise<{ 42 | data: any; 43 | error: null; 44 | } | { 45 | data: null; 46 | error: any; 47 | } | undefined>; 48 | /** 49 | * Transfer funds between two paybills 50 | * @param receiver Receiving party phone 51 | * @param amount Amount to transfer 52 | * @param command Command ID 53 | * @param occassion 54 | * @param remarks 55 | * 56 | * @return Promise 57 | */ 58 | sendB2C(phone: string | number, amount?: number, command?: B2CCommands, remarks?: string, occassion?: string): Promise; 59 | /** 60 | * Transfer funds between two paybills 61 | * @param receiver Receiving party paybill 62 | * @param receiver_type Receiver party type 63 | * @param amount Amount to transfer 64 | * @param command Command ID 65 | * @param reference Account Reference mandatory for “BusinessPaybill” CommandID. 66 | * @param remarks 67 | * 68 | * @return Promise 69 | */ 70 | sendB2B(receiver: string | number, receiver_type: string | number, amount: number, command?: B2BCommands, reference?: string | number, remarks?: string): Promise; 71 | /** 72 | * Generate QR Code 73 | * @param QRVersion Version number of the QR. e.g "01" 74 | * @param QRFormat Format of QR output: ("1": Image Format. "2": QR Format. "3": Binary Data Format. "4": PDF Format.) 75 | * @param QRType The type of QR being used : ("D": Dynamic QR Type) 76 | * @param MerchantName Name of the Company/M-Pesa Merchant Name 77 | * @param RefNo Transaction Reference 78 | * @param Amount The total amount for the sale/transaction 79 | * @param TrxCode Transaction Type: (BG: Pay Merchant (Buy Goods). WA: Withdraw Cash at Agent Till. PB: Paybill or Business number. SM: Send Money(Mobile number). SB: Sent to Business. Business number CPI in MSISDN format. 80 | * @param CPI Credit Party Identifier. Can be a Mobile Number, Business Number, Agent Till, Paybill or Business number, Merchant Buy Goods. 81 | */ 82 | generateQR(Amount: string | number, MerchantName: string, CPI: string | number, RefNo: string, TrxCode?: string, QRVersion?: string, QRFormat?: string, QRType?: string): Promise; 83 | /** 84 | * Get Status of a Transaction 85 | * 86 | * @param transaction 87 | * @param command 88 | * @param remarks 89 | * @param occassion 90 | * 91 | * @return Promise Result 92 | */ 93 | checkStatus(transaction: string, command?: string, remarks?: string, occasion?: string): Promise; 94 | /** 95 | * Reverse a Transaction 96 | * 97 | * @param transaction 98 | * @param amount 99 | * @param receiver 100 | * @param receiver_type 101 | * @param remarks 102 | * @param occassion 103 | * 104 | * @return Promise Result 105 | */ 106 | reverseTransaction(transaction: string, amount: number, receiver: number, receiver_type?: number, remarks?: string, occasion?: string): Promise; 107 | /** 108 | * Check Account Balance 109 | * 110 | * @param command 111 | * @param remarks 112 | * @param occassion 113 | * 114 | * @return Promise Result 115 | */ 116 | checkBalance(command: string, remarks?: string): Promise; 117 | /** 118 | * Validate Transaction Data 119 | * 120 | * @param callback Defined function or closure to process data and return true/false 121 | * 122 | * @return Promise 123 | */ 124 | validateTransaction(ok: boolean): { 125 | ResultCode: number; 126 | ResultDesc: string; 127 | }; 128 | /** 129 | * Confirm Transaction Data 130 | * 131 | * @param callback Defined function or closure to process data and return true/false 132 | * 133 | * @return Promise 134 | */ 135 | confirmTransaction(ok: boolean): { 136 | ResultCode: number; 137 | ResultDesc: string; 138 | }; 139 | /** 140 | * Reconcile Transaction Using Instant Payment Notification from M-PESA 141 | * 142 | * @param callback Defined function or closure to process data and return true/false 143 | * 144 | * @return Promise 145 | */ 146 | reconcileTransaction(ok: boolean): { 147 | ResultCode: number; 148 | ResultDesc: string; 149 | }; 150 | /** 151 | * Process Results of an API Request 152 | * 153 | * @param callback Defined function or closure to process data and return true/false 154 | * 155 | * @return Promise 156 | */ 157 | processResults(ok: boolean): { 158 | ResultCode: number; 159 | ResultDesc: string; 160 | }; 161 | /** 162 | * Process Transaction Timeout 163 | * 164 | * @param callback Defined function or closure to process data and return true/false 165 | * 166 | * @return Promise 167 | */ 168 | processTimeout(ok: boolean): { 169 | ResultCode: number; 170 | ResultDesc: string; 171 | }; 172 | } 173 | -------------------------------------------------------------------------------- /dist/useMpesa.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"useMpesa.js","sourceRoot":"","sources":["../src/useMpesa.ts"],"names":[],"mappings":";;;;;;AAAA,kDAA6C;AAC7C,uCAAkC;AAClC,uCAAwC;AACxC,uCAAoC;AAU7B,MAAM,QAAQ,GAAG,CAAC,OAAoB,EAAE,QAAuB,IAAI,EAAE,EAAE;IAC7E,MAAM,GAAG,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,WAAW,EAAE,CAAC;IAEjE;;;;;OAKG;IACH,MAAM,QAAQ,GAAgB;QAC7B,GAAG,EAAE,SAAS;QACd,IAAI,EAAE,CAAC;QACP,SAAS,EAAE,MAAM;QACjB,KAAK,EAAE,MAAM;QACb,GAAG,EAAE,kCAAkC;QACvC,MAAM,EAAE,kBAAkB;QAC1B,QAAQ,EAAE,SAAS;QACnB,QAAQ,EAAE,EAAE;QACZ,OAAO,EACN,kEAAkE;QACnE,aAAa,EAAE,iBAAiB;QAChC,eAAe,EAAE,gBAAgB;QACjC,WAAW,EAAE,kBAAkB;QAC/B,UAAU,EAAE,gBAAgB;QAC5B,SAAS,EAAE,gBAAgB;QACrB,UAAU,EAAE,gBAAgB;KAClC,CAAC;IAEF,IAAI,CAAC,OAAO,IAAI,CAAC,OAAO,CAAC,KAAK,IAAI,OAAO,CAAC,IAAI,IAAI,CAAC,EAAE;QACpD,OAAO,CAAC,KAAK,GAAG,OAAO,CAAC,SAA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BAAwB;aACnC,CAAC;IACN,CAAC;IAED;;;;;;OAMG;IACH,SAAS,cAAc,CAAC,EAAW;QAClC,OAAO,EAAE;YACR,CAAC,CAAC;gBACA,UAAU,EAAE,CAAC;gBACb,UAAU,EAAE,4BAA4B;aACvC;YACH,CAAC,CAAC;gBACA,UAAU,EAAE,CAAC;gBACb,UAAU,EAAE,wBAAwB;aACnC,CAAC;IACN,CAAC;IAED;;;;;;OAMG;IACH,SAAS,cAAc,CAAC,QAA0B,EAAE,EAAW;QAC9D,OAAO,EAAE;YACR,CAAC,CAAC;gBACA,UAAU,EAAE,CAAC;gBACb,UAAU,EAAE,4BAA4B;aACvC;YACH,CAAC,CAAC;gBACA,UAAU,EAAE,CAAC;gBACb,UAAU,EAAE,wBAAwB;aACnC,CAAC;IACN,CAAC;IAED,OAAO;QACN,WAAW;QACX,OAAO;QACP,YAAY;QACZ,WAAW;QACX,OAAO;QACP,OAAO;QACP,YAAY;QACZ,WAAW;QACX,kBAAkB;QAClB,mBAAmB;QACnB,kBAAkB;QAClB,oBAAoB;QACpB,cAAc;QACd,cAAc;KACd,CAAC;AACH,CAAC,CAAC;AAhgBW,QAAA,QAAQ,YAggBnB"} -------------------------------------------------------------------------------- /dist/mpesa.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"mpesa.js","sourceRoot":"","sources":["../src/mpesa.ts"],"names":[],"mappings":";;;AAAA,uCAAkC;AAClC,uCAAwC;AACxC,uCAAoC;AASpC,MAAa,KAAK;IA2BjB;;;;;OAKG;IACH,YAAY,OAAoB;QA9BhC;;WAEG;QACI,WAAM,GAAgB;YAC5B,GAAG,EAAE,SAAS;YACd,IAAI,EAAE,CAAC;YACP,SAAS,EAAE,MAAM;YACjB,KAAK,EAAE,MAAM;YACb,GAAG,EAAE,kCAAkC;YACvC,MAAM,EAAE,kBAAkB;YAC1B,QAAQ,EAAE,SAAS;YACnB,QAAQ,EAAE,EAAE;YACZ,OAAO,EACN,kEAAkE;YACnE,aAAa,EAA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vC;YACH,CAAC,CAAC;gBACA,UAAU,EAAE,CAAC;gBACb,UAAU,EAAE,wBAAwB;aACnC,CAAC;IACN,CAAC;IAED;;;;;;OAMG;IACI,cAAc,CAAC,EAAW;QAChC,OAAO,EAAE;YACR,CAAC,CAAC;gBACA,UAAU,EAAE,CAAC;gBACb,UAAU,EAAE,4BAA4B;aACvC;YACH,CAAC,CAAC;gBACA,UAAU,EAAE,CAAC;gBACb,UAAU,EAAE,wBAAwB;aACnC,CAAC;IACN,CAAC;IAED;;;;;;OAMG;IACI,cAAc,CAAC,EAAW;QAChC,OAAO,EAAE;YACR,CAAC,CAAC;gBACA,UAAU,EAAE,CAAC;gBACb,UAAU,EAAE,4BAA4B;aACvC;YACH,CAAC,CAAC;gBACA,UAAU,EAAE,CAAC;gBACb,UAAU,EAAE,wBAAwB;aACnC,CAAC;IACN,CAAC;CACD;AAjiBD,sBAiiBC"} -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # M-Pesa TypeScript SDK 2 | 3 | This is a simple wrapper for Mpesa Daraja API using typescript 4 | 5 | ## Installation 6 | 7 | ### Via npm 8 | 9 | ``` javascript 10 | npm install --save @osenco/mpesa 11 | ``` 12 | 13 | ### Or yarn 14 | 15 | ``` javascript 16 | yarn add @osenco/mpesa 17 | ``` 18 | 19 | ## Usage 20 | 21 | ### Terms definitions 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 86 | 87 | 88 | 89 | 90 | 92 | 93 | 94 | 95 | 96 | 99 | 100 | 101 | 102 | 103 | 106 | 107 | 108 | 109 |
TermDescriptionType
envYour API environment. Either `sandbox` or 'live'string
typeIdentifier type 2 for Till, 4 for Paybillnumber
storeStore number if using a till numbernumber
shortcodeYour Buy Goods number or Paybillnumber
keyApp consumer key from Darajastring
secretApp consumer secret from Darajastring
passkeyYour online passkeystring
usernameOrg portal usernamestring
passwordOrg portal passwordstring
validationUrlA valid secure URL that is used to validate your transaction detailsstring
confirmationUrl 85 | A valid secure URL that is used to receive payment notifications from C2B API.string
callbackUrl 91 | A valid secure URL that is used to receive payment notifications from M-Pesa API.string
timeoutUrl 97 | This is the URL to be specified in your request that will be used by API Proxy to send notification incase the payment request is timed out while awaiting processing in the queue. 98 | string
resultsUrl 104 | This is the URL to be specified in your request that will be used by M-PESA to send notification upon processing of the payment request. 105 | string
110 | 111 | ### Import what you need 112 | 113 | ``` javascript 114 | import { Mpesa, useMpesa } from "@osenco/mpesa" 115 | ``` 116 | 117 | or 118 | 119 | ```javascript 120 | const { Mpesa, useMpesa } = require("@osenco/mpesa") 121 | ``` 122 | 123 | ### Instantiation 124 | 125 | ``` javascript 126 | const mpesa = new Mpesa( 127 | { 128 | env //"sandbox", 129 | type //4, 130 | shortcode //174379, 131 | store //174379, 132 | key // Your app consumer key, 133 | secret // Your app consumer secret, 134 | username // Your M-Pesa org username, 135 | password // Your M-Pesa org pass, 136 | passkey // Your online passkey "bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919", 137 | validationUrl //"/lipwa/validate", 138 | confirmationUrl //"/lipwa/confirm", 139 | callbackUrl //"/lipwa/reconcile", 140 | timeoutUrl //"/lipwa/timeout", OPTIONAL 141 | resultUrl //"/lipwa/results", OPTIONAL 142 | billingUrl //"/lipwa/billing", OPTIONAL 143 | } 144 | ) 145 | ``` 146 | 147 | Or, individual APIs 148 | 149 | ``` javascript 150 | const { stkPush, registerUrls, billManager } = useMpesa( 151 | { 152 | env //"sandbox", 153 | type //4, 154 | shortcode //174379, 155 | store //174379, 156 | key // Your app consumer key, 157 | secret // Your app consumer secret, 158 | username // Your M-Pesa org username, 159 | password // Your M-Pesa org pass, 160 | passkey // Your online passkey "bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919", 161 | validationUrl //"/lipwa/validate", 162 | confirmationUrl //"/lipwa/confirm", 163 | callbackUrl //"/lipwa/reconcile", 164 | timeoutUrl //"/lipwa/timeout", OPTIONAL 165 | resultUrl //"/lipwa/results", OPTIONAL 166 | billingUrl //"/lipwa/billing", OPTIONAL 167 | } 168 | ) 169 | ``` 170 | 171 | ### Send an STK push request 172 | 173 | ``` javascript 174 | mpesa.stkPush( 175 | 254705459494, 176 | 10, 177 | "ACCOUNT", // You can ignore this, the code will generate a unique string 178 | "Transaction Description", // Optional 179 | "Remark" // optional 180 | ).then(({ error, data }) => { 181 | if (data) { 182 | const { 183 | MerchantRequestID, 184 | CheckoutRequestID, 185 | ResponseCode, 186 | ResponseDescription, 187 | CustomerMessage 188 | } = data 189 | console.log(MerchantRequestID) 190 | } 191 | 192 | if (error) { 193 | const { errorCode, errorMessage } = error 194 | console.log(errorCode, errorMessage); 195 | } 196 | }) 197 | 198 | // Or use the API directly 199 | 200 | stkPush( 201 | 254705459494, 202 | 10, 203 | "ACCOUNT", // You can ignore this, the code will generate a unique string 204 | "Transaction Description", // Optional 205 | "Remark" // optional 206 | ).then(({ 207 | error, 208 | data 209 | }) => { 210 | if (data) { 211 | const { 212 | MerchantRequestID, 213 | CheckoutRequestID, 214 | ResponseCode, 215 | ResponseDescription, 216 | CustomerMessage 217 | } = data 218 | console.log(MerchantRequestID) 219 | } 220 | 221 | if (error) { 222 | const { errorCode, errorMessage } = error 223 | console.log(errorCode, errorMessage); 224 | } 225 | }) 226 | ``` 227 | 228 | Or, if inside an async function 229 | 230 | ```javascript 231 | async () => { 232 | const { error: { errorCode, errorMessage }, data: { 233 | MerchantRequestID, 234 | CheckoutRequestID, 235 | ResponseCode, 236 | ResponseDescription, 237 | CustomerMessage 238 | } 239 | } = await mpesa.stkPush( 240 | 254705459494, 241 | 10, 242 | "ACCOUNT", 243 | "Transaction Description", 244 | "Remark" 245 | ) 246 | 247 | console.log(MerchantRequestID) 248 | 249 | // TIP: Save MerchantRequestID and update when you receive the IPN 250 | } 251 | ``` 252 | 253 | ### C2B register callback URLs 254 | 255 | ``` javascript 256 | mpesa.registerUrls("Completed" | "Cancelled").then(({ 257 | error, 258 | data 259 | }) => { 260 | if (data) { 261 | const { 262 | ResponseCode, 263 | ResponseDescription 264 | } = data 265 | console.log(ResponseDescription) 266 | } 267 | 268 | if (error) { 269 | const { errorCode, errorMessage } = error 270 | console.log(errorCode, errorMessage); 271 | } 272 | }) 273 | ``` 274 | 275 | ### Send B2C 276 | 277 | ``` javascript 278 | mpesa.sendB2C( 279 | phone, 280 | amount, 281 | "BusinessPayment" | "SalaryPayment" | "PromotionPayment", 282 | "Some remark", 283 | "Some occasion" 284 | ).then(({ 285 | error, 286 | data 287 | }) => { 288 | if (data) { 289 | const { 290 | ConversationID, 291 | OriginatorConversationID, 292 | ResponseCode, 293 | ResponseDescription 294 | } = data 295 | console.log(OriginatorConversationID) 296 | 297 | // TIP: Save `OriginatorConversationID` in the database, and use it as a key once you receive the IPN 298 | } 299 | 300 | if (error) { 301 | const { errorCode, errorMessage } = error 302 | console.log(errorCode, errorMessage); 303 | } 304 | }) 305 | ``` 306 | 307 | ### Send B2B 308 | 309 | ``` javascript 310 | mpesa.sendB2B( 311 | phone, 312 | amount, 313 | "BusinessPayBill" | "BusinessBuyGoods" | "DisburseFundsToBusiness" | "BusinessToBusinessTransfer" | "MerchantToMerchantTransfer", 314 | "Some remark", 315 | "Some occasion" 316 | ).then(({ 317 | error, 318 | data 319 | }) => { 320 | if (data) { 321 | const { 322 | ConversationID, 323 | OriginatorConversationID, 324 | ResponseCode, 325 | ResponseDescription 326 | } = data 327 | console.log(OriginatorConversationID) 328 | 329 | // TIP: Save `OriginatorConversationID` in the database, and use it as a key for update 330 | } 331 | 332 | if (error) { 333 | const { errorCode, errorMessage } = error 334 | console.log(errorCode, errorMessage); 335 | } 336 | }) 337 | ``` 338 | 339 | ### Generate QR Codes 340 | 341 | ``` javascript 342 | mpesa 343 | .generateQR(100, 'Osen Concepts', 254700900499, 'AC6G9GB', 'PB') 344 | .then(({ error, data: { QRCode } }) => { 345 | if (QRCode) { 346 | const imgSrc = `data:image/png;base64, ${QRCode}`; 347 | } else { 348 | console.log(error); 349 | } 350 | }); 351 | 352 | ``` 353 | 354 | ### Bill Manager 355 | 356 | #### Onboard 357 | 358 | ``` javascript 359 | const onboard = mpesa.billing() 360 | .onboard() 361 | .then(({ error, data }) => {}) 362 | .catch((e) => {}); 363 | ``` 364 | 365 | #### Send Invoice(s) 366 | 367 | ``` javascript 368 | const invoice = mpesa.billing() 369 | .sendInvoice({}) 370 | .then(({ error, data }) => {}) 371 | .catch((e) => {}); 372 | 373 | const invoices = billManager() 374 | .sendInvoices([{}]) 375 | .then(({ error, data }) => {}) 376 | .catch((e) => {}); 377 | ``` 378 | -------------------------------------------------------------------------------- /src/useMpesa.ts: -------------------------------------------------------------------------------- 1 | import axios, { AxiosInstance } from "axios"; 2 | import { format } from "date-fns"; 3 | import { BillManager } from "./billing"; 4 | import { Service } from "./service"; 5 | import { 6 | MpesaResponse, 7 | MpesaSTKResponse, 8 | B2BCommands, 9 | B2CCommands, 10 | MpesaConfig, 11 | ResponseType, 12 | } from "./types"; 13 | 14 | export const useMpesa = (configs: MpesaConfig, token: string | null = null) => { 15 | const ref = Math.random().toString(16).slice(2, 8).toUpperCase(); 16 | 17 | /** 18 | * Setup global configuration for classes 19 | * @param MpesaConfig configs Formatted configuration options 20 | * 21 | * @return void 22 | */ 23 | const defaults: MpesaConfig = { 24 | env: "sandbox", 25 | type: 4, 26 | shortcode: 174379, 27 | store: 174379, 28 | key: "9v38Dtu5u2BpsITPmLcXNWGMsjZRWSTG", 29 | secret: "bclwIPkcRqw61yUt", 30 | username: "apitest", 31 | password: "", 32 | passkey: 33 | "bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919", 34 | }; 35 | 36 | if (!configs || !configs.store || configs.type == 4) { 37 | configs.store = configs.shortcode; 38 | } 39 | 40 | const config = { ...defaults, ...configs }; 41 | 42 | const service = new Service(config); 43 | 44 | if (token) { 45 | service.token = token; 46 | } else { 47 | service.authenticate(); 48 | } 49 | 50 | const http: AxiosInstance = axios.create({ 51 | baseURL: 52 | config.env == "live" 53 | ? "https://api.safaricom.co.ke" 54 | : "https://sandbox.safaricom.co.ke", 55 | withCredentials: true, 56 | }); 57 | 58 | http.defaults.headers.common = { 59 | Accept: "application/json", 60 | "Content-Type": "application/json", 61 | }; 62 | 63 | function billManager(): BillManager { 64 | return new BillManager(configs); 65 | } 66 | 67 | /** 68 | * @param phone The MSISDN sending the funds. 69 | * @param amount The amount to be transacted. 70 | * @param reference Used with M-Pesa PayBills. 71 | * @param description A description of the transaction. 72 | * @param remark Remarks 73 | * 74 | * @return Promise Response 75 | */ 76 | async function stkPush( 77 | phone: string | number, 78 | Amount: number, 79 | AccountReference: string | number = ref, 80 | CallBackURL: string = "/lipwa/reconcile", 81 | TransactionDesc = "Transaction Description", 82 | Remark = "Remark" 83 | ): Promise { 84 | const PartyA = "254" + +String(phone).slice(-9); 85 | const Timestamp = format(new Date(), "yyyyMMddHHmmss"); 86 | const Password = Buffer.from( 87 | config.shortcode + config.passkey + Timestamp 88 | ).toString("base64"); 89 | 90 | const response = await service.post("mpesa/stkpush/v1/processrequest", { 91 | BusinessShortCode: config.store, 92 | Password, 93 | Timestamp, 94 | TransactionType: 95 | Number(config.type) == 4 96 | ? "CustomerPayBillOnline" 97 | : "CustomerBuyGoodsOnline", 98 | Amount, 99 | PartyA, 100 | PartyB: config.shortcode, 101 | PhoneNumber: PartyA, 102 | CallBackURL, 103 | AccountReference, 104 | TransactionDesc, 105 | Remark, 106 | }); 107 | 108 | if (response.MerchantRequestID) { 109 | return { data: response, error: null }; 110 | } 111 | 112 | if (response.errorCode) { 113 | return { data: null, error: response }; 114 | } 115 | 116 | return response; 117 | } 118 | 119 | async function registerUrls( 120 | ConfirmationURL: string, 121 | ValidationURL: string, 122 | ResponseType: ResponseType = "Completed" 123 | ): Promise { 124 | const response = await service.post("mpesa/c2b/v1/registerurl", { 125 | ShortCode: config.store, 126 | ResponseType, 127 | ConfirmationURL, 128 | ValidationURL, 129 | }); 130 | 131 | if (response.errorCode) { 132 | return { data: null, error: response }; 133 | } 134 | 135 | if (response.MerchantRequestID) { 136 | return { data: response, error: null }; 137 | } 138 | 139 | return response; 140 | } 141 | 142 | /** 143 | * Simulates a C2B request 144 | * 145 | * @param phone Receiving party phone 146 | * @param amount Amount to transfer 147 | * @param command Command ID 148 | * @param reference 149 | * @param callback Defined function or closure to process data and return true/false 150 | * 151 | * @return Promise 152 | */ 153 | async function simulateC2B( 154 | phone: string | number, 155 | amount = 10, 156 | reference: string | number = "TRX", 157 | command = "" 158 | ) { 159 | phone = phone; 160 | phone = "254" + +String(phone).slice(-9); 161 | 162 | const response = await service.post("mpesa/c2b/v1/simulate", { 163 | ShortCode: config.shortcode, 164 | CommandID: command, 165 | Amount: Number(amount), 166 | Msisdn: phone, 167 | BillRefNumber: reference, 168 | }); 169 | 170 | if (response.MerchantRequestID) { 171 | return { data: response, error: null }; 172 | } 173 | 174 | if (response.errorCode) { 175 | return { data: null, error: response }; 176 | } 177 | } 178 | 179 | /** 180 | * Transfer funds between two paybills 181 | * 182 | * @param receiver Receiving party phone 183 | * @param amount Amount to transfer 184 | * @param command Command ID 185 | * @param occassion 186 | * @param remarks 187 | * 188 | * @return Promise 189 | */ 190 | async function sendB2C( 191 | phone: string | number, 192 | amount = 10, 193 | command: B2CCommands = "BusinessPayment", 194 | remarks = "", 195 | occassion = "", 196 | QueueTimeOutURL = "/lipwa/timeout", 197 | ResultURL = "/lipwa/result" 198 | ): Promise { 199 | phone = phone; 200 | phone = "254" + String(phone).slice(-9); 201 | 202 | const response = await service.post("mpesa/b2c/v1/paymentrequest", { 203 | InitiatorName: config.username, 204 | SecurityCredential: await service.generateSecurityCredential(), 205 | CommandID: command, 206 | Amount: Number(amount), 207 | PartyA: config.shortcode, 208 | PartyB: phone, 209 | Remarks: remarks, 210 | QueueTimeOutURL, 211 | ResultURL, 212 | Occasion: occassion, 213 | }); 214 | 215 | if (response.OriginatorConversationID) { 216 | return { data: response, error: null }; 217 | } 218 | 219 | if (response.ResultCode && response.ResultCode !== 0) { 220 | return { 221 | data: null, 222 | error: { 223 | errorCode: response.ResultCode, 224 | errorMessage: response.ResultDesc, 225 | }, 226 | }; 227 | } 228 | 229 | if (response.errorCode) { 230 | return { data: null, error: response }; 231 | } 232 | 233 | return response; 234 | } 235 | 236 | /** 237 | * Transfer funds between two paybills 238 | * @param receiver Receiving party paybill 239 | * @param receiver_type Receiver party type 240 | * @param amount Amount to transfer 241 | * @param command Command ID 242 | * @param reference Account Reference mandatory for “BusinessPaybill” CommandID. 243 | * @param remarks 244 | * 245 | * @return Promise 246 | */ 247 | async function sendB2B( 248 | receiver: string | number, 249 | receiver_type: string | number, 250 | amount: number, 251 | command: B2BCommands = "BusinessBuyGoods", 252 | reference: string | number = "TRX", 253 | remarks = "", 254 | QueueTimeOutURL = "/lipwa/timeout", 255 | ResultURL = "/lipwa/result" 256 | ): Promise { 257 | const response = await service.post("mpesa/b2b/v1/paymentrequest", { 258 | Initiator: config.username, 259 | SecurityCredential: await service.generateSecurityCredential(), 260 | CommandID: command, 261 | SenderIdentifierType: config.type, 262 | RecieverIdentifierType: receiver_type, 263 | Amount: amount, 264 | PartyA: config.shortcode, 265 | PartyB: receiver, 266 | AccountReference: reference, 267 | Remarks: remarks, 268 | QueueTimeOutURL, 269 | ResultURL, 270 | }); 271 | 272 | if (response.MerchantRequestID) { 273 | return { data: response, error: null }; 274 | } 275 | 276 | if (response.errorCode) { 277 | return { data: null, error: response }; 278 | } 279 | 280 | return response; 281 | } 282 | 283 | /** 284 | * Get Status of a Transaction 285 | * 286 | * @param transaction 287 | * @param command 288 | * @param remarks 289 | * @param occassion 290 | * 291 | * @return Promise Result 292 | */ 293 | async function checkStatus( 294 | transaction: string, 295 | command = "TransactionStatusQuery", 296 | QueueTimeOutURL: string = "/lipwa/timeout", 297 | ResultURL: string = "/lipwa/result", 298 | remarks = "Transaction Status Query", 299 | occasion = "Transaction Status Query" 300 | ): Promise { 301 | const response: MpesaSTKResponse = await service.post( 302 | "mpesa/transactionstatus/v1/query", 303 | { 304 | Initiator: config.username, 305 | SecurityCredential: await service.generateSecurityCredential(), 306 | CommandID: command, 307 | TransactionID: transaction, 308 | PartyA: config.shortcode, 309 | IdentifierType: config.type, 310 | ResultURL, 311 | QueueTimeOutURL, 312 | Remarks: remarks, 313 | Occasion: occasion, 314 | } 315 | ); 316 | 317 | if (response.MerchantRequestID) { 318 | return { data: response, error: null }; 319 | } 320 | 321 | if (response.errorCode) { 322 | return { data: null, error: response }; 323 | } 324 | 325 | return { data: response, error: null }; 326 | } 327 | 328 | /** 329 | * Reverse a Transaction 330 | * 331 | * @param transaction 332 | * @param amount 333 | * @param receiver 334 | * @param receiver_type 335 | * @param remarks 336 | * @param occassion 337 | * 338 | * @return Promise Result 339 | */ 340 | async function reverseTransaction( 341 | TransactionID: string, 342 | Amount: number, 343 | ReceiverParty: number, 344 | RecieverIdentifierType = 3, 345 | QueueTimeOutURL: string = "/lipwa/timeout", 346 | ResultURL: string = "/lipwa/result", 347 | Remarks = "Transaction Reversal", 348 | Occasion = "Transaction Reversal" 349 | ): Promise { 350 | const response = await service.post("mpesa/reversal/v1/request", { 351 | CommandID: "TransactionReversal", 352 | Initiator: config.username, 353 | SecurityCredential: await service.generateSecurityCredential(), 354 | TransactionID, 355 | Amount, 356 | ReceiverParty, 357 | RecieverIdentifierType, 358 | ResultURL, 359 | QueueTimeOutURL, 360 | Remarks, 361 | Occasion, 362 | }); 363 | 364 | if (response.MerchantRequestID) { 365 | return { data: response, error: null }; 366 | } 367 | 368 | if (response.errorCode) { 369 | return { data: null, error: response }; 370 | } 371 | 372 | return response; 373 | } 374 | 375 | /** 376 | * Check Account Balance 377 | * 378 | * @param command 379 | * @param remarks 380 | * @param occassion 381 | * 382 | * @return Promise Result 383 | */ 384 | async function checkBalance( 385 | CommandID: string, 386 | QueueTimeOutURL: string = "/lipwa/timeout", 387 | ResultURL: string = "/lipwa/result", 388 | Remarks = "Balance Query" 389 | ): Promise { 390 | const response = await service.post("mpesa/accountbalance/v1/query", { 391 | CommandID, 392 | Initiator: config.username, 393 | SecurityCredential: await service.generateSecurityCredential(), 394 | PartyA: config.shortcode, 395 | IdentifierType: config.type, 396 | Remarks, 397 | QueueTimeOutURL, 398 | ResultURL, 399 | }); 400 | 401 | if (response.MerchantRequestID) { 402 | return { data: response, error: null }; 403 | } 404 | 405 | if (response.errorCode) { 406 | return { data: null, error: response }; 407 | } 408 | 409 | return response; 410 | } 411 | 412 | /** 413 | * Validate Transaction Data 414 | * 415 | * @param callback Defined function or closure to process data and return true/false 416 | * 417 | * @return Promise 418 | */ 419 | function validateTransaction(ok: boolean) { 420 | return ok 421 | ? { 422 | ResultCode: 0, 423 | ResultDesc: "Success", 424 | } 425 | : { 426 | ResultCode: 1, 427 | ResultDesc: "Failed", 428 | }; 429 | } 430 | 431 | /** 432 | * Confirm Transaction Data 433 | * 434 | * @param callback Defined function or closure to process data and return true/false 435 | * 436 | * @return Promise 437 | */ 438 | function confirmTransaction(ok: boolean, data: any, callback: Function) { 439 | if (callback) { 440 | ok = callback(data); 441 | } 442 | 443 | return ok 444 | ? { 445 | ResultCode: 0, 446 | ResultDesc: "Success", 447 | } 448 | : { 449 | ResultCode: 1, 450 | ResultDesc: "Failed", 451 | }; 452 | } 453 | 454 | /** 455 | * Reconcile Transaction Using Instant Payment Notification from M-PESA 456 | * 457 | * @param callback Defined function or closure to process data and return true/false 458 | * 459 | * @return Promise 460 | */ 461 | function reconcileTransaction(ok: boolean) { 462 | return ok 463 | ? { 464 | ResultCode: 0, 465 | ResultDesc: "Service request successful", 466 | } 467 | : { 468 | ResultCode: 1, 469 | ResultDesc: "Service request failed", 470 | }; 471 | } 472 | 473 | /** 474 | * Process Results of an API Request 475 | * 476 | * @param callback Defined function or closure to process data and return true/false 477 | * 478 | * @return Promise 479 | */ 480 | function processResults(ok: boolean) { 481 | return ok 482 | ? { 483 | ResultCode: 0, 484 | ResultDesc: "Service request successful", 485 | } 486 | : { 487 | ResultCode: 1, 488 | ResultDesc: "Service request failed", 489 | }; 490 | } 491 | 492 | /** 493 | * Process Transaction Timeout 494 | * 495 | * @param callback Defined function or closure to process data and return true/false 496 | * 497 | * @return Promise 498 | */ 499 | function processTimeout(callback: CallableFunction, ok: boolean) { 500 | return ok 501 | ? { 502 | ResultCode: 0, 503 | ResultDesc: "Service request successful", 504 | } 505 | : { 506 | ResultCode: 1, 507 | ResultDesc: "Service request failed", 508 | }; 509 | } 510 | 511 | return { 512 | billManager, 513 | stkPush, 514 | registerUrls, 515 | simulateC2B, 516 | sendB2B, 517 | sendB2C, 518 | checkBalance, 519 | checkStatus, 520 | reverseTransaction, 521 | validateTransaction, 522 | confirmTransaction, 523 | reconcileTransaction, 524 | processResults, 525 | processTimeout, 526 | }; 527 | }; 528 | -------------------------------------------------------------------------------- /tsconfig.tsbuildinfo: -------------------------------------------------------------------------------- 1 | {"program":{"fileNames":["./node_modules/typescript/lib/lib.es5.d.ts","./node_modules/typescript/lib/lib.es2015.d.ts","./node_modules/typescript/lib/lib.es2016.d.ts","./node_modules/typescript/lib/lib.es2017.d.ts","./node_modules/typescript/lib/lib.es2018.d.ts","./node_modules/typescript/lib/lib.es2019.d.ts","./node_modules/typescript/lib/lib.es2020.d.ts","./node_modules/typescript/lib/lib.es2015.core.d.ts","./node_modules/typescript/lib/lib.es2015.collection.d.ts","./node_modules/typescript/lib/lib.es2015.generator.d.ts","./node_modules/typescript/lib/lib.es2015.iterable.d.ts","./node_modules/typescript/lib/lib.es2015.promise.d.ts","./node_modules/typescript/lib/lib.es2015.proxy.d.ts","./node_modules/typescript/lib/lib.es2015.reflect.d.ts","./node_modules/typescript/lib/lib.es2015.symbol.d.ts","./node_modules/typescript/lib/lib.es2015.symbol.wellknown.d.ts","./node_modules/typescript/lib/lib.es2016.array.include.d.ts","./node_modules/typescript/lib/lib.es2017.object.d.ts","./node_modules/typescript/lib/lib.es2017.sharedmemory.d.ts","./node_modules/typescript/lib/lib.es2017.string.d.ts","./node_modules/typescript/lib/lib.es2017.intl.d.ts","./node_modules/typescript/lib/lib.es2017.typedarrays.d.ts","./node_modules/typescript/lib/lib.es2018.asyncgenerator.d.ts","./node_modules/typescript/lib/lib.es2018.asynciterable.d.ts","./node_modules/typescript/lib/lib.es2018.intl.d.ts","./node_modules/typescript/lib/lib.es2018.promise.d.ts","./node_modules/typescript/lib/lib.es2018.regexp.d.ts","./node_modules/typescript/lib/lib.es2019.array.d.ts","./node_modules/typescript/lib/lib.es2019.object.d.ts","./node_modules/typescript/lib/lib.es2019.string.d.ts","./node_modules/typescript/lib/lib.es2019.symbol.d.ts","./node_modules/typescript/lib/lib.es2020.bigint.d.ts","./node_modules/typescript/lib/lib.es2020.promise.d.ts","./node_modules/typescript/lib/lib.es2020.sharedmemory.d.ts","./node_modules/typescript/lib/lib.es2020.string.d.ts","./node_modules/typescript/lib/lib.es2020.symbol.wellknown.d.ts","./node_modules/typescript/lib/lib.es2020.intl.d.ts","./node_modules/typescript/lib/lib.esnext.intl.d.ts","./node_modules/date-fns/typings.d.ts","./node_modules/axios/index.d.ts","./src/types.ts","./src/service.ts","./src/mpesa.ts","./src/billing.ts","./src/usempesa.ts","./src/index.ts","./node_modules/@types/node/assert.d.ts","./node_modules/@types/node/assert/strict.d.ts","./node_modules/@types/node/globals.d.ts","./node_modules/@types/node/async_hooks.d.ts","./node_modules/@types/node/buffer.d.ts","./node_modules/@types/node/child_process.d.ts","./node_modules/@types/node/cluster.d.ts","./node_modules/@types/node/console.d.ts","./node_modules/@types/node/constants.d.ts","./node_modules/@types/node/crypto.d.ts","./node_modules/@types/node/dgram.d.ts","./node_modules/@types/node/diagnostics_channel.d.ts","./node_modules/@types/node/dns.d.ts","./node_modules/@types/node/dns/promises.d.ts","./node_modules/@types/node/domain.d.ts","./node_modules/@types/node/events.d.ts","./node_modules/@types/node/fs.d.ts","./node_modules/@types/node/fs/promises.d.ts","./node_modules/@types/node/http.d.ts","./node_modules/@types/node/http2.d.ts","./node_modules/@types/node/https.d.ts","./node_modules/@types/node/inspector.d.ts","./node_modules/@types/node/module.d.ts","./node_modules/@types/node/net.d.ts","./node_modules/@types/node/os.d.ts","./node_modules/@types/node/path.d.ts","./node_modules/@types/node/perf_hooks.d.ts","./node_modules/@types/node/process.d.ts","./node_modules/@types/node/punycode.d.ts","./node_modules/@types/node/querystring.d.ts","./node_modules/@types/node/readline.d.ts","./node_modules/@types/node/repl.d.ts","./node_modules/@types/node/stream.d.ts","./node_modules/@types/node/stream/promises.d.ts","./node_modules/@types/node/stream/consumers.d.ts","./node_modules/@types/node/stream/web.d.ts","./node_modules/@types/node/string_decoder.d.ts","./node_modules/@types/node/timers.d.ts","./node_modules/@types/node/timers/promises.d.ts","./node_modules/@types/node/tls.d.ts","./node_modules/@types/node/trace_events.d.ts","./node_modules/@types/node/tty.d.ts","./node_modules/@types/node/url.d.ts","./node_modules/@types/node/util.d.ts","./node_modules/@types/node/v8.d.ts","./node_modules/@types/node/vm.d.ts","./node_modules/@types/node/wasi.d.ts","./node_modules/@types/node/worker_threads.d.ts","./node_modules/@types/node/zlib.d.ts","./node_modules/@types/node/globals.global.d.ts","./node_modules/@types/node/index.d.ts"],"fileInfos":[{"version":"3ac1b83264055b28c0165688fda6dfcc39001e9e7828f649299101c23ad0a0c3","affectsGlobalScope":true},"dc47c4fa66b9b9890cf076304de2a9c5201e94b740cffdf09f87296d877d71f6","7a387c58583dfca701b6c85e0adaf43fb17d590fb16d5b2dc0a2fbd89f35c467","8a12173c586e95f4433e0c6dc446bc88346be73ffe9ca6eec7aa63c8f3dca7f9","5f4e733ced4e129482ae2186aae29fde948ab7182844c3a5a51dd346182c7b06","e6b724280c694a9f588847f754198fb96c43d805f065c3a5b28bbc9594541c84","e21c071ca3e1b4a815d5f04a7475adcaeea5d64367e840dd0154096d705c3940",{"version":"d8996609230d17e90484a2dd58f22668f9a05a3bfe00bfb1d6271171e54a31fb","affectsGlobalScope":true},{"version":"43fb1d932e4966a39a41b464a12a81899d9ae5f2c829063f5571b6b87e6d2f9c","affectsGlobalScope":true},{"version":"cdccba9a388c2ee3fd6ad4018c640a471a6c060e96f1232062223063b0a5ac6a","affectsGlobalScope":true},{"version":"c5c05907c02476e4bde6b7e76a79ffcd948aedd14b6a8f56e4674221b0417398","affectsGlobalScope":true},{"version":"0d5f52b3174bee6edb81260ebcd792692c32c81fd55499d69531496f3f2b25e7","affectsGlobalScope":true},{"version":"810627a82ac06fb5166da5ada4159c4ec11978dfbb0805fe804c86406dab8357","affectsGlobalScope":true},{"version":"62d80405c46c3f4c527ee657ae9d43fda65a0bf582292429aea1e69144a522a6","affectsGlobalScope":true},{"version":"3013574108c36fd3aaca79764002b3717da09725a36a6fc02eac386593110f93","affectsGlobalScope":true},{"version":"75ec0bdd727d887f1b79ed6619412ea72ba3c81d92d0787ccb64bab18d261f14","affectsGlobalScope":true},{"version":"3be5a1453daa63e031d266bf342f3943603873d890ab8b9ada95e22389389006","affectsGlobalScope":true},{"version":"17bb1fc99591b00515502d264fa55dc8370c45c5298f4a5c2083557dccba5a2a","affectsGlobalScope":true},{"version":"7ce9f0bde3307ca1f944119f6365f2d776d281a393b576a18a2f2893a2d75c98","affectsGlobalScope":true},{"version":"6a6b173e739a6a99629a8594bfb294cc7329bfb7b227f12e1f7c11bc163b8577","affectsGlobalScope":true},{"version":"12a310447c5d23c7d0d5ca2af606e3bd08afda69100166730ab92c62999ebb9d","affectsGlobalScope":true},{"version":"b0124885ef82641903d232172577f2ceb5d3e60aed4da1153bab4221e1f6dd4e","affectsGlobalScope":true},{"version":"0eb85d6c590b0d577919a79e0084fa1744c1beba6fd0d4e951432fa1ede5510a","affectsGlobalScope":true},{"version":"da233fc1c8a377ba9e0bed690a73c290d843c2c3d23a7bd7ec5cd3d7d73ba1e0","affectsGlobalScope":true},{"version":"d154ea5bb7f7f9001ed9153e876b2d5b8f5c2bb9ec02b3ae0d239ec769f1f2ae","affectsGlobalScope":true},{"version":"bb2d3fb05a1d2ffbca947cc7cbc95d23e1d053d6595391bd325deb265a18d36c","affectsGlobalScope":true},{"version":"c80df75850fea5caa2afe43b9949338ce4e2de086f91713e9af1a06f973872b8","affectsGlobalScope":true},{"version":"9d57b2b5d15838ed094aa9ff1299eecef40b190722eb619bac4616657a05f951","affectsGlobalScope":true},{"version":"6c51b5dd26a2c31dbf37f00cfc32b2aa6a92e19c995aefb5b97a3a64f1ac99de","affectsGlobalScope":true},{"version":"6e7997ef61de3132e4d4b2250e75343f487903ddf5370e7ce33cf1b9db9a63ed","affectsGlobalScope":true},{"version":"2ad234885a4240522efccd77de6c7d99eecf9b4de0914adb9a35c0c22433f993","affectsGlobalScope":true},{"version":"1b3fe904465430e030c93239a348f05e1be80640d91f2f004c3512c2c2c89f34","affectsGlobalScope":true},{"version":"3787b83e297de7c315d55d4a7c546ae28e5f6c0a361b7a1dcec1f1f50a54ef11","affectsGlobalScope":true},{"version":"e7e8e1d368290e9295ef18ca23f405cf40d5456fa9f20db6373a61ca45f75f40","affectsGlobalScope":true},{"version":"faf0221ae0465363c842ce6aa8a0cbda5d9296940a8e26c86e04cc4081eea21e","affectsGlobalScope":true},{"version":"06393d13ea207a1bfe08ec8d7be562549c5e2da8983f2ee074e00002629d1871","affectsGlobalScope":true},{"version":"5075b36ab861c8c0c45377cb8c96270d7c65f0eeaf105d53fac6850da61f1027","affectsGlobalScope":true},{"version":"10bbdc1981b8d9310ee75bfac28ee0477bb2353e8529da8cff7cb26c409cb5e8","affectsGlobalScope":true},{"version":"fff697b90126bd057f5c4607e2b2168c92884843070ae44789fb96f3547b6d69","affectsGlobalScope":true},"2808645b990069e5f8b5ff14c9f1e6077eb642583c3f7854012d60757f23c70e",{"version":"c6272782116d1980f0d031f7b5708208dd9b9493ac838c3efd5cee1f129094a8","signature":"c5c0dfd2929845552382551cfe67eaf4c4bd3b370bee514888428524ba38ad86"},{"version":"f6e81f9432de672877a7aa25ee1f526d56900b3ba4350e77da7457aa29b69840","signature":"960c94a506c6873ad0c9e60e10682973ef5ee5b0bc9cc61fe53bafed257cfae6"},{"version":"b25aa1d09bf8de1773adfcf95b4620052e86d8d9e98eb8d2b85a06d00596d284","signature":"d434c513ea73369999c64d50d08476b7224ac73f7ca73bdf51912d92885ad164"},{"version":"353f4824f9c85b58f6400b8c415fff40d4bad096639e130f7fbd3dac4ee86322","signature":"fe568674ce687c9049772ac74f8ce83e6113ecda791e696a4a4aaf8c5e462986"},"d44455a97e2227739b895233b83fe187fed56abebb33bad95f925344fef8dac4",{"version":"907cc9a109aaca5fd6830b2553198bf5d74f35fd085f3e0e53e9ce43c47c3613","signature":"0b78fb5590f3f52d9815b0e82b3793a5aab010d2bd5587033043b58c4873d2c2"},"0cba3a5d7b81356222594442753cf90dd2892e5ccfe1d262aaca6896ba6c1380","a69c09dbea52352f479d3e7ac949fde3d17b195abe90b045d619f747b38d6d1a",{"version":"c2ab70bbc7a24c42a790890739dd8a0ba9d2e15038b40dff8163a97a5d148c00","affectsGlobalScope":true},"422dbb183fdced59425ca072c8bd09efaa77ce4e2ab928ec0d8a1ce062d2a45a",{"version":"712ba0d43b44d144dfd01593f61af6e2e21cfae83e834d297643e7973e55ed61","affectsGlobalScope":true},"1dab5ab6bcf11de47ab9db295df8c4f1d92ffa750e8f095e88c71ce4c3299628","f71f46ccd5a90566f0a37b25b23bc4684381ab2180bdf6733f4e6624474e1894",{"version":"54e65985a3ee3cec182e6a555e20974ea936fc8b8d1738c14e8ed8a42bd921d4","affectsGlobalScope":true},"82408ed3e959ddc60d3e9904481b5a8dc16469928257af22a3f7d1a3bc7fd8c4","98a3ebfa494b46265634a73459050befba5da8fdc6ca0ef9b7269421780f4ff3","34e5de87d983bc6aefef8b17658556e3157003e8d9555d3cb098c6bef0b5fbc8","cc0b61316c4f37393f1f9595e93b673f4184e9d07f4c127165a490ec4a928668","f27371653aded82b2b160f7a7033fb4a5b1534b6f6081ef7be1468f0f15327d3","c762cd6754b13a461c54b59d0ae0ab7aeef3c292c6cf889873f786ee4d8e75c9","f4ea7d5df644785bd9fbf419930cbaec118f0d8b4160037d2339b8e23c059e79",{"version":"bfea28e6162ed21a0aeed181b623dcf250aa79abf49e24a6b7e012655af36d81","affectsGlobalScope":true},"b8aca9d0c81abb02bec9b7621983ae65bde71da6727580070602bd2500a9ce2a","ae97e20f2e10dbeec193d6a2f9cd9a367a1e293e7d6b33b68bacea166afd7792","10d4796a130577d57003a77b95d8723530bbec84718e364aa2129fa8ffba0378","ad41bb744149e92adb06eb953da195115620a3f2ad48e7d3ae04d10762dae197","bf73c576885408d4a176f44a9035d798827cc5020d58284cb18d7573430d9022","7ae078ca42a670445ae0c6a97c029cb83d143d62abd1730efb33f68f0b2c0e82",{"version":"e8b18c6385ff784228a6f369694fcf1a6b475355ba89090a88de13587a9391d5","affectsGlobalScope":true},"287b21dc1d1b9701c92e15e7dd673dfe6044b15812956377adffb6f08825b1bc","12eea70b5e11e924bb0543aea5eadc16ced318aa26001b453b0d561c2fd0bd1e","08777cd9318d294646b121838574e1dd7acbb22c21a03df84e1f2c87b1ad47f2","08a90bcdc717df3d50a2ce178d966a8c353fd23e5c392fd3594a6e39d9bb6304",{"version":"4cd4cff679c9b3d9239fd7bf70293ca4594583767526916af8e5d5a47d0219c7","affectsGlobalScope":true},"2a12d2da5ac4c4979401a3f6eaafa874747a37c365e4bc18aa2b171ae134d21b","002b837927b53f3714308ecd96f72ee8a053b8aeb28213d8ec6de23ed1608b66","1dc9c847473bb47279e398b22c740c83ea37a5c88bf66629666e3cf4c5b9f99c","a9e4a5a24bf2c44de4c98274975a1a705a0abbaad04df3557c2d3cd8b1727949","00fa7ce8bc8acc560dc341bbfdf37840a8c59e6a67c9bfa3fa5f36254df35db2","1b952304137851e45bc009785de89ada562d9376177c97e37702e39e60c2f1ff",{"version":"806ef4cac3b3d9fa4a48d849c8e084d7c72fcd7b16d76e06049a9ed742ff79c0","affectsGlobalScope":true},"44b8b584a338b190a59f4f6929d072431950c7bd92ec2694821c11bce180c8a5","5f0ed51db151c2cdc4fa3bb0f44ce6066912ad001b607a34e65a96c52eb76248",{"version":"3345c276cab0e76dda86c0fb79104ff915a4580ba0f3e440870e183b1baec476","affectsGlobalScope":true},"664d8f2d59164f2e08c543981453893bc7e003e4dfd29651ce09db13e9457980","103d70bfbeb3cd3a3f26d1705bf986322d8738c2c143f38ebb743b1e228d7444","f52fbf64c7e480271a9096763c4882d356b05cab05bf56a64e68a95313cd2ce2","59bdb65f28d7ce52ccfc906e9aaf422f8b8534b2d21c32a27d7819be5ad81df7",{"version":"3a2da34079a2567161c1359316a32e712404b56566c45332ac9dcee015ecce9f","affectsGlobalScope":true},"28a2e7383fd898c386ffdcacedf0ec0845e5d1a86b5a43f25b86bc315f556b79","3aff9c8c36192e46a84afe7b926136d520487155154ab9ba982a8b544ea8fc95","a880cf8d85af2e4189c709b0fea613741649c0e40fffb4360ec70762563d5de0","85bbf436a15bbeda4db888be3062d47f99c66fd05d7c50f0f6473a9151b6a070","9f9c49c95ecd25e0cb2587751925976cf64fd184714cb11e213749c80cf0f927","f0c75c08a71f9212c93a719a25fb0320d53f2e50ca89a812640e08f8ad8c408c",{"version":"ab9b9a36e5284fd8d3bf2f7d5fcbc60052f25f27e4d20954782099282c60d23e","affectsGlobalScope":true},"9cafe917bf667f1027b2bb62e2de454ecd2119c80873ad76fc41d941089753b8"],"options":{"declaration":true,"emitDecoratorMetadata":true,"esModuleInterop":true,"experimentalDecorators":true,"module":1,"noImplicitAny":true,"outDir":"./dist","rootDir":"./src","skipLibCheck":true,"sourceMap":true,"strictNullChecks":true,"target":4},"fileIdsList":[[39,47,90],[39,50,90],[39,51,56,90],[39,52,62,63,70,79,89,90],[39,52,53,62,70,90],[39,54,90],[39,55,56,63,71,90],[39,56,79,86,90],[39,57,59,62,70,90],[39,58,90],[39,59,60,90],[39,61,62,90],[39,62,90],[39,62,63,64,79,89,90],[39,62,63,64,79,90],[39,90],[39,65,70,79,89,90],[39,62,63,65,66,70,79,86,89,90],[39,65,67,79,86,89,90],[39,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96],[39,62,68,90],[39,69,89,90],[39,59,62,70,79,90],[39,71,90],[39,72,90],[39,50,73,90],[39,74,88,90,94],[39,75,90],[39,76,90],[39,62,77,90],[39,77,78,90,92],[39,62,79,80,81,90],[39,79,81,90],[39,79,80,90],[39,82,90],[39,83,90],[39,62,84,85,90],[39,84,85,90],[39,56,70,79,86,90],[39,87,90],[39,70,88,90],[39,51,65,76,89,90],[39,56,90],[39,79,90,91],[39,90,92],[39,90,93],[39,51,56,62,64,73,79,89,90,92,94],[39,79,90,95],[39,41,43,90],[39,41,43,44,45,90],[39,41,42,44,90],[39,40,41,55,56,63,72,90],[39,40,41,42,44,90],[41,43],[41,43,44,45],[41,42,44],[40,41]],"referencedMap":[[47,1],[48,1],[50,2],[51,3],[52,4],[53,5],[54,6],[55,7],[56,8],[57,9],[58,10],[59,11],[60,11],[61,12],[62,13],[63,14],[64,15],[49,16],[96,16],[65,17],[66,18],[67,19],[97,20],[68,21],[69,22],[70,23],[71,24],[72,25],[73,26],[74,27],[75,28],[76,29],[77,30],[78,31],[79,32],[81,33],[80,34],[82,35],[83,36],[84,37],[85,38],[86,39],[87,40],[88,41],[89,42],[90,43],[91,44],[92,45],[93,46],[94,47],[95,48],[40,16],[39,16],[9,16],[8,16],[2,16],[10,16],[11,16],[12,16],[13,16],[14,16],[15,16],[16,16],[17,16],[3,16],[4,16],[21,16],[18,16],[19,16],[20,16],[22,16],[23,16],[24,16],[5,16],[25,16],[26,16],[27,16],[28,16],[6,16],[29,16],[30,16],[31,16],[32,16],[7,16],[37,16],[33,16],[34,16],[35,16],[36,16],[1,16],[38,16],[44,49],[46,50],[43,51],[42,52],[41,16],[45,53]],"exportedModulesMap":[[47,1],[48,1],[50,2],[51,3],[52,4],[53,5],[54,6],[55,7],[56,8],[57,9],[58,10],[59,11],[60,11],[61,12],[62,13],[63,14],[64,15],[49,16],[96,16],[65,17],[66,18],[67,19],[97,20],[68,21],[69,22],[70,23],[71,24],[72,25],[73,26],[74,27],[75,28],[76,29],[77,30],[78,31],[79,32],[81,33],[80,34],[82,35],[83,36],[84,37],[85,38],[86,39],[87,40],[88,41],[89,42],[90,43],[91,44],[92,45],[93,46],[94,47],[95,48],[40,16],[39,16],[9,16],[8,16],[2,16],[10,16],[11,16],[12,16],[13,16],[14,16],[15,16],[16,16],[17,16],[3,16],[4,16],[21,16],[18,16],[19,16],[20,16],[22,16],[23,16],[24,16],[5,16],[25,16],[26,16],[27,16],[28,16],[6,16],[29,16],[30,16],[31,16],[32,16],[7,16],[37,16],[33,16],[34,16],[35,16],[36,16],[1,16],[38,16],[44,54],[46,55],[43,56],[42,57],[45,53]],"semanticDiagnosticsPerFile":[47,48,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,49,96,65,66,67,97,68,69,70,71,72,73,74,75,76,77,78,79,81,80,82,83,84,85,86,87,88,89,90,91,92,93,94,95,40,39,9,8,2,10,11,12,13,14,15,16,17,3,4,21,18,19,20,22,23,24,5,25,26,27,28,6,29,30,31,32,7,37,33,34,35,36,1,38,44,46,43,42,41,45]},"version":"4.6.3"} -------------------------------------------------------------------------------- /dist/useMpesa.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | var __importDefault = (this && this.__importDefault) || function (mod) { 3 | return (mod && mod.__esModule) ? mod : { "default": mod }; 4 | }; 5 | Object.defineProperty(exports, "__esModule", { value: true }); 6 | exports.useMpesa = void 0; 7 | const axios_1 = __importDefault(require("axios")); 8 | const date_fns_1 = require("date-fns"); 9 | const billing_1 = require("./billing"); 10 | const service_1 = require("./service"); 11 | const useMpesa = (configs, token = null) => { 12 | const ref = Math.random().toString(16).slice(2, 8).toUpperCase(); 13 | /** 14 | * Setup global configuration for classes 15 | * @param MpesaConfig configs Formatted configuration options 16 | * 17 | * @return void 18 | */ 19 | const defaults = { 20 | env: "sandbox", 21 | type: 4, 22 | shortcode: 174379, 23 | store: 174379, 24 | key: "9v38Dtu5u2BpsITPmLcXNWGMsjZRWSTG", 25 | secret: "bclwIPkcRqw61yUt", 26 | username: "apitest", 27 | password: "", 28 | passkey: "bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919", 29 | validationUrl: "/lipwa/validate", 30 | confirmationUrl: "/lipwa/confirm", 31 | callbackUrl: "/lipwa/reconcile", 32 | timeoutUrl: "/lipwa/timeout", 33 | resultUrl: "/lipwa/results", 34 | billingUrl: "/lipwa/billing", 35 | }; 36 | if (!configs || !configs.store || configs.type == 4) { 37 | configs.store = configs.shortcode; 38 | } 39 | const config = Object.assign(Object.assign({}, defaults), configs); 40 | const service = new service_1.Service(config); 41 | if (token) { 42 | service.token = token; 43 | } 44 | else { 45 | service.authenticate(); 46 | } 47 | const http = axios_1.default.create({ 48 | baseURL: config.env == "live" 49 | ? "https://api.safaricom.co.ke" 50 | : "https://sandbox.safaricom.co.ke", 51 | withCredentials: true, 52 | }); 53 | http.defaults.headers.common = { 54 | Accept: "application/json", 55 | "Content-Type": "application/json", 56 | }; 57 | function billManager() { 58 | return new billing_1.BillManager(configs); 59 | } 60 | /** 61 | * @param phone The MSISDN sending the funds. 62 | * @param amount The amount to be transacted. 63 | * @param reference Used with M-Pesa PayBills. 64 | * @param description A description of the transaction. 65 | * @param remark Remarks 66 | * 67 | * @return Promise Response 68 | */ 69 | async function stkPush(phone, amount, reference = ref, description = "Transaction Description", remark = "Remark") { 70 | phone = (phone); 71 | phone = "254" + +String(phone).slice(-9); 72 | const timestamp = (0, date_fns_1.format)(new Date(), "yyyyMMddHHmmss"); 73 | const password = Buffer.from(config.shortcode + config.passkey + timestamp).toString("base64"); 74 | const response = await service.post("mpesa/stkpush/v1/processrequest", { 75 | BusinessShortCode: config.store, 76 | Password: password, 77 | Timestamp: timestamp, 78 | TransactionType: Number(config.type) == 4 79 | ? "CustomerPayBillOnline" 80 | : "CustomerBuyGoodsOnline", 81 | Amount: Number(amount), 82 | PartyA: phone, 83 | PartyB: config.shortcode, 84 | PhoneNumber: phone, 85 | CallBackURL: config.callbackUrl, 86 | AccountReference: reference, 87 | TransactionDesc: description, 88 | Remark: remark, 89 | }); 90 | if (response.MerchantRequestID) { 91 | return { data: response, error: null }; 92 | } 93 | if (response.errorCode) { 94 | return { data: null, error: response }; 95 | } 96 | return response; 97 | } 98 | async function registerUrls(response_type = "Completed") { 99 | const response = await service.post("mpesa/c2b/v1/registerurl", { 100 | ShortCode: config.store, 101 | ResponseType: response_type, 102 | ConfirmationURL: config.confirmationUrl, 103 | ValidationURL: config.validationUrl, 104 | }); 105 | if (response.errorCode) { 106 | return { data: null, error: response }; 107 | } 108 | if (response.MerchantRequestID) { 109 | return { data: response, error: null }; 110 | } 111 | return response; 112 | } 113 | /** 114 | * Simulates a C2B request 115 | * 116 | * @param phone Receiving party phone 117 | * @param amount Amount to transfer 118 | * @param command Command ID 119 | * @param reference 120 | * @param callback Defined function or closure to process data and return true/false 121 | * 122 | * @return Promise 123 | */ 124 | async function simulateC2B(phone, amount = 10, reference = "TRX", command = "") { 125 | phone = (phone); 126 | phone = "254" + +String(phone).slice(-9); 127 | const response = await service.post("mpesa/c2b/v1/simulate", { 128 | ShortCode: config.shortcode, 129 | CommandID: command, 130 | Amount: Number(amount), 131 | Msisdn: phone, 132 | BillRefNumber: reference, 133 | }); 134 | if (response.MerchantRequestID) { 135 | return { data: response, error: null }; 136 | } 137 | if (response.errorCode) { 138 | return { data: null, error: response }; 139 | } 140 | } 141 | /** 142 | * Transfer funds between two paybills 143 | * @param receiver Receiving party phone 144 | * @param amount Amount to transfer 145 | * @param command Command ID 146 | * @param occassion 147 | * @param remarks 148 | * 149 | * @return Promise 150 | */ 151 | async function sendB2C(phone, amount = 10, command = "BusinessPayment", remarks = "", occassion = "") { 152 | phone = (phone); 153 | phone = "254" + +String(phone).slice(-9); 154 | const response = await service.post("mpesa/b2c/v1/paymentrequest", { 155 | InitiatorName: config.username, 156 | SecurityCredential: await service.generateSecurityCredential(), 157 | CommandID: command, 158 | Amount: Number(amount), 159 | PartyA: config.shortcode, 160 | PartyB: phone, 161 | Remarks: remarks, 162 | QueueTimeOutURL: config.timeoutUrl, 163 | ResultURL: config.resultUrl, 164 | Occasion: occassion, 165 | }); 166 | if (response.OriginatorConversationID) { 167 | return { data: response, error: null }; 168 | } 169 | if (response.ResultCode && response.ResultCode !== 0) { 170 | return { 171 | data: null, 172 | error: { 173 | errorCode: response.ResultCode, 174 | errorMessage: response.ResultDesc, 175 | }, 176 | }; 177 | } 178 | if (response.errorCode) { 179 | return { data: null, error: response }; 180 | } 181 | return response; 182 | } 183 | /** 184 | * Transfer funds between two paybills 185 | * @param receiver Receiving party paybill 186 | * @param receiver_type Receiver party type 187 | * @param amount Amount to transfer 188 | * @param command Command ID 189 | * @param reference Account Reference mandatory for “BusinessPaybill” CommandID. 190 | * @param remarks 191 | * 192 | * @return Promise 193 | */ 194 | async function sendB2B(receiver, receiver_type, amount, command = "BusinessBuyGoods", reference = "TRX", remarks = "") { 195 | const response = await service.post("mpesa/b2b/v1/paymentrequest", { 196 | Initiator: config.username, 197 | SecurityCredential: await service.generateSecurityCredential(), 198 | CommandID: command, 199 | SenderIdentifierType: config.type, 200 | RecieverIdentifierType: receiver_type, 201 | Amount: amount, 202 | PartyA: config.shortcode, 203 | PartyB: receiver, 204 | AccountReference: reference, 205 | Remarks: remarks, 206 | QueueTimeOutURL: config.timeoutUrl, 207 | ResultURL: config.resultUrl, 208 | }); 209 | if (response.MerchantRequestID) { 210 | return { data: response, error: null }; 211 | } 212 | if (response.errorCode) { 213 | return { data: null, error: response }; 214 | } 215 | return response; 216 | } 217 | /** 218 | * Get Status of a Transaction 219 | * 220 | * @param transaction 221 | * @param command 222 | * @param remarks 223 | * @param occassion 224 | * 225 | * @return Promise Result 226 | */ 227 | async function checkStatus(transaction, command = "TransactionStatusQuery", remarks = "Transaction Status Query", occasion = "Transaction Status Query") { 228 | const response = await service.post("mpesa/transactionstatus/v1/query", { 229 | Initiator: config.username, 230 | SecurityCredential: await service.generateSecurityCredential(), 231 | CommandID: command, 232 | TransactionID: transaction, 233 | PartyA: config.shortcode, 234 | IdentifierType: config.type, 235 | ResultURL: config.resultUrl, 236 | QueueTimeOutURL: config.timeoutUrl, 237 | Remarks: remarks, 238 | Occasion: occasion, 239 | }); 240 | if (response.MerchantRequestID) { 241 | return { data: response, error: null }; 242 | } 243 | if (response.errorCode) { 244 | return { data: null, error: response }; 245 | } 246 | return { data: response, error: null }; 247 | } 248 | /** 249 | * Reverse a Transaction 250 | * 251 | * @param transaction 252 | * @param amount 253 | * @param receiver 254 | * @param receiver_type 255 | * @param remarks 256 | * @param occassion 257 | * 258 | * @return Promise Result 259 | */ 260 | async function reverseTransaction(transaction, amount, receiver, receiver_type = 3, remarks = "Transaction Reversal", occasion = "Transaction Reversal") { 261 | const response = await service.post("mpesa/reversal/v1/request", { 262 | CommandID: "TransactionReversal", 263 | Initiator: config.username, 264 | SecurityCredential: await service.generateSecurityCredential(), 265 | TransactionID: transaction, 266 | Amount: amount, 267 | ReceiverParty: receiver, 268 | RecieverIdentifierType: receiver_type, 269 | ResultURL: config.resultUrl, 270 | QueueTimeOutURL: config.timeoutUrl, 271 | Remarks: remarks, 272 | Occasion: occasion, 273 | }); 274 | if (response.MerchantRequestID) { 275 | return { data: response, error: null }; 276 | } 277 | if (response.errorCode) { 278 | return { data: null, error: response }; 279 | } 280 | return response; 281 | } 282 | /** 283 | * Check Account Balance 284 | * 285 | * @param command 286 | * @param remarks 287 | * @param occassion 288 | * 289 | * @return Promise Result 290 | */ 291 | async function checkBalance(command, remarks = "Balance Query") { 292 | const response = await service.post("mpesa/accountbalance/v1/query", { 293 | CommandID: command, 294 | Initiator: config.username, 295 | SecurityCredential: await service.generateSecurityCredential(), 296 | PartyA: config.shortcode, 297 | IdentifierType: config.type, 298 | Remarks: remarks, 299 | QueueTimeOutURL: config.timeoutUrl, 300 | ResultURL: config.resultUrl, 301 | }); 302 | if (response.MerchantRequestID) { 303 | return { data: response, error: null }; 304 | } 305 | if (response.errorCode) { 306 | return { data: null, error: response }; 307 | } 308 | return response; 309 | } 310 | /** 311 | * Validate Transaction Data 312 | * 313 | * @param callback Defined function or closure to process data and return true/false 314 | * 315 | * @return Promise 316 | */ 317 | function validateTransaction(ok) { 318 | return ok 319 | ? { 320 | ResultCode: 0, 321 | ResultDesc: "Success", 322 | } 323 | : { 324 | ResultCode: 1, 325 | ResultDesc: "Failed", 326 | }; 327 | } 328 | /** 329 | * Confirm Transaction Data 330 | * 331 | * @param callback Defined function or closure to process data and return true/false 332 | * 333 | * @return Promise 334 | */ 335 | function confirmTransaction(ok, data, callback) { 336 | if (callback) { 337 | ok = callback(data); 338 | } 339 | return ok 340 | ? { 341 | ResultCode: 0, 342 | ResultDesc: "Success", 343 | } 344 | : { 345 | ResultCode: 1, 346 | ResultDesc: "Failed", 347 | }; 348 | } 349 | /** 350 | * Reconcile Transaction Using Instant Payment Notification from M-PESA 351 | * 352 | * @param callback Defined function or closure to process data and return true/false 353 | * 354 | * @return Promise 355 | */ 356 | function reconcileTransaction(ok) { 357 | return ok 358 | ? { 359 | ResultCode: 0, 360 | ResultDesc: "Service request successful", 361 | } 362 | : { 363 | ResultCode: 1, 364 | ResultDesc: "Service request failed", 365 | }; 366 | } 367 | /** 368 | * Process Results of an API Request 369 | * 370 | * @param callback Defined function or closure to process data and return true/false 371 | * 372 | * @return Promise 373 | */ 374 | function processResults(ok) { 375 | return ok 376 | ? { 377 | ResultCode: 0, 378 | ResultDesc: "Service request successful", 379 | } 380 | : { 381 | ResultCode: 1, 382 | ResultDesc: "Service request failed", 383 | }; 384 | } 385 | /** 386 | * Process Transaction Timeout 387 | * 388 | * @param callback Defined function or closure to process data and return true/false 389 | * 390 | * @return Promise 391 | */ 392 | function processTimeout(callback, ok) { 393 | return ok 394 | ? { 395 | ResultCode: 0, 396 | ResultDesc: "Service request successful", 397 | } 398 | : { 399 | ResultCode: 1, 400 | ResultDesc: "Service request failed", 401 | }; 402 | } 403 | return { 404 | billManager, 405 | stkPush, 406 | registerUrls, 407 | simulateC2B, 408 | sendB2B, 409 | sendB2C, 410 | checkBalance, 411 | checkStatus, 412 | reverseTransaction, 413 | validateTransaction, 414 | confirmTransaction, 415 | reconcileTransaction, 416 | processResults, 417 | processTimeout, 418 | }; 419 | }; 420 | exports.useMpesa = useMpesa; 421 | //# sourceMappingURL=useMpesa.js.map -------------------------------------------------------------------------------- /src/mpesa.ts: -------------------------------------------------------------------------------- 1 | import { format } from "date-fns"; 2 | import { BillManager } from "./billing"; 3 | import { Service } from "./service"; 4 | import { 5 | MpesaResponse, 6 | B2BCommands, 7 | B2CCommands, 8 | MpesaConfig, 9 | ResponseType, 10 | } from "./types"; 11 | 12 | export class Mpesa { 13 | protected service: Service; 14 | 15 | /** 16 | * @param object config Configuration options 17 | */ 18 | public config: MpesaConfig = { 19 | env: "sandbox", 20 | type: 4, 21 | shortcode: 174379, 22 | store: 174379, 23 | key: "9v38Dtu5u2BpsITPmLcXNWGMsjZRWSTG", 24 | secret: "bclwIPkcRqw61yUt", 25 | username: "apitest", 26 | password: "", 27 | passkey: 28 | "bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919", 29 | }; 30 | 31 | public ref: string = Math.random().toString(16).slice(2, 8).toUpperCase(); 32 | 33 | /** 34 | * Setup global configuration for classes 35 | * @param Array configs Formatted configuration options 36 | * 37 | * @return void 38 | */ 39 | constructor(configs: MpesaConfig) { 40 | const defaults: MpesaConfig = { 41 | env: "sandbox", 42 | type: 4, 43 | shortcode: 174379, 44 | store: 174379, 45 | key: "9v38Dtu5u2BpsITPmLcXNWGMsjZRWSTG", 46 | secret: "bclwIPkcRqw61yUt", 47 | username: "apitest", 48 | password: "", 49 | passkey: 50 | "bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919", 51 | }; 52 | 53 | if (!configs || !configs.store || configs.type == 4) { 54 | configs.store = configs.shortcode; 55 | } 56 | 57 | this.config = { ...defaults, ...configs }; 58 | 59 | this.service = new Service(this.config); 60 | } 61 | 62 | public billing(): BillManager { 63 | return new BillManager(this.config); 64 | } 65 | 66 | /** 67 | * @param phone The MSISDN sending the funds. 68 | * @param amount The amount to be transacted. 69 | * @param reference Used with M-Pesa PayBills. 70 | * @param description A description of the transaction. 71 | * @param remark Remarks 72 | * 73 | * @return Promise Response 74 | */ 75 | public async stkPush( 76 | phone: string | number, 77 | amount: number, 78 | reference: string | number = this.ref, 79 | CallBackURL: string = "/lipwa/reconcile", 80 | description = "Transaction Description", 81 | remark = "Remark" 82 | ): Promise { 83 | phone = "254" + String(phone).slice(-9); 84 | 85 | const timestamp = format(new Date(), "yyyyMMddHHmmss"); 86 | const password = Buffer.from( 87 | this.config.shortcode + this.config.passkey + timestamp 88 | ).toString("base64"); 89 | 90 | const response = await this.service.post( 91 | "mpesa/stkpush/v1/processrequest", 92 | { 93 | BusinessShortCode: this.config.store, 94 | Password: password, 95 | Timestamp: timestamp, 96 | TransactionType: 97 | Number(this.config.type) == 4 98 | ? "CustomerPayBillOnline" 99 | : "CustomerBuyGoodsOnline", 100 | Amount: Number(amount), 101 | PartyA: phone, 102 | PartyB: this.config.shortcode, 103 | PhoneNumber: phone, 104 | CallBackURL, 105 | AccountReference: reference, 106 | TransactionDesc: description, 107 | Remark: remark, 108 | } 109 | ); 110 | 111 | if (response.MerchantRequestID) { 112 | return { data: response, error: null }; 113 | } 114 | 115 | if (response.errorCode) { 116 | return { data: null, error: response }; 117 | } 118 | 119 | return response; 120 | } 121 | 122 | public async registerUrls( 123 | ConfirmationURL: string, 124 | ValidationURL: string, 125 | response_type: ResponseType = "Completed" 126 | ): Promise { 127 | const response = await this.service.post("mpesa/c2b/v1/registerurl", { 128 | ShortCode: this.config.store, 129 | ResponseType: response_type, 130 | ConfirmationURL, 131 | ValidationURL, 132 | }); 133 | 134 | if (response.errorCode) { 135 | return { data: null, error: response }; 136 | } 137 | 138 | if (response.MerchantRequestID) { 139 | return { data: response, error: null }; 140 | } 141 | 142 | return response; 143 | } 144 | 145 | /** 146 | * Simulates a C2B request 147 | * 148 | * @param phone Receiving party phone 149 | * @param amount Amount to transfer 150 | * @param command Command ID 151 | * @param reference 152 | * @param callback Defined function or closure to process data and return true/false 153 | * 154 | * @return Promise 155 | */ 156 | public async simulateC2B( 157 | phone: string | number, 158 | amount = 10, 159 | reference: string | number = "TRX", 160 | command = "" 161 | ) { 162 | phone = "254" + String(phone).slice(-9); 163 | 164 | const response = await this.service.post("mpesa/c2b/v1/simulate", { 165 | ShortCode: this.config.shortcode, 166 | CommandID: command, 167 | Amount: Number(amount), 168 | Msisdn: phone, 169 | BillRefNumber: reference, 170 | }); 171 | 172 | if (response.MerchantRequestID) { 173 | return { data: response, error: null }; 174 | } 175 | 176 | if (response.errorCode) { 177 | return { data: null, error: response }; 178 | } 179 | } 180 | 181 | /** 182 | * Transfer funds between two paybills 183 | * @param receiver Receiving party phone 184 | * @param amount Amount to transfer 185 | * @param command Command ID 186 | * @param occassion 187 | * @param remarks 188 | * 189 | * @return Promise 190 | */ 191 | public async sendB2C( 192 | PartyB: string | number, 193 | Amount: number = 10, 194 | CommandID: B2CCommands = "BusinessPayment", 195 | QueueTimeOutURL: string = "/lipwa/timeout", 196 | ResultURL: string = "/lipwa/result", 197 | Remarks = "", 198 | Occasion = "" 199 | ): Promise { 200 | PartyB = "254" + String(PartyB).slice(-9); 201 | 202 | const response = await this.service.post( 203 | "mpesa/b2c/v1/paymentrequest", 204 | { 205 | InitiatorName: this.config.username, 206 | SecurityCredential: 207 | await this.service.generateSecurityCredential(), 208 | CommandID, 209 | Amount, 210 | PartyA: this.config.shortcode, 211 | PartyB, 212 | Remarks, 213 | QueueTimeOutURL, 214 | ResultURL, 215 | Occasion, 216 | } 217 | ); 218 | 219 | if (response.OriginatorConversationID) { 220 | return { data: response, error: null }; 221 | } 222 | 223 | if (response.ResultCode && response.ResultCode !== 0) { 224 | return { 225 | data: null, 226 | error: { 227 | errorCode: response.ResultCode, 228 | errorMessage: response.ResultDesc, 229 | }, 230 | }; 231 | } 232 | 233 | if (response.errorCode) { 234 | return { data: null, error: response }; 235 | } 236 | 237 | return response; 238 | } 239 | 240 | /** 241 | * Transfer funds between two paybills 242 | * @param receiver Receiving party paybill 243 | * @param receiver_type Receiver party type 244 | * @param amount Amount to transfer 245 | * @param command Command ID 246 | * @param reference Account Reference mandatory for “BusinessPaybill” CommandID. 247 | * @param remarks 248 | * 249 | * @return Promise 250 | */ 251 | public async sendB2B( 252 | receiver: string | number, 253 | receiver_type: string | number, 254 | amount: number, 255 | command: B2BCommands = "BusinessBuyGoods", 256 | reference: string | number = "TRX", 257 | QueueTimeOutURL: string = "/lipwa/timeout", 258 | ResultURL: string = "/lipwa/result", 259 | remarks = "" 260 | ): Promise { 261 | const response = await this.service.post( 262 | "mpesa/b2b/v1/paymentrequest", 263 | { 264 | Initiator: this.config.username, 265 | SecurityCredential: 266 | await this.service.generateSecurityCredential(), 267 | CommandID: command, 268 | SenderIdentifierType: this.config.type, 269 | RecieverIdentifierType: receiver_type, 270 | Amount: amount, 271 | PartyA: this.config.shortcode, 272 | PartyB: receiver, 273 | AccountReference: reference, 274 | Remarks: remarks, 275 | QueueTimeOutURL, 276 | ResultURL, 277 | } 278 | ); 279 | 280 | if (response.MerchantRequestID) { 281 | return { data: response, error: null }; 282 | } 283 | 284 | if (response.errorCode) { 285 | return { data: null, error: response }; 286 | } 287 | 288 | return response; 289 | } 290 | 291 | /** 292 | * Generate QR Code 293 | * @param QRVersion Version number of the QR. e.g "01" 294 | * @param QRFormat Format of QR output: ("1": Image Format. "2": QR Format. "3": Binary Data Format. "4": PDF Format.) 295 | * @param QRType The type of QR being used : ("D": Dynamic QR Type) 296 | * @param MerchantName Name of the Company/M-Pesa Merchant Name 297 | * @param RefNo Transaction Reference 298 | * @param Amount The total amount for the sale/transaction 299 | * @param TrxCode Transaction Type: (BG: Pay Merchant (Buy Goods). WA: Withdraw Cash at Agent Till. PB: Paybill or Business number. SM: Send Money(Mobile number). SB: Sent to Business. Business number CPI in MSISDN format. 300 | * @param CPI Credit Party Identifier. Can be a Mobile Number, Business Number, Agent Till, Paybill or Business number, Merchant Buy Goods. 301 | */ 302 | public async generateQR( 303 | Amount: string|number, 304 | MerchantName: string, 305 | CPI: string|number, 306 | RefNo: string, 307 | TrxCode: string = "BG", 308 | QRVersion: string = "01", 309 | QRFormat: string = "1", 310 | QRType: string = "D" 311 | ): Promise { 312 | const response = await this.service.post("mpesa/qrcode/v1/generate", { 313 | QRVersion, 314 | QRFormat, 315 | QRType, 316 | MerchantName, 317 | RefNo, 318 | Amount: Number(Amount), 319 | TrxCode, 320 | CPI, 321 | }); 322 | 323 | if (response.QRCode) { 324 | return { data: response, error: null }; 325 | } else { 326 | return { data: null, error: response }; 327 | } 328 | } 329 | 330 | /** 331 | * Get Status of a Transaction 332 | * 333 | * @param transaction 334 | * @param command 335 | * @param remarks 336 | * @param occassion 337 | * 338 | * @return Promise Result 339 | */ 340 | public async checkStatus( 341 | transaction: string, 342 | command = "TransactionStatusQuery", 343 | QueueTimeOutURL: string = "/lipwa/timeout", 344 | ResultURL: string = "/lipwa/result", 345 | remarks = "Transaction Status Query", 346 | occasion = "Transaction Status Query" 347 | ): Promise { 348 | const response = await this.service.post( 349 | "mpesa/transactionstatus/v1/query", 350 | { 351 | Initiator: this.config.username, 352 | SecurityCredential: 353 | await this.service.generateSecurityCredential(), 354 | CommandID: command, 355 | TransactionID: transaction, 356 | PartyA: this.config.shortcode, 357 | IdentifierType: this.config.type, 358 | ResultURL, 359 | QueueTimeOutURL, 360 | Remarks: remarks, 361 | Occasion: occasion, 362 | } 363 | ); 364 | 365 | if (response.MerchantRequestID) { 366 | return { data: response, error: null }; 367 | } 368 | 369 | if (response.errorCode) { 370 | return { data: null, error: response }; 371 | } 372 | 373 | return response; 374 | } 375 | 376 | /** 377 | * Reverse a Transaction 378 | * 379 | * @param transaction 380 | * @param amount 381 | * @param receiver 382 | * @param receiver_type 383 | * @param remarks 384 | * @param occassion 385 | * 386 | * @return Promise Result 387 | */ 388 | public async reverseTransaction( 389 | transaction: string, 390 | amount: number, 391 | receiver: number, 392 | receiver_type = 3, 393 | QueueTimeOutURL: string = "/lipwa/timeout", 394 | ResultURL: string = "/lipwa/result", 395 | remarks = "Transaction Reversal", 396 | occasion = "Transaction Reversal" 397 | ): Promise { 398 | const response = await this.service.post("mpesa/reversal/v1/request", { 399 | CommandID: "TransactionReversal", 400 | Initiator: this.config.username, 401 | SecurityCredential: await this.service.generateSecurityCredential(), 402 | TransactionID: transaction, 403 | Amount: amount, 404 | ReceiverParty: receiver, 405 | RecieverIdentifierType: receiver_type, 406 | ResultURL, 407 | QueueTimeOutURL, 408 | Remarks: remarks, 409 | Occasion: occasion, 410 | }); 411 | 412 | if (response.MerchantRequestID) { 413 | return { data: response, error: null }; 414 | } 415 | 416 | if (response.errorCode) { 417 | return { data: null, error: response }; 418 | } 419 | 420 | return response; 421 | } 422 | 423 | /** 424 | * Check Account Balance 425 | * 426 | * @param command 427 | * @param remarks 428 | * @param occassion 429 | * 430 | * @return Promise Result 431 | */ 432 | public async checkBalance( 433 | command: string, 434 | QueueTimeOutURL: string = "/lipwa/timeout", 435 | ResultURL: string = "/lipwa/result", 436 | remarks = "Balance Query" 437 | ): Promise { 438 | const response = await this.service.post( 439 | "mpesa/accountbalance/v1/query", 440 | { 441 | CommandID: command, 442 | Initiator: this.config.username, 443 | SecurityCredential: 444 | await this.service.generateSecurityCredential(), 445 | PartyA: this.config.shortcode, 446 | IdentifierType: this.config.type, 447 | Remarks: remarks, 448 | QueueTimeOutURL, 449 | ResultURL, 450 | } 451 | ); 452 | 453 | if (response.MerchantRequestID) { 454 | return { data: response, error: null }; 455 | } 456 | 457 | if (response.errorCode) { 458 | return { data: null, error: response }; 459 | } 460 | 461 | return response; 462 | } 463 | 464 | /** 465 | * Validate Transaction Data 466 | * 467 | * @param callback Defined function or closure to process data and return true/false 468 | * 469 | * @return Promise 470 | */ 471 | public validateTransaction(ok: boolean) { 472 | return ok 473 | ? { 474 | ResultCode: 0, 475 | ResultDesc: "Success", 476 | } 477 | : { 478 | ResultCode: 1, 479 | ResultDesc: "Failed", 480 | }; 481 | } 482 | 483 | /** 484 | * Confirm Transaction Data 485 | * 486 | * @param callback Defined function or closure to process data and return true/false 487 | * 488 | * @return Promise 489 | */ 490 | public confirmTransaction(ok: boolean) { 491 | return ok 492 | ? { 493 | ResultCode: 0, 494 | ResultDesc: "Success", 495 | } 496 | : { 497 | ResultCode: 1, 498 | ResultDesc: "Failed", 499 | }; 500 | } 501 | 502 | /** 503 | * Reconcile Transaction Using Instant Payment Notification from M-PESA 504 | * 505 | * @param callback Defined function or closure to process data and return true/false 506 | * 507 | * @return Promise 508 | */ 509 | public reconcileTransaction(ok: boolean) { 510 | return ok 511 | ? { 512 | ResultCode: 0, 513 | ResultDesc: "Service request successful", 514 | } 515 | : { 516 | ResultCode: 1, 517 | ResultDesc: "Service request failed", 518 | }; 519 | } 520 | 521 | /** 522 | * Process Results of an API Request 523 | * 524 | * @param callback Defined function or closure to process data and return true/false 525 | * 526 | * @return Promise 527 | */ 528 | public processResults(ok: boolean) { 529 | return ok 530 | ? { 531 | ResultCode: 0, 532 | ResultDesc: "Service request successful", 533 | } 534 | : { 535 | ResultCode: 1, 536 | ResultDesc: "Service request failed", 537 | }; 538 | } 539 | 540 | /** 541 | * Process Transaction Timeout 542 | * 543 | * @param callback Defined function or closure to process data and return true/false 544 | * 545 | * @return Promise 546 | */ 547 | public processTimeout(ok: boolean) { 548 | return ok 549 | ? { 550 | ResultCode: 0, 551 | ResultDesc: "Service request successful", 552 | } 553 | : { 554 | ResultCode: 1, 555 | ResultDesc: "Service request failed", 556 | }; 557 | } 558 | } 559 | -------------------------------------------------------------------------------- /dist/mpesa.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | Object.defineProperty(exports, "__esModule", { value: true }); 3 | exports.Mpesa = void 0; 4 | const date_fns_1 = require("date-fns"); 5 | const billing_1 = require("./billing"); 6 | const service_1 = require("./service"); 7 | class Mpesa { 8 | /** 9 | * Setup global configuration for classes 10 | * @param Array configs Formatted configuration options 11 | * 12 | * @return void 13 | */ 14 | constructor(configs) { 15 | /** 16 | * @param object config Configuration options 17 | */ 18 | this.config = { 19 | env: "sandbox", 20 | type: 4, 21 | shortcode: 174379, 22 | store: 174379, 23 | key: "9v38Dtu5u2BpsITPmLcXNWGMsjZRWSTG", 24 | secret: "bclwIPkcRqw61yUt", 25 | username: "apitest", 26 | password: "", 27 | passkey: "bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919", 28 | validationUrl: "/lipwa/validate", 29 | confirmationUrl: "/lipwa/confirm", 30 | callbackUrl: "/lipwa/reconcile", 31 | timeoutUrl: "/lipwa/timeout", 32 | resultUrl: "/lipwa/results", 33 | billingUrl: "/lipwa/billing", 34 | }; 35 | this.ref = Math.random().toString(16).slice(2, 8).toUpperCase(); 36 | const defaults = { 37 | env: "sandbox", 38 | type: 4, 39 | shortcode: 174379, 40 | store: 174379, 41 | key: "9v38Dtu5u2BpsITPmLcXNWGMsjZRWSTG", 42 | secret: "bclwIPkcRqw61yUt", 43 | username: "apitest", 44 | password: "", 45 | passkey: "bfb279f9aa9bdbcf158e97dd71a467cd2e0c893059b10f78e6b72ada1ed2c919", 46 | validationUrl: "/lipwa/validate", 47 | confirmationUrl: "/lipwa/confirm", 48 | callbackUrl: "/lipwa/reconcile", 49 | timeoutUrl: "/lipwa/timeout", 50 | resultUrl: "/lipwa/results", 51 | billingUrl: "/lipwa/billing", 52 | }; 53 | if (!configs || !configs.store || configs.type == 4) { 54 | configs.store = configs.shortcode; 55 | } 56 | this.config = Object.assign(Object.assign({}, defaults), configs); 57 | this.service = new service_1.Service(this.config); 58 | } 59 | billing() { 60 | return new billing_1.BillManager(this.config); 61 | } 62 | /** 63 | * @param phone The MSISDN sending the funds. 64 | * @param amount The amount to be transacted. 65 | * @param reference Used with M-Pesa PayBills. 66 | * @param description A description of the transaction. 67 | * @param remark Remarks 68 | * 69 | * @return Promise Response 70 | */ 71 | async stkPush(phone, amount, reference = this.ref, description = "Transaction Description", remark = "Remark") { 72 | phone = "254" + String(phone).slice(-9); 73 | const timestamp = (0, date_fns_1.format)(new Date(), "yyyyMMddHHmmss"); 74 | const password = Buffer.from(this.config.shortcode + this.config.passkey + timestamp).toString("base64"); 75 | const response = await this.service.post("mpesa/stkpush/v1/processrequest", { 76 | BusinessShortCode: this.config.store, 77 | Password: password, 78 | Timestamp: timestamp, 79 | TransactionType: Number(this.config.type) == 4 80 | ? "CustomerPayBillOnline" 81 | : "CustomerBuyGoodsOnline", 82 | Amount: Number(amount), 83 | PartyA: phone, 84 | PartyB: this.config.shortcode, 85 | PhoneNumber: phone, 86 | CallBackURL: this.config.callbackUrl, 87 | AccountReference: reference, 88 | TransactionDesc: description, 89 | Remark: remark, 90 | }); 91 | if (response.MerchantRequestID) { 92 | return { data: response, error: null }; 93 | } 94 | if (response.errorCode) { 95 | return { data: null, error: response }; 96 | } 97 | return response; 98 | } 99 | async registerUrls(response_type = "Completed") { 100 | const response = await this.service.post("mpesa/c2b/v1/registerurl", { 101 | ShortCode: this.config.store, 102 | ResponseType: response_type, 103 | ConfirmationURL: this.config.confirmationUrl, 104 | ValidationURL: this.config.validationUrl, 105 | }); 106 | if (response.errorCode) { 107 | return { data: null, error: response }; 108 | } 109 | if (response.MerchantRequestID) { 110 | return { data: response, error: null }; 111 | } 112 | return response; 113 | } 114 | /** 115 | * Simulates a C2B request 116 | * 117 | * @param phone Receiving party phone 118 | * @param amount Amount to transfer 119 | * @param command Command ID 120 | * @param reference 121 | * @param callback Defined function or closure to process data and return true/false 122 | * 123 | * @return Promise 124 | */ 125 | async simulateC2B(phone, amount = 10, reference = "TRX", command = "") { 126 | phone = "254" + String(phone).slice(-9); 127 | const response = await this.service.post("mpesa/c2b/v1/simulate", { 128 | ShortCode: this.config.shortcode, 129 | CommandID: command, 130 | Amount: Number(amount), 131 | Msisdn: phone, 132 | BillRefNumber: reference, 133 | }); 134 | if (response.MerchantRequestID) { 135 | return { data: response, error: null }; 136 | } 137 | if (response.errorCode) { 138 | return { data: null, error: response }; 139 | } 140 | } 141 | /** 142 | * Transfer funds between two paybills 143 | * @param receiver Receiving party phone 144 | * @param amount Amount to transfer 145 | * @param command Command ID 146 | * @param occassion 147 | * @param remarks 148 | * 149 | * @return Promise 150 | */ 151 | async sendB2C(phone, amount = 10, command = "BusinessPayment", remarks = "", occassion = "") { 152 | phone = "254" + String(phone).slice(-9); 153 | const response = await this.service.post("mpesa/b2c/v1/paymentrequest", { 154 | InitiatorName: this.config.username, 155 | SecurityCredential: await this.service.generateSecurityCredential(), 156 | CommandID: command, 157 | Amount: Number(amount), 158 | PartyA: this.config.shortcode, 159 | PartyB: phone, 160 | Remarks: remarks, 161 | QueueTimeOutURL: this.config.timeoutUrl, 162 | ResultURL: this.config.resultUrl, 163 | Occasion: occassion, 164 | }); 165 | if (response.OriginatorConversationID) { 166 | return { data: response, error: null }; 167 | } 168 | if (response.ResultCode && response.ResultCode !== 0) { 169 | return { 170 | data: null, 171 | error: { 172 | errorCode: response.ResultCode, 173 | errorMessage: response.ResultDesc, 174 | }, 175 | }; 176 | } 177 | if (response.errorCode) { 178 | return { data: null, error: response }; 179 | } 180 | return response; 181 | } 182 | /** 183 | * Transfer funds between two paybills 184 | * @param receiver Receiving party paybill 185 | * @param receiver_type Receiver party type 186 | * @param amount Amount to transfer 187 | * @param command Command ID 188 | * @param reference Account Reference mandatory for “BusinessPaybill” CommandID. 189 | * @param remarks 190 | * 191 | * @return Promise 192 | */ 193 | async sendB2B(receiver, receiver_type, amount, command = "BusinessBuyGoods", reference = "TRX", remarks = "") { 194 | const response = await this.service.post("mpesa/b2b/v1/paymentrequest", { 195 | Initiator: this.config.username, 196 | SecurityCredential: await this.service.generateSecurityCredential(), 197 | CommandID: command, 198 | SenderIdentifierType: this.config.type, 199 | RecieverIdentifierType: receiver_type, 200 | Amount: amount, 201 | PartyA: this.config.shortcode, 202 | PartyB: receiver, 203 | AccountReference: reference, 204 | Remarks: remarks, 205 | QueueTimeOutURL: this.config.timeoutUrl, 206 | ResultURL: this.config.resultUrl, 207 | }); 208 | if (response.MerchantRequestID) { 209 | return { data: response, error: null }; 210 | } 211 | if (response.errorCode) { 212 | return { data: null, error: response }; 213 | } 214 | return response; 215 | } 216 | /** 217 | * Generate QR Code 218 | * @param QRVersion Version number of the QR. e.g "01" 219 | * @param QRFormat Format of QR output: ("1": Image Format. "2": QR Format. "3": Binary Data Format. "4": PDF Format.) 220 | * @param QRType The type of QR being used : ("D": Dynamic QR Type) 221 | * @param MerchantName Name of the Company/M-Pesa Merchant Name 222 | * @param RefNo Transaction Reference 223 | * @param Amount The total amount for the sale/transaction 224 | * @param TrxCode Transaction Type: (BG: Pay Merchant (Buy Goods). WA: Withdraw Cash at Agent Till. PB: Paybill or Business number. SM: Send Money(Mobile number). SB: Sent to Business. Business number CPI in MSISDN format. 225 | * @param CPI Credit Party Identifier. Can be a Mobile Number, Business Number, Agent Till, Paybill or Business number, Merchant Buy Goods. 226 | */ 227 | async generateQR(Amount, MerchantName, CPI, RefNo, TrxCode = "BG", QRVersion = "01", QRFormat = "1", QRType = "D") { 228 | const response = await this.service.post("mpesa/qrcode/v1/generate", { 229 | QRVersion, 230 | QRFormat, 231 | QRType, 232 | MerchantName, 233 | RefNo, 234 | Amount: Number(Amount), 235 | TrxCode, 236 | CPI, 237 | }); 238 | if (response.QRCode) { 239 | return { data: response, error: null }; 240 | } 241 | else { 242 | return { data: null, error: response }; 243 | } 244 | } 245 | /** 246 | * Get Status of a Transaction 247 | * 248 | * @param transaction 249 | * @param command 250 | * @param remarks 251 | * @param occassion 252 | * 253 | * @return Promise Result 254 | */ 255 | async checkStatus(transaction, command = "TransactionStatusQuery", remarks = "Transaction Status Query", occasion = "Transaction Status Query") { 256 | const response = await this.service.post("mpesa/transactionstatus/v1/query", { 257 | Initiator: this.config.username, 258 | SecurityCredential: await this.service.generateSecurityCredential(), 259 | CommandID: command, 260 | TransactionID: transaction, 261 | PartyA: this.config.shortcode, 262 | IdentifierType: this.config.type, 263 | ResultURL: this.config.resultUrl, 264 | QueueTimeOutURL: this.config.timeoutUrl, 265 | Remarks: remarks, 266 | Occasion: occasion, 267 | }); 268 | if (response.MerchantRequestID) { 269 | return { data: response, error: null }; 270 | } 271 | if (response.errorCode) { 272 | return { data: null, error: response }; 273 | } 274 | return response; 275 | } 276 | /** 277 | * Reverse a Transaction 278 | * 279 | * @param transaction 280 | * @param amount 281 | * @param receiver 282 | * @param receiver_type 283 | * @param remarks 284 | * @param occassion 285 | * 286 | * @return Promise Result 287 | */ 288 | async reverseTransaction(transaction, amount, receiver, receiver_type = 3, remarks = "Transaction Reversal", occasion = "Transaction Reversal") { 289 | const response = await this.service.post("mpesa/reversal/v1/request", { 290 | CommandID: "TransactionReversal", 291 | Initiator: this.config.username, 292 | SecurityCredential: await this.service.generateSecurityCredential(), 293 | TransactionID: transaction, 294 | Amount: amount, 295 | ReceiverParty: receiver, 296 | RecieverIdentifierType: receiver_type, 297 | ResultURL: this.config.resultUrl, 298 | QueueTimeOutURL: this.config.timeoutUrl, 299 | Remarks: remarks, 300 | Occasion: occasion, 301 | }); 302 | if (response.MerchantRequestID) { 303 | return { data: response, error: null }; 304 | } 305 | if (response.errorCode) { 306 | return { data: null, error: response }; 307 | } 308 | return response; 309 | } 310 | /** 311 | * Check Account Balance 312 | * 313 | * @param command 314 | * @param remarks 315 | * @param occassion 316 | * 317 | * @return Promise Result 318 | */ 319 | async checkBalance(command, remarks = "Balance Query") { 320 | const response = await this.service.post("mpesa/accountbalance/v1/query", { 321 | CommandID: command, 322 | Initiator: this.config.username, 323 | SecurityCredential: await this.service.generateSecurityCredential(), 324 | PartyA: this.config.shortcode, 325 | IdentifierType: this.config.type, 326 | Remarks: remarks, 327 | QueueTimeOutURL: this.config.timeoutUrl, 328 | ResultURL: this.config.resultUrl, 329 | }); 330 | if (response.MerchantRequestID) { 331 | return { data: response, error: null }; 332 | } 333 | if (response.errorCode) { 334 | return { data: null, error: response }; 335 | } 336 | return response; 337 | } 338 | /** 339 | * Validate Transaction Data 340 | * 341 | * @param callback Defined function or closure to process data and return true/false 342 | * 343 | * @return Promise 344 | */ 345 | validateTransaction(ok) { 346 | return ok 347 | ? { 348 | ResultCode: 0, 349 | ResultDesc: "Success", 350 | } 351 | : { 352 | ResultCode: 1, 353 | ResultDesc: "Failed", 354 | }; 355 | } 356 | /** 357 | * Confirm Transaction Data 358 | * 359 | * @param callback Defined function or closure to process data and return true/false 360 | * 361 | * @return Promise 362 | */ 363 | confirmTransaction(ok) { 364 | return ok 365 | ? { 366 | ResultCode: 0, 367 | ResultDesc: "Success", 368 | } 369 | : { 370 | ResultCode: 1, 371 | ResultDesc: "Failed", 372 | }; 373 | } 374 | /** 375 | * Reconcile Transaction Using Instant Payment Notification from M-PESA 376 | * 377 | * @param callback Defined function or closure to process data and return true/false 378 | * 379 | * @return Promise 380 | */ 381 | reconcileTransaction(ok) { 382 | return ok 383 | ? { 384 | ResultCode: 0, 385 | ResultDesc: "Service request successful", 386 | } 387 | : { 388 | ResultCode: 1, 389 | ResultDesc: "Service request failed", 390 | }; 391 | } 392 | /** 393 | * Process Results of an API Request 394 | * 395 | * @param callback Defined function or closure to process data and return true/false 396 | * 397 | * @return Promise 398 | */ 399 | processResults(ok) { 400 | return ok 401 | ? { 402 | ResultCode: 0, 403 | ResultDesc: "Service request successful", 404 | } 405 | : { 406 | ResultCode: 1, 407 | ResultDesc: "Service request failed", 408 | }; 409 | } 410 | /** 411 | * Process Transaction Timeout 412 | * 413 | * @param callback Defined function or closure to process data and return true/false 414 | * 415 | * @return Promise 416 | */ 417 | processTimeout(ok) { 418 | return ok 419 | ? { 420 | ResultCode: 0, 421 | ResultDesc: "Service request successful", 422 | } 423 | : { 424 | ResultCode: 1, 425 | ResultDesc: "Service request failed", 426 | }; 427 | } 428 | } 429 | exports.Mpesa = Mpesa; 430 | //# sourceMappingURL=mpesa.js.map -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------