sdk-php icon indicating copy to clipboard operation
sdk-php copied to clipboard

Verify if response error has required values

Open ingfdoaguirre opened this issue 2 years ago • 7 comments

If you get an error from response, the function process_error_body will be called.

Sometimes the required params from the response body will not met the required in the RecuperableError class.

The simple solution is to first verify if the required params exist in response body.

ingfdoaguirre avatar Jan 20 '22 20:01 ingfdoaguirre

Hello @ingfdoaguirre! Thank you for your contribution!

Could you please send a sample payload when this error occurs so that we can validate the solution?

lucmantovani avatar Jan 24 '22 13:01 lucmantovani

It also happened to me during a chargeback: Warning: Undefined array key "error" in /var/www/html/vendor/mercadopago/dx-php/src/MercadoPago/Entity.php on line 268

Executed code was:

$payment ....
$refund = new Refund();
$refund->amount = $payment->total_paid_amount;
$refund->metadata = 'test';
$success = $refund->save();

The chargeback was NOT successful:

Array
(
    [status] => 404
    [message] => Not Found
)

By the way, i copied that code from https://github.com/mercadopago/sdk-php/blob/master/samples/payment/minimal/refund.php , how can that code work if you don't set the payment id ref during the refund?

psociety avatar Jan 27 '22 13:01 psociety

Hi, I'm using this code. In development I made a mistake defining the sender id as the collector id (I was trying to create a subscription for myself) and obviously I got an error.

But the response error message, didnt had the "message" property on the returned array from Mercadopago, so a warning was throwed, but I on development in debug mode, and a warning throw a fatal error in this scenario.

So, to avoid this warning in the case that the MP API error response doesnt have all the array properties ("error", "message" and "status"), the addition to the code is to define the inexistent properties required by the class RecuperableError constructor.

My used code

SDK::setClientId($_ENV['MERCADOLIBRE_APP_ID']);
        SDK::setClientSecret($_ENV['MERCADOLIBRE_APP_SECRET']);

            $preapproval = new Preapproval();
            $preapproval->__set('id', 2222222 );//Is an example user, not real.
            $preapproval->__set('payer_email', $user->getMlMail() );
            $preapproval->__set('back_url', $_ENV['MERCADOPAGO_BACK_URL'] );
            $preapproval->__set('reason', $plan['name'] );
            $preapproval->__set( 'external_reference', $plan['reference']);
            $preapproval->__set( 'auto_recurring', [
                "frequency" => $plan['frequency'],
                "frequency_type" => 'months',
                "transaction_amount" => $planAmount,
                "currency_id" => 'MXN',
            ]);

            if( $preapproval->save() ){
                return $preapproval->__get('init_point');
            } else {
                throw new Exception( $preapproval->Error() );
            }

ingfdoaguirre avatar Jan 28 '22 20:01 ingfdoaguirre

Hello @psociety and @ingfdoaguirre!

Thank you for your information! We could validate the errors and will proceed with this solution.

@ingfdoaguirre, could you please update your branch with the new commits from master before we continue?

lucmantovani avatar Feb 01 '22 12:02 lucmantovani

Hi! PR proved to solve the problem, tests were done to receive returns with errors and the process_error_body() worked, returning the error attribute with the instance of the MercadoPago\RecuperableError class.

But another problem was observed, the process_error_body() function is expecting the $message to have the key ['cause'] to process the causes of the error, but the causes of the error came in the key ['cause_error'], so it didn't process the causes.

Return received when sending wrong data from a card:

(
    [message] => User cards bad request
    [code] => bad_request
    [status] => 400
    [cause_error] => Array
        (
            [0] => Array
                (
                    [description] => Invalid parameter 'number' in site_id: MLB
                    [code] => 120
                )
        )
)

So it is necessary to add an extra check to check the key ['cause_error'] to process the causes of the error, in addition to the ['cause'] already existing, example below:

if(isset($message['cause_error']))
    $recuperable_error->proccess_causes($message['cause_error']);

rodrigopeixotobr avatar Feb 07 '22 21:02 rodrigopeixotobr

Hi there!

Could you please let us know if there is a roadmap for this PR and/or a due date for releasing this fix?

xavierlesa avatar Feb 09 '22 17:02 xavierlesa

Hello @xavierlesa!

We are currently waiting @ingfdoaguirre to update this branch with master and to include the recommendation from @rodrigopeixotobr.

lucmantovani avatar Feb 10 '22 15:02 lucmantovani

Since we the author didn't follow up on the requests for over a year, this PR is closed.

@xavierlesa if you still need the changes proposed by this PR, please open a new one with the changes.

romerosilva-meli avatar Sep 04 '23 19:09 romerosilva-meli

Since we the author didn't follow up on the requests for over a year, this PR is closed.

@xavierlesa if you still need the changes proposed by this PR, please open a new one with the changes.

No worries, I finished the project where we were supposed to use it and just implemented my version of the PHP-Client.

xavierlesa avatar Sep 15 '23 00:09 xavierlesa