|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ShopperLibrary; |
|
4
|
|
|
|
|
5
|
|
|
use ShopperLibrary\ObjectModule\ObjectModuleInterface; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Class AdaptorClient |
|
9
|
|
|
* @package ShopperLibrary |
|
10
|
|
|
*/ |
|
11
|
|
|
class ShopperClient |
|
12
|
|
|
{ |
|
13
|
|
|
const ADAPTOR_SERVICE_URL = 'https://shopper.pagamastarde.com'; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Host to call |
|
17
|
|
|
* |
|
18
|
|
|
* @var string $host |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $host; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var ObjectModuleInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $objectModule; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var string |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $curlErrorMsg; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var int |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $curlErrorCode; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* ShopperClient constructor. |
|
39
|
|
|
* |
|
40
|
|
|
* @param null $host |
|
|
|
|
|
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct($host = null) |
|
43
|
|
|
{ |
|
44
|
|
|
$this->host = $host !== null ? $host : self::ADAPTOR_SERVICE_URL; |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return ObjectModuleInterface |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getObjectModule() |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->objectModule; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get the error message of a curl request, you can call this method if you get an unexpected false return |
|
57
|
|
|
* |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
|
|
public function getCurlError() |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->curlErrorMsg; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Get the error code of a curl request, you can call this method if you get an unexpected false return |
|
67
|
|
|
* |
|
68
|
|
|
* @return int |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getCurlErrorCode() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->curlErrorCode; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return null|string |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getHost() |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->host; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param ObjectModuleInterface $objectModule |
|
85
|
|
|
* |
|
86
|
|
|
* @return ShopperClient |
|
87
|
|
|
*/ |
|
88
|
|
|
public function setObjectModule(ObjectModuleInterface $objectModule) |
|
89
|
|
|
{ |
|
90
|
|
|
$this->objectModule = $objectModule; |
|
91
|
|
|
|
|
92
|
|
|
return $this; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return bool|mixed |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getPaymentForm() |
|
99
|
|
|
{ |
|
100
|
|
|
if (!$this->objectModule instanceof ObjectModuleInterface) { |
|
|
|
|
|
|
101
|
|
|
return false; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$this->objectModule->prepare(); |
|
105
|
|
|
|
|
106
|
|
|
try { |
|
107
|
|
|
$result = $this->postCall( |
|
108
|
|
|
$this->host, |
|
109
|
|
|
serialize($this->objectModule) |
|
110
|
|
|
); |
|
111
|
|
|
|
|
112
|
|
|
return $result ? $result : false; |
|
113
|
|
|
} catch (\Exception $exception) { |
|
114
|
|
|
return false; |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param $url |
|
120
|
|
|
* @param $body |
|
121
|
|
|
* |
|
122
|
|
|
* @return bool | string |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function postCall($url, $body) |
|
125
|
|
|
{ |
|
126
|
|
|
$ch = curl_init($url); |
|
127
|
|
|
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); |
|
128
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $body); |
|
129
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
|
130
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array( |
|
131
|
|
|
'Content-Type: application/json', |
|
132
|
|
|
'Content-Length: ' . strlen($body) |
|
133
|
|
|
)); |
|
134
|
|
|
|
|
135
|
|
|
$result = curl_exec($ch); |
|
136
|
|
|
|
|
137
|
|
|
$this->curlErrorMsg = curl_error($ch); |
|
138
|
|
|
$this->curlErrorCode = curl_errno($ch); |
|
139
|
|
|
|
|
140
|
|
|
return $result; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|