1 | <?php |
||
23 | abstract class XmlRequest extends AbstractRequest |
||
24 | { |
||
25 | /** |
||
26 | * The XML API Endpoint Base URL |
||
27 | * |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $apiBaseProdUrl = 'https://payment.datatrans.biz/upp/jsp'; |
||
31 | /** |
||
32 | * The XML API Endpoint Base URL |
||
33 | * |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $apiBaseTestUrl = 'https://pilot.datatrans.biz/upp/jsp'; |
||
37 | |||
38 | /** |
||
39 | * defines the endpoint for a specific api |
||
40 | * |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $apiEndpoint = null; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | protected $serviceName = null; |
||
49 | |||
50 | /** |
||
51 | * @var int |
||
52 | */ |
||
53 | protected $serviceVersion = null; |
||
54 | |||
55 | /** |
||
56 | * @param $requestChild |
||
57 | * @return mixed |
||
58 | */ |
||
59 | 10 | protected function prepareRequestXml($requestChild) |
|
77 | |||
78 | /** |
||
79 | * Generate XML for request |
||
80 | * |
||
81 | * @return \SimpleXMLElement |
||
82 | */ |
||
83 | 10 | protected function getRequestXml() |
|
103 | |||
104 | /** |
||
105 | * @return string |
||
106 | */ |
||
107 | 10 | public function getServiceName() |
|
111 | |||
112 | /** |
||
113 | * @return int |
||
114 | */ |
||
115 | 10 | public function getServiceVersion() |
|
119 | |||
120 | /** |
||
121 | * Get HTTP Method. |
||
122 | * |
||
123 | * This is nearly always POST but can be over-ridden in sub classes. |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | 10 | protected function getHttpMethod() |
|
131 | |||
132 | /** |
||
133 | * @param mixed $data |
||
134 | * @return XmlResponse |
||
135 | * |
||
136 | * @throws InvalidResponseException |
||
137 | */ |
||
138 | 10 | public function sendData($data) |
|
139 | { |
||
140 | 10 | $httpRequest = $this->httpClient->createRequest( |
|
141 | 10 | $this->getHttpMethod(), |
|
142 | 10 | $this->getEndpoint(), |
|
143 | array( |
||
144 | 10 | 'Accept' => 'application/xml', |
|
145 | 10 | 'Content-type' => 'application/xml', |
|
146 | 10 | ), |
|
147 | 10 | $this->getRequestXml()->asXML() |
|
|
|||
148 | 10 | ); |
|
149 | |||
150 | |||
151 | // Might be useful to have some debug code here, PayPal especially can be |
||
152 | // a bit fussy about data formats and ordering. Perhaps hook to whatever |
||
153 | // logging engine is being used. |
||
154 | // echo "Data == " . json_encode($data) . "\n"; |
||
155 | |||
156 | try { |
||
157 | 10 | $httpRequest->getCurlOptions()->set(CURLOPT_SSLVERSION, 6); // CURL_SSLVERSION_TLSv1_2 for libcurl < 7.35 |
|
158 | 10 | $httpResponse = $httpRequest->send(); |
|
159 | // Empty response body should be parsed also as and empty array |
||
160 | 10 | $body = $httpResponse->getBody(true); |
|
161 | 10 | $xmlResponse = !empty($body) ? $httpResponse->xml() : ''; |
|
162 | |||
163 | 10 | if ($xmlResponse instanceof \SimpleXMLElement) { |
|
164 | 10 | $response = $xmlResponse->body->transaction; |
|
165 | |||
166 | 10 | $response = json_decode(json_encode($response), true); |
|
167 | |||
168 | 10 | return $this->response = $this->createResponse($response); |
|
169 | } |
||
170 | |||
171 | throw new InvalidResponseException('Error communicating with payment gateway'); |
||
172 | } catch (\Exception $e) { |
||
173 | throw new InvalidResponseException( |
||
174 | 'Error communicating with payment gateway: ' . $e->getMessage(), |
||
175 | $e->getCode() |
||
176 | ); |
||
177 | } |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * @param $data |
||
182 | * |
||
183 | * @return XmlResponse |
||
184 | */ |
||
185 | 10 | protected function createResponse($data) |
|
189 | |||
190 | /** |
||
191 | * @return string |
||
192 | */ |
||
193 | 10 | protected function getEndpoint() |
|
198 | |||
199 | /** |
||
200 | * @return string |
||
201 | */ |
||
202 | public function getApiBaseProdUrl() |
||
206 | |||
207 | /** |
||
208 | * @return string |
||
209 | */ |
||
210 | 10 | public function getApiBaseTestUrl() |
|
214 | |||
215 | /** |
||
216 | * @return string |
||
217 | */ |
||
218 | 10 | public function getApiEndpoint() |
|
222 | } |
||
223 |