From 19e499acc996166f7923e2d5ebb517c4a7f814d8 Mon Sep 17 00:00:00 2001 From: Danilo Elias Date: Mon, 12 Jul 2021 18:24:06 -0300 Subject: [PATCH 1/3] Fix customer card creation Add "token" attribute to the Card class. --- src/MercadoPago/Entities/Card.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/MercadoPago/Entities/Card.php b/src/MercadoPago/Entities/Card.php index a5b3c071..dd652cd0 100755 --- a/src/MercadoPago/Entities/Card.php +++ b/src/MercadoPago/Entities/Card.php @@ -20,7 +20,14 @@ class Card extends Entity protected $id; /** - * @Attribute(required = true) + * token + * @Attribute() + * @var string + */ + protected $token; + + /** + * @Attribute(required = true, serialize = false) */ protected $customer_id; /** From b059e7ba9134ae0b7fca0ac0f250f9dd2dccca49 Mon Sep 17 00:00:00 2001 From: Danilo Elias Date: Tue, 13 Jul 2021 17:10:46 -0300 Subject: [PATCH 2/3] Add search method to Preference --- src/MercadoPago/Entities/Preference.php | 1 + src/MercadoPago/Entity.php | 24 +++++---- .../Generic/SearchResultsArray.php | 49 ++++++++++++++----- src/MercadoPago/Manager.php | 15 +++--- 4 files changed, 58 insertions(+), 31 deletions(-) diff --git a/src/MercadoPago/Entities/Preference.php b/src/MercadoPago/Entities/Preference.php index cd2b29e8..0c8188a2 100755 --- a/src/MercadoPago/Entities/Preference.php +++ b/src/MercadoPago/Entities/Preference.php @@ -8,6 +8,7 @@ /** * @RestMethod(resource="/checkout/preferences", method="create") * @RestMethod(resource="/checkout/preferences/:id", method="read") + * @RestMethod(resource="/checkout/preferences/search", method="search") * @RestMethod(resource="/checkout/preferences/:id", method="update") */ class Preference extends Entity diff --git a/src/MercadoPago/Entity.php b/src/MercadoPago/Entity.php index 10567c28..e0dfb10e 100755 --- a/src/MercadoPago/Entity.php +++ b/src/MercadoPago/Entity.php @@ -173,19 +173,12 @@ public static function search($filters = [], $options = []) $response = self::$_manager->execute($entityToQuery, 'get'); if ($response['code'] == "200" || $response['code'] == "201") { - $results = $response['body']['results']; - foreach ($results as $result) { - $entity = new $class(); - $entity->_fillFromArray($entity, $result); - $searchResult->append($entity); - } - $searchResult->setPaginateParams($response['body']['paging']); - $searchResult->_filters = $filters; + $searchResult->fetch($filters, $response['body']); } elseif (intval($response['code']) >= 400 && intval($response['code']) < 500) { $searchResult->process_error_body($response['body']); - throw new Exception ($response['body']['message']); + throw new Exception($response['body']['message']); } else { - throw new Exception ("Internal API Error"); + throw new Exception("Internal API Error"); } return $searchResult; } @@ -458,6 +451,17 @@ protected function tryFormat($value, $type, $property) } throw new \Exception('Wrong type ' . gettype($value) . '. It should be ' . $type . ' for property ' . $property); } + + /** + * Fill entity from data with nested object creation + * + * @param $entity + * @param $data + */ + public function fillFromArray($entity, $data) { + $this->_fillFromArray($entity, $data); + } + /** * Fill entity from data with nested object creation * diff --git a/src/MercadoPago/Generic/SearchResultsArray.php b/src/MercadoPago/Generic/SearchResultsArray.php index 460f5c2c..a2c56962 100644 --- a/src/MercadoPago/Generic/SearchResultsArray.php +++ b/src/MercadoPago/Generic/SearchResultsArray.php @@ -24,25 +24,37 @@ public function setPaginateParams($params){ } public function next() { - $new_offset = $this->limit + $this->offset; - echo "\n new offset" . $new_offset ; + $this->_filters['offset'] = $new_offset; + $class = $this->_class; + $result = $class::search($this->_filters); - $this->_filters['offset'] = $new_offset; - - $result = $this->_class::search($this->_filters); - - - - echo "\nlimit" . $result->limit ; - echo "\nresult offset" . $result->offset ; - $this->limit = $result->limit; $this->offset = $result->offset; $this->total = $result->total; $this->exchangeArray($result->getArrayCopy()); + } + + public function fetch($filters, $body) { + $this->_filters = $filters; + + if ($body) { + $results = []; + if (array_key_exists("results", $body)) { + $results = $body["results"]; + } else if (array_key_exists("elements", $body)) { + $results = $body["elements"]; + } + foreach ($results as $result) { + $entity = new $this->_class(); + $entity->fillFromArray($entity, $result); + $this->append($entity); + } + + $this->fetchPaging($filters, $body); + } } public function process_error_body($message){ @@ -66,6 +78,19 @@ public function process_error_body($message){ $this->errors = $recuperable_error; } + private function fetchPaging($filters, $body) { + if (array_key_exists("paging", $body)) { + $paging = $body["paging"]; + $this->limit = $paging["limit"]; + $this->total = $paging["total"]; + $this->offset = $paging["offset"]; + } else { + $this->offset = array_key_exists("offset", $filters) ? $filters["offset"] : 0; + $this->limit = array_key_exists("limit", $filters) ? $filters["limit"] : 20; + $this->total = array_key_exists("total", $body) ? $body["total"] : 0; + } + } + } -?> \ No newline at end of file +?> diff --git a/src/MercadoPago/Manager.php b/src/MercadoPago/Manager.php index addee125..f4146e15 100755 --- a/src/MercadoPago/Manager.php +++ b/src/MercadoPago/Manager.php @@ -264,23 +264,20 @@ public function cleanQueryParams($entity) public function setQueryParams($entity, $urlParams = []) { $configuration = $this->_getEntityConfiguration($entity); - $params = []; - - if (!isset($configuration->query) || !isset($configuration->query['url_query'])) { - $configuration->query['url_query'] = $params; + $configuration->query['url_query'] = []; } + + $params = []; if (isset($configuration->params)) { foreach ($configuration->params as $value) { $params[$value] = $this->_config->get(strtoupper($value)); } - if (count($params) > 0) { - $arrayMerge = array_merge($urlParams, $params, $configuration->query['url_query']); - $configuration->query['url_query'] = $arrayMerge; - } } - + + $arrayMerge = array_merge($urlParams, $params, $configuration->query['url_query']); + $configuration->query['url_query'] = $arrayMerge; } /** * @param $entity From 0963c3b6ca1a706869889d26ba97ba58196e2e84 Mon Sep 17 00:00:00 2001 From: Danilo Elias Date: Tue, 13 Jul 2021 17:14:49 -0300 Subject: [PATCH 3/3] Increase version to 1.12.0 --- README.md | 2 +- composer.json | 2 +- src/MercadoPago/Version.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 90d1b162..1477dc7f 100755 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ The SDK supports PHP 5.6 or major #### Using Composer 1. Download [Composer](https://getcomposer.org/doc/00-intro.md#installation-linux-unix-macos) if not already installed -2. Go to your project directory and run `composer require "mercadopago/dx-php:1.11.0"` on the command line. +2. Go to your project directory and run `composer require "mercadopago/dx-php:1.12.0"` on the command line. 3. This how your directory structure would look like. 4. Thats all, you have Mercado Pago SDK installed. diff --git a/composer.json b/composer.json index c8d640a4..970b43e9 100755 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "type": "library", "homepage": "https://github.com/mercadopago/sdk-php", "license": "MIT", - "version": "1.11.0", + "version": "1.12.0", "config": { "platform": { "php": "5.6" diff --git a/src/MercadoPago/Version.php b/src/MercadoPago/Version.php index bdb12ec5..8818e852 100755 --- a/src/MercadoPago/Version.php +++ b/src/MercadoPago/Version.php @@ -4,5 +4,5 @@ class Version { public static - $_VERSION = '1.11.0'; + $_VERSION = '1.12.0'; }