Skip to content

Commit 70a9648

Browse files
author
Andy Pieters
committed
Added addRecurring API for recurring creditcard payments
1 parent ff40358 commit 70a9648

File tree

4 files changed

+267
-17
lines changed

4 files changed

+267
-17
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
try {
23+
$result = \Paynl\Transaction::addRecurring(array(
24+
'transactionId' => '12345678Xbf1234',
25+
'amount' => 0.01,
26+
'description' => 'Your recurring payment',
27+
'extra1' => 'SDK',
28+
'extra2' => 'extra2',
29+
'extra3' => 'extra3'
30+
));
31+
32+
echo $result->getTransactionId();
33+
} catch (\Paynl\Error\Error $e) {
34+
echo $e->getMessage();
35+
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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+
/**
24+
* Description of Approve
25+
*
26+
* @author Andy Pieters <andy@pay.nl>
27+
*/
28+
class AddRecurring extends Transaction
29+
{
30+
protected $apiTokenRequired = true;
31+
protected $serviceIdRequired = false;
32+
33+
/**
34+
* @var string
35+
*/
36+
private $transactionId;
37+
38+
private $amount;
39+
private $description;
40+
private $extra1;
41+
private $extra2;
42+
private $extra3;
43+
44+
/**
45+
* @param mixed $amount
46+
*/
47+
public function setAmount($amount)
48+
{
49+
if (!is_numeric($amount)) {
50+
throw new Error\Error('Amount must be numeric');
51+
}
52+
$this->amount = $amount;
53+
}
54+
55+
/**
56+
* @param mixed $description
57+
*/
58+
public function setDescription($description)
59+
{
60+
$this->description = $description;
61+
}
62+
63+
/**
64+
* @param mixed $extra1
65+
*/
66+
public function setExtra1($extra1)
67+
{
68+
$this->extra1 = $extra1;
69+
}
70+
71+
/**
72+
* @param mixed $extra2
73+
*/
74+
public function setExtra2($extra2)
75+
{
76+
$this->extra2 = $extra2;
77+
}
78+
79+
/**
80+
* @param mixed $extra3
81+
*/
82+
public function setExtra3($extra3)
83+
{
84+
$this->extra3 = $extra3;
85+
}
86+
87+
/**
88+
* Set the transactionId
89+
*
90+
* @param string $transactionId
91+
*/
92+
public function setTransactionId($transactionId)
93+
{
94+
$this->transactionId = $transactionId;
95+
}
96+
97+
/**
98+
* Do the request
99+
*
100+
* @param null $endpoint
101+
* @param null $version
102+
* @return array the result
103+
*/
104+
public function doRequest($endpoint = null, $version = null)
105+
{
106+
return parent::doRequest('transaction/addRecurring');
107+
}
108+
109+
/**
110+
* Get data to send to the api
111+
*
112+
* @return array
113+
* @throws Error\Required
114+
*/
115+
protected function getData()
116+
{
117+
if (empty($this->transactionId)) {
118+
throw new Error\Required('TransactionId is niet geset');
119+
}
120+
$this->data['transactionId'] = $this->transactionId;
121+
122+
if (isset($this->amount)) {
123+
$this->data['amount'] = $this->amount;
124+
}
125+
126+
if (isset($this->description)) {
127+
$this->data['description'] = $this->description;
128+
}
129+
if (isset($this->extra1)) {
130+
$this->data['extra1'] = $this->extra1;
131+
}
132+
if (isset($this->extra2)) {
133+
$this->data['extra2'] = $this->extra2;
134+
}
135+
if (isset($this->extra3)) {
136+
$this->data['extra3'] = $this->extra3;
137+
}
138+
139+
return parent::getData();
140+
}
141+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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\Result\Transaction;
20+
21+
use Paynl\Result\Result;
22+
23+
/**
24+
* Result class for a refund
25+
*
26+
* @author Andy Pieters <andy@pay.nl>
27+
*/
28+
class AddRecurring extends Result
29+
{
30+
/**
31+
* @return string The id of the newly created transaction
32+
*/
33+
public function getTransactionId()
34+
{
35+
return $this->data['transactionId'];
36+
}
37+
}

src/Transaction.php

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,8 @@
1818

1919
namespace Paynl;
2020

21-
use Paynl\Result\Transaction as Result;
22-
2321
use Paynl\Api\Transaction as Api;
22+
use Paynl\Result\Transaction as Result;
2423

2524
/**
2625
* Description of Transaction
@@ -111,7 +110,7 @@ public static function start($options = array())
111110

112111
if (isset($options['products'])) {
113112
foreach ($options['products'] as $product) {
114-
if(isset($product['tax'])) {
113+
if (isset($product['tax'])) {
115114
$taxClass = Helper::calculateTaxClass($product['price'], $product['tax']);
116115
} else {
117116
$taxClass = 'N';
@@ -215,6 +214,18 @@ public static function start($options = array())
215214
return new Result\Start($result);
216215
}
217216

217+
/**
218+
* Get the transaction in a return script.
219+
* This will automatically load orderId from the get string to fetch the transaction
220+
*
221+
* @return \Paynl\Result\Transaction\Transaction
222+
*/
223+
public static function getForReturn()
224+
{
225+
$transactionId = $_GET['orderId'];
226+
return self::get($transactionId);
227+
}
228+
218229
/**
219230
* Get the transaction
220231
*
@@ -230,18 +241,6 @@ public static function get($transactionId)
230241
return new Result\Transaction($result);
231242
}
232243

233-
/**
234-
* Get the transaction in a return script.
235-
* This will automatically load orderId from the get string to fetch the transaction
236-
*
237-
* @return \Paynl\Result\Transaction\Transaction
238-
*/
239-
public static function getForReturn()
240-
{
241-
$transactionId = $_GET['orderId'];
242-
return self::get($transactionId);
243-
}
244-
245244
/**
246245
* Get the transaction in an exchange script.
247246
* This will work for all kinds of exchange calls (GET, POST AND POST_XML)
@@ -309,17 +308,55 @@ public static function decline($transactionId)
309308
return $result['request']['result'] == 1;
310309
}
311310

312-
public static function capture($transactionId){
311+
public static function capture($transactionId)
312+
{
313313
$api = new Api\Capture();
314314
$api->setTransactionId($transactionId);
315315
$result = $api->doRequest();
316316
return $result['request']['result'] == 1;
317317
}
318318

319-
public static function void($transactionId){
319+
public static function void($transactionId)
320+
{
320321
$api = new Api\Void();
321322
$api->setTransactionId($transactionId);
322323
$result = $api->doRequest();
323324
return $result['request']['result'] == 1;
324325
}
326+
327+
/**
328+
* Create a recurring transaction from an existing transaction
329+
* This is currently only suitable for VISA and MasterCard Ask Pay.nl to activate this option for you.
330+
*
331+
* @param array $options An array that contains the following elements: transactionId (required), amount, description, extra1, extra2, extra3
332+
* @return Result\AddRecurring
333+
*/
334+
public static function addRecurring($options = array())
335+
{
336+
$api = new Api\AddRecurring();
337+
338+
if (isset($options['transactionId'])) {
339+
$api->setTransactionId($options['transactionId']);
340+
}
341+
if (isset($options['amount'])) {
342+
$amount = round($options['amount'] * 100);
343+
$api->setAmount(round($amount));
344+
}
345+
if (isset($options['description'])) {
346+
$api->setDescription($options['description']);
347+
}
348+
if (isset($options['extra1'])) {
349+
$api->setExtra1($options['extra1']);
350+
}
351+
if (isset($options['extra2'])) {
352+
$api->setExtra2($options['extra2']);
353+
}
354+
if (isset($options['extra3'])) {
355+
$api->setExtra3($options['extra3']);
356+
}
357+
358+
$result = $api->doRequest();
359+
360+
return new Result\AddRecurring($result);
361+
}
325362
}

0 commit comments

Comments
 (0)