Skip to content

Commit

Permalink
Added transaction Capture and Void functions
Browse files Browse the repository at this point in the history
Added isAuthorized() function to transaction Result object
  • Loading branch information
Andy Pieters committed Mar 14, 2017
1 parent e1f9e14 commit 3521f99
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 26 deletions.
4 changes: 4 additions & 0 deletions samples/return.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
} elseif ($transaction->isCanceled()) {
// redirect back to checkout
echo "Payment canceled <br /><a href='transaction/start.php'>Try again</a>";
} elseif ($transaction->isAuthorized()) {
echo "Payment authorized<br />";
echo "<a href='transaction/capture.php?transactionId=" . $transaction->getId() . "'>capture</a><br />";
echo "<a href='transaction/void.php?transactionId=" . $transaction->getId() . "'>void</a>";
}
} catch (\Paynl\Error\Error $e) {
echo "Fout: " . $e->getMessage();
Expand Down
27 changes: 27 additions & 0 deletions samples/transaction/capture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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/>.
*/

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();
}
27 changes: 27 additions & 0 deletions samples/transaction/void.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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/>.
*/

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();
}
70 changes: 70 additions & 0 deletions src/Api/Transaction/Capture.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 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');
}
}
70 changes: 70 additions & 0 deletions src/Api/Transaction/Void.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 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');
}
}
74 changes: 48 additions & 26 deletions src/Result/Transaction/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,16 @@

namespace Paynl\Result\Transaction;

use Paynl\Result\Result;
use Paynl\Error\Error;
use Paynl\Result\Result;

/**
* Description of Transaction
*
* @author Andy Pieters <[email protected]>
*/
class Transaction extends Result
{
/**
* @return string The transaction id
*/
public function getId()
{
return $this->data['transactionId'];
}

/**
* @return bool Transaction is paid
*/
Expand Down Expand Up @@ -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
*
Expand All @@ -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
*/
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions src/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit 3521f99

Please sign in to comment.