From 42c94af778fbf73514e78bf28f814f25c22ee356 Mon Sep 17 00:00:00 2001 From: Andy Pieters Date: Mon, 27 Jun 2016 17:22:07 +0200 Subject: [PATCH] Added functions to approve or decline a transaction that is being verified --- samples/exchange.php | 15 ++++++ src/Api/Transaction/Approve.php | 70 ++++++++++++++++++++++++++ src/Api/Transaction/Decline.php | 70 ++++++++++++++++++++++++++ src/Result/Transaction/Transaction.php | 25 ++++++++- src/Transaction.php | 13 +++++ 5 files changed, 192 insertions(+), 1 deletion(-) create mode 100644 src/Api/Transaction/Approve.php create mode 100644 src/Api/Transaction/Decline.php diff --git a/samples/exchange.php b/samples/exchange.php index a63f5c7a..944a9ef6 100644 --- a/samples/exchange.php +++ b/samples/exchange.php @@ -21,12 +21,27 @@ try { $transaction = \Paynl\Transaction::getForExchange(); + if($transaction->isBeingVerified()){ + // here you can do your own checks en approve or decline the order yourself + // we stop the script after approving or declining, because a new exchange call will follow after declining or approving. + // the status of the new exchange call will be paid (approved) or canceled (declined) + $approved = false; // use your own function to determine if this should be true or false. + $declined = false; // use your own function to determine if this should be true or false. + if($approved){ + $transaction->approve(); + die("TRUE| Transaction approved"); + } elseif($declined) { + $transaction->decline(); + die("TRUE| Transaction declined"); + } + } if ($transaction->isPaid()) { // process the payment } elseif ($transaction->isCanceled()) { // payment canceled, restock items } + // always start your response with TRUE| echo "TRUE| "; // Optionally you can send a message after TRUE|, you can view these messages in the logs. https://admin.pay.nl/logs/payment_state diff --git a/src/Api/Transaction/Approve.php b/src/Api/Transaction/Approve.php new file mode 100644 index 00000000..1432764e --- /dev/null +++ b/src/Api/Transaction/Approve.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 Approve 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['orderId'] = $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/approve'); + } +} \ No newline at end of file diff --git a/src/Api/Transaction/Decline.php b/src/Api/Transaction/Decline.php new file mode 100644 index 00000000..af13c3d5 --- /dev/null +++ b/src/Api/Transaction/Decline.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 Decline 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['orderId'] = $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/decline'); + } +} \ No newline at end of file diff --git a/src/Result/Transaction/Transaction.php b/src/Result/Transaction/Transaction.php index 0d2c9def..116a5f8d 100644 --- a/src/Result/Transaction/Transaction.php +++ b/src/Result/Transaction/Transaction.php @@ -19,7 +19,7 @@ namespace Paynl\Result\Transaction; use Paynl\Result\Result; - +use Paynl\Error\Error; /** * Description of Transaction * @@ -183,4 +183,27 @@ 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()){ + \Paynl\Transaction::approve($this->getId()); + $this->_reload(); //status is changed, so refresh the object + } else { + throw new Error("Cannot approve transaction because it does not have the status 'verify'"); + } + } + + public function decline(){ + if($this->isBeingVerified()){ + \Paynl\Transaction::decline($this->getId()); + $this->_reload();//status is changed, so refresh the object + } else { + throw new Error("Cannot decline transaction because it does not have the status 'verify'"); + } + } + } diff --git a/src/Transaction.php b/src/Transaction.php index 91b6840b..b6acf919 100644 --- a/src/Transaction.php +++ b/src/Transaction.php @@ -258,4 +258,17 @@ public static function refund($transactionId, $amount = null, return new Result\Refund($result); } + public static function approve($transactionId){ + $api = new Api\Approve(); + $api->setTransactionId($transactionId); + $result = $api->doRequest(); + return $result['request']['result'] == 1; + } + + public static function decline($transactionId){ + $api = new Api\Decline(); + $api->setTransactionId($transactionId); + $result = $api->doRequest(); + return $result['request']['result'] == 1; + } }