sdk-php
sdk-php copied to clipboard
Verify if response error has required values
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.
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?
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?
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() );
}
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?
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']);
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?
Hello @xavierlesa!
We are currently waiting @ingfdoaguirre to update this branch with master
and to include the recommendation from @rodrigopeixotobr.
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.
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.