Skip to content

Commit

Permalink
Merge pull request #1 from WhitePayments/curl
Browse files Browse the repository at this point in the history
Switching from Guzzle to CURL
  • Loading branch information
Yazin committed Sep 28, 2014
2 parents 01de731 + 82908af commit 01d58b1
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 148 deletions.
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
{
"name": "white/white",
"description": "White makes accepting online payments in the Middle East a piece of cake",
"require": {
"guzzlehttp/guzzle": "~4.0"
},
"require": {},
"require-dev": {
"phpunit/phpunit": "4.2.*"
},
"lib-curl": "*",
"autoload": {
"psr-0": {
"White": "src/",
Expand Down
121 changes: 2 additions & 119 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions src/White.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,8 @@ public static function getEndPoint($name)
return self::$baseURL . self::$endpoints[$name];
}

public static function handleErrors(\Exception $e)
{
$code = $e->getResponse()->getStatusCode();
$result = $e->getResponse()->json();

public static function handleErrors($result, $code)
{
if($code == White_Error_Card::$CODE && $result['error']['type'] == White_Error_Card::$TYPE) {
throw new White_Error_Card($result['error']['message'], $result['error']['code']);
}
Expand Down
55 changes: 34 additions & 21 deletions src/White/Charge.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,25 @@ public static function create(array $data)
{
$url = White::getEndPoint('charge');

$client = new GuzzleHttp\Client();
try {
$response = $client->post($url, array(
'auth' => array(White::getApiKey(), ''),
'body' => $data,
));
$result = $response->json();
} catch (\GuzzleHttp\Exception\TransferException $e) { //catch all Guzzle exceptions
White::handleErrors($e);
} catch(\Exception $e) { //Rethrow any other errors
throw $e;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, White::getApiKey() . ':');
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = json_decode(curl_exec($ch), true);

// Check for errors and such.
$info = curl_getinfo($ch);
$errno = curl_errno($ch);
if( $result === false || $errno != 0 ) {
// Do error checking
throw new Exception(curl_error($ch));
} else if($info['http_code'] != 200) {
// Got a non-200 error code.
White::handleErrors($result, $info['http_code']);
}
curl_close($ch);

return $result;
}
Expand All @@ -53,17 +60,23 @@ public static function all()
{
$url = White::getEndPoint('charge_list');

$client = new GuzzleHttp\Client();
try {
$response = $client->get($url, array(
'auth' => array(White::getApiKey(), '')
));
$result = $response->json();
} catch (\GuzzleHttp\Exception\TransferException $e) { //catch all Guzzle exceptions
White::handleErrors($e);
} catch(\Exception $e) { //Rethrow any other errors
throw $e;
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_USERPWD, White::getApiKey() . ':');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = json_decode(curl_exec($ch), true);

// Check for errors and such.
$info = curl_getinfo($ch);
$errno = curl_errno($ch);
if( $result === false || $errno != 0 ) {
// Do error checking
throw new Exception(curl_error($ch));
} else if($info['http_code'] != 200) {
// Got a non-200 error code.
White::handleErrors($result, $info['http_code']);
}
curl_close($ch);

return $result;
}
Expand Down

0 comments on commit 01d58b1

Please sign in to comment.