Skip to content

Commit

Permalink
Merge pull request #94 from Shoperti/conekta-error-code
Browse files Browse the repository at this point in the history
Add http response code to jsonError response
  • Loading branch information
joecohens authored Oct 4, 2017
2 parents 5fc8ffb + 8a1b3b2 commit 9591842
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
19 changes: 10 additions & 9 deletions src/Gateways/Conekta/ConektaGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,11 @@ public function commit($method, $url, $params = [], $options = [])
}

$rawResponse = $this->getHttpClient()->{$method}($url, $request);
$statusCode = $rawResponse->getStatusCode();

if ($rawResponse->getStatusCode() == 200) {
$response = $this->parseResponse($rawResponse->getBody());
} else {
$response = $this->responseError($rawResponse->getBody());
}
$response = $statusCode == 200
? $this->parseResponse($rawResponse->getBody())
: $this->responseError($rawResponse->getBody(), $statusCode);

return $this->respond($response);
}
Expand Down Expand Up @@ -364,25 +363,27 @@ protected function parseResponse($body)
* Get error response from server or fallback to general error.
*
* @param string $body
* @param int $httpCode
*
* @return array
*/
protected function responseError($body)
protected function responseError($body, $httpCode)
{
return $this->parseResponse($body) ?: $this->jsonError($body);
return $this->parseResponse($body) ?: $this->jsonError($body, $httpCode);
}

/**
* Default JSON response.
*
* @param string $rawResponse
* @param int $httpCode
*
* @return array
*/
public function jsonError($rawResponse)
public function jsonError($rawResponse, $httpCode)
{
$msg = 'API Response not valid.';
$msg .= " (Raw response API {$rawResponse})";
$msg .= " (Raw response: '{$rawResponse}', HTTP code: {$httpCode})";

return [
'message_to_purchaser' => $msg,
Expand Down
6 changes: 4 additions & 2 deletions tests/Functional/ConektaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Shoperti\Tests\PayMe\Functional;

use Shoperti\PayMe\Gateways\Conekta\Charges;
use Shoperti\PayMe\Gateways\Conekta\ConektaGateway;
use Shoperti\PayMe\PayMe;

class ConektaTest extends AbstractFunctionalTestCase
Expand All @@ -11,8 +13,8 @@ public function it_should_create_a_new_conekta_gateway()
{
$gateway = PayMe::make($this->credentials['conekta']);

$this->assertInstanceOf('Shoperti\PayMe\Gateways\Conekta\ConektaGateway', $gateway->getGateway());
$this->assertInstanceOf('Shoperti\PayMe\Gateways\Conekta\Charges', $gateway->charges());
$this->assertInstanceOf(ConektaGateway::class, $gateway->getGateway());
$this->assertInstanceOf(Charges::class, $gateway->charges());
}

/** @test */
Expand Down

0 comments on commit 9591842

Please sign in to comment.