diff --git a/README.md b/README.md index 80bec1c..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']; } ``` @@ -127,7 +133,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 @@ -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,7 +185,7 @@ if ($result['status'] == 'ok') { echo "List of swik(s) = "; print_r($result['list']); } else { - echo "Failed getting the swik list"; + echo "Failed to get the swik list"; echo "Error = " . $result['message']; } ``` diff --git a/src/SwiklyAPI.php b/src/SwiklyAPI.php index 633344d..dd7d0fd 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; } @@ -122,15 +134,28 @@ public function newDirectSwik(\Swikly\Swik $swik) { curl_setopt($ch, CURLOPT_HEADER, 1); $resp = curl_exec($ch); + + // Check for curl error and set the result accordingly + $result = $this->getCurlError($ch, $resp); + 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]) : ''; + // Handle the data when request succeed + 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); - $response = json_decode($jsonResponse, true); + // Parse the header for redirection + preg_match_all('/^Location:(.*)$/mi', $headers, $matches); - return is_array($response) ? array_merge($result, $response) : $result; + $result = !empty($matches[1]) ? array('redirect' => trim($matches[1][0])) : array('redirect' => ''); + + // Create a json object01 + $response = json_decode($jsonResponse, true); + } + + return isset($response) && is_array($response) ? array_merge($result, $response) : $result; } public function deleteSwik(\Swikly\Swik $swik) { @@ -163,6 +188,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; } @@ -203,10 +231,11 @@ public function newPayment(\Swikly\Swik $swik) { curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); - error_log("New payment result = " . serialize($result)); - $json = json_decode($result, true); + // Check for curl error and set the result accordingly + $json = $this->getCurlError($ch, $json); + return $json; } @@ -248,15 +277,28 @@ public function newDirectPayment(\Swikly\Swik $swik) { curl_setopt($ch, CURLOPT_HEADER, 1); $resp = curl_exec($ch); + + // Check for curl error and set the result accordingly + $result = $this->getCurlError($ch, $resp); + 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]) : ''; + // Handle the data when request succeed + 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); - $response = json_decode($jsonResponse, true); + // Parse the header for redirection + preg_match_all('/^Location:(.*)$/mi', $headers, $matches); + + $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 getListSwik() { @@ -274,24 +316,37 @@ 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; } public function getSwik($swik) { $headerData = array( 'Content-type: application/x-www-form-urlencoded', - "API_KEY: " . $this->apiKey, + "API_KEY: " . $this->apiKey, "API_SECRET: " . $this->apiSecret ); + $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=' . $swik->getSwikId()); + 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; }