Skip to content

Commit

Permalink
changes for omnipay v3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Macedo committed May 17, 2019
1 parent dbe8859 commit c95c315
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 34 deletions.
2 changes: 0 additions & 2 deletions src/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,10 @@ public function purchase(array $parameters = array())
{
return $this->createRequest('\Omnipay\MercadoPago\Message\PurchaseRequest', $parameters);
}

public function requestToken(array $parameters = array())
{
return $this->createRequest('\Omnipay\MercadoPago\Message\TokenRequest', $parameters);
}

/**
* @param array $parameters
* @return \Omnipay\MercadoPago\Message\CompletePurchaseRequest
Expand Down
3 changes: 3 additions & 0 deletions src/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@ public function getCategoryId()
{
return $this->getParameter('category_id');
}

public function setCategoryId($value)
{
return $this->setParameter('category_id', $value);
}

public function getCurrencyId()
{
return $this->getParameter('currency_id');
}

public function setCurrencyId($value)
{
return $this->setParameter('currency_id', $value);
Expand Down
27 changes: 24 additions & 3 deletions src/Message/AbstractRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ public function getData()
public function sendData($data)
{
$url = $this->getEndpoint() . '?access_token=' . $this->getAccessToken();
$httpRequest = $this->httpClient->createRequest(
$httpRequest = $this->httpClient->request(
'POST',
$url,
array(
'Content-type' => 'application/json',
),
$this->toJSON($data)
);
$httpResponse = $httpRequest->send();
return $this->createResponse($httpResponse->json());

return $this->createResponse(json_decode($httpRequest->getBody()->getContents()));
}

public function setExternalReference($value)
Expand All @@ -48,6 +48,27 @@ public function getAccessToken()
return $this->getParameter('access_token');
}

/**
* Get Customer Data
*
* @return array customer data
*/
public function getCustomer()
{
return $this->getParameter('customer');
}

/**
* Set Customer data
*
* @param array $value
* @return AbstractRequest provides a fluent interface.
*/
public function setCustomer($value)
{
return $this->setParameter('customer', $value);
}

protected function getEndpoint()
{
return $this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint;
Expand Down
41 changes: 23 additions & 18 deletions src/Message/PurchaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,57 +4,62 @@

class PurchaseRequest extends AbstractRequest
{

public function getItemData()
{
$data = [];
$items = $this->getItems();

if ($items) {
foreach ($items as $n => $item) {

$item_array = [];
$item_array['title'] = $item->getName();
$item_array['category_id'] = $item->getCategoryId();
$item_array['quantity'] = $item->getQuantity();
$item_array['currency_id'] = $item->getCurrencyId();
$item_array['description'] = $item->getDescription();
// $item_array['category_id'] = $item->getCategoryId();
$item_array['quantity'] = (int)$item->getQuantity();
$item_array['currency_id'] = $this->getCurrency();
$item_array['unit_price'] = (double)($this->formatCurrency($item->getPrice()));

array_push($data, $item_array);
}
}

return $data;
}

public function getData()
{

$data = array(
"items" => array(
array(
'title' => 'PurchaseTest',
'quantity' => 1,
'category_id' => 'tickets',
'currency_id' => 'BRL',
'unit_price' => 10.0
)
));
"items" => array(
array(
'title' => 'PurchaseTest',
'quantity' => 1,
'category_id' => 'tickets',
'currency_id' => 'BRL',
'unit_price' => 10.0
)
));

$items = $this->getItemData();
$external_reference = parent::getData();
$purchaseObject = [
'items' => $items,
$purchaseObject = [
'items' => $items,
'external_reference' => $external_reference,
'auto_return' => 'approved',
'back_urls' => [
'success' => $this->getReturnUrl()
'success' => $this->getReturnUrl()
],

//TODO add option for that
'payment_methods' => [
'payment_methods' => [
'excluded_payment_types' => [
["id" => "ticket"],
["id" => "atm"]
]
]
];
return $purchaseObject;

}

protected function createResponse($data)
Expand Down
8 changes: 4 additions & 4 deletions src/Message/PurchaseResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Omnipay\Common\Message\RedirectResponseInterface;

class PurchaseResponse extends AbstractResponse implements RedirectResponseInterface
class PurchaseResponse extends AbstractResponse implements RedirectResponseInterface
{
public function isSuccessful()
{
Expand All @@ -19,15 +19,15 @@ public function isSuccessful()
*/
public function isRedirect()
{
return isset($this->data['init_point']) && $this->data['init_point'];
return isset($this->data->init_point) && $this->data->init_point;
}


public function getRedirectMethod()
{
return 'GET';
}

public function getRedirectData()
{
return null;
Expand All @@ -36,7 +36,7 @@ public function getRedirectData()
public function getRedirectUrl()
{
if ($this->isRedirect()) {
return $this->data['init_point'];
return $this->data->init_point;
}
}
}
Expand Down
13 changes: 7 additions & 6 deletions src/Message/TokenRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@ public function getData()
{
$this->setGrantType("client_credentials");
return [
'grant_type' => $this->getGrantType(),
'client_id' => $this->getClientId(),
'client_secret' => $this->getClientSecret()
'client_id' => $this->getClientId(),
'client_secret' => $this->getClientSecret(),
'grant_type' => $this->getGrantType(),
];
}

public function sendData($data)
{
$url = $this->getEndpoint();
$headers = [
'headers' => ['Content-Type' => 'x-www-form-urlencoded; charset=UTF-8', 'Accept' => 'application/json']
'Content-Type' => 'application/x-www-form-urlencoded'
];
$httpResponse = $this->httpClient->post($url, $headers, $data)->send();
return $this->createResponse($httpResponse->json());
$httpResponse = $this->httpClient->request('POST', $url, $headers, http_build_query($data, '', '&'));

return ($this->createResponse(json_decode($httpResponse->getBody()->getContents())));
}

protected function getEndpoint()
Expand Down
8 changes: 7 additions & 1 deletion src/Message/TokenResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ class TokenResponse extends AbstractResponse
{
public function isSuccessful()
{
return isset($this->data['status']) && in_array($this->data['status'], array('Success', 'SuccessWithWarning'));
return isset($this->data->access_token);
}

public function getToken()
{
return $this->data->access_token;
}

}

?>

0 comments on commit c95c315

Please sign in to comment.