plivo / plivo-php

Plivo PHP Helper Library
http://www.plivo.com
MIT License
110 stars 117 forks source link

Exception when trying to update phone app_id #201

Closed ryantxr closed 3 years ago

ryantxr commented 3 years ago

I am attempting to update the app_id for a phone number and I am getting an exception in the library code.

        $client = app('plivoApi')->main;
        $number = $this->argument('number');
        try {
            $appId = '344248b1-b193-44eb-af8e-xxxxxxxxxxxx';
            //$appIdDev = 'fea6db84-e4c6-44b8-b803-xxxxxxxxxxxxx';
            $response = $client->numbers->update(
                $number,
                [
                    'alias' => json_encode(['name' => 'Remelo', "production" => 1, 'client' => '']),
                    'app_id' => $appId,
                    'subaccount' => config('plivo.subaccount.auth_id')
                ]
            );

        }
        catch (\Plivo\Exceptions\PlivoRestException $ex) {
            $this->error($ex->getMessage());
        }

If I comment out the app_id, it works.

This is the exception/error

Wrong parameters for Plivo\Exceptions\PlivoResponseException([string $message [, long $code [, Throwable $previous = NULL]]])

  at vendor/plivo/plivo-php/src/Plivo/Exceptions/PlivoResponseException.php:36
     32▕     public function __construct($message = "", $code = 0, Throwable $previous = null, $responseData, $statusCode)
     33▕     {
     34▕         $this->responseData = $responseData;
     35▕         $this->statusCode = $statusCode;
  ➜  36▕         parent::__construct($message, $code, $this->getException($this->getErrorMessage()));
     37▕     }
     38▕ 
     39▕     /**
     40▕      * Retrieves the error message from the response

      +2 vendor frames 
  3   app/Console/Commands/PlivoUpdatePhoneNumber.php:52
      Plivo\Resources\Number\NumberInterface::update("14449994444", ["{"name":"Remelo","production":1,"client":""}", "344248b1-b193-44eb-af8e-dcc3009a2f9e", "SAMGY4MGEWMJVKOWQ0OD"])
buttflattery commented 3 years ago

i get the same when i try to update a phone numbers sub-account using the $client->number->update()

Exception 'Error' with message 'Wrong parameters for Plivo\Exceptions\PlivoResponseException([string $message [, long $code [, Throwable $previous = NULL]]])'

in /home/omeraslam/www/html/projects/FCS/text-calibur/vendor/plivo/plivo-php/src/Plivo/Exceptions/PlivoResponseException.php:36

Stack trace:
#0 /home/omeraslam/www/html/projects/FCS/text-calibur/vendor/plivo/plivo-php/src/Plivo/Exceptions/PlivoResponseException.php(36): Exception->__construct(Array, 0, Object(Plivo\Exceptions\PlivoValidationException))
#1 /home/omeraslam/www/html/projects/FCS/text-calibur/vendor/plivo/plivo-php/src/Plivo/Resources/Number/NumberInterface.php(130): Plivo\Exceptions\PlivoResponseException->__construct(Array, 0, NULL, Array, 400)
#2 /home/omeraslam/www/html/projects/FCS/text-calibur/vendor/tc/sms-providers/src/plivo/Plivo.php(317): Plivo\Resources\Number\NumberInterface->update('16626057518', Array)
#3 /home/omeraslam/www/html/projects/FCS/text-calibur/vendor/tc/sms-providers/src/SMS.php(54): TC\SmsProviders\plivo\Plivo->updatePhoneNumber('16626057518', Array)
#4 /home/omeraslam/www/html/projects/FCS/text-calibur/console/controllers/MovePhoneNumbersController.php(35): TC\SmsProviders\SMS->__call('updatePhoneNumb...', Array)
#5 [internal function]: console\controllers\MovePhoneNumbersController->actionIndex('2', '3', 'plivo')
#6 /home/omeraslam/www/html/projects/FCS/text-calibur/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#7 /home/omeraslam/www/html/projects/FCS/text-calibur/vendor/yiisoft/yii2/base/Controller.php(181): yii\base\InlineAction->runWithParams(Array)
#8 /home/omeraslam/www/html/projects/FCS/text-calibur/vendor/yiisoft/yii2/console/Controller.php(184): yii\base\Controller->runAction('', Array)
#9 /home/omeraslam/www/html/projects/FCS/text-calibur/vendor/yiisoft/yii2/base/Module.php(534): yii\console\Controller->runAction('', Array)
#10 /home/omeraslam/www/html/projects/FCS/text-calibur/vendor/yiisoft/yii2/console/Application.php(181): yii\base\Module->runAction('move-phone-numb...', Array)
#11 /home/omeraslam/www/html/projects/FCS/text-calibur/vendor/yiisoft/yii2/console/Application.php(148): yii\console\Application->runAction('move-phone-numb...', Array)
#12 /home/omeraslam/www/html/projects/FCS/text-calibur/vendor/yiisoft/yii2/base/Application.php(392): yii\console\Application->handleRequest(Object(yii\console\Request))
#13 /home/omeraslam/www/html/projects/FCS/text-calibur/yii(27): yii\base\Application->run()
#14 {main}

Code

 $response = $this->getClient($phone['provider'], $user_id)->updatePhoneNumber(
                    $phone['phone'],
                    [
                        'app_id' => Yii::$app->params['PLIVO_APP_ID'],
                        'subaccount' => $authIdTo,
                    ]
                );

/**
     * @param $phone
     * @param $options
     * @return mixed
     */
    public function updatePhoneNumber($phone, $options)
    {
        return $this->getClient()->numbers->update($phone, $options);
    }

Reason

the PlivoResponseException has the first param of the construct defined as string

public function __construct($message = "", $code = 0, Throwable $previous = null, $responseData, $statusCode)

where as in the NumberInterface has the if statement which checks if there is no error key defined and in the else case it throws the PlivoRespnseException on line 122

} else {
            throw new PlivoResponseException(
                $responseContents['error'],
                0,
                null,
                $response->getContent(),
                $response->getStatusCode()

            );
        }

but the $responseContents has the format returned as

Array
(
    [api_id] => MY_APP_ID
    [error] => Array
        (
            [error] => This Application-id does not belong to this Account.
        )

)

so you are passing an array instead of a string, it should have been $responseContents['error']['error'] as the first parameter of the PlivoResponseException

Plus, the above exception should not be there, as i am using the correct App id and the account if i use Curl command to update the phone number it works correctly with the same set of configurations. Or using postman it works correctly without any exceptions

huzaif-plivo commented 3 years ago

@ryantxr @buttflattery This has been fixed in our latest update, i.e. v4.18.0. Feel free to contact our support team with any questions.