Skip to content
This repository has been archived by the owner on Jul 12, 2021. It is now read-only.

Commit

Permalink
Fix bug: production version response 409 when service is not paid
Browse files Browse the repository at this point in the history
  • Loading branch information
Miroslav NB committed Apr 4, 2017
1 parent 23dbbec commit b418952
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 18 deletions.
15 changes: 12 additions & 3 deletions src/Balikobot/Balikobot.php
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ class Balikobot {
*/
const
EXCEPTION_INVALID_REQUEST = 400, /*< Invalid request */
EXCEPTION_NOT_SUPPORTED = 401, /*< Not supported */
EXCEPTION_SERVER_ERROR = 500; /*< Unexpected response from the server */


Expand Down Expand Up @@ -711,6 +712,8 @@ public function getServices($shipper) {

$response = $this->call(self::REQUEST_SERVICES, $shipper);

if (isset($response['status']) && ($response['status'] == 409))
throw new \InvalidArgumentException("The $shipper shipper is not supported.", self::EXCEPTION_NOT_SUPPORTED);
if (!isset($response['status']) || ($response['status'] != 200)) {
$code = isset($response['status']) ? $response['status'] : 0;
throw new \UnexpectedValueException("Unexpected server response, code = $code.", self::EXCEPTION_SERVER_ERROR);
Expand All @@ -731,6 +734,8 @@ public function getManipulationUnits($shipper) {

$response = $this->call(self::REQUEST_MANIPULATIONUNITS, $shipper);

if (isset($response['status']) && ($response['status'] == 409))
throw new \InvalidArgumentException("The $shipper shipper is not supported.", self::EXCEPTION_NOT_SUPPORTED);
if (!isset($response['status']) || ($response['status'] != 200)) {
$code = isset($response['status']) ? $response['status'] : 0;
throw new \UnexpectedValueException("Unexpected server response, code = $code.", self::EXCEPTION_SERVER_ERROR);
Expand Down Expand Up @@ -762,6 +767,8 @@ public function getBranches($shipper, $service = null, $full = false) {

$response = $this->call($full ? self::REQUEST_FULLBRANCHES : self::REQUEST_BRANCHES, $shipper, [], $service);

if (isset($response['status']) && ($response['status'] == 409))
throw new \InvalidArgumentException("The $shipper shipper is not supported.", self::EXCEPTION_NOT_SUPPORTED);
if (!isset($response['status']) || ($response['status'] != 200)) {
$code = isset($response['status']) ? $response['status'] : 0;
throw new \UnexpectedValueException("Unexpected server response, code = $code.", self::EXCEPTION_SERVER_ERROR);
Expand Down Expand Up @@ -798,6 +805,8 @@ public function getCountriesForService($shipper) {

$response = $this->call(self::REQUEST_COUNTRIES4SERVICE, $shipper);

if (isset($response['status']) && ($response['status'] == 409))
throw new \InvalidArgumentException("The $shipper shipper is not supported.", self::EXCEPTION_NOT_SUPPORTED);
if (!isset($response['status']) || ($response['status'] != 200)) {
$code = isset($response['status']) ? $response['status'] : 0;
throw new \UnexpectedValueException("Unexpected server response, code = $code.", self::EXCEPTION_SERVER_ERROR);
Expand Down Expand Up @@ -830,6 +839,8 @@ public function getZipCodes($shipper, $service, $country = self::COUNTRY_CZECHIA

$response = $this->call(self::REQUEST_ZIPCODES, $shipper, [], "$service/$country");

if (isset($response['status']) && ($response['status'] == 409))
throw new \InvalidArgumentException("The $shipper shipper is not supported.", self::EXCEPTION_NOT_SUPPORTED);
if (!isset($response['status']) || ($response['status'] != 200)) {
$code = isset($response['status']) ? $response['status'] : 0;
throw new \UnexpectedValueException("Unexpected server response, code = $code.", self::EXCEPTION_SERVER_ERROR);
Expand Down Expand Up @@ -1305,8 +1316,6 @@ private function call($request, $shipper, array $data = [], $url = null) {
if (empty($request) || empty ($shipper))
throw new \InvalidArgumentException('Invalid argument has been entered.');

//printf("\nRequest: $this->apiUrl/$shipper/$request\n");

$r = curl_init();
curl_setopt($r, CURLOPT_URL, $url ? "$this->apiUrl/$shipper/$request/$url" : "$this->apiUrl/$shipper/$request");
curl_setopt($r, CURLOPT_RETURNTRANSFER, true);
Expand All @@ -1321,7 +1330,7 @@ private function call($request, $shipper, array $data = [], $url = null) {
]);
$response = curl_exec($r);
curl_close($r);
//var_dump($response);

return json_decode($response, true);
}

Expand Down
72 changes: 57 additions & 15 deletions test/balikobot.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,15 @@
if ($testGetters) {
printf("Shippers & its services\n");
foreach ($shippers as $itemShippers) {
printf("%s: %s\n", $itemShippers, arrayToString($b->getServices($itemShippers)));
try {
printf("%s: %s\n", $itemShippers, arrayToString($b->getServices($itemShippers)));
} catch (\InvalidArgumentException $e) {
if ($e->getCode() == Balikobot::EXCEPTION_NOT_SUPPORTED) {
printf("%s: %s\n", $itemShippers, 'Not supported');
} else {
throw $e;
}
}
}
printf("\n\n");

Expand All @@ -39,19 +47,36 @@
// shippers + services
printf("Countries For Service\n");
foreach ($shippers as $itemShippers) {
printf("%s: %s\n", $itemShippers, arrayToString($b->getCountriesForService($itemShippers)));
try {
printf("%s: %s\n", $itemShippers, arrayToString($b->getCountriesForService($itemShippers)));
} catch (\InvalidArgumentException $e) {
if ($e->getCode() == Balikobot::EXCEPTION_NOT_SUPPORTED) {
printf("%s: %s\n", $itemShippers, 'Not supported');
} else {
throw $e;
}
}
}
printf("\n\n");

// branches
printf("Branches\n");
foreach ($shippers as $itemShippers) {
$services = $b->getServices($itemShippers);
foreach ($services as $servicesKey => $servicesItem) {
try {
printf("%s:%s: %s\n", $itemShippers, $servicesKey, arrayToString($b->getBranches($itemShippers, $servicesKey)));
} catch (\Exception $e) {
printf("\nException: %d: %s\n", $e->getCode(), $e->getMessage());
try {
$services = $b->getServices($itemShippers);

foreach ($services as $servicesKey => $servicesItem) {
try {
printf("%s:%s: %s\n", $itemShippers, $servicesKey, arrayToString($b->getBranches($itemShippers, $servicesKey)));
} catch (\Exception $e) {
printf("\nException: %d: %s\n", $e->getCode(), $e->getMessage());
}
}
} catch (\InvalidArgumentException $e) {
if ($e->getCode() == Balikobot::EXCEPTION_NOT_SUPPORTED) {
printf("%s: %s\n", $itemShippers, 'Not supported');
} else {
throw $e;
}
}
}
Expand All @@ -60,12 +85,21 @@
// zip
printf("Zip\n");
foreach ($shippers as $itemShippers) {
$services = $b->getServices($itemShippers);
foreach ($services as $servicesKey => $servicesItem) {
try {
printf("%s:%s: %s\n", $itemShippers, $servicesKey, arrayToString($b->getZipCodes($itemShippers, $servicesKey)));
} catch (\Exception $e) {
printf("\nException: %d: %s\n", $e->getCode(), $e->getMessage());
try {
$services = $b->getServices($itemShippers);

foreach ($services as $servicesKey => $servicesItem) {
try {
printf("%s:%s: %s\n", $itemShippers, $servicesKey, arrayToString($b->getZipCodes($itemShippers, $servicesKey)));
} catch (\Exception $e) {
printf("\nException: %d: %s\n", $e->getCode(), $e->getMessage());
}
}
} catch (\InvalidArgumentException $e) {
if ($e->getCode() == Balikobot::EXCEPTION_NOT_SUPPORTED) {
printf("%s: %s\n", $itemShippers, 'Not supported');
} else {
throw $e;
}
}
}
Expand All @@ -86,7 +120,15 @@
// manipulation units
printf("Manipulation units\n");
foreach ($shippers as $itemShippers) {
printf("%s: %s\n", $itemShippers, arrayToString($b->getManipulationUnits($itemShippers)));
try {
printf("%s: %s\n", $itemShippers, arrayToString($b->getManipulationUnits($itemShippers)));
} catch (\InvalidArgumentException $e) {
if ($e->getCode() == Balikobot::EXCEPTION_NOT_SUPPORTED) {
printf("%s: %s\n", $itemShippers, 'Not supported');
} else {
throw $e;
}
}
}
printf("\n\n");
}
Expand Down

0 comments on commit b418952

Please sign in to comment.