{"id":26409436,"url":"https://github.com/sainingo/mpesa-connect","last_synced_at":"2025-07-31T14:14:52.040Z","repository":{"id":282689641,"uuid":"949368719","full_name":"sainingo/mpesa-connect","owner":"sainingo","description":null,"archived":false,"fork":false,"pushed_at":"2025-03-16T10:11:09.000Z","size":0,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-16T10:29:05.595Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sainingo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-16T09:52:51.000Z","updated_at":"2025-03-16T10:11:12.000Z","dependencies_parsed_at":"2025-03-16T10:29:14.705Z","dependency_job_id":"33167bd3-e80a-4883-b1cc-45bb2221a913","html_url":"https://github.com/sainingo/mpesa-connect","commit_stats":null,"previous_names":["sainingo/mpesa-connect"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sainingo%2Fmpesa-connect","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sainingo%2Fmpesa-connect/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sainingo%2Fmpesa-connect/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sainingo%2Fmpesa-connect/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sainingo","download_url":"https://codeload.github.com/sainingo/mpesa-connect/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244097379,"owners_count":20397590,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-17T19:34:17.918Z","updated_at":"2025-03-17T19:34:18.296Z","avatar_url":"https://github.com/sainingo.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MPESA Connect\n\n[![npm version](https://img.shields.io/npm/v/mpesa-connect.svg)](https://www.npmjs.com/package/mpesa-connect)\n[![License](https://img.shields.io/npm/l/mpesa-connect.svg)](https://github.com/yourusername/mpesa-connect/blob/main/LICENSE)\n[![Build Status](https://img.shields.io/travis/yourusername/mpesa-connect.svg)](https://travis-ci.org/yourusername/mpesa-connect)\n\nA powerful, developer-friendly middleware for Safaricom's MPESA payment APIs. MPESA Connect provides a simplified interface to integrate mobile payments into your applications without the complexity of dealing with the raw MPESA APIs.\n\n## 🚀 Features\n\n- **Simplified API Integration**: Clean, RESTful endpoints for all MPESA operations\n- **Authentication Handling**: Automatic OAuth token management\n- **Comprehensive Coverage**: Support for STK Push, C2B, B2C, and more\n- **Robust Error Handling**: Detailed error messages and logging\n- **Webhook Management**: Easy configuration of callback URLs and processing\n- **Transaction Tracking**: Built-in transaction status tracking\n- **Flexible Deployment**: Use as a standalone service (middleware)\n\n## 📦 Installation\n\n\u003c!-- ### As an npm package\n\n```bash\nnpm install mpesa-connect\n``` --\u003e\n\n### As a standalone service\n\n```bash\n# Clone the repository\ngit clone https://github.com/sainingo/mpesa-connect.git\n\n# Install dependencies\ncd mpesa-connect\nnpm install\n\n# Configure environment variables\ncp .env.example .env\n\n# Start the server\nnpm start\n```\n\n## ⚙️ Configuration\n\nCreate a `.env` file with the following variables:\n\n```\n# Server Configuration\nPORT=3000\nHOST=localhost\nNODE_ENV=development\n\n# MPESA API Configuration\nMPESA_CONSUMER_KEY=your_consumer_key\nMPESA_CONSUMER_SECRET=your_consumer_secret\nMPESA_PASSKEY=your_passkey\nMPESA_SHORT_CODE=your_shortcode\nMPESA_ENVIRONMENT=sandbox  # or production\n\n# Database Configuration (if applicable)\nMONGODB_URI=mongodb://localhost:27017/mpesa-connect\n\n# Security\nJWT_SECRET=your_jwt_secret\n```\n\n## 🔧 Usage\n\n\u003c!-- ### As an npm package\n\n```javascript\n// Initialize the client\nconst { MpesaConnect } = require('mpesa-connect');\n\nconst mpesa = new MpesaConnect({\n  consumerKey: 'your_consumer_key',\n  consumerSecret: 'your_consumer_secret',\n  shortCode: 'your_shortcode',\n  passkey: 'your_passkey',\n  environment: 'sandbox' // or 'production'\n});\n\n// STK Push Example\nasync function initiatePayment() {\n  try {\n    const result = await mpesa.stkPush({\n      amount: 1,\n      phoneNumber: '254712345678',\n      accountReference: 'Test',\n      transactionDesc: 'Test Payment'\n    });\n    \n    console.log('Payment initiated:', result);\n  } catch (error) {\n    console.error('Payment failed:', error);\n  }\n}\n\n// C2B Registration Example\nasync function registerUrls() {\n  try {\n    const result = await mpesa.c2b.registerUrls({\n      ValidationURL: 'https://example.com/validation',\n      ConfirmationURL: 'https://example.com/confirmation',\n      ResponseType: 'Completed'\n    });\n    \n    console.log('URLs registered:', result);\n  } catch (error) {\n    console.error('Registration failed:', error);\n  }\n}\n``` --\u003e\n\n### As a standalone service\n\n#### STK Push\n\n```bash\ncurl -X POST http://localhost:3000/api/v1/stk/push \\\n  -H 'Content-Type: application/json' \\\n  -H 'Authorization: Bearer YOUR_API_KEY' \\\n  -d '{\n    \"phoneNumber\": \"254712345678\",\n    \"amount\": 1,\n    \"accountReference\": \"Test\",\n    \"transactionDesc\": \"Test Payment\"\n  }'\n```\n\n#### B2C Payment\n\n```bash\ncurl -X POST http://localhost:3000/api/v1/b2c/payment \\\n  -H 'Content-Type: application/json' \\\n  -H 'Authorization: Bearer YOUR_API_KEY' \\\n  -d '{\n    \"phoneNumber\": \"254712345678\",\n    \"amount\": 1,\n    \"occasion\": \"Promotion\",\n    \"remarks\": \"Promotion payment\"\n  }'\n```\n\n## 🔄 API Reference\n\n### STK Push\n\n| Endpoint | Method | Description |\n|----------|--------|-------------|\n| `/api/v1/stk/push` | POST | Initiate STK Push prompt |\n| `/api/v1/stk/query` | POST | Query STK Push transaction status |\n\n### C2B\n\n| Endpoint | Method | Description |\n|----------|--------|-------------|\n| `/api/v1/c2b/register` | POST | Register validation and confirmation URLs |\n| `/api/v1/c2b/simulate` | POST | Simulate C2B transaction (sandbox only) |\n\n### B2C\n\n| Endpoint | Method | Description |\n|----------|--------|-------------|\n| `/api/v1/b2c/payment` | POST | Send money to customer |\n| `/api/v1/b2c/status` | POST | Query B2C transaction status |\n\n### Authentication\n\n| Endpoint | Method | Description |\n|----------|--------|-------------|\n| `/api/v1/auth/token` | GET | Get OAuth token (for testing) |\n| `/api/v1/auth/register` | POST | Register API user |\n| `/api/v1/auth/login` | POST | Login to get API key |\n\n## 📚 Webhook Handling\n\nMPESA Connect automatically processes incoming webhooks. Set up your callback URLs using the C2B registration endpoint, and implement handlers for incoming notifications:\n\n```javascript\n// TODO: When using as an npm package \nmpesa.on('payment.received', (payment) =\u003e {\n  console.log('Payment received:', payment);\n  // Update your database, notify your user, etc.\n});\n\nmpesa.on('payment.failed', (error) =\u003e {\n  console.error('Payment failed:', error);\n  // Handle the failure\n});\n```\n\n## 🧪 Testing\n\n```bash\n# Run tests\nnpm test\n\n# Run tests with coverage\nnpm run test:coverage\n```\n\n## 🤝 Contributing\n\n1. Fork the repository\n2. Create your feature branch (`git checkout -b feature/amazing-feature`)\n3. Commit your changes (`git commit -m 'Add some amazing feature'`)\n4. Push to the branch (`git push origin feature/amazing-feature`)\n5. Open a Pull Request\n\n## 📄 License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n## 🙏 Acknowledgements\n\n- [Safaricom Developer Portal](https://developer.safaricom.co.ke/)\n- [Hapi.js](https://hapi.dev/) - The framework used\n- Contributors who have helped improve this project\n\n---\n\nBuilt with ❤️ by Kantush","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsainingo%2Fmpesa-connect","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsainingo%2Fmpesa-connect","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsainingo%2Fmpesa-connect/lists"}