Skip to content

Commit

Permalink
Added functions to approve or decline a transaction that is being ver…
Browse files Browse the repository at this point in the history
…ified
  • Loading branch information
Andy Pieters committed Jun 27, 2016
1 parent cfd0adb commit 42c94af
Show file tree
Hide file tree
Showing 5 changed files with 192 additions and 1 deletion.
15 changes: 15 additions & 0 deletions samples/exchange.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
70 changes: 70 additions & 0 deletions src/Api/Transaction/Approve.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/*
* Copyright (C) 2015 Andy Pieters <[email protected]>
*
* 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 <http://www.gnu.org/licenses/>.
*/

namespace Paynl\Api\Transaction;

use Paynl\Error;
/**
* Description of Approve
*
* @author Andy Pieters <[email protected]>
*/
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');
}
}
70 changes: 70 additions & 0 deletions src/Api/Transaction/Decline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php
/*
* Copyright (C) 2015 Andy Pieters <[email protected]>
*
* 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 <http://www.gnu.org/licenses/>.
*/

namespace Paynl\Api\Transaction;

use Paynl\Error;
/**
* Description of Approve
*
* @author Andy Pieters <[email protected]>
*/
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');
}
}
25 changes: 24 additions & 1 deletion src/Result/Transaction/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
namespace Paynl\Result\Transaction;

use Paynl\Result\Result;

use Paynl\Error\Error;
/**
* Description of Transaction
*
Expand Down Expand Up @@ -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'");
}
}

}
13 changes: 13 additions & 0 deletions src/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 42c94af

Please sign in to comment.