Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add http response code to jsonError response #94

Merged
merged 1 commit into from
Oct 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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