kopokopo / k2-connect-php

MIT License
6 stars 26 forks source link

Fatal error: Uncaught Error: Call to undefined method Kopokopo\SDK\K2::initiateIncomingPayment() #25

Closed britisharmy closed 2 years ago

britisharmy commented 2 years ago

I have the following code from the docs

I have installed the php package from composer using

composer require kopokopo/k2-connect-php

and this is the code

<?php
use Kopokopo\SDK\K2;

require 'vendor/autoload.php';

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$options = [
    'clientId' => '', 
    'clientSecret' => '',
    'apiKey' => '',
    'baseUrl' => 'https://api.kopokopo.com'
];

$stk = new K2($options);

$response = $stk->initiateIncomingPayment([
  'paymentChannel' => 'M-PESA STK Push',
  'tillNumber' => '',
  'firstName' => '',
  'lastName' => '',
  'phoneNumber' => '',
  'amount' => 1,
  'currency' => 'KES',
  'email' => '@gmail.com',
  'callbackUrl' => '',
  'accessToken' => '',
]);

if($response['status'] == 'success')
{
    echo "The resource location is:" . json_encode($response['location']);
}

but i keep getting this error

Fatal error: Uncaught Error: Call to undefined method Kopokopo\SDK\K2::initiateIncomingPayment() in /var/www/html/stk.php:21

NicolletNjora commented 2 years ago

@britisharmy you are supposed to initialise the STKService using an initialised K2 instance. Below is a screenshot from the repo's README.

image

britisharmy commented 2 years ago

I have made amends and my code looks like this

<?php
use Kopokopo\SDK\K2;

require 'vendor/autoload.php';

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$options = [
    'clientId' => '', 
    'clientSecret' => '',
    'apiKey' => '',
    'baseUrl' => 'https://api.kopokopo.com'
];

$stk = new K2($options);
$mpesa = $stk->StkService();
$response = $mpesa->initiateIncomingPayment([
  'paymentChannel' => 'M-PESA STK Push',
  'tillNumber' => '',
  'firstName' => '',
  'lastName' => '',
  'phoneNumber' => '',
  'amount' => 100,
  'currency' => 'KES',
  'email' => '',
  'callbackUrl' => '',
  'accessToken' => '',
]);

if($response['status'] == 'success')
{
    echo "The resource location is:" . json_encode($response['location']);
}

However i still cannot trigger stk push on customers phone.

NicolletNjora commented 2 years ago

@britisharmy This is an issue with your implementation and not the SDK itself. You are only echoing if the response is successful so how would you know what error is being returned if you aren't printing it? Also, please note that you should be using the sandbox for development purposes, not production.

You can start by using postman requests so that you can troubleshoot and see where you are going wrong. You can also join discord and get help from the community using the following link: https://discord.gg/gjJyjjWvG9

Kindly note that there is already vast documentation that you are not utilising as there is a whole example app that shows how to use the SDK and 3 other documentations that cover the same. Ensure you read it properly before opening issues so that this platform is left open for actual issues with the SDK.