Skip to content

Commit daac5fc

Browse files
author
Marco Dania
committed
integrate scheduler
1 parent 712fc74 commit daac5fc

File tree

13 files changed

+1098
-13
lines changed

13 files changed

+1098
-13
lines changed

src/Client.php

Lines changed: 165 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Ixopay\Client;
44

5+
use Ixopay\Client\Schedule\ScheduleData;
56
use Ixopay\Client\Exception\ClientException;
67
use Ixopay\Client\Exception\InvalidValueException;
78
use Ixopay\Client\Exception\TimeoutException;
@@ -36,8 +37,16 @@ class Client {
3637

3738
const TRANSACTION_ROUTE = 'transaction';
3839

40+
const SCHEDULE_ROUTE = 'schedule';
41+
3942
const OPTIONS_ROUTE = 'options';
4043

44+
const SCHEDULE_ACTION_START = 'startSchedule';
45+
const SCHEDULE_ACTION_SHOW = 'showSchedule';
46+
const SCHEDULE_ACTION_PAUSE = 'pauseSchedule';
47+
const SCHEDULE_ACTION_CONTINUE = 'continueSchedule';
48+
const SCHEDULE_ACTION_CANCEL = 'cancelSchedule';
49+
4150
/**
4251
* @var string
4352
*/
@@ -147,26 +156,148 @@ public function log($level, $message, array $context = array()) {
147156
* @return Result
148157
*
149158
* @throws ClientException
150-
* @throws Exception\InvalidValueException
159+
* @throws Http\Exception\ClientException
160+
* @throws InvalidValueException
161+
* @throws TimeoutException
151162
*/
152163
protected function sendTransaction($transactionMethod, AbstractTransaction $transaction) {
153164
$dom = $this->getGenerator()->generateTransaction($transactionMethod, $transaction, $this->username,
154165
$this->password, $this->language);
155166
$xml = $dom->saveXML();
167+
168+
$httpResponse= $this->sendRequest($xml, self::$ixopayUrl.self::TRANSACTION_ROUTE);
156169

157-
$response = $this->signAndSendXml($xml, $this->apiKey, $this->sharedSecret, self::$ixopayUrl.self::TRANSACTION_ROUTE);
170+
return $this->getParser()->parseResult($httpResponse->getBody());
171+
}
158172

159-
if ($response->getErrorCode() || $response->getErrorMessage()) {
160-
throw new ClientException('Request failed: ' . $response->getErrorCode() . ' ' . $response->getErrorMessage());
173+
/**
174+
* @param ScheduleData $schedule
175+
*
176+
* @return Schedule\ScheduleResult
177+
* @throws ClientException
178+
* @throws Exception\TypeException
179+
* @throws Http\Exception\ClientException
180+
* @throws InvalidValueException
181+
* @throws TimeoutException
182+
*/
183+
public function startSchedule(ScheduleData $schedule) {
184+
return $this->sendScheduleRequest(self::SCHEDULE_ACTION_START, $schedule);
185+
}
186+
187+
/**
188+
* @param ScheduleData $schedule
189+
*
190+
* @return Schedule\ScheduleResult
191+
* @throws ClientException
192+
* @throws Exception\TypeException
193+
* @throws Http\Exception\ClientException
194+
* @throws InvalidValueException
195+
* @throws TimeoutException
196+
*/
197+
public function showSchedule(ScheduleData $schedule) {
198+
return $this->sendScheduleRequest(self::SCHEDULE_ACTION_SHOW, $schedule);
199+
}
200+
201+
/**
202+
* @param ScheduleData $schedule
203+
*
204+
* @return Schedule\ScheduleResult
205+
* @throws ClientException
206+
* @throws Exception\TypeException
207+
* @throws Http\Exception\ClientException
208+
* @throws InvalidValueException
209+
* @throws TimeoutException
210+
*/
211+
public function pauseSchedule(ScheduleData $schedule) {
212+
return $this->sendScheduleRequest(self::SCHEDULE_ACTION_PAUSE, $schedule);
213+
}
214+
215+
/**
216+
* @param ScheduleData $schedule
217+
*
218+
* @return Schedule\ScheduleResult
219+
* @throws ClientException
220+
* @throws Exception\TypeException
221+
* @throws Http\Exception\ClientException
222+
* @throws InvalidValueException
223+
* @throws TimeoutException
224+
*/
225+
public function continueSchedule(ScheduleData $schedule) {
226+
return $this->sendScheduleRequest(self::SCHEDULE_ACTION_CONTINUE, $schedule);
227+
}
228+
229+
/**
230+
* @param ScheduleData $schedule
231+
*
232+
* @return Schedule\ScheduleResult
233+
* @throws ClientException
234+
* @throws Exception\TypeException
235+
* @throws Http\Exception\ClientException
236+
* @throws InvalidValueException
237+
* @throws TimeoutException
238+
*/
239+
public function cancelSchedule(ScheduleData $schedule) {
240+
return $this->sendScheduleRequest(self::SCHEDULE_ACTION_CANCEL, $schedule);
241+
}
242+
243+
/**
244+
* @param $scheduleAction
245+
* @param ScheduleData $schedule
246+
*
247+
* @return Schedule\ScheduleResult
248+
* @throws ClientException
249+
* @throws Exception\TypeException
250+
* @throws Http\Exception\ClientException
251+
* @throws InvalidValueException
252+
* @throws TimeoutException
253+
*/
254+
public function sendScheduleRequest($scheduleAction, ScheduleData $schedule) {
255+
256+
$scheduleXml = $this->getGenerator()->generateScheduleXml($scheduleAction, $schedule, $this->username, $this->password);
257+
258+
$httpResponse = $this->sendRequest($scheduleXml, self::$ixopayUrl.self::SCHEDULE_ROUTE);
259+
260+
return $this->getParser()->parseScheduleResult($httpResponse->getBody());
261+
}
262+
263+
/**
264+
* @param string $xml
265+
*
266+
* @return Response
267+
* @throws ClientException
268+
* @throws Http\Exception\ClientException
269+
* @throws TimeoutException
270+
*/
271+
protected function sendRequest($xml, $url) {
272+
273+
$httpResponse = $this->signAndSendXml($xml, $this->apiKey, $this->sharedSecret, $url);
274+
275+
if ($httpResponse->getErrorCode() || $httpResponse->getErrorMessage()) {
276+
throw new ClientException('Request failed: ' . $httpResponse->getErrorCode() . ' ' . $httpResponse->getErrorMessage());
161277
}
162-
if ($response->getStatusCode() == 504 || $response->getStatusCode() == 522) {
278+
if ($httpResponse->getStatusCode() == 504 || $httpResponse->getStatusCode() == 522) {
163279
throw new TimeoutException('Request timed-out');
164280
}
165281

166-
$parser = $this->getParser();
167-
return $parser->parseResult($response->getBody());
282+
return $httpResponse;
168283
}
169284

285+
/**
286+
* @param string[] $compareValues
287+
* @param string $subject
288+
*
289+
* @return bool
290+
*/
291+
protected function startsWith(array $compareValues, $subject) {
292+
$firstLetter = substr( $subject, 0, 1 );
293+
foreach($compareValues as $compareValue) {
294+
if ($firstLetter == $compareValue) {
295+
return true;
296+
}
297+
}
298+
299+
return false;
300+
}
170301

171302
/**
172303
* signs and send a well-formed transaction xml
@@ -177,6 +308,7 @@ protected function sendTransaction($transactionMethod, AbstractTransaction $tran
177308
* @param string $url
178309
*
179310
* @return Response
311+
* @throws Http\Exception\ClientException
180312
*/
181313
public function signAndSendXml($xml, $apiKey, $sharedSecret, $url) {
182314
$this->log(LogLevel::DEBUG, "POST $url ",
@@ -210,6 +342,8 @@ public function signAndSendXml($xml, $apiKey, $sharedSecret, $url) {
210342
* @param Register $transactionData
211343
*
212344
* @return Result
345+
* @throws ClientException
346+
* @throws InvalidValueException
213347
*/
214348
public function register(Register $transactionData) {
215349
return $this->sendTransaction('register', $transactionData);
@@ -223,6 +357,8 @@ public function register(Register $transactionData) {
223357
* @param Register $transactionData
224358
*
225359
* @return Result
360+
* @throws ClientException
361+
* @throws InvalidValueException
226362
*/
227363
public function completeRegister(Register $transactionData) {
228364
return $this->sendTransaction('completeRegister', $transactionData);
@@ -236,6 +372,8 @@ public function completeRegister(Register $transactionData) {
236372
* @param Deregister $transactionData
237373
*
238374
* @return Result
375+
* @throws ClientException
376+
* @throws InvalidValueException
239377
*/
240378
public function deregister(Deregister $transactionData) {
241379
return $this->sendTransaction('deregister', $transactionData);
@@ -249,6 +387,8 @@ public function deregister(Deregister $transactionData) {
249387
* @param Preauthorize $transactionData
250388
*
251389
* @return Result
390+
* @throws ClientException
391+
* @throws InvalidValueException
252392
*/
253393
public function preauthorize(Preauthorize $transactionData) {
254394
return $this->sendTransaction('preauthorize', $transactionData);
@@ -260,6 +400,8 @@ public function preauthorize(Preauthorize $transactionData) {
260400
* @param Preauthorize $transactionData
261401
*
262402
* @return Result
403+
* @throws ClientException
404+
* @throws InvalidValueException
263405
*/
264406
public function completePreauthorize(Preauthorize $transactionData) {
265407
return $this->sendTransaction('completePreauthorize', $transactionData);
@@ -271,6 +413,8 @@ public function completePreauthorize(Preauthorize $transactionData) {
271413
* @param \Ixopay\Client\Transaction\VoidTransaction $transactionData
272414
*
273415
* @return Result
416+
* @throws ClientException
417+
* @throws InvalidValueException
274418
*/
275419
public function void(VoidTransaction $transactionData) {
276420
return $this->sendTransaction('void', $transactionData);
@@ -282,6 +426,8 @@ public function void(VoidTransaction $transactionData) {
282426
* @param Capture $transactionData
283427
*
284428
* @return Result
429+
* @throws ClientException
430+
* @throws InvalidValueException
285431
*/
286432
public function capture(Capture $transactionData) {
287433
return $this->sendTransaction('capture', $transactionData);
@@ -293,6 +439,8 @@ public function capture(Capture $transactionData) {
293439
* @param Refund $transactionData
294440
*
295441
* @return Result
442+
* @throws ClientException
443+
* @throws InvalidValueException
296444
*/
297445
public function refund(Refund $transactionData) {
298446
return $this->sendTransaction('refund', $transactionData);
@@ -304,6 +452,8 @@ public function refund(Refund $transactionData) {
304452
* @param Debit $transactionData
305453
*
306454
* @return Result
455+
* @throws ClientException
456+
* @throws InvalidValueException
307457
*/
308458
public function debit(Debit $transactionData) {
309459
return $this->sendTransaction('debit', $transactionData);
@@ -558,6 +708,13 @@ public function getGenerator() {
558708
return $this->generator;
559709
}
560710

711+
/**
712+
* @param string $namespaceRoot
713+
*/
714+
public function setNamespaceRoot($namespaceRoot) {
715+
$this->getGenerator()->setNamespaceRoot($namespaceRoot);
716+
}
717+
561718
/**
562719
* @return Parser
563720
*/
@@ -633,4 +790,5 @@ public static function getDefaultUrl() {
633790
public static function resetApiUrl() {
634791
static::setApiUrl(static::DEFAULT_IXOPAY_URL);
635792
}
793+
636794
}

0 commit comments

Comments
 (0)