Skip to content

Commit

Permalink
Added addRecurring API for recurring creditcard payments
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Pieters committed Apr 25, 2017
1 parent ff40358 commit 70a9648
Show file tree
Hide file tree
Showing 4 changed files with 267 additions and 17 deletions.
35 changes: 35 additions & 0 deletions samples/transaction/addRecurring.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?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';

try {
$result = \Paynl\Transaction::addRecurring(array(
'transactionId' => '12345678Xbf1234',
'amount' => 0.01,
'description' => 'Your recurring payment',
'extra1' => 'SDK',
'extra2' => 'extra2',
'extra3' => 'extra3'
));

echo $result->getTransactionId();
} catch (\Paynl\Error\Error $e) {
echo $e->getMessage();
}
141 changes: 141 additions & 0 deletions src/Api/Transaction/AddRecurring.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?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 AddRecurring extends Transaction
{
protected $apiTokenRequired = true;
protected $serviceIdRequired = false;

/**
* @var string
*/
private $transactionId;

private $amount;
private $description;
private $extra1;
private $extra2;
private $extra3;

/**
* @param mixed $amount
*/
public function setAmount($amount)
{
if (!is_numeric($amount)) {
throw new Error\Error('Amount must be numeric');
}
$this->amount = $amount;
}

/**
* @param mixed $description
*/
public function setDescription($description)
{
$this->description = $description;
}

/**
* @param mixed $extra1
*/
public function setExtra1($extra1)
{
$this->extra1 = $extra1;
}

/**
* @param mixed $extra2
*/
public function setExtra2($extra2)
{
$this->extra2 = $extra2;
}

/**
* @param mixed $extra3
*/
public function setExtra3($extra3)
{
$this->extra3 = $extra3;
}

/**
* 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/addRecurring');
}

/**
* 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;

if (isset($this->amount)) {
$this->data['amount'] = $this->amount;
}

if (isset($this->description)) {
$this->data['description'] = $this->description;
}
if (isset($this->extra1)) {
$this->data['extra1'] = $this->extra1;
}
if (isset($this->extra2)) {
$this->data['extra2'] = $this->extra2;
}
if (isset($this->extra3)) {
$this->data['extra3'] = $this->extra3;
}

return parent::getData();
}
}
37 changes: 37 additions & 0 deletions src/Result/Transaction/AddRecurring.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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\Result\Transaction;

use Paynl\Result\Result;

/**
* Result class for a refund
*
* @author Andy Pieters <[email protected]>
*/
class AddRecurring extends Result
{
/**
* @return string The id of the newly created transaction
*/
public function getTransactionId()
{
return $this->data['transactionId'];
}
}
71 changes: 54 additions & 17 deletions src/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@

namespace Paynl;

use Paynl\Result\Transaction as Result;

use Paynl\Api\Transaction as Api;
use Paynl\Result\Transaction as Result;

/**
* Description of Transaction
Expand Down Expand Up @@ -111,7 +110,7 @@ public static function start($options = array())

if (isset($options['products'])) {
foreach ($options['products'] as $product) {
if(isset($product['tax'])) {
if (isset($product['tax'])) {
$taxClass = Helper::calculateTaxClass($product['price'], $product['tax']);
} else {
$taxClass = 'N';
Expand Down Expand Up @@ -215,6 +214,18 @@ public static function start($options = array())
return new Result\Start($result);
}

/**
* Get the transaction in a return script.
* This will automatically load orderId from the get string to fetch the transaction
*
* @return \Paynl\Result\Transaction\Transaction
*/
public static function getForReturn()
{
$transactionId = $_GET['orderId'];
return self::get($transactionId);
}

/**
* Get the transaction
*
Expand All @@ -230,18 +241,6 @@ public static function get($transactionId)
return new Result\Transaction($result);
}

/**
* Get the transaction in a return script.
* This will automatically load orderId from the get string to fetch the transaction
*
* @return \Paynl\Result\Transaction\Transaction
*/
public static function getForReturn()
{
$transactionId = $_GET['orderId'];
return self::get($transactionId);
}

/**
* Get the transaction in an exchange script.
* This will work for all kinds of exchange calls (GET, POST AND POST_XML)
Expand Down Expand Up @@ -309,17 +308,55 @@ public static function decline($transactionId)
return $result['request']['result'] == 1;
}

public static function capture($transactionId){
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){
public static function void($transactionId)
{
$api = new Api\Void();
$api->setTransactionId($transactionId);
$result = $api->doRequest();
return $result['request']['result'] == 1;
}

/**
* Create a recurring transaction from an existing transaction
* This is currently only suitable for VISA and MasterCard Ask Pay.nl to activate this option for you.
*
* @param array $options An array that contains the following elements: transactionId (required), amount, description, extra1, extra2, extra3
* @return Result\AddRecurring
*/
public static function addRecurring($options = array())
{
$api = new Api\AddRecurring();

if (isset($options['transactionId'])) {
$api->setTransactionId($options['transactionId']);
}
if (isset($options['amount'])) {
$amount = round($options['amount'] * 100);
$api->setAmount(round($amount));
}
if (isset($options['description'])) {
$api->setDescription($options['description']);
}
if (isset($options['extra1'])) {
$api->setExtra1($options['extra1']);
}
if (isset($options['extra2'])) {
$api->setExtra2($options['extra2']);
}
if (isset($options['extra3'])) {
$api->setExtra3($options['extra3']);
}

$result = $api->doRequest();

return new Result\AddRecurring($result);
}
}

0 comments on commit 70a9648

Please sign in to comment.