Skip to content

Commit

Permalink
Fixing search method (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Ibaceta committed Jul 10, 2018
1 parent f1f599f commit 7636398
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 25 deletions.
33 changes: 22 additions & 11 deletions src/MercadoPago/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand All @@ -96,23 +95,35 @@ public static function read($params = [])
$entity->_last = clone $entity;
return $entity;
}

/**
* @return mixed
*/
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
Expand Down
10 changes: 8 additions & 2 deletions src/MercadoPago/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 6 additions & 2 deletions src/MercadoPago/RestClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ public function getHttpRequest()
* @throws Exception
*/
protected function exec($options)
{
{

$method = key($options);
$requestPath = reset($options);
$verb = self::$verbArray[$method];
Expand All @@ -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 = '';
Expand Down Expand Up @@ -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) {
Expand Down
25 changes: 24 additions & 1 deletion src/MercadoPago/SDK.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 .
Expand All @@ -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 .
Expand All @@ -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=[])
{
Expand Down Expand Up @@ -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);
}

Expand Down
115 changes: 115 additions & 0 deletions tests/resources/PaymentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<?php



/**
* EntityTest Class Doc Comment
*
* @package MercadoPago
*/
class PaymentTest extends \PHPUnit\Framework\TestCase
{

private static $last_payment;


public static function setUpBeforeClass()
{
MercadoPago\SDK::setAccessToken($_ENV['ACCESS_TOKEN']);
}

public function testCreatePrefence()
{


$payment = new MercadoPago\Payment();
$payment->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" => "[email protected]"
);
$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'];

}


}

?>
19 changes: 10 additions & 9 deletions tests/resources/PreferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
?>

0 comments on commit 7636398

Please sign in to comment.