├── .gitignore ├── README.MD ├── apiUser.php ├── collections.php ├── composer.json ├── disbursement.php └── remittance.php /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor/ 2 | -------------------------------------------------------------------------------- /README.MD: -------------------------------------------------------------------------------- 1 | # PHP Implementation for MTN Momo API 2 | 3 | This is a PHP package for the MTN's Mobile Money REST API dubbed Momo API. The official Momo API documentation is available on https://momodeveloper.mtn.com/api-documentation. 4 | 5 | ## Installation 6 | 7 | - Run the following command to clone this repo 8 | 9 | ``` 10 | git clone https://github.com/Bascil/mtn-momo-api-php.git 11 | 12 | ``` 13 | 14 | OR 15 | 16 | - Download the source code as zipped 17 | 18 | ## Linux Hosting 19 | 20 | If you need VPS or dedicated hosting, please visit this link [Server Host](https://serverhost53.com) 21 | 22 | ## Support 23 | 24 | Need support using this package:- 25 | 26 | Email basilndonga@gmail.com or skype me at `basilndonga`. 27 | 28 | If you wish to be added as a contributor to this project let me know. If you wish to buy me a coffee, you can support me on this [link](https://buymeacoffee.com/basilndonga). 29 | 30 | If you were inspired by this project, don't forget to follow me on github and on twitter `@basilndonga` as well. 31 | 32 | If you wish to engage me as a developer for your project, feel free to contact me 33 | 34 | ## License 35 | 36 | This package is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT). 37 | 38 | Happy coding!!!!!!! 39 | -------------------------------------------------------------------------------- /apiUser.php: -------------------------------------------------------------------------------- 1 | 'https://localhost', 14 | ); 15 | 16 | $curl = curl_init(); 17 | curl_setopt($curl, CURLOPT_POST, 1); 18 | curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data); 19 | curl_setopt($curl, CURLOPT_URL, 'https://sandbox.momodeveloper.mtn.com/v1_0/apiuser'); 20 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 21 | curl_setopt( 22 | $curl, 23 | CURLOPT_HTTPHEADER, 24 | array( 25 | 'Content-Type: application/json', 26 | 'X-Reference-Id: ' . $token, 27 | 'Ocp-Apim-Subscription-Key: ' . $apiKey, 28 | ) 29 | ); 30 | 31 | $result = curl_exec($curl); 32 | if (!$result) {die("Connection Failure");} 33 | curl_close($curl); 34 | echo $result; 35 | 36 | } 37 | 38 | function get_uuid() 39 | { 40 | return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x', 41 | // 32 bits for "time_low" 42 | mt_rand(0, 0xffff), mt_rand(0, 0xffff), 43 | 44 | // 16 bits for "time_mid" 45 | mt_rand(0, 0xffff), 46 | 47 | // 16 bits for "time_hi_and_version", 48 | // four most significant bits holds version number 4 49 | mt_rand(0, 0x0fff) | 0x4000, 50 | 51 | // 16 bits, 8 bits for "clk_seq_hi_res", 52 | // 8 bits for "clk_seq_low", 53 | // two most significant bits holds zero and one for variant DCE1.1 54 | mt_rand(0, 0x3fff) | 0x8000, 55 | 56 | // 48 bits for "node" 57 | mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff) 58 | ); 59 | } -------------------------------------------------------------------------------- /collections.php: -------------------------------------------------------------------------------- 1 | access_token; 35 | if(!$access_token){ 36 | throw new Exception("Invalid access token generated"); 37 | return FALSE; 38 | } 39 | return $access_token; 40 | 41 | } 42 | 43 | // request payment from customer 44 | function requestToPay(){ 45 | 46 | $access_token = get_accesstoken(); 47 | $endpoint_url = 'https://ericssonbasicapi1.azure-api.net/collection/v1_0/requesttopay'; 48 | 49 | # Parameters 50 | $data = array( 51 | "amount" => "1", 52 | "currency" => "GHS", //default for sandbox 53 | "externalId" => "123456", //reference number 54 | 55 | "payer" => array( 56 | 57 | "partyIdType" => "MSISDN", 58 | "partyId" => "233XXXXXXXXX" //user phone number, these are test numbers) 59 | ), 60 | 61 | "payerMessage"=> "Payment Request", 62 | "payeeNote"=> "Please confirm payment" 63 | 64 | 65 | ); 66 | 67 | $data_string = json_encode($data); 68 | 69 | $curl = curl_init(); 70 | curl_setopt($curl, CURLOPT_URL, $endpoint_url); 71 | curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); 72 | //curl_setopt($curl, CURLOPT_TIMEOUT, 50); 73 | curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string); 74 | 75 | curl_setopt( 76 | $curl, 77 | CURLOPT_HTTPHEADER, 78 | array( 79 | 'Content-Type: application/json', //optional 80 | 'Authorization: Bearer '.$access_token, //optional 81 | //'X-Callback-Url: https://anzilasandbox.ngrok.io', //optional, not required for sandbox 82 | 'X-Reference-Id: '.get_uuid(), 83 | 'X-Target-Environment: sandbox', 84 | 'Ocp-Apim-Subscription-Key: '.COLLECTION_SUBSCRIPTION_KEY, 85 | 86 | ) 87 | ); 88 | 89 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 90 | 91 | $curl_response = curl_exec($curl); //will respond with HTTP 202 Accepted 92 | // close curl resource to free up system resources 93 | 94 | curl_close($curl); 95 | } 96 | 97 | 98 | function get_uuid() { 99 | return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', 100 | // 32 bits for "time_low" 101 | mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), 102 | 103 | // 16 bits for "time_mid" 104 | mt_rand( 0, 0xffff ), 105 | 106 | // 16 bits for "time_hi_and_version", 107 | // four most significant bits holds version number 4 108 | mt_rand( 0, 0x0fff ) | 0x4000, 109 | 110 | // 16 bits, 8 bits for "clk_seq_hi_res", 111 | // 8 bits for "clk_seq_low", 112 | // two most significant bits holds zero and one for variant DCE1.1 113 | mt_rand( 0, 0x3fff ) | 0x8000, 114 | 115 | // 48 bits for "node" 116 | mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) 117 | ); 118 | } -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "bascil/mtn-momo-api-php", 3 | "description": "PHP Implementation for MTN Momo API", 4 | "license": "MIT", 5 | "authors": [ 6 | { 7 | "name": "Basil Ndonga", 8 | "email": "basilndonga@gmail.com" 9 | } 10 | ], 11 | "require": {} 12 | } 13 | -------------------------------------------------------------------------------- /disbursement.php: -------------------------------------------------------------------------------- 1 | access_token; 35 | // The above $access_token expires after an hour, find a way to cache it to minimize requests to the server 36 | if(!$access_token){ 37 | throw new Exception("Invalid access token generated"); 38 | return FALSE; 39 | } 40 | return $access_token; 41 | 42 | } 43 | 44 | // request payment from customer 45 | function disburse(){ 46 | 47 | $access_token = get_accesstoken(); 48 | $endpoint_url = 'https://sandbox.momodeveloper.mtn.com/disbursement/v1_0/transfer'; 49 | 50 | # Parameters 51 | $data = array( 52 | "amount" => "3000", 53 | "currency" => "EUR", //default for sandbox 54 | "externalId" => "123456", //reference number 55 | 56 | "payee" => array( 57 | 58 | "partyIdType" => "MSISDN", 59 | "partyId" => "46733123453" //user phone number, these are test numbers) 60 | ), 61 | 62 | "payerMessage"=> "Funds Transfer", 63 | "payeeNote"=> "We have transfered funds" 64 | 65 | 66 | ); 67 | 68 | $data_string = json_encode($data); 69 | 70 | $curl = curl_init(); 71 | curl_setopt($curl, CURLOPT_URL, $endpoint_url); 72 | curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); 73 | //curl_setopt($curl, CURLOPT_TIMEOUT, 50); 74 | curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string); 75 | 76 | curl_setopt( 77 | $curl, 78 | CURLOPT_HTTPHEADER, 79 | array( 80 | 'Content-Type: application/json', //optional 81 | 'Authorization: Bearer '.$access_token, //optional 82 | //'X-Callback-Url: https://anzilasandbox.ngrok.io', //optional, not required for sandbox 83 | 'X-Reference-Id: '.get_uuid(), 84 | 'X-Target-Environment: sandbox', 85 | 'Ocp-Apim-Subscription-Key: '.DISBURSEMENT_SUBSCRIPTION_KEY, 86 | 87 | ) 88 | ); 89 | 90 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 91 | 92 | $curl_response = curl_exec($curl); //will respond with HTTP 202 Accepted 93 | // close curl resource to free up system resources 94 | curl_close($curl); 95 | } 96 | 97 | 98 | function get_uuid() { 99 | return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', 100 | // 32 bits for "time_low" 101 | mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), 102 | 103 | // 16 bits for "time_mid" 104 | mt_rand( 0, 0xffff ), 105 | 106 | // 16 bits for "time_hi_and_version", 107 | // four most significant bits holds version number 4 108 | mt_rand( 0, 0x0fff ) | 0x4000, 109 | 110 | // 16 bits, 8 bits for "clk_seq_hi_res", 111 | // 8 bits for "clk_seq_low", 112 | // two most significant bits holds zero and one for variant DCE1.1 113 | mt_rand( 0, 0x3fff ) | 0x8000, 114 | 115 | // 48 bits for "node" 116 | mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) 117 | ); 118 | } 119 | 120 | -------------------------------------------------------------------------------- /remittance.php: -------------------------------------------------------------------------------- 1 | access_token; 34 | // The above $access_token expires after an hour, find a way to cache it to minimize requests to the server 35 | if(!$access_token){ 36 | throw new Exception("Invalid access token generated"); 37 | return FALSE; 38 | } 39 | return $access_token; 40 | 41 | } 42 | 43 | // request payment from customer 44 | function disburse(){ 45 | 46 | $access_token = get_accesstoken(); 47 | $endpoint_url = 'https://sandbox.momodeveloper.mtn.com/remittance/v1_0/transfer'; 48 | 49 | # Parameters 50 | $data = array( 51 | "amount" => "3000", 52 | "currency" => "EUR", //default for sandbox 53 | "externalId" => "123456", //reference number 54 | 55 | "payee" => array( 56 | "partyIdType" => "MSISDN", 57 | "partyId" => "46733123453" //user phone number, these are test numbers) 58 | ), 59 | 60 | "payerMessage"=> "International Funds Transfer", 61 | "payeeNote"=> "We have transfered funds" 62 | 63 | 64 | ); 65 | 66 | $data_string = json_encode($data); 67 | 68 | $curl = curl_init(); 69 | curl_setopt($curl, CURLOPT_URL, $endpoint_url); 70 | curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST"); 71 | //curl_setopt($curl, CURLOPT_TIMEOUT, 50); 72 | curl_setopt($curl, CURLOPT_POSTFIELDS, $data_string); 73 | 74 | curl_setopt( 75 | $curl, 76 | CURLOPT_HTTPHEADER, 77 | array( 78 | 'Content-Type: application/json', //optional 79 | 'Authorization: Bearer '.$access_token, //optional 80 | //'X-Callback-Url: https://anzilasandbox.ngrok.io', //optional, not required for sandbox 81 | 'X-Reference-Id: '.get_uuid(), 82 | 'X-Target-Environment: sandbox', 83 | 'Ocp-Apim-Subscription-Key: '.REMITTANCE_SUBSCRIPTION_KEY, 84 | 85 | ) 86 | ); 87 | 88 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 89 | 90 | $curl_response = curl_exec($curl); //will respond with HTTP 202 Accepted 91 | // close curl resource to free up system resources 92 | curl_close($curl); 93 | } 94 | 95 | 96 | function get_uuid() { 97 | return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', 98 | // 32 bits for "time_low" 99 | mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), 100 | 101 | // 16 bits for "time_mid" 102 | mt_rand( 0, 0xffff ), 103 | 104 | // 16 bits for "time_hi_and_version", 105 | // four most significant bits holds version number 4 106 | mt_rand( 0, 0x0fff ) | 0x4000, 107 | 108 | // 16 bits, 8 bits for "clk_seq_hi_res", 109 | // 8 bits for "clk_seq_low", 110 | // two most significant bits holds zero and one for variant DCE1.1 111 | mt_rand( 0, 0x3fff ) | 0x8000, 112 | 113 | // 48 bits for "node" 114 | mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ) 115 | ); 116 | } --------------------------------------------------------------------------------