Question

Failure to intergrate request payment

  • 26 December 2023
  • 1 reply
  • 14 views

<?php

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    // Replace these with your actual API credentials

    $api_key = '';

    $api_secret = '';

 

    // Update the API endpoint

    $api_endpoint = 'https://api.mtn.com/collection';

 

    // Data for the collection

    $data = array(

        // Validate and sanitize the amount input

        'amount' => filter_var($_POST['amount'], FILTER_SANITIZE_NUMBER_INT),

        'currency' => 'UGX',

        // Validate and sanitize the phone number input

        'payer' => filter_var($_POST['phone_number'], FILTER_SANITIZE_FULL_SPECIAL_CHARS),

        // Specify the receiver of the money

        'payee' => '256789123456', // Replace this with your own phone number or another phone number that you want to receive the money

    );

 

    // Headers for the API request

    $headers = array(

        'Authorization: Basic ' . base64_encode($api_key . ':' . $api_secret),

        'Content-Type: application/json',

    );

 

    // Initialize cURL session

    $ch = curl_init($api_endpoint);

 

    // Set cURL options

    curl_setopt($ch, CURLOPT_POST, 1);

    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    // Enable SSL verification

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);

    // Specify the path to the CA certificate file

    curl_setopt($ch, CURLOPT_CAINFO, 'C:\wamp64\www\mtn\cacert.pem');

 

    // Execute cURL session

    $response = curl_exec($ch);

 

    // Check for errors

    if (curl_errno($ch)) {

        echo 'Error: ' . curl_error($ch);

    } else {

        // Process the API response (JSON format)

        $result = json_decode($response, true);

 

        // Check the response status

        if ($result['status'] == 'SUCCESS') {

            // Escape the user input before displaying it

            $amount = htmlspecialchars($data['amount']);

            $payer = htmlspecialchars($data['payer']);

            echo "Collection successful. You have requested $amount UGX from $payer. Transaction ID: " . $result['transaction_id'];

        } else {

            echo 'Collection failed. Reason: ' . $result['reason'];

        }

    }

 

    // Close cURL session

    curl_close($ch);

} else {

    // Display the form for the user to enter the amount and the sender's phone number

    echo '<form method="post" action="tranfer.php">';

    echo '<label for="amount">Amount (UGX):</label>';

    echo '<input type="number" id="amount" name="amount" required>';

    echo '<label for="phone_number">Sender\'s phone number:</label>';

    echo '<input type="tel" id="phone_number" name="phone_number" required>';

    echo '<button type="submit">Request money</button>';

    echo '</form>';

}

?>
here is my code however i keep getting errors with in anyone can guide me on what to do


1 reply

Userlevel 2

You need the MoMo API sandbox documentation, here you simply copied sample code and replaced with basic information.
The URL you are using is wrong, 
You have no API key and API USER, 
the aprty ID is wrong,
the Currency is Wrong.
Go to your  MoMo Developer Portal and endavor reading through, then you can come back and inquire more.
You can also find more information on the home page here in the developer community.
Please, endavor to read through

Reply