Skip to content

Commit 3521f99

Browse files
author
Andy Pieters
committed
Added transaction Capture and Void functions
Added isAuthorized() function to transaction Result object
1 parent e1f9e14 commit 3521f99

File tree

7 files changed

+260
-26
lines changed

7 files changed

+260
-26
lines changed

samples/return.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
} elseif ($transaction->isCanceled()) {
3535
// redirect back to checkout
3636
echo "Payment canceled <br /><a href='transaction/start.php'>Try again</a>";
37+
} elseif ($transaction->isAuthorized()) {
38+
echo "Payment authorized<br />";
39+
echo "<a href='transaction/capture.php?transactionId=" . $transaction->getId() . "'>capture</a><br />";
40+
echo "<a href='transaction/void.php?transactionId=" . $transaction->getId() . "'>void</a>";
3741
}
3842
} catch (\Paynl\Error\Error $e) {
3943
echo "Fout: " . $e->getMessage();

samples/transaction/capture.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/*
3+
* Copyright (C) 2015 Andy Pieters <andy@pay.nl>
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
require_once '../../vendor/autoload.php';
20+
require_once '../config.php';
21+
22+
$transactionId = $_GET['transactionId'];
23+
try {
24+
$result = \Paynl\Transaction::capture($transactionId);
25+
} catch (\Paynl\Error\Error $e) {
26+
echo $e->getMessage();
27+
}

samples/transaction/void.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/*
3+
* Copyright (C) 2015 Andy Pieters <andy@pay.nl>
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
require_once '../../vendor/autoload.php';
20+
require_once '../config.php';
21+
22+
$transactionId = $_GET['transactionId'];
23+
try {
24+
$result = \Paynl\Transaction::void($transactionId);
25+
} catch (\Paynl\Error\Error $e) {
26+
echo $e->getMessage();
27+
}

src/Api/Transaction/Capture.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/*
3+
* Copyright (C) 2015 Andy Pieters <andy@pay.nl>
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
namespace Paynl\Api\Transaction;
20+
21+
use Paynl\Error;
22+
/**
23+
* Description of Approve
24+
*
25+
* @author Andy Pieters <andy@pay.nl>
26+
*/
27+
class Capture extends Transaction
28+
{
29+
protected $apiTokenRequired = true;
30+
protected $serviceIdRequired = false;
31+
32+
/**
33+
* @var string
34+
*/
35+
private $transactionId;
36+
37+
/**
38+
* Get data to send to the api
39+
*
40+
* @return array
41+
* @throws Error\Required
42+
*/
43+
protected function getData() {
44+
if(empty($this->transactionId)){
45+
throw new Error\Required('TransactionId is niet geset');
46+
}
47+
$this->data['transactionId'] = $this->transactionId;
48+
return parent::getData();
49+
}
50+
51+
/**
52+
* Set the transactionId
53+
*
54+
* @param string $transactionId
55+
*/
56+
public function setTransactionId($transactionId){
57+
$this->transactionId = $transactionId;
58+
}
59+
60+
/**
61+
* Do the request
62+
*
63+
* @param null $endpoint
64+
* @param null $version
65+
* @return array the result
66+
*/
67+
public function doRequest($endpoint = null, $version = null) {
68+
return parent::doRequest('transaction/capture');
69+
}
70+
}

src/Api/Transaction/Void.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
/*
3+
* Copyright (C) 2015 Andy Pieters <andy@pay.nl>
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
17+
*/
18+
19+
namespace Paynl\Api\Transaction;
20+
21+
use Paynl\Error;
22+
/**
23+
* Description of Approve
24+
*
25+
* @author Andy Pieters <andy@pay.nl>
26+
*/
27+
class Void extends Transaction
28+
{
29+
protected $apiTokenRequired = true;
30+
protected $serviceIdRequired = false;
31+
32+
/**
33+
* @var string
34+
*/
35+
private $transactionId;
36+
37+
/**
38+
* Get data to send to the api
39+
*
40+
* @return array
41+
* @throws Error\Required
42+
*/
43+
protected function getData() {
44+
if(empty($this->transactionId)){
45+
throw new Error\Required('TransactionId is niet geset');
46+
}
47+
$this->data['transactionId'] = $this->transactionId;
48+
return parent::getData();
49+
}
50+
51+
/**
52+
* Set the transactionId
53+
*
54+
* @param string $transactionId
55+
*/
56+
public function setTransactionId($transactionId){
57+
$this->transactionId = $transactionId;
58+
}
59+
60+
/**
61+
* Do the request
62+
*
63+
* @param null $endpoint
64+
* @param null $version
65+
* @return array the result
66+
*/
67+
public function doRequest($endpoint = null, $version = null) {
68+
return parent::doRequest('transaction/voidAuthorization');
69+
}
70+
}

src/Result/Transaction/Transaction.php

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,16 @@
1818

1919
namespace Paynl\Result\Transaction;
2020

21-
use Paynl\Result\Result;
2221
use Paynl\Error\Error;
22+
use Paynl\Result\Result;
23+
2324
/**
2425
* Description of Transaction
2526
*
2627
* @author Andy Pieters <andy@pay.nl>
2728
*/
2829
class Transaction extends Result
2930
{
30-
/**
31-
* @return string The transaction id
32-
*/
33-
public function getId()
34-
{
35-
return $this->data['transactionId'];
36-
}
37-
3831
/**
3932
* @return bool Transaction is paid
4033
*/
@@ -69,6 +62,24 @@ public function isCanceled()
6962
return $this->data['paymentDetails']['state'] < 0;
7063
}
7164

65+
public function isAuthorized()
66+
{
67+
return $this->data['paymentDetails']['state'] == 95;
68+
}
69+
70+
public function void(){
71+
if(!$this->isAuthorized()){
72+
throw new Error('Cannod void transaction, status is not authorized');
73+
}
74+
return \Paynl\Transaction::void($this->getId());
75+
}
76+
public function capture(){
77+
if(!$this->isAuthorized()){
78+
throw new Error('Cannod capture transaction, status is not authorized');
79+
}
80+
return \Paynl\Transaction::capture($this->getId());
81+
}
82+
7283
/**
7384
* @param bool|true $allowPartialRefunds
7485
*
@@ -95,14 +106,6 @@ public function isPartiallyRefunded()
95106
return $this->data['paymentDetails']['stateName'] == 'PARTIAL_REFUND';
96107
}
97108

98-
/**
99-
* @return bool
100-
*/
101-
public function isBeingVerified()
102-
{
103-
return $this->data['paymentDetails']['stateName'] == 'VERIFY';
104-
}
105-
106109
/**
107110
* @return float Paid amount in original currency
108111
*/
@@ -191,13 +194,9 @@ public function getExtra3()
191194
return $this->data['statsDetails']['extra3'];
192195
}
193196

194-
private function _reload(){
195-
$result = \Paynl\Transaction::get($this->getId());
196-
$this->data = $result->getData();
197-
}
198-
199-
public function approve(){
200-
if($this->isBeingVerified()){
197+
public function approve()
198+
{
199+
if ($this->isBeingVerified()) {
201200
$result = \Paynl\Transaction::approve($this->getId());
202201
$this->_reload(); //status is changed, so refresh the object
203202
return $result;
@@ -206,8 +205,31 @@ public function approve(){
206205
}
207206
}
208207

209-
public function decline(){
210-
if($this->isBeingVerified()){
208+
/**
209+
* @return bool
210+
*/
211+
public function isBeingVerified()
212+
{
213+
return $this->data['paymentDetails']['stateName'] == 'VERIFY';
214+
}
215+
216+
/**
217+
* @return string The transaction id
218+
*/
219+
public function getId()
220+
{
221+
return $this->data['transactionId'];
222+
}
223+
224+
private function _reload()
225+
{
226+
$result = \Paynl\Transaction::get($this->getId());
227+
$this->data = $result->getData();
228+
}
229+
230+
public function decline()
231+
{
232+
if ($this->isBeingVerified()) {
211233
$result = \Paynl\Transaction::decline($this->getId());
212234
$this->_reload();//status is changed, so refresh the object
213235
return $result;

src/Transaction.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,18 @@ public static function decline($transactionId)
308308
$result = $api->doRequest();
309309
return $result['request']['result'] == 1;
310310
}
311+
312+
public static function capture($transactionId){
313+
$api = new Api\Capture();
314+
$api->setTransactionId($transactionId);
315+
$result = $api->doRequest();
316+
return $result['request']['result'] == 1;
317+
}
318+
319+
public static function void($transactionId){
320+
$api = new Api\Void();
321+
$api->setTransactionId($transactionId);
322+
$result = $api->doRequest();
323+
return $result['request']['result'] == 1;
324+
}
311325
}

0 commit comments

Comments
 (0)