From 892a33c6190a43a2406e97025a6a6bd1bb57f754 Mon Sep 17 00:00:00 2001 From: Danilo Elias Date: Mon, 12 Jul 2021 14:42:33 -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 00a89632..d5ec7f60 100755 --- a/src/MercadoPago/Entities/Card.php +++ b/src/MercadoPago/Entities/Card.php @@ -29,9 +29,16 @@ class Card extends Entity */ protected $id; + /** + * token + * @Attribute() + * @var string + */ + protected $token; + /** * customer_id - * @Attribute(required = true) + * @Attribute(required = true, serialize = false) * @var string */ protected $customer_id; From f71a8dbdcfd986298c4ecdbb49d208bbe1e6226c Mon Sep 17 00:00:00 2001 From: Danilo Elias Date: Mon, 12 Jul 2021 17:43:50 -0300 Subject: [PATCH 2/3] Add search method to Preference --- src/MercadoPago/Entities/Preference.php | 1 + src/MercadoPago/Entity.php | 24 ++++++---- .../Generic/SearchResultsArray.php | 46 ++++++++++++++----- 3 files changed, 50 insertions(+), 21 deletions(-) diff --git a/src/MercadoPago/Entities/Preference.php b/src/MercadoPago/Entities/Preference.php index f05d170e..09d949e6 100755 --- a/src/MercadoPago/Entities/Preference.php +++ b/src/MercadoPago/Entities/Preference.php @@ -12,6 +12,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..b4f69a23 100644 --- a/src/MercadoPago/Generic/SearchResultsArray.php +++ b/src/MercadoPago/Generic/SearchResultsArray.php @@ -24,25 +24,36 @@ public function setPaginateParams($params){ } public function next() { - $new_offset = $this->limit + $this->offset; - echo "\n new offset" . $new_offset ; - - $this->_filters['offset'] = $new_offset; - + $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 +77,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 +?> From 97962eb6ae2119d348122ae1ce46722d7d8ca75a Mon Sep 17 00:00:00 2001 From: Danilo Elias Date: Mon, 12 Jul 2021 17:48:36 -0300 Subject: [PATCH 3/3] Increase version to 2.4.0 --- README.md | 2 +- composer.json | 2 +- docs/classes/MercadoPago-Version.html | 2 +- src/MercadoPago/Version.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index added062..831c88b0 100755 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ First time using Mercado Pago? Create your [Mercado Pago account](https://www.me 1. Download [Composer](https://getcomposer.org/doc/00-intro.md) if not already installed 2. On your project directory run on the command line -`composer require "mercadopago/dx-php:2.3.0"` for PHP7 or `composer require "mercadopago/dx-php:1.11.0"` for PHP5.6. +`composer require "mercadopago/dx-php:2.4.0"` for PHP7 or `composer require "mercadopago/dx-php:1.12.0"` for PHP5.6. 3. Copy the access_token in the [credentials](https://www.mercadopago.com/mlb/account/credentials) section of the page and replace YOUR_ACCESS_TOKEN with it. diff --git a/composer.json b/composer.json index 2a99678e..7b630e11 100755 --- a/composer.json +++ b/composer.json @@ -4,7 +4,7 @@ "type": "library", "homepage": "https://github.com/mercadopago/sdk-php", "license": "MIT", - "version": "2.3.0", + "version": "2.4.0", "config": { "platform": { "php": "7.1" diff --git a/docs/classes/MercadoPago-Version.html b/docs/classes/MercadoPago-Version.html index 1ea8c61a..53c054b1 100644 --- a/docs/classes/MercadoPago-Version.html +++ b/docs/classes/MercadoPago-Version.html @@ -168,7 +168,7 @@

public static mixed $_VERSION - = '2.3.0' + = '2.4.0' diff --git a/src/MercadoPago/Version.php b/src/MercadoPago/Version.php index ab4c5764..015accbf 100755 --- a/src/MercadoPago/Version.php +++ b/src/MercadoPago/Version.php @@ -4,5 +4,5 @@ class Version { public static - $_VERSION = '2.3.0'; + $_VERSION = '2.4.0'; }