diff --git a/src/Gateways/MercadoPago/MercadoPagoGateway.php b/src/Gateways/MercadoPago/MercadoPagoGateway.php index a659d29..3ac490f 100644 --- a/src/Gateways/MercadoPago/MercadoPagoGateway.php +++ b/src/Gateways/MercadoPago/MercadoPagoGateway.php @@ -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'); diff --git a/src/Gateways/MercadoPagoBasic/Charges.php b/src/Gateways/MercadoPagoBasic/Charges.php index 537928f..f083176 100644 --- a/src/Gateways/MercadoPagoBasic/Charges.php +++ b/src/Gateways/MercadoPagoBasic/Charges.php @@ -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); } /** diff --git a/src/Gateways/MercadoPagoBasic/Events.php b/src/Gateways/MercadoPagoBasic/Events.php index 889c28a..30ed113 100644 --- a/src/Gateways/MercadoPagoBasic/Events.php +++ b/src/Gateways/MercadoPagoBasic/Events.php @@ -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); } } diff --git a/src/Gateways/MercadoPagoBasic/MercadoPagoBasicGateway.php b/src/Gateways/MercadoPagoBasic/MercadoPagoBasicGateway.php index 636f119..e997635 100644 --- a/src/Gateways/MercadoPagoBasic/MercadoPagoBasicGateway.php +++ b/src/Gateways/MercadoPagoBasic/MercadoPagoBasicGateway.php @@ -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. * @@ -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); } @@ -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'), + ]); } /** diff --git a/tests/Functional/MercadoPagoBasicTest.php b/tests/Functional/MercadoPagoBasicTest.php index 5193756..982a6c1 100644 --- a/tests/Functional/MercadoPagoBasicTest.php +++ b/tests/Functional/MercadoPagoBasicTest.php @@ -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 */