PHP code example of escarter / openapimomo

1. Go to this page and download the library: Download escarter/openapimomo library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

escarter / openapimomo example snippets


composer 

php artisan vendor:publish --provider="Escarter\Openapimomo\OpenapiMoMoServiceProvider"

  /*
    |--------------------------------------------------------------------------
    | General configuration
    |--------------------------------------------------------------------------
    | This section contains general configurations parameters
    |
    |   environment           - specifies the api environment. -- takes 'sandbox','mtncameroon'
    |   currency              - specifies the currency you are using. -- takes 'XAF','EUR','UGX'
    |   party_id_type         - specifies the party id type. -- takes 'MSISDN'
    |   transaction_id        - specifies the transaction id. 
    | 
    */

    'environment' => 'sandbox',
    'currency' => 'EUR',
    'party_id_type' => 'MSISDN',
    'transaction_id' => Illuminate\Support\Str::uuid(), 


    /*
    |--------------------------------------------------------------------------
    |                        COLLECTION API SECTION
    |--------------------------------------------------------------------------
    */

    /*
    |--------------------------------------------------------------------------
    | collection api user and key for sandbox and production environment
    |--------------------------------------------------------------------------
    | This section contains configuration parameters for collection api
    |
    |   collection_user_id              - collection user for production environment
    |   collection_api_key              - collection api key for production environemt
    |   sandbox_collection_user_id      - collection user for sandbox 
    |   sandbox_collection_api_key      - collection api key for sandbox 
    */

    'collection_user_id' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', //change this to your production collection user -- _apim_subscription_key for sandbox 
    |   sandbox_collection_token_url        - collection token url for sandbox 
    */

    'collection_ocp_apim_sub_key'=> 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', //change this to your collection Ocp-Apim-Subscription-Key -- ction call back url -- 

    /*
    |--------------------------------------------------------------------------
    |                        DISBURSEMENT API SECTION
    |--------------------------------------------------------------------------
    */

    /*
    |--------------------------------------------------------------------------
    | disbursement api user and key for sandbox and production environment
    |--------------------------------------------------------------------------
    | This section contains configuration parameters for disbursement api
    |
    |   disbursement_user_id              - disbursement user for production environment
    |   disbursement_api_key              - disbursement api key for production environemt
    |   sandbox_disbursement_user_id      - disbursement user for sandbox 
    |   sandbox_disbursement_api_key      - disbursement api key for sandbox 
    */

    'disbursement_user_id' => 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx', //change this to your disbursement user -- ocp_apim_subscription_key for sandbox 
    |   sandbox_disbursement_token_url        - disbursement token url for sandbox 
    */

    'disbursement_ocp_apim_sub_key'=> 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', //change this to your disbursement Ocp-Apim-Subscription-Key  -- - 



namespace App\Http\Controllers;

use Escarter\Openapimomo\OpenapiMoMo;


class ProcessPaymentController extends Controller
{
    public function handlePayment(Request $request){
        .....

        $momoapi = new OpenapiMoMo();

        $trans_id = $momoapi->requestPayment('354660098865', '1', 'payer_message', 'payee_notes');

        $init_trans_status = $momoapi->getCollectionTransactionStatus($trans_id);

        $current_trans_status = $init_trans_status['status'];

        /** Note: when a request is made to the requesttopay endpoint its default status on success is 'PENDING' (waiting for user confirmation)
         * so you might want to write some logic that waits for user's confirmation before you proceed or peform this in the background depending on your application logic.
         * below is the sample code i use since i need to confirm payment before proceeding to next step in my application(this has it's drawbacks) :(
         * 
         *    while($current_trans_status == 'PENDING'){
         *          $init_trans_status = $momoapi->getCollectionTransactionStatus($trans_id);
         *          $current_trans_status = $init_trans_status['status'];
         *     }
        */

        if($current_trans_status == "SUCCESSFUL") {
            // persist some data in your application 
            return 'to some view with success message!';
        }else{
            // persist some data in your application
            return 'to some view with error message!';
        }
    }

    }
}



$momoapi = new OpenapiMoMo();
$coll_acc_balance = $momoapi->getCollectionAccountBalance();
dd($coll_acc_balance);




namespace App\Http\Controllers;

use Escarter\Openapimomo\OpenapiMoMo;


class ProcessPayoutController extends Controller
{
    public function handlePayout(Request $request){
        .....

        $momoapi = new OpenapiMoMo();

        $trans_id = $momoapi->disbursementTransfer('354660098865', '1', 'payer_message', 'payee_notes');

        $trans_status = $momoapi->getDisbursementTransactionStatus($trans_id);
        
        if($trans_status == "SUCCESSFUL") {
            // persist some data in your application 
            return 'to some view with success message!';
        }else{
            // persist some data in your application
            return 'to some view with error message!';
        }
    }

    }
}



$momoapi = new OpenapiMoMo();
$disburs_acc_balance = $momoapi->getDisbursementAccountBalance();
dd($disburs_acc_balance);