Skip to content

Commit 07b679d

Browse files
committed
Initial
0 parents  commit 07b679d

File tree

3 files changed

+295
-0
lines changed

3 files changed

+295
-0
lines changed

composer.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "sequra/php-client",
3+
"description": "PHP client for SeQura API",
4+
"version": "1.0.0",
5+
"license": [
6+
"proprietary"
7+
],
8+
"type": "library",
9+
"authors": [
10+
{
11+
"name": "SeQura Engineering",
12+
"email": "[email protected]"
13+
}
14+
],
15+
"minimum-stability": "alpha",
16+
"require": {
17+
"php": ">=5.4.0",
18+
"ext-curl": "*",
19+
"ext-json": "*"
20+
},
21+
"autoload": {
22+
"psr-4": {
23+
"Sequra\\PhpClient\\": "src/"
24+
}
25+
}
26+
}

src/Client.php

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
<?php
2+
/**
3+
* Copyright © 2017 SeQura Engineering. All rights reserved.
4+
*/
5+
6+
namespace Sequra\PhpClient;
7+
use Sequra\PhpClient\Helper;
8+
9+
class Client {
10+
11+
public static $endpoint = '';
12+
public static $user = '';
13+
public static $password = '';
14+
public static $user_agent = null;
15+
private $_endpoint;
16+
private $_user;
17+
private $_password;
18+
private $_user_agent;
19+
private $_debug;
20+
private $_logfile;
21+
22+
private $success;
23+
private $cart_has_changed;
24+
private $headers;
25+
private $status;
26+
private $curl_result = null;
27+
private $json = null;
28+
29+
public function __construct($user = null, $password = null, $endpoint = null, $debug = false, $logfile = null) {
30+
$this->_debug = $debug;
31+
$this->_user = Helper::notNull($user, self::$user);
32+
$this->_password = Helper::notNull($password, self::$password);
33+
$url_parts = parse_url(Helper::notNull($endpoint, self::$endpoint));
34+
if (count($url_parts)>1) {
35+
$this->_endpoint = $url_parts['scheme'] . '://' . $url_parts['host'] . (isset($url_parts['port']) ? ':' . $url_parts['port'] : '');
36+
}
37+
$this->_user_agent = Helper::notNull(self::$user_agent, 'cURL php ' . phpversion());
38+
}
39+
40+
public function startSolicitation($order) {
41+
if (!$this->qualifyForSolicitation($order) && !$this->_debug)
42+
return;
43+
44+
$this->initCurl($this->_endpoint . '/orders');
45+
$this->verbThePayload('POST', array('order' => $order));
46+
if ($this->status == 204) {
47+
$this->success = true;
48+
$this->log("Start " . $this->status . ": Ok!");
49+
} elseif ($this->status >= 200 && $this->status <= 299 || $this->status == 409) {
50+
$this->json = json_decode($this->curl_result, true); // return array, not object
51+
$this->log("Start " . $this->status . ": " . $this->curl_result);
52+
}
53+
curl_close($this->ch);
54+
}
55+
56+
public function qualifyForSolicitation($order) {
57+
if ($order['cart']['order_total_with_tax'] <= 0)
58+
return false;
59+
if (!Helper::isConsistentCart($order['cart']))
60+
return false;
61+
return true;
62+
}
63+
64+
public function getIdentificationForm($uri, $options = array()) {
65+
$options["product"] = array_key_exists('product', $options) ? $options["product"] : "i1";
66+
$options["ajax"] = (isset($options["ajax"]) && $options["ajax"]) ? "true" : "false";
67+
$this->initCurl($uri . '/form_v2' . '?'.http_build_query($options));
68+
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, "GET");
69+
curl_setopt($this->ch, CURLOPT_HTTPHEADER, array('Accept: text/html'));
70+
$this->sendRequest();
71+
72+
if ($this->status >= 200 && $this->status <= 299) {
73+
curl_close($this->ch);
74+
return $this->curl_result;
75+
} elseif ($this->status >= 400 && $this->status <= 499) {
76+
$this->log("Error " . $this->status . ": " . print_r($this->curl_result, true));
77+
} else {
78+
$this->log("Error " . $this->status . ": " . print_r($this->curl_result, true));
79+
}
80+
curl_close($this->ch);
81+
}
82+
83+
public function getCreditAgreements($amount, $merchant) {
84+
$uri = $this->_endpoint . '/merchants/' . $merchant . '/credit_agreements?total_with_tax=' . $amount . '&currency=EUR&locale=es-ES&country=ES';
85+
$this->initCurl($uri);
86+
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, "GET");
87+
$this->sendRequest();
88+
89+
if ($this->status >= 200 && $this->status <= 299) {
90+
curl_close($this->ch);
91+
return json_decode($this->curl_result, true);
92+
} elseif ($this->status >= 400 && $this->status <= 499) {
93+
$this->log("Error " . $this->status . ": " . print_r($this->curl_result, true));
94+
} else {
95+
$this->log("Error " . $this->status . ": " . print_r($this->curl_result, true));
96+
}
97+
curl_close($this->ch);
98+
}
99+
100+
public function updateOrder($uri, $order) {
101+
if (! preg_match('!^https?://!', $uri)) {
102+
$uri = $this->_endpoint .'/orders/'. $uri;
103+
}
104+
$this->initCurl($uri);
105+
$this->verbThePayload('PUT', array('order' => $order));
106+
107+
if ($this->status >= 200 && $this->status <= 299) {
108+
$this->success = true;
109+
} elseif ($this->status == 409) {
110+
$this->cart_has_changed = true;
111+
$this->json = json_decode($this->curl_result, true);
112+
}
113+
curl_close($this->ch);
114+
}
115+
116+
public function sendDeliveryReport($delivery_report) {
117+
$this->initCurl($this->_endpoint . '/delivery_reports');
118+
$this->verbThePayload('POST', array('delivery_report' => $delivery_report));
119+
120+
if ($this->status >= 200 && $this->status <= 299) {
121+
$this->success = true;
122+
$this->log("Delivery " . $this->status . ": Ok!");
123+
} elseif ($this->status >= 200 && $this->status <= 299 || $this->status == 409) {
124+
$this->json = json_decode($this->curl_result, true); // return array, not object
125+
$this->log("Delivery " . $this->status . ": " . print_r($this->json, true));
126+
}
127+
curl_close($this->ch);
128+
}
129+
130+
public function callCron($cron_url) {
131+
$this->_user_agent = 'sequra-cron';
132+
$this->initCurl($cron_url);
133+
curl_setopt($this->ch, CURLOPT_HTTPGET, 1);
134+
$this->sendRequest();
135+
curl_close($this->ch);
136+
}
137+
138+
public function succeeded() {
139+
return $this->success;
140+
}
141+
142+
public function getJson() {
143+
return $this->json;
144+
}
145+
146+
public function getStatus() {
147+
return $this->status;
148+
}
149+
150+
public function cartHasChanged() {
151+
return $this->cart_has_changed;
152+
}
153+
154+
public function getOrderUri() {
155+
if (preg_match('/^Location:\s+([^\n\r]+)/mi', $this->headers, $m)) {
156+
return $m[1];
157+
}
158+
}
159+
160+
public function dump() {
161+
echo "Endpoit: \n";
162+
var_dump($this->_endpoint);
163+
echo "\nStatus: \n";
164+
var_dump($this->status);
165+
echo "\njson: \n";
166+
var_dump($this->json);
167+
echo "\nsuccess: \n";
168+
var_dump($this->success);
169+
}
170+
171+
// Private methods below
172+
173+
private function initCurl($url) {
174+
$this->success = $this->json = null;
175+
$this->ch = curl_init($url);
176+
curl_setopt($this->ch, CURLOPT_USERPWD, $this->_user . ':' . $this->_password);
177+
curl_setopt($this->ch, CURLOPT_USERAGENT, $this->_user_agent);
178+
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
179+
curl_setopt($this->ch, CURLOPT_FAILONERROR, false);
180+
// Some versions of openssl seem to need this
181+
// http://www.supermind.org/blog/763/solved-curl-56-received-problem-2-in-the-chunky-parser
182+
curl_setopt($this->ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
183+
// From http://it.toolbox.com/wiki/index.php/Use_curl_from_PHP_-_processing_response_headers
184+
curl_setopt($this->ch, CURLOPT_HEADERFUNCTION, array(&$this, 'storeHeaders'));
185+
$this->headers = '';
186+
}
187+
188+
private function storeHeaders($ch, $header) {
189+
$this->headers .= $header;
190+
return strlen($header);
191+
}
192+
193+
private function verbThePayload($verb, $payload) {
194+
$data_string = json_encode(Helper::removeNulls($payload));
195+
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, $verb);
196+
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $data_string);
197+
if($this->_debug){
198+
curl_setopt($this->ch, CURLOPT_SSL_VERIFYPEER, false);
199+
}
200+
curl_setopt($this->ch, CURLOPT_HTTPHEADER, array(
201+
'Accept: application/json',
202+
'Content-Type: application/json',
203+
'Content-Length: ' . strlen($data_string))
204+
);
205+
$this->sendRequest();
206+
}
207+
208+
private function sendRequest() {
209+
$this->curl_result = curl_exec($this->ch);
210+
$this->status = curl_getinfo($this->ch, CURLINFO_HTTP_CODE);
211+
}
212+
213+
function log($msg) {
214+
if (!$this->_debug) return;
215+
if (!$this->_logfile){
216+
error_log($msg);
217+
} else {
218+
error_log($msg."\n",3,$this->_logfile);
219+
}
220+
}
221+
}

src/Helper.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Copyright © 2017 SeQura Engineering. All rights reserved.
4+
*/
5+
6+
7+
namespace Sequra\PhpClient;
8+
9+
class Helper
10+
{
11+
public static function isConsistentCart($cart)
12+
{
13+
$totals = self::totals($cart);
14+
15+
return $cart['order_total_without_tax'] == $totals['without_tax'] && $cart['order_total_with_tax'] == $totals['with_tax'];
16+
}
17+
18+
public static function totals($cart)
19+
{
20+
$total_without_tax = $total_with_tax = 0;
21+
foreach ($cart['items'] as $item) {
22+
$total_without_tax += isset($item['total_without_tax']) ? $item['total_without_tax'] : 0;
23+
$total_with_tax += isset($item['total_with_tax']) ? $item['total_with_tax'] : 0;
24+
}
25+
26+
return array('without_tax' => $total_without_tax, 'with_tax' => $total_with_tax);
27+
}
28+
29+
public static function removeNulls($data)
30+
{
31+
foreach ($data as $key => $value) {
32+
if (is_null($value)) {
33+
unset($data[$key]);
34+
} else {
35+
if (is_array($value)) {
36+
$data[$key] = self::removeNulls($value);
37+
}
38+
}
39+
}
40+
41+
return $data;
42+
}
43+
44+
public static function notNull($value1, $value2)
45+
{
46+
return is_null($value1) ? $value2 : $value1;
47+
}
48+
}

0 commit comments

Comments
 (0)