Skip to content

Commit

Permalink
Added functions to manage tradeNames
Browse files Browse the repository at this point in the history
Created tests
  • Loading branch information
Andy Pieters committed Nov 1, 2017
1 parent ca7b913 commit 762cd3b
Show file tree
Hide file tree
Showing 18 changed files with 544 additions and 114 deletions.
5 changes: 5 additions & 0 deletions samples/merchant/addTradeName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
require_once '../../vendor/autoload.php';
require_once '../config.php';

\Paynl\Merchant::addTradeName('pay.be');
7 changes: 7 additions & 0 deletions samples/merchant/deleteTradeName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
require_once '../../vendor/autoload.php';
require_once '../config.php';

$result = \Paynl\Merchant::deleteTradeName('TM-3311-7380');

var_dump($result);
5 changes: 5 additions & 0 deletions samples/merchant/getTradeNames.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
require_once '../../vendor/autoload.php';
require_once '../config.php';

$tradeNames = \Paynl\Merchant::getTradeNames('M-3421-2120');
5 changes: 5 additions & 0 deletions samples/merchant/info.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
require_once '../../vendor/autoload.php';
require_once '../config.php';

$result = \Paynl\Merchant::info('M-1234-5678');
232 changes: 118 additions & 114 deletions src/Api/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,118 +27,122 @@
*
* @author Andy Pieters <[email protected]>
*/
class Api
{
/**
* @var int the version of the api
*/
protected $version = 1;

/**
* @var array
*/
protected $data = array();

/**
* @var bool Is the ApiToken required for this API
*/
protected $apiTokenRequired = false;
/**
* @var bool Is the serviceId required for this API
*/
protected $serviceIdRequired = false;

/**
* @return bool
*/
public function isApiTokenRequired()
{
return $this->apiTokenRequired;
}

/**
* @return bool
*/
public function isServiceIdRequired()
{
return $this->serviceIdRequired;
}

/**
* @return array
* @throws Error\Required\ApiToken
* @throws Error\Required\ServiceId
*/
protected function getData()
{
if($this->isApiTokenRequired()) {
Helper::requireApiToken();
$this->data['token'] = Config::getApiToken();
}
if($this->isServiceIdRequired()){
Helper::requireServiceId();
$this->data['serviceId'] = Config::getServiceId();
}
return $this->data;
}

/**
* @param object|array $result
* @return array
* @throws Error\Api
*/
protected function processResult($result)
{
$output = Helper::objectToArray($result);

if(!is_array($output)){
throw new Error\Api($output);
}

if ($output['request']['result'] != 1 && $output['request']['result'] !== 'TRUE') {
throw new Error\Api($output['request']['errorId'] . ' - ' . $output['request']['errorMessage']);
}
return $output;
}

/**
* @param $endpoint
* @param null|int $version
* @return array
*
* @throws Error\Api
* @throws Error\Error
*/
public function doRequest($endpoint, $version = null)
{
if($version === null){
$version = $this->version;
}

$data = $this->getData();

$uri = Config::getApiUrl($endpoint, (int) $version);

$curl = Config::getCurl();

if(Config::getCAInfoLocation()){
// set a custom CAInfo file
$curl->setOpt(CURLOPT_CAINFO, Config::getCAInfoLocation());
}

$curl->setOpt(CURLOPT_SSL_VERIFYPEER, Config::getVerifyPeer());

$result = $curl->post($uri, $data);

if($curl->error){
if(!empty($result)) {
if ($result->status === 'FALSE') {
throw new Error\Api($result->error);
}
}
throw new Error\Error($curl->errorMessage);
}

return $this->processResult($result);
}
class Api {
/**
* @var int the version of the api
*/
protected $version = 1;

/**
* @var array
*/
protected $data = array();

/**
* @var bool Is the ApiToken required for this API
*/
protected $apiTokenRequired = false;
/**
* @var bool Is the serviceId required for this API
*/
protected $serviceIdRequired = false;

/**
* @param $endpoint
* @param null|int $version
*
* @return array
*
* @throws Error\Api
* @throws Error\Error
*/
public function doRequest( $endpoint, $version = null ) {
if ( $version === null ) {
$version = $this->version;
}

$data = $this->getData();

$uri = Config::getApiUrl( $endpoint, (int) $version );

$curl = Config::getCurl();

if ( Config::getCAInfoLocation() ) {
// set a custom CAInfo file
$curl->setOpt( CURLOPT_CAINFO, Config::getCAInfoLocation() );
}

$curl->setOpt( CURLOPT_SSL_VERIFYPEER, Config::getVerifyPeer() );

$result = $curl->post( $uri, $data );

if ( isset( $result->status ) && $result->status === 'FALSE' ) {
throw new Error\Api( $result->error );
}

if ( $curl->error ) {
throw new Error\Error( $curl->errorMessage );
}

return $this->processResult( $result );
}

/**
* @return array
* @throws Error\Required\ApiToken
* @throws Error\Required\ServiceId
*/
protected function getData() {
if ( $this->isApiTokenRequired() ) {
Helper::requireApiToken();
$this->data['token'] = Config::getApiToken();
}
if ( $this->isServiceIdRequired() ) {
Helper::requireServiceId();
$this->data['serviceId'] = Config::getServiceId();
}

return $this->data;
}

/**
* @return bool
*/
public function isApiTokenRequired() {
return $this->apiTokenRequired;
}

/**
* @return bool
*/
public function isServiceIdRequired() {
return $this->serviceIdRequired;
}

/**
* @param object|array $result
*
* @return array
* @throws Error\Api
*/
protected function processResult( $result ) {
$output = Helper::objectToArray( $result );

if ( ! is_array( $output ) ) {
throw new Error\Api( $output );
}

if ( isset( $output['result'] ) ) {
return $output;
}

if (
isset( $output['request'] ) &&
$output['request']['result'] != 1 &&
$output['request']['result'] !== 'TRUE' ) {
throw new Error\Api( $output['request']['errorId'] . ' - ' . $output['request']['errorMessage'] );
}

return $output;
}
}
38 changes: 38 additions & 0 deletions src/Api/Merchant/AddTrademark.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php


namespace Paynl\Api\Merchant;



use Paynl\Error;

class AddTrademark extends Merchant {
protected $trademark;

/**
* @param string $trademark
*/
public function setTrademark( $trademark ) {
$this->trademark = $trademark;
}

/**
* @inheritdoc
*/
protected function getData() {
if(empty($this->trademark)){
throw new Error\Required('Trade Name is required');
}
$this->data['trademark'] = $this->trademark;

return parent::getData();
}

/**
* @inheritdoc
*/
public function doRequest( $endpoint = null, $version = null ) {
return parent::doRequest( 'Merchant/addTrademark' );
}
}
37 changes: 37 additions & 0 deletions src/Api/Merchant/DeleteTrademark.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php


namespace Paynl\Api\Merchant;


use Paynl\Error;

class DeleteTrademark extends Merchant {
protected $trademarkId;

/**
* @param string $trademarkId
*/
public function setTrademarkId( $trademarkId ) {
$this->trademarkId = $trademarkId;
}

/**
* @inheritdoc
*/
protected function getData() {
if(empty($this->trademarkId)){
throw new Error\Required('trademarkId is required');
}
$this->data['trademarkId'] = $this->trademarkId;

return parent::getData();
}

/**
* @inheritdoc
*/
public function doRequest( $endpoint = null, $version = null ) {
return parent::doRequest( 'Merchant/deleteTrademark' );
}
}
34 changes: 34 additions & 0 deletions src/Api/Merchant/Info.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php


namespace Paynl\Api\Merchant;


class Info extends Merchant {
protected $merchantId;

/**
* @param string $merchantId
*/
public function setMerchantId( $merchantId ) {
$this->merchantId = $merchantId;
}

/**
* @inheritdoc
*/
protected function getData() {
if(!empty($this->merchantId)){
$this->data['merchantId'] = $this->merchantId;
}

return parent::getData();
}

/**
* @inheritdoc
*/
public function doRequest( $endpoint = null, $version = null ) {
return parent::doRequest( 'Merchant/info' );
}
}
13 changes: 13 additions & 0 deletions src/Api/Merchant/Merchant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php


namespace Paynl\Api\Merchant;


use Paynl\Api\Api;

class Merchant extends Api {
protected $version = 2;
protected $apiTokenRequired = true;

}
Loading

0 comments on commit 762cd3b

Please sign in to comment.