diff --git a/src/MercadoPago/Entity.php b/src/MercadoPago/Entity.php index a6b12f06..b99f3e89 100755 --- a/src/MercadoPago/Entity.php +++ b/src/MercadoPago/Entity.php @@ -77,17 +77,16 @@ public static function getCustomHeaders() * @return mixed */ public static function read($params = []) - { - - + { $class = get_called_class(); $entity = new $class(); self::$_manager->setEntityUrl($entity, 'read', $params); - self::$_manager->setQueryParams($entity, $params); + self::$_manager->setQueryParams($entity, $params); $response = self::$_manager->execute($entity, 'get'); + if ($response['code'] == "200" || $response['code'] == "201") { $entity->_fillFromArray($entity, $response['body']); @@ -96,6 +95,7 @@ public static function read($params = []) $entity->_last = clone $entity; return $entity; } + /** * @return mixed */ @@ -103,16 +103,27 @@ public static function search($filters) { $class = get_called_class(); - $entity = new $class(); + $entities = array(); + $entityToQuery = new $class(); - self::$_manager->setEntityUrl($entity, 'search'); - self::$_manager->setQueryParams($entity, $filters); - $response = self::$_manager->execute($entity, 'get'); + self::$_manager->setEntityUrl($entityToQuery, 'search'); + self::$_manager->setQueryParams($entityToQuery, $filters); + + $response = self::$_manager->execute($entityToQuery, 'get'); if ($response['code'] == "200" || $response['code'] == "201") { - $entity->_fillFromArray($entity, $response['body']['results'][0]); - } - return $entity; + $results = $response['body']['results']; + + foreach ($results as $result) { + + $entity = new $class(); + $entity->_fillFromArray($entity, $result); + array_push($entities, $entity); + } + + } + return $entities; + } /** * @codeCoverageIgnore diff --git a/src/MercadoPago/Manager.php b/src/MercadoPago/Manager.php index a2b389ee..70edb0e5 100755 --- a/src/MercadoPago/Manager.php +++ b/src/MercadoPago/Manager.php @@ -78,17 +78,19 @@ public function addCustomTrackingParam($key, $value) public function execute($entity, $method = 'get') { $configuration = $this->_getEntityConfiguration($entity); + if ($method != 'get'){ foreach ($configuration->attributes as $key => $attribute) { $this->validateAttribute($entity, $key, ['required']); - } + } } $this->_setDefaultHeaders($configuration->query); $this->_setCustomHeaders($entity, $configuration->query); $this->_setIdempotencyHeader($configuration->query, $configuration, $method); $this->setQueryParams($entity); - + + return $this->_client->{$method}($configuration->url, $configuration->query); } @@ -124,11 +126,15 @@ protected function _isValidProperty($key, $property, $entity, $attribute, $value */ public function setEntityUrl($entity, $ormMethod, $params = []) { + + + $className = $this->_getEntityClassName($entity); if (!isset($this->_entityConfiguration[$className]->methods[$ormMethod])) { throw new \Exception('ORM method ' . $ormMethod . ' not available for entity:' . $className); } $url = $this->_entityConfiguration[$className]->methods[$ormMethod]['resource']; + $matches = []; preg_match_all('/\\:\\w+/', $url, $matches); diff --git a/src/MercadoPago/RestClient.php b/src/MercadoPago/RestClient.php index 75d1ce24..033bdbde 100755 --- a/src/MercadoPago/RestClient.php +++ b/src/MercadoPago/RestClient.php @@ -104,7 +104,8 @@ public function getHttpRequest() * @throws Exception */ protected function exec($options) - { + { + $method = key($options); $requestPath = reset($options); $verb = self::$verbArray[$method]; @@ -113,6 +114,9 @@ protected function exec($options) $url_query = $this->getArrayValue($options, 'url_query'); $formData = $this->getArrayValue($options, 'form_data'); $jsonData = $this->getArrayValue($options, 'json_data'); + + + $defaultHttpParams = self::$defaultParams; $connectionParams = array_merge($defaultHttpParams, $this->customParams); $query = ''; @@ -163,7 +167,7 @@ protected function exec($options) if ($jsonData) { $this->setData($connect, $jsonData, "application/json"); } - + $apiResult = $connect->execute(); $apiHttpCode = $connect->getInfo(CURLINFO_HTTP_CODE); if ($apiResult === false) { diff --git a/src/MercadoPago/SDK.php b/src/MercadoPago/SDK.php index a3cb297c..af17718e 100755 --- a/src/MercadoPago/SDK.php +++ b/src/MercadoPago/SDK.php @@ -45,6 +45,10 @@ public static function setAccessToken($access_token){ self::$_config->configure(['ACCESS_TOKEN' => $access_token]); } + + public static function getAccessToken(){ + self::$_config->get('ACCESS_TOKEN'); + } /** * Set Access ClientId for SDK . @@ -55,6 +59,10 @@ public static function setClientId($client_id){ } self::$_config->configure(['CLIENT_ID' => $client_id]); } + + public static function getClientId(){ + self::$_config->get('CLIENT_ID'); + } /** * Set Access ClientSecret for SDK . @@ -65,6 +73,21 @@ public static function setClientSecret($client_secret){ } self::$_config->configure(['CLIENT_SECRET' => $client_secret]); } + + public static function getClientSecret(){ + self::$_config->get('CLIENT_SECRET'); + } + + /** + * Set Access ClientSecret for SDK . + */ + public static function setPublicKey($public_key){ + self::$_config->configure(['PUBLIC_KEY' => $public_key]); + } + + public static function getPublicKey(){ + self::$_config->get('PUBLIC_KEY'); + } public static function configure($data=[]) { @@ -92,7 +115,7 @@ public static function get($uri, $options=[]) { if ($token = self::$_config->get('ACCESS_TOKEN')) { $uri = $uri . "?access_token=" . $token; - } + } return self::$_restClient->get($uri, $options); } diff --git a/tests/resources/PaymentTest.php b/tests/resources/PaymentTest.php new file mode 100644 index 00000000..a84cc681 --- /dev/null +++ b/tests/resources/PaymentTest.php @@ -0,0 +1,115 @@ +transaction_amount = 141; + $payment->token = $this->SingleUseCardToken('approved'); + $payment->description = "Ergonomic Silk Shirt"; + $payment->installments = 1; + $payment->payment_method_id = "visa"; + $payment->payer = array( + "email" => "larue.nienow@hotmail.com" + ); + $payment->external_reference = uniqid(); + + $payment->save(); + + self::$last_payment = $payment; + + + + $this->assertEquals($payment->status, 'approved'); + + } + + public function testFindPaymentById() { + + + $payment = MercadoPago\Payment::find_by_id(self::$last_payment->id); + + $this->assertEquals($payment->id, self::$last_payment->id); + } + + public function testPaymentsSearch() { + + $filters = array( + "external_reference" => self::$last_payment->$external_reference + ); + + $payments = MercadoPago\Payment::search($filters); + + $payment = end($payments); + + $this->assertTrue(count($payments) > 0); + $this->assertEquals($payment->external_reference, self::$last_payment->external_reference); + + } + + + private function SingleUseCardToken($status){ + + $cards_name_for_status = array( + "approved" => "APRO", + "pending" => "CONT", + "call_for_auth" => "CALL", + "not_founds" => "FUND", + "expirated" => "EXPI", + "form_error" => "FORM", + "general_error" => "OTHE", + ); + + $i_current_month = intval(date('m')); + $i_current_year = intval(date('Y')); + + $security_code = rand(111, 999); + $expiration_month = rand($i_current_month, 12); + $expiration_year = rand($i_current_year + 2, 2999); + $dni = rand(11111111, 99999999); + + $payload = array( + "json_data" => array( + "card_number" => "4509953566233704", + "security_code" => (string)$security_code, + "expiration_month" => str_pad($expiration_month, 2, '0', STR_PAD_LEFT), + "expiration_year" => str_pad($expiration_year, 4, '0', STR_PAD_LEFT), + "cardholder" => array( + "name" => $cards_name_for_status[$status], + "identification" => array( + "type" => "DNI", + "number" => (string)$dni + ) + ) + ) + ); + + $response = MercadoPago\SDK::post('/v1/card_tokens', $payload); + + return $response['body']['id']; + + } + + +} + +?> \ No newline at end of file diff --git a/tests/resources/PreferenceTest.php b/tests/resources/PreferenceTest.php index db6aff5d..2696d751 100644 --- a/tests/resources/PreferenceTest.php +++ b/tests/resources/PreferenceTest.php @@ -7,16 +7,18 @@ class PreferenceTest extends \PHPUnit\Framework\TestCase { - private $last_preference = null; + private static $last_preference; - protected function setUp() + public static function setUpBeforeClass() { MercadoPago\SDK::setClientId($_ENV['CLIENT_ID']); - MercadoPago\SDK::setClientSecret($_ENV['CLIENT_SECRET']); + MercadoPago\SDK::setClientSecret($_ENV['CLIENT_SECRET']); + } public function testCreatePrefence() { + $preference = new MercadoPago\Preference(); # Building an item @@ -27,17 +29,16 @@ public function testCreatePrefence() $item->unit_price = 100; $preference->items = array($item); - $preference->save(); - $last_preference = $preference; - + self::$last_preference = $preference; + $this->assertTrue($preference->sandbox_init_point != null); } - public function testFindPreferenceById(){ - $preference = MercadoPago\Preference::find_by_id($this->$last_preference->id); - $this->assertEquals($preference->id, $this->$last_preference->id); + public function testFindPreferenceById(){ + $preference = MercadoPago\Preference::find_by_id(self::$last_preference->id); + $this->assertEquals($preference->id, self::$last_preference->id); } } ?> \ No newline at end of file