Skip to content

Commit 762cd3b

Browse files
author
Andy Pieters
committed
Added functions to manage tradeNames
Created tests
1 parent ca7b913 commit 762cd3b

File tree

18 files changed

+544
-114
lines changed

18 files changed

+544
-114
lines changed

samples/merchant/addTradeName.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
require_once '../../vendor/autoload.php';
3+
require_once '../config.php';
4+
5+
\Paynl\Merchant::addTradeName('pay.be');
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
require_once '../../vendor/autoload.php';
3+
require_once '../config.php';
4+
5+
$result = \Paynl\Merchant::deleteTradeName('TM-3311-7380');
6+
7+
var_dump($result);

samples/merchant/getTradeNames.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
require_once '../../vendor/autoload.php';
3+
require_once '../config.php';
4+
5+
$tradeNames = \Paynl\Merchant::getTradeNames('M-3421-2120');

samples/merchant/info.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
require_once '../../vendor/autoload.php';
3+
require_once '../config.php';
4+
5+
$result = \Paynl\Merchant::info('M-1234-5678');

src/Api/Api.php

Lines changed: 118 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -27,118 +27,122 @@
2727
*
2828
* @author Andy Pieters <andy@andypieters.nl>
2929
*/
30-
class Api
31-
{
32-
/**
33-
* @var int the version of the api
34-
*/
35-
protected $version = 1;
36-
37-
/**
38-
* @var array
39-
*/
40-
protected $data = array();
41-
42-
/**
43-
* @var bool Is the ApiToken required for this API
44-
*/
45-
protected $apiTokenRequired = false;
46-
/**
47-
* @var bool Is the serviceId required for this API
48-
*/
49-
protected $serviceIdRequired = false;
50-
51-
/**
52-
* @return bool
53-
*/
54-
public function isApiTokenRequired()
55-
{
56-
return $this->apiTokenRequired;
57-
}
58-
59-
/**
60-
* @return bool
61-
*/
62-
public function isServiceIdRequired()
63-
{
64-
return $this->serviceIdRequired;
65-
}
66-
67-
/**
68-
* @return array
69-
* @throws Error\Required\ApiToken
70-
* @throws Error\Required\ServiceId
71-
*/
72-
protected function getData()
73-
{
74-
if($this->isApiTokenRequired()) {
75-
Helper::requireApiToken();
76-
$this->data['token'] = Config::getApiToken();
77-
}
78-
if($this->isServiceIdRequired()){
79-
Helper::requireServiceId();
80-
$this->data['serviceId'] = Config::getServiceId();
81-
}
82-
return $this->data;
83-
}
84-
85-
/**
86-
* @param object|array $result
87-
* @return array
88-
* @throws Error\Api
89-
*/
90-
protected function processResult($result)
91-
{
92-
$output = Helper::objectToArray($result);
93-
94-
if(!is_array($output)){
95-
throw new Error\Api($output);
96-
}
97-
98-
if ($output['request']['result'] != 1 && $output['request']['result'] !== 'TRUE') {
99-
throw new Error\Api($output['request']['errorId'] . ' - ' . $output['request']['errorMessage']);
100-
}
101-
return $output;
102-
}
103-
104-
/**
105-
* @param $endpoint
106-
* @param null|int $version
107-
* @return array
108-
*
109-
* @throws Error\Api
110-
* @throws Error\Error
111-
*/
112-
public function doRequest($endpoint, $version = null)
113-
{
114-
if($version === null){
115-
$version = $this->version;
116-
}
117-
118-
$data = $this->getData();
119-
120-
$uri = Config::getApiUrl($endpoint, (int) $version);
121-
122-
$curl = Config::getCurl();
123-
124-
if(Config::getCAInfoLocation()){
125-
// set a custom CAInfo file
126-
$curl->setOpt(CURLOPT_CAINFO, Config::getCAInfoLocation());
127-
}
128-
129-
$curl->setOpt(CURLOPT_SSL_VERIFYPEER, Config::getVerifyPeer());
130-
131-
$result = $curl->post($uri, $data);
132-
133-
if($curl->error){
134-
if(!empty($result)) {
135-
if ($result->status === 'FALSE') {
136-
throw new Error\Api($result->error);
137-
}
138-
}
139-
throw new Error\Error($curl->errorMessage);
140-
}
141-
142-
return $this->processResult($result);
143-
}
30+
class Api {
31+
/**
32+
* @var int the version of the api
33+
*/
34+
protected $version = 1;
35+
36+
/**
37+
* @var array
38+
*/
39+
protected $data = array();
40+
41+
/**
42+
* @var bool Is the ApiToken required for this API
43+
*/
44+
protected $apiTokenRequired = false;
45+
/**
46+
* @var bool Is the serviceId required for this API
47+
*/
48+
protected $serviceIdRequired = false;
49+
50+
/**
51+
* @param $endpoint
52+
* @param null|int $version
53+
*
54+
* @return array
55+
*
56+
* @throws Error\Api
57+
* @throws Error\Error
58+
*/
59+
public function doRequest( $endpoint, $version = null ) {
60+
if ( $version === null ) {
61+
$version = $this->version;
62+
}
63+
64+
$data = $this->getData();
65+
66+
$uri = Config::getApiUrl( $endpoint, (int) $version );
67+
68+
$curl = Config::getCurl();
69+
70+
if ( Config::getCAInfoLocation() ) {
71+
// set a custom CAInfo file
72+
$curl->setOpt( CURLOPT_CAINFO, Config::getCAInfoLocation() );
73+
}
74+
75+
$curl->setOpt( CURLOPT_SSL_VERIFYPEER, Config::getVerifyPeer() );
76+
77+
$result = $curl->post( $uri, $data );
78+
79+
if ( isset( $result->status ) && $result->status === 'FALSE' ) {
80+
throw new Error\Api( $result->error );
81+
}
82+
83+
if ( $curl->error ) {
84+
throw new Error\Error( $curl->errorMessage );
85+
}
86+
87+
return $this->processResult( $result );
88+
}
89+
90+
/**
91+
* @return array
92+
* @throws Error\Required\ApiToken
93+
* @throws Error\Required\ServiceId
94+
*/
95+
protected function getData() {
96+
if ( $this->isApiTokenRequired() ) {
97+
Helper::requireApiToken();
98+
$this->data['token'] = Config::getApiToken();
99+
}
100+
if ( $this->isServiceIdRequired() ) {
101+
Helper::requireServiceId();
102+
$this->data['serviceId'] = Config::getServiceId();
103+
}
104+
105+
return $this->data;
106+
}
107+
108+
/**
109+
* @return bool
110+
*/
111+
public function isApiTokenRequired() {
112+
return $this->apiTokenRequired;
113+
}
114+
115+
/**
116+
* @return bool
117+
*/
118+
public function isServiceIdRequired() {
119+
return $this->serviceIdRequired;
120+
}
121+
122+
/**
123+
* @param object|array $result
124+
*
125+
* @return array
126+
* @throws Error\Api
127+
*/
128+
protected function processResult( $result ) {
129+
$output = Helper::objectToArray( $result );
130+
131+
if ( ! is_array( $output ) ) {
132+
throw new Error\Api( $output );
133+
}
134+
135+
if ( isset( $output['result'] ) ) {
136+
return $output;
137+
}
138+
139+
if (
140+
isset( $output['request'] ) &&
141+
$output['request']['result'] != 1 &&
142+
$output['request']['result'] !== 'TRUE' ) {
143+
throw new Error\Api( $output['request']['errorId'] . ' - ' . $output['request']['errorMessage'] );
144+
}
145+
146+
return $output;
147+
}
144148
}

src/Api/Merchant/AddTrademark.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
4+
namespace Paynl\Api\Merchant;
5+
6+
7+
8+
use Paynl\Error;
9+
10+
class AddTrademark extends Merchant {
11+
protected $trademark;
12+
13+
/**
14+
* @param string $trademark
15+
*/
16+
public function setTrademark( $trademark ) {
17+
$this->trademark = $trademark;
18+
}
19+
20+
/**
21+
* @inheritdoc
22+
*/
23+
protected function getData() {
24+
if(empty($this->trademark)){
25+
throw new Error\Required('Trade Name is required');
26+
}
27+
$this->data['trademark'] = $this->trademark;
28+
29+
return parent::getData();
30+
}
31+
32+
/**
33+
* @inheritdoc
34+
*/
35+
public function doRequest( $endpoint = null, $version = null ) {
36+
return parent::doRequest( 'Merchant/addTrademark' );
37+
}
38+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
4+
namespace Paynl\Api\Merchant;
5+
6+
7+
use Paynl\Error;
8+
9+
class DeleteTrademark extends Merchant {
10+
protected $trademarkId;
11+
12+
/**
13+
* @param string $trademarkId
14+
*/
15+
public function setTrademarkId( $trademarkId ) {
16+
$this->trademarkId = $trademarkId;
17+
}
18+
19+
/**
20+
* @inheritdoc
21+
*/
22+
protected function getData() {
23+
if(empty($this->trademarkId)){
24+
throw new Error\Required('trademarkId is required');
25+
}
26+
$this->data['trademarkId'] = $this->trademarkId;
27+
28+
return parent::getData();
29+
}
30+
31+
/**
32+
* @inheritdoc
33+
*/
34+
public function doRequest( $endpoint = null, $version = null ) {
35+
return parent::doRequest( 'Merchant/deleteTrademark' );
36+
}
37+
}

src/Api/Merchant/Info.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
4+
namespace Paynl\Api\Merchant;
5+
6+
7+
class Info extends Merchant {
8+
protected $merchantId;
9+
10+
/**
11+
* @param string $merchantId
12+
*/
13+
public function setMerchantId( $merchantId ) {
14+
$this->merchantId = $merchantId;
15+
}
16+
17+
/**
18+
* @inheritdoc
19+
*/
20+
protected function getData() {
21+
if(!empty($this->merchantId)){
22+
$this->data['merchantId'] = $this->merchantId;
23+
}
24+
25+
return parent::getData();
26+
}
27+
28+
/**
29+
* @inheritdoc
30+
*/
31+
public function doRequest( $endpoint = null, $version = null ) {
32+
return parent::doRequest( 'Merchant/info' );
33+
}
34+
}

src/Api/Merchant/Merchant.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
4+
namespace Paynl\Api\Merchant;
5+
6+
7+
use Paynl\Api\Api;
8+
9+
class Merchant extends Api {
10+
protected $version = 2;
11+
protected $apiTokenRequired = true;
12+
13+
}

0 commit comments

Comments
 (0)