Skip to content

Commit

Permalink
Merge pull request #89 from Shoperti/mercadopago-basic-sandbox
Browse files Browse the repository at this point in the history
Mercadopago basic sandbox
  • Loading branch information
joecohens committed Oct 3, 2017
2 parents 56b6012 + 2ddfcb9 commit d70d8fb
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 19 deletions.
2 changes: 2 additions & 0 deletions src/Gateways/MercadoPago/MercadoPagoGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,12 @@ protected function getStatus(array $response)
case 'charged_back':
return new Status($status);
case 'approved':
case 'closed':
return new Status('paid');
case 'pending':
case 'in_process':
case 'in_mediation':
case 'open':
return new Status('pending');
case 'cancelled':
return new Status('canceled');
Expand Down
12 changes: 10 additions & 2 deletions src/Gateways/MercadoPagoBasic/Charges.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,17 @@ public function complete($options = [])
{
$id = Arr::get($options, 'collection_id');

$version = $this->gateway->getConfig()['version'];
$response = $this->gateway->commit('get', $this->gateway->buildUrlFromString('collections/notifications').'/'.$id);

return $this->gateway->commit('get', $this->gateway->buildUrlFromString($version.'/payments').'/'.$id);
if (!$response->success()) {
return $response;
}

$responseData = $response->data();

$orderId = Arr::get($responseData, 'merchant_order_id');

return $this->gateway->commit('get', $this->gateway->buildUrlFromString('merchant_orders').'/'.$orderId);
}

/**
Expand Down
17 changes: 11 additions & 6 deletions src/Gateways/MercadoPagoBasic/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,20 @@ public function all()
*/
public function find($id, array $options = [])
{
$type = Arr::get($options, 'type');
$topic = Arr::get($options, 'topic');

if ($topic == 'merchant_order') {
return $this->gateway->commit('get', $this->gateway->buildUrlFromString('merchant_orders').'/'.$id);
}
if ($topic == 'payment') {
$response = $this->gateway->commit('get', $this->gateway->buildUrlFromString('collections/notifications').'/'.$id);

if (!$response->success()) {
return $response;
}

$version = $this->gateway->getConfig()['version'];
$responseData = $response->data();

$id = Arr::get($responseData, 'merchant_order_id');
}

return $this->gateway->commit('get', $this->gateway->buildUrlFromString($version.'/payments').'/'.$id);
return $this->gateway->commit('get', $this->gateway->buildUrlFromString('merchant_orders').'/'.$id, [], $options);
}
}
45 changes: 35 additions & 10 deletions src/Gateways/MercadoPagoBasic/MercadoPagoBasicGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ class MercadoPagoBasicGateway extends MercadoPagoGateway
*/
protected $apiVersion = 'v1';

/**
* MercadoPagoBasic OAuth token.
*
* @var null|string
*/
protected $oauthToken = null;

/**
* Inject the configuration for a Gateway.
*
Expand Down Expand Up @@ -103,15 +110,20 @@ public function commit($method, $url, $params = [], $options = [])
],
]);

$authResponse = $this->parseResponse($authRawResponse);
if (!$this->oauthToken) {
$authResponse = $this->parseResponse($authRawResponse);

$this->oauthToken = Arr::get($authResponse, 'access_token');
}

$authUrl = sprintf('%s?access_token=%s', $url, Arr::get($authResponse, 'access_token', ''));
$authUrl = sprintf('%s?access_token=%s', $url, $this->oauthToken);

$rawResponse = $this->getHttpClient()->{$method}($authUrl, $request);

$response = $this->parseResponse($rawResponse);

$response['isRedirect'] = Arr::get($options, 'isRedirect', false);
$response['topic'] = Arr::get($options, 'topic');

return $this->respond($response);
}
Expand All @@ -126,29 +138,42 @@ public function commit($method, $url, $params = [], $options = [])
*/
public function mapResponse($success, $response)
{
if (array_key_exists('collection', $response)) {
$response = array_merge($response, $response['collection']);

unset($response['collection']);
}

$rawResponse = $response;

unset($rawResponse['isRedirect']);
unset($rawResponse['topic']);

if ($response['isRedirect']) {
$authorization = $success
? $this->config['test'] ? $response['sandbox_init_point'] : $response['init_point']
: null;

return (new Response())->setRaw($rawResponse)->map([
'isRedirect' => $response['isRedirect'],
'success' => $response['isRedirect'] ? false : $success,
'reference' => $success ? $response['id'] : false,
'reference' => $success ? Arr::get($response, 'id') : null,
'message' => $success ? 'Transaction approved' : 'Redirect',
'test' => $this->config['test'],
'authorization' => $authorization,
'authorization' => $success ? Arr::get($response, 'init_point') : null,
'status' => $success ? new Status('pending') : new Status('failed'),
'errorCode' => $success ? null : $this->getErrorCode($response),
'type' => false,
'type' => null,
]);
}

return parent::mapResponse($success, $response);
return (new Response())->setRaw($rawResponse)->map([
'isRedirect' => false,
'success' => $success,
'reference' => $success ? Arr::get($response, 'id') : null,
'message' => $success ? 'Transaction approved' : null,
'test' => $this->config['test'],
'authorization' => $success ? Arr::get($response, 'preference_id') : null,
'status' => $success ? $this->getStatus($response) : new Status('failed'),
'errorCode' => $success ? null : $this->getErrorCode($response),
'type' => Arr::get($response, 'topic'),
]);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Functional/MercadoPagoBasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function it_should_succeed_to_create_a_charge()

$this->assertFalse($charge->success());
$this->assertTrue($charge->isRedirect());
$this->assertContains('https://sandbox.mercadopago.com/mlm/checkout/pay', $charge->authorization());
$this->assertContains('https://www.mercadopago.com/mlm/checkout/start', $charge->authorization());
}

/** @test */
Expand Down

0 comments on commit d70d8fb

Please sign in to comment.