From 0858938c5e691e91a65944d0052e1e834132f559 Mon Sep 17 00:00:00 2001 From: JuniorFt Date: Thu, 9 Feb 2017 10:22:57 +0100 Subject: [PATCH 1/6] Improve curl's error handling --- src/SwiklyAPI.php | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/src/SwiklyAPI.php b/src/SwiklyAPI.php index 5bdcb0f..638945d 100644 --- a/src/SwiklyAPI.php +++ b/src/SwiklyAPI.php @@ -37,6 +37,15 @@ public function setApiSecret($secret) { return $this; } + // Handle curl error + private function getCurlError($ch, $response) { + if(curl_errno($ch)) { + $response['status'] = 'ko'; + $response['message'] = 'Connection error: ' . curl_error($ch); + } + return $response; + } + public function newSwik(\Swikly\Swik $swik) { // set required parameters $data = array ( @@ -78,6 +87,9 @@ public function newSwik(\Swikly\Swik $swik) { $result = curl_exec($ch); $json = json_decode($result, true); + // Check for curl error and set the result accordingly + $json = $this->getCurlError($ch, $json); + return $json; } @@ -124,11 +136,21 @@ public function newDirectSwik(\Swikly\Swik $swik) { $resp = curl_exec($ch); curl_close($ch); - list($headers, $jsonResponse) = explode("\r\n\r\n", $resp, 2); - preg_match_all('/^Location:(.*)$/mi', $headers, $matches); - $result['redirect'] = !empty($matches[1]) ? trim($matches[1][0]) : ''; + // Check for curl error and set the result accordingly + $result = $this->getCurlError($ch, $resp); + + // Handle the data when request succeed + if (isset($result['status']) && $result['status'] != 'ko') { + // Split the header and body data + list($headers, $jsonResponse) = explode("\r\n\r\n", $resp, 2); - $response = json_decode($jsonResponse, true); + // Parse the header for redirection + preg_match_all('/^Location:(.*)$/mi', $headers, $matches); + $result['redirect'] = !empty($matches[1]) ? trim($matches[1][0]) : ''; + + // Create a json object + $response = json_decode($jsonResponse, true); + } return is_array($response) ? array_merge($result, $response) : $result; } @@ -163,6 +185,9 @@ public function deleteSwik(\Swikly\Swik $swik) { $result = curl_exec($ch); $json = json_decode($result, true); + // Check for curl error and set the result accordingly + $json = $this->getCurlError($ch, $json); + return $json; } @@ -181,6 +206,10 @@ public function getListSwik() { $result = curl_exec($ch); $json = json_decode($result, true); + // Check for curl error and set the result accordingly + $json = $this->getCurlError($ch, $json); + return $json; } + } From 80181344a228f8962b342da685d4387b21f5f0f4 Mon Sep 17 00:00:00 2001 From: JuniorFt Date: Thu, 9 Feb 2017 10:24:12 +0100 Subject: [PATCH 2/6] Add function getSwik to have informations about a particular Swik --- src/SwiklyAPI.php | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/SwiklyAPI.php b/src/SwiklyAPI.php index 638945d..0b5139b 100644 --- a/src/SwiklyAPI.php +++ b/src/SwiklyAPI.php @@ -212,4 +212,28 @@ public function getListSwik() { return $json; } + public function getSwik($swik) { + $headerData = array( + 'Content-type: application/x-www-form-urlencoded', + "API_KEY: " . $this->apiKey, + "API_SECRET: " . $this->apiSecret + ); + + $swikId = $swik->getSwikId(); + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $this->url . '/v1_0/getSwik?id=' . $swikId); + curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE); + curl_setopt($ch, CURLOPT_HTTPHEADER, $headerData); + curl_setopt($ch, CURLOPT_POST, 0); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + $result = curl_exec($ch); + $json = json_decode($result, true); + + // Check for curl error and set the result accordingly + $json = $this->getCurlError($ch, $json); + + return $json; + } + } From dce290dfac964db1f9eeb8312f564ad1063d877c Mon Sep 17 00:00:00 2001 From: JuniorFt Date: Thu, 9 Feb 2017 10:31:55 +0100 Subject: [PATCH 3/6] Add getSwik() example in the README --- README.md | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index eb6e562..38dc393 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ if ($result['status'] == 'ok') { // Create a swik object $swik = new Swik(); -// Set the Id you used to create it +// Set the Swik Id (yours or the one from Swikly) $swik->setSwikId("YOUR_ID"); // Deleting the swik @@ -128,3 +128,27 @@ if ($result['status'] == 'ok') { echo "Error = " . $result['message']; } ``` + +### Get Swik + +```PHP +setSwikId("YOUR_ID"); + +// Get the list of your swiks +$result = $swkAPI->getSwik($swik); + +// Print result of the operation +if ($result['status'] == 'ok') { + echo "Swik detail = "; + print_r($result['swik']); +} else { + echo "Failed getting the swik list"; + echo "Error = " . $result['message']; +} +``` From 6001e6db1afa53baed1238ff09b4b77433185f6a Mon Sep 17 00:00:00 2001 From: JuniorFt Date: Tue, 14 Feb 2017 14:51:36 +0100 Subject: [PATCH 4/6] Fix error check on direct swik --- src/SwiklyAPI.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/SwiklyAPI.php b/src/SwiklyAPI.php index 0b5139b..1bddfa5 100644 --- a/src/SwiklyAPI.php +++ b/src/SwiklyAPI.php @@ -134,25 +134,28 @@ public function newDirectSwik(\Swikly\Swik $swik) { curl_setopt($ch, CURLOPT_HEADER, 1); $resp = curl_exec($ch); - curl_close($ch); // Check for curl error and set the result accordingly $result = $this->getCurlError($ch, $resp); + curl_close($ch); + // Handle the data when request succeed - if (isset($result['status']) && $result['status'] != 'ko') { + if ((isset($result['status']) && $result['status'] != 'ko') || !isset($result['status'])) { + // Split the header and body data list($headers, $jsonResponse) = explode("\r\n\r\n", $resp, 2); // Parse the header for redirection preg_match_all('/^Location:(.*)$/mi', $headers, $matches); - $result['redirect'] = !empty($matches[1]) ? trim($matches[1][0]) : ''; - // Create a json object + $result = !empty($matches[1]) ? array('redirect' => trim($matches[1][0])) : array('redirect' => ''); + + // Create a json object01 $response = json_decode($jsonResponse, true); } - return is_array($response) ? array_merge($result, $response) : $result; + return isset($response) && is_array($response) ? array_merge($result, $response) : $result; } public function deleteSwik(\Swikly\Swik $swik) { From 46e976ba5968303cffcbd4bc5558b3961b3ae940 Mon Sep 17 00:00:00 2001 From: JuniorFt Date: Mon, 13 Mar 2017 17:03:22 +0100 Subject: [PATCH 5/6] Update README with the Swikly's description --- README.md | 46 ++++++++++++++-------------------------------- 1 file changed, 14 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index 0b49a26..a768d2f 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,11 @@ # Swikly PHP SDK +Swikly provides a new and easy way to request the equivalent to a down payment or security deposit (we call that "a swik") but with no funds being transferred. + +When you request a swik, you're effectively asking your customer to provide a credit card hold, specifying an amount and a period during which the deposit will be held. + +This allows you to secure your bookings or request a down payment or security deposit remotely, simply, and with complete confidence. Swikly also provides a payment option which is cheaper than most of its competitors. + You can sign up to get a [Swikly](https://www.swikly.com) account [here](https://www.swikly.com/user_signup_self.php). ## Requirements @@ -81,14 +87,14 @@ $result = $swkAPI->newSwik($swik); // Print result of the operation if ($result['status'] == 'ok') { echo "New swik created\n"; - echo "Your client can accept the swik at that address: " . $result['acceptUrl']; + echo "Your client can accept the swik at this address: " . $result['acceptUrl']; } else { - echo "Failed create swik"; + echo "Failed to create the swik"; echo "Error = " . $result['message']; } ``` -### Create a payment: +### Create a new payment: ```PHP newPayment($swik); // Print result of the operation if ($result['status'] == 'ok') { echo "New payment created\n"; - echo "Your client can pay you at that address: " . $result['acceptUrl']; + echo "Your client can pay you at this address: " . $result['acceptUrl']; } else { - echo "Failed create a payment"; + echo "Failed to create a newPayment"; echo "Error = " . $result['message']; } ``` @@ -137,7 +143,7 @@ $result = $swkAPI->deleteSwik($swik); if ($result['status'] == 'ok') { echo "Swik deleted correctly"; } else { - echo "Failed delete swik"; + echo "Failed to delete the swik"; echo "Error = " . $result['message']; } ``` @@ -161,7 +167,7 @@ if ($result['status'] == 'ok') { echo "My swik = "; print_r($result['swik']); } else { - echo "Failed getting the swik list"; + echo "Failed to get the swik"; echo "Error = " . $result['message']; } ``` @@ -179,31 +185,7 @@ if ($result['status'] == 'ok') { echo "List of swik(s) = "; print_r($result['list']); } else { - echo "Failed getting the swik list"; - echo "Error = " . $result['message']; -} -``` - -### Get Swik - -```PHP -setSwikId("YOUR_ID"); - -// Get the list of your swiks -$result = $swkAPI->getSwik($swik); - -// Print result of the operation -if ($result['status'] == 'ok') { - echo "Swik detail = "; - print_r($result['swik']); -} else { - echo "Failed getting the swik list"; + echo "Failed to get the swik list"; echo "Error = " . $result['message']; } ``` From 24a5663ca347ee8b543b817290a881fcd747f11f Mon Sep 17 00:00:00 2001 From: JuniorFt Date: Mon, 13 Mar 2017 17:18:17 +0100 Subject: [PATCH 6/6] Add check for Swik Id when using getSwik --- src/SwiklyAPI.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/SwiklyAPI.php b/src/SwiklyAPI.php index dc059fd..dd7d0fd 100644 --- a/src/SwiklyAPI.php +++ b/src/SwiklyAPI.php @@ -331,6 +331,10 @@ public function getSwik($swik) { $swikId = $swik->getSwikId(); + if (!$swikId || ($swikId && $swikId == "")) { + return array("status" => "ko", "message" => "Missing Swik Id"); + } + $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->url . '/v1_0/getSwik?id=' . $swikId); curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, FALSE);