Skip to content

Commit

Permalink
Merge pull request #1 from swikly/dev
Browse files Browse the repository at this point in the history
v1.1.0 with new features
  • Loading branch information
JuniorFt authored Mar 13, 2017
2 parents 357d803 + 24a5663 commit 8d33b59
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 23 deletions.
24 changes: 15 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
<?php
// Create a swik object
Expand All @@ -112,9 +118,9 @@ $result = $swkAPI->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'];
}
```
Expand All @@ -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
Expand All @@ -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'];
}
```
Expand All @@ -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'];
}
```
Expand All @@ -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'];
}
```
83 changes: 69 additions & 14 deletions src/SwiklyAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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() {
Expand All @@ -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;
}

Expand Down

0 comments on commit 8d33b59

Please sign in to comment.