Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Pieters committed Jul 19, 2017
1 parent cc2583e commit af3e577
Show file tree
Hide file tree
Showing 178 changed files with 3,289 additions and 0 deletions.
116 changes: 116 additions & 0 deletions upload/Pay/Api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

class Pay_Api {

const REQUEST_TYPE_POST = 1;
const REQUEST_TYPE_GET = 0;

protected $_apiUrl = 'https://rest-api.pay.nl';
protected $_version = 'v3';
protected $_controller = '';
protected $_action = '';
protected $_serviceId = '';
protected $_apiToken = '';
protected $_requestType = self::REQUEST_TYPE_POST;
protected $_postData = array();


/**
* Set the serviceid
* The serviceid always starts with SL- and can be found on: https://admin.pay.nl/programs/programs
*
* @param string $serviceId
*/
public function setServiceId($serviceId) {
$this->_serviceId = $serviceId;
}

/**
* Set the API token
* The API token is used to identify your company.
* The API token can be found on: https://admin.pay.nl/my_merchant on the bottom
*
* @param string $apiToken
*/
public function setApiToken($apiToken) {
$this->_apiToken = $apiToken;
}

protected function _getPostData() {

return $this->_postData;
}

protected function _processResult($data) {
return $data;
}

private function _getApiUrl() {
if ($this->_version == '') {
throw new Pay_Exception('version not set', 1);
}
if ($this->_controller == '') {
throw new Pay_Exception('controller not set', 1);
}
if ($this->_action == '') {
throw new Pay_Exception('action not set', 1);
}

return $this->_apiUrl . '/' . $this->_version . '/' . $this->_controller . '/' . $this->_action . '/json/';
}

public function getPostData(){
return $this->_getPostData();
}
public function doRequest() {
if ($this->_getPostData()) {

$url = $this->_getApiUrl();
$data = $this->_getPostData();

$strData = http_build_query($data);

$apiUrl = $url;

$ch = curl_init();
if ($this->_requestType == self::REQUEST_TYPE_GET) {
$apiUrl .= '?' . $strData;
} else {
curl_setopt($ch, CURLOPT_POSTFIELDS, $strData);
}


curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

$result = curl_exec($ch);


if ($result == false) {
$error = curl_error($ch);
throw new Pay_Api_Exception("Curl error: ".$error);
}
curl_close($ch);

$arrResult = json_decode($result, true);

if ($this->validateResult($arrResult)) {
return $this->_processResult($arrResult);
}
}
}

protected function validateResult($arrResult) {
if ($arrResult['request']['result'] == 1) {
return true;
} else {
if(isset($arrResult['request']['errorId']) && isset($arrResult['request']['errorMessage']) ){
throw new Pay_Api_Exception($arrResult['request']['errorId'] . ' - ' . $arrResult['request']['errorMessage']);
} elseif(isset($arrResult['error'])){
throw new Pay_Api_Exception($arrResult['error']);
} else {
throw new Pay_Api_Exception('Unexpected api result');
}
}
}
}
4 changes: 4 additions & 0 deletions upload/Pay/Api/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
class Pay_Api_Exception extends Pay_Exception{

}
65 changes: 65 additions & 0 deletions upload/Pay/Api/Getservice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

class Pay_Api_Getservice extends Pay_Api {

protected $_version = 'v3';
protected $_controller = 'transaction';
protected $_action = 'getService';

protected function _getPostData() {
$data = parent::_getPostData();

// Checken of alle verplichte velden geset zijn
if ($this->_apiToken == '') {
throw new Pay_Exception('apiToken not set', 1);
} else {
$data['token'] = $this->_apiToken;
}
if (empty($this->_serviceId)) {
throw new Pay_Exception('serviceId not set', 1);
} else {
$data['serviceId'] = $this->_serviceId;
}
return $data;
}
protected function _processResult($arrReturn) {
if (!$arrReturn['request']['result']) {
return $arrReturn;
}

$arrReturn['paymentOptions'] = array();

$countryOptionList = $arrReturn['countryOptionList'];
unset($arrReturn['countryOptionList']);
if (isset($countryOptionList) && is_array($countryOptionList)) {
foreach ($countryOptionList AS $strCountrCode => $arrCountry) {
foreach ($arrCountry['paymentOptionList'] AS $arrPaymentProfile) {

if (!isset($arrReturn['paymentOptions'][$arrPaymentProfile['id']])) {
$arrReturn['paymentOptions'][$arrPaymentProfile['id']] = array(
'id' => $arrPaymentProfile['id'],
'name' => $arrPaymentProfile['name'],
'visibleName' => $arrPaymentProfile['name'],
'img' => $arrPaymentProfile['img'],
'path' => $arrPaymentProfile['path'],
'paymentOptionSubList' => array(),
'countries' => array(),
);
}

if (!empty($arrPaymentProfile['paymentOptionSubList'])) {
$arrReturn['paymentOptions'][$arrPaymentProfile['id']]['paymentOptionSubList'] = $arrPaymentProfile['paymentOptionSubList'];
}


$arrReturn['paymentOptions'][$arrPaymentProfile['id']]['countries'][$strCountrCode] = array(
'id' => $strCountrCode,
'name' => $arrCountry['visibleName'],
);
}
}
}
return $arrReturn;
}

}
24 changes: 24 additions & 0 deletions upload/Pay/Api/Info.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

class Pay_Api_Info extends Pay_Api {

protected $_version = 'v3';
protected $_controller = 'transaction';
protected $_action = 'info';

public function setTransactionId($transactionId){
$this->_postData['transactionId'] = $transactionId;
}
protected function _getPostData() {
$data = parent::_getPostData();
if ($this->_apiToken == '') {
throw new Pay_Exception('apiToken not set', 1);
} else {
$data['token'] = $this->_apiToken;
}
if(!isset($this->_postData['transactionId'])){
throw new Pay_Exception('transactionId is not set', 1);
}
return $data;
}
}
Loading

0 comments on commit af3e577

Please sign in to comment.