From 70a9648d408218276ebdc02f03e053d40baddf10 Mon Sep 17 00:00:00 2001 From: Andy Pieters Date: Tue, 25 Apr 2017 17:05:26 +0200 Subject: [PATCH] Added addRecurring API for recurring creditcard payments --- samples/transaction/addRecurring.php | 35 ++++++ src/Api/Transaction/AddRecurring.php | 141 ++++++++++++++++++++++++ src/Result/Transaction/AddRecurring.php | 37 +++++++ src/Transaction.php | 71 +++++++++--- 4 files changed, 267 insertions(+), 17 deletions(-) create mode 100644 samples/transaction/addRecurring.php create mode 100644 src/Api/Transaction/AddRecurring.php create mode 100644 src/Result/Transaction/AddRecurring.php diff --git a/samples/transaction/addRecurring.php b/samples/transaction/addRecurring.php new file mode 100644 index 00000000..f04b535d --- /dev/null +++ b/samples/transaction/addRecurring.php @@ -0,0 +1,35 @@ + + * + * 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 . + */ + +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(); +} \ No newline at end of file diff --git a/src/Api/Transaction/AddRecurring.php b/src/Api/Transaction/AddRecurring.php new file mode 100644 index 00000000..a3fadccf --- /dev/null +++ b/src/Api/Transaction/AddRecurring.php @@ -0,0 +1,141 @@ + + * + * 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 . + */ + +namespace Paynl\Api\Transaction; + +use Paynl\Error; + +/** + * Description of Approve + * + * @author Andy Pieters + */ +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(); + } +} \ No newline at end of file diff --git a/src/Result/Transaction/AddRecurring.php b/src/Result/Transaction/AddRecurring.php new file mode 100644 index 00000000..98d70a75 --- /dev/null +++ b/src/Result/Transaction/AddRecurring.php @@ -0,0 +1,37 @@ + + * + * 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 . + */ + +namespace Paynl\Result\Transaction; + +use Paynl\Result\Result; + +/** + * Result class for a refund + * + * @author Andy Pieters + */ +class AddRecurring extends Result +{ + /** + * @return string The id of the newly created transaction + */ + public function getTransactionId() + { + return $this->data['transactionId']; + } +} \ No newline at end of file diff --git a/src/Transaction.php b/src/Transaction.php index 08e9f5b1..5a92ae52 100644 --- a/src/Transaction.php +++ b/src/Transaction.php @@ -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 @@ -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'; @@ -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 * @@ -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) @@ -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); + } }