Replies: 7 comments 5 replies
-
Hola, @jojanny |
Beta Was this translation helpful? Give feedback.
-
Hola! Estoy teniendo el mismo error al implementar Checkout Pro en php 8.2 con versión del sdk 3.0.5, esto me ocurre al intentar ejecutar el ejemplo que viene en la documentación: A PHP Error was encountered Message Filename Line Number |
Beta Was this translation helpful? Give feedback.
-
El error viene de esta linea Información relacionada para solucionarlo: |
Beta Was this translation helpful? Give feedback.
-
Having the same issue here on PHP 8.3 |
Beta Was this translation helpful? Give feedback.
-
For those looking for a quick solution while this is not officially fixed, I created a version of that class on my side and I'm using the response array directly: <?php
namespace app\components;
use MercadoPago\Client\Common\RequestOptions;
use MercadoPago\Client\MercadoPagoClient;
use MercadoPago\MercadoPagoConfig;
use MercadoPago\Net\HttpMethod;
use MercadoPago\Net\MPHttpClient;
use MercadoPago\Net\MPSearchRequest;
use MercadoPago\Resources\Preference;
use MercadoPago\Resources\PreferenceSearch;
use MercadoPago\Serialization\Serializer;
/** Client responsible for performing preference actions. */
final class MercadoPagoCustomPreferenceClient extends MercadoPagoClient
{
private const URL = "/checkout/preferences";
private const URL_WITH_ID = "/checkout/preferences/%s";
private const URL_SEARCH = "/checkout/preferences/search";
/** Default constructor. Uses the default http client used by the SDK or custom http client provided. */
public function __construct(?MPHttpClient $MPHttpClient = null)
{
parent::__construct($MPHttpClient ?: MercadoPagoConfig::getHttpClient());
}
/**
* Method responsible for creating preference.
* @param array $request preference data.
* @param \MercadoPago\Client\Common\RequestOptions request options to be sent.
* @return \MercadoPago\Resources\Preference preference created.
* @throws \MercadoPago\Exceptions\MPApiException if the request fails.
* @throws \Exception if the request fails.
*/
public function create(array $request, ?RequestOptions $request_options = null): array
{
$response = parent::send(self::URL, HttpMethod::POST, json_encode($request), null, $request_options);
// I made it return the payload as an array instead of deserializing
// $result = Serializer::deserializeFromJson(Preference::class, $response->getContent());
// $result->setResponse($response);
return $response->getContent();
}
/**
* Method responsible for getting preference.
* @param string $id preference ID.
* @param \MercadoPago\Client\Common\RequestOptions request options to be sent.
* @return \MercadoPago\Resources\Preference preference found.
* @throws \MercadoPago\Exceptions\MPApiException if the request fails.
* @throws \Exception if the request fails.
*/
public function get(string $id, ?RequestOptions $request_options = null): Preference
{
$response = parent::send(sprintf(self::URL_WITH_ID, $id), HttpMethod::GET, null, null, $request_options);
$result = Serializer::deserializeFromJson(Preference::class, $response->getContent());
$result->setResponse($response);
return $result;
}
/**
* Method responsible for update preference.
* @param string $id preference ID.
* @param array $request preference data.
* @param \MercadoPago\Client\Common\RequestOptions request options to be sent.
* @return \MercadoPago\Resources\Preference preference canceled.
* @throws \MercadoPago\Exceptions\MPApiException if the request fails.
* @throws \Exception if the request fails.
*/
public function update(string $id, array $request, ?RequestOptions $request_options = null): Preference
{
$response = parent::send(sprintf(self::URL_WITH_ID, $id), HttpMethod::PUT, json_encode($request), null, $request_options);
$result = Serializer::deserializeFromJson(Preference::class, $response->getContent());
$result->setResponse($response);
return $result;
}
/**
* Method responsible for search preferences.
* @param \MercadoPago\Net\MPSearchRequest $request search request.
* @param \MercadoPago\Client\Common\RequestOptions request options to be sent.
* @return \MercadoPago\Resources\PreferenceSearch search results.
* @throws \MercadoPago\Exceptions\MPApiException if the request fails.
* @throws \Exception if the request fails.
*/
public function search(MPSearchRequest $request, ?RequestOptions $request_options = null): PreferenceSearch
{
$query_params = isset($request) ? $request->getParameters() : null;
$response = parent::send(self::URL_SEARCH, HttpMethod::GET, null, $query_params, $request_options);
$result = Serializer::deserializeFromJson(PreferenceSearch::class, $response->getContent());
$result->setResponse($response);
return $result;
}
} |
Beta Was this translation helpful? Give feedback.
-
De acordo com minha pesquisa, a partir do PHP 8.2 ao criar propriedades dinamicamente gera esse erro.
Solução: Usar anotação, --> mercadopago\dx-php\src\MercadoPago\Resources\Preference.php
|
Beta Was this translation helpful? Give feedback.
-
Eu adicionei a notação #[\AllowDynamicProperties] na classe de destino mas retornou outro erro. Acabou não funcionando. Aí resolvi mudar o Serializer.php e acabou funcionando para mim. parece que código está atribuindo valores a propriedades que não existem na classe de destino. versão do meu PHP: 8.3.17 if (property_exists($object, $key)) {
$object->{$key} = $value;
} Meu Serializer.php <?php
namespace MercadoPago\Serialization;
use MercadoPago\Net\MPResource;
/** Serializer class, responsible for objects serialization and deserialization. */
class Serializer
{
/**
* Method responsible for deserialize objects.
* @param mixed $entity entity to be deserialized.
* @param mixed $data data to be deserialized.
* @return \MercadoPago\Net\MPResource deserialized object.
*/
public static function deserializeFromJson(mixed $entity, mixed $data): MPResource
{
return self::_deserializeFromJson($entity, $data);
}
private static function _deserializeFromJson(mixed $entity, mixed $data): object|null
{
if (!$data) {
return null;
}
$object = new $entity();
foreach ($data as $key => $value) {
if (!is_null($value) && !empty($value) && is_array($value) && method_exists($object, "map")) {
$class_name = $object->map($key);
if (!is_null($class_name) && class_exists($class_name, true)) {
if (is_array($value) && is_numeric(key($value))) {
$deserialized_values = [];
foreach ($value as $item) {
$deserialized_values[] = self::_deserializeFromJson($class_name, $item);
}
$object->$key = $deserialized_values;
} else {
$object->$key = self::_deserializeFromJson($class_name, $value);
}
}
} else {
// added this
if (property_exists($object, $key)) {
$object->{$key} = $value;
}
}
}
return $object;
}
} |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Creation of dynamic property MercadoPago\Resources\MerchantOrder::$client_id is deprecated
FCPATH/vendor/mercadopago/dx-php/src/MercadoPago/Serialization/Serializer.php at line 44
El error solo pasa con pagos que hago desde la app de "Mercado Pago" y no con los que hago desde "Checkout Pro",
el pago realizado con la app de "Mercado Pago" trae la propiedad client_id entonces la solución es:
Agregar public ?string $client_id; a:
vendor/mercadopago/dx-php/src/MercadoPago/Resources/MerchantOrder.php
Beta Was this translation helpful? Give feedback.
All reactions