From 3521f99df7f9d34709b8fb1acfab10423084407f Mon Sep 17 00:00:00 2001 From: Andy Pieters Date: Tue, 14 Mar 2017 17:42:11 +0100 Subject: [PATCH] Added transaction Capture and Void functions Added isAuthorized() function to transaction Result object --- samples/return.php | 4 ++ samples/transaction/capture.php | 27 ++++++++++ samples/transaction/void.php | 27 ++++++++++ src/Api/Transaction/Capture.php | 70 ++++++++++++++++++++++++ src/Api/Transaction/Void.php | 70 ++++++++++++++++++++++++ src/Result/Transaction/Transaction.php | 74 +++++++++++++++++--------- src/Transaction.php | 14 +++++ 7 files changed, 260 insertions(+), 26 deletions(-) create mode 100644 samples/transaction/capture.php create mode 100644 samples/transaction/void.php create mode 100644 src/Api/Transaction/Capture.php create mode 100644 src/Api/Transaction/Void.php diff --git a/samples/return.php b/samples/return.php index f224e68f..5734a1b5 100644 --- a/samples/return.php +++ b/samples/return.php @@ -34,6 +34,10 @@ } elseif ($transaction->isCanceled()) { // redirect back to checkout echo "Payment canceled
Try again"; + } elseif ($transaction->isAuthorized()) { + echo "Payment authorized
"; + echo "capture
"; + echo "void"; } } catch (\Paynl\Error\Error $e) { echo "Fout: " . $e->getMessage(); diff --git a/samples/transaction/capture.php b/samples/transaction/capture.php new file mode 100644 index 00000000..ed397e86 --- /dev/null +++ b/samples/transaction/capture.php @@ -0,0 +1,27 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +require_once '../../vendor/autoload.php'; +require_once '../config.php'; + +$transactionId = $_GET['transactionId']; +try { + $result = \Paynl\Transaction::capture($transactionId); +} catch (\Paynl\Error\Error $e) { + echo $e->getMessage(); +} \ No newline at end of file diff --git a/samples/transaction/void.php b/samples/transaction/void.php new file mode 100644 index 00000000..1862a4db --- /dev/null +++ b/samples/transaction/void.php @@ -0,0 +1,27 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +require_once '../../vendor/autoload.php'; +require_once '../config.php'; + +$transactionId = $_GET['transactionId']; +try { + $result = \Paynl\Transaction::void($transactionId); +} catch (\Paynl\Error\Error $e) { + echo $e->getMessage(); +} \ No newline at end of file diff --git a/src/Api/Transaction/Capture.php b/src/Api/Transaction/Capture.php new file mode 100644 index 00000000..5086ceaf --- /dev/null +++ b/src/Api/Transaction/Capture.php @@ -0,0 +1,70 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +namespace Paynl\Api\Transaction; + +use Paynl\Error; +/** + * Description of Approve + * + * @author Andy Pieters + */ +class Capture extends Transaction +{ + protected $apiTokenRequired = true; + protected $serviceIdRequired = false; + + /** + * @var string + */ + private $transactionId; + + /** + * Get data to send to the api + * + * @return array + * @throws Error\Required + */ + protected function getData() { + if(empty($this->transactionId)){ + throw new Error\Required('TransactionId is niet geset'); + } + $this->data['transactionId'] = $this->transactionId; + return parent::getData(); + } + + /** + * Set the transactionId + * + * @param string $transactionId + */ + public function setTransactionId($transactionId){ + $this->transactionId = $transactionId; + } + + /** + * Do the request + * + * @param null $endpoint + * @param null $version + * @return array the result + */ + public function doRequest($endpoint = null, $version = null) { + return parent::doRequest('transaction/capture'); + } +} \ No newline at end of file diff --git a/src/Api/Transaction/Void.php b/src/Api/Transaction/Void.php new file mode 100644 index 00000000..3279a1d8 --- /dev/null +++ b/src/Api/Transaction/Void.php @@ -0,0 +1,70 @@ + + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +namespace Paynl\Api\Transaction; + +use Paynl\Error; +/** + * Description of Approve + * + * @author Andy Pieters + */ +class Void extends Transaction +{ + protected $apiTokenRequired = true; + protected $serviceIdRequired = false; + + /** + * @var string + */ + private $transactionId; + + /** + * Get data to send to the api + * + * @return array + * @throws Error\Required + */ + protected function getData() { + if(empty($this->transactionId)){ + throw new Error\Required('TransactionId is niet geset'); + } + $this->data['transactionId'] = $this->transactionId; + return parent::getData(); + } + + /** + * Set the transactionId + * + * @param string $transactionId + */ + public function setTransactionId($transactionId){ + $this->transactionId = $transactionId; + } + + /** + * Do the request + * + * @param null $endpoint + * @param null $version + * @return array the result + */ + public function doRequest($endpoint = null, $version = null) { + return parent::doRequest('transaction/voidAuthorization'); + } +} \ No newline at end of file diff --git a/src/Result/Transaction/Transaction.php b/src/Result/Transaction/Transaction.php index df65e2c0..4d6c9b50 100644 --- a/src/Result/Transaction/Transaction.php +++ b/src/Result/Transaction/Transaction.php @@ -18,8 +18,9 @@ namespace Paynl\Result\Transaction; -use Paynl\Result\Result; use Paynl\Error\Error; +use Paynl\Result\Result; + /** * Description of Transaction * @@ -27,14 +28,6 @@ */ class Transaction extends Result { - /** - * @return string The transaction id - */ - public function getId() - { - return $this->data['transactionId']; - } - /** * @return bool Transaction is paid */ @@ -69,6 +62,24 @@ public function isCanceled() return $this->data['paymentDetails']['state'] < 0; } + public function isAuthorized() + { + return $this->data['paymentDetails']['state'] == 95; + } + + public function void(){ + if(!$this->isAuthorized()){ + throw new Error('Cannod void transaction, status is not authorized'); + } + return \Paynl\Transaction::void($this->getId()); + } + public function capture(){ + if(!$this->isAuthorized()){ + throw new Error('Cannod capture transaction, status is not authorized'); + } + return \Paynl\Transaction::capture($this->getId()); + } + /** * @param bool|true $allowPartialRefunds * @@ -95,14 +106,6 @@ public function isPartiallyRefunded() return $this->data['paymentDetails']['stateName'] == 'PARTIAL_REFUND'; } - /** - * @return bool - */ - public function isBeingVerified() - { - return $this->data['paymentDetails']['stateName'] == 'VERIFY'; - } - /** * @return float Paid amount in original currency */ @@ -191,13 +194,9 @@ public function getExtra3() return $this->data['statsDetails']['extra3']; } - private function _reload(){ - $result = \Paynl\Transaction::get($this->getId()); - $this->data = $result->getData(); - } - - public function approve(){ - if($this->isBeingVerified()){ + public function approve() + { + if ($this->isBeingVerified()) { $result = \Paynl\Transaction::approve($this->getId()); $this->_reload(); //status is changed, so refresh the object return $result; @@ -206,8 +205,31 @@ public function approve(){ } } - public function decline(){ - if($this->isBeingVerified()){ + /** + * @return bool + */ + public function isBeingVerified() + { + return $this->data['paymentDetails']['stateName'] == 'VERIFY'; + } + + /** + * @return string The transaction id + */ + public function getId() + { + return $this->data['transactionId']; + } + + private function _reload() + { + $result = \Paynl\Transaction::get($this->getId()); + $this->data = $result->getData(); + } + + public function decline() + { + if ($this->isBeingVerified()) { $result = \Paynl\Transaction::decline($this->getId()); $this->_reload();//status is changed, so refresh the object return $result; diff --git a/src/Transaction.php b/src/Transaction.php index 23d5fb0f..08e9f5b1 100644 --- a/src/Transaction.php +++ b/src/Transaction.php @@ -308,4 +308,18 @@ public static function decline($transactionId) $result = $api->doRequest(); return $result['request']['result'] == 1; } + + public static function capture($transactionId){ + $api = new Api\Capture(); + $api->setTransactionId($transactionId); + $result = $api->doRequest(); + return $result['request']['result'] == 1; + } + + public static function void($transactionId){ + $api = new Api\Void(); + $api->setTransactionId($transactionId); + $result = $api->doRequest(); + return $result['request']['result'] == 1; + } }