diff --git a/samples/merchant/addTradeName.php b/samples/merchant/addTradeName.php new file mode 100644 index 00000000..33da6735 --- /dev/null +++ b/samples/merchant/addTradeName.php @@ -0,0 +1,5 @@ + */ -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; + } } \ No newline at end of file diff --git a/src/Api/Merchant/AddTrademark.php b/src/Api/Merchant/AddTrademark.php new file mode 100644 index 00000000..f2999508 --- /dev/null +++ b/src/Api/Merchant/AddTrademark.php @@ -0,0 +1,38 @@ +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' ); + } +} \ No newline at end of file diff --git a/src/Api/Merchant/DeleteTrademark.php b/src/Api/Merchant/DeleteTrademark.php new file mode 100644 index 00000000..48e3dd34 --- /dev/null +++ b/src/Api/Merchant/DeleteTrademark.php @@ -0,0 +1,37 @@ +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' ); + } +} \ No newline at end of file diff --git a/src/Api/Merchant/Info.php b/src/Api/Merchant/Info.php new file mode 100644 index 00000000..53f9f102 --- /dev/null +++ b/src/Api/Merchant/Info.php @@ -0,0 +1,34 @@ +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' ); + } +} \ No newline at end of file diff --git a/src/Api/Merchant/Merchant.php b/src/Api/Merchant/Merchant.php new file mode 100644 index 00000000..3513c5ce --- /dev/null +++ b/src/Api/Merchant/Merchant.php @@ -0,0 +1,13 @@ +setTrademark($tradeName); + $result = $api->doRequest(); + if(!isset($result['result'])){ + throw new Error('Unexpected response'); + } + return $result['result']; + } + + /** + * @param $tradeNameId + * + * @return bool + */ + static function deleteTradeName($tradeNameId){ + $api= new Api\DeleteTrademark(); + + $api->setTrademarkId($tradeNameId); + + $result = $api->doRequest(); + + if(!isset($result['result'])){ + throw new Error('Unexpected response'); + } + return $result['result'] == true; + } + + /** + * @param null $merchantId + * + * @return Result\Merchant\Info + */ + static function info($merchantId = null){ + $api = new Api\Info(); + + if($merchantId !== null) $api->setMerchantId($merchantId); + + $result = $api->doRequest(); + + return new Result\Merchant\Info($result); + } + + + /** + * @param null $merchantId + * + * @return array + */ + static function getTradeNames($merchantId = null){ + return self::info($merchantId)->getTradeNames(); + } +} \ No newline at end of file diff --git a/src/Result/Merchant/Info.php b/src/Result/Merchant/Info.php new file mode 100644 index 00000000..a48d8111 --- /dev/null +++ b/src/Result/Merchant/Info.php @@ -0,0 +1,12 @@ +data['merchant']['tradeNames']; + } +} \ No newline at end of file diff --git a/tests/MerchantTest.php b/tests/MerchantTest.php new file mode 100644 index 00000000..edf336c3 --- /dev/null +++ b/tests/MerchantTest.php @@ -0,0 +1,123 @@ +testApiResult = file_get_contents( $filename ); + + $curl = new \Paynl\Curl\Dummy(); + $curl->setResult( $this->testApiResult ); + + \Paynl\Config::setCurl( $curl ); + } + + public function testAddTradeNameWithoutToken() { + \Paynl\Config::setApiToken( '' ); + + $this->setExpectedException( '\Paynl\Error\Required\ApiToken' ); + + Merchant::addTradeName( 'Some tradename' ); + } + + public function testAddTradeNameEmpty() { + $this->setDummyData( 'addTrademark.json' ); + \Paynl\Config::setApiToken( 'abcdefghijklmnopqrstuvwxys' ); + + $this->setExpectedException( '\Paynl\Error\Required' ); + + Merchant::addTradeName( '' ); + } + + + public function testAddTradeName() { + $this->setDummyData( 'addTrademark.json' ); + \Paynl\Config::setApiToken( 'abcdefghijklmnopqrstuvwxys' ); + + $result = Merchant::addTradeName( 'Some tradename' ); + $this->assertStringMatchesFormat( 'TM-%d-%d', $result ); + } + + public function testAddTradeNameError() { + $this->setDummyData( 'addTrademarkError.json' ); + \Paynl\Config::setApiToken( 'abcdefghijklmnopqrstuvwxys' ); + + $this->setExpectedException( '\Paynl\Error\Api' ); + + Merchant::addTradeName( 'Some tradename' ); + } + + public function testDeleteTradeNameWithoutToken() { + \Paynl\Config::setApiToken( '' ); + + $this->setExpectedException( '\Paynl\Error\Required\ApiToken' ); + + Merchant::deleteTradeName( 'TM-0123-0987' ); + } + + public function testDeleteTradeNameWithoutId() { + \Paynl\Config::setApiToken( 'abcdefghijklmnopqrstuvwxys' ); + + $this->setExpectedException( '\Paynl\Error\Required' ); + + Merchant::deleteTradeName( '' ); + } + + public function testDeleteTradeName() { + $this->setDummyData( 'deleteTrademark.json' ); + \Paynl\Config::setApiToken( 'abcdefghijklmnopqrstuvwxys' ); + + $result = Merchant::deleteTradeName( 'TM-0123-0987' ); + + $this->assertTrue( $result ); + } + + public function testInfoWithoutToken() { + \Paynl\Config::setApiToken( '' ); + + $this->setExpectedException( '\Paynl\Error\Required\ApiToken' ); + + Merchant::info(); + } + + public function testInfoWithoutMerchantError() { + $this->setDummyData( 'infoError.json' ); + \Paynl\Config::setApiToken( 'abcdefghijklmnopqrstuvwxys' ); + + $this->setExpectedException( '\Paynl\Error\Api' ); + Merchant::info(); + } + + public function testInfoWithoutMerchantId() { + $this->setDummyData( 'info.json' ); + \Paynl\Config::setApiToken( 'abcdefghijklmnopqrstuvwxys' ); + + $result = Merchant::info(); + $this->assertInstanceOf( '\Paynl\Result\Merchant\Info', $result ); + } + + public function testInfoWithMerchantId() { + $this->setDummyData( 'info.json' ); + \Paynl\Config::setApiToken( 'abcdefghijklmnopqrstuvwxys' ); + + $result = Merchant::info( 'M-1234-9876' ); + $this->assertInstanceOf( '\Paynl\Result\Merchant\Info', $result ); + } + + public function testGetTradeNames() { + $this->setDummyData( 'info.json' ); + \Paynl\Config::setApiToken( 'abcdefghijklmnopqrstuvwxys' ); + + $result = Merchant::getTradeNames(); + + $this->assertInternalType( 'array', $result ); + foreach ( $result as $tradeName ) { + $this->assertArrayHasKey( 'id', $tradeName ); + $this->assertArrayHasKey( 'name', $tradeName ); + } + + } +} \ No newline at end of file diff --git a/tests/dummyData/Merchant/addTrademark.json b/tests/dummyData/Merchant/addTrademark.json new file mode 100644 index 00000000..c7cde6f6 --- /dev/null +++ b/tests/dummyData/Merchant/addTrademark.json @@ -0,0 +1,3 @@ +{ + "result": "TM-4781-3071" +} \ No newline at end of file diff --git a/tests/dummyData/Merchant/addTrademarkError.json b/tests/dummyData/Merchant/addTrademarkError.json new file mode 100644 index 00000000..30ff1a35 --- /dev/null +++ b/tests/dummyData/Merchant/addTrademarkError.json @@ -0,0 +1,4 @@ +{ + "status": "FALSE", + "error": "Maximum number of trademarks reached" +} \ No newline at end of file diff --git a/tests/dummyData/Merchant/deleteTrademark.json b/tests/dummyData/Merchant/deleteTrademark.json new file mode 100644 index 00000000..bf874eab --- /dev/null +++ b/tests/dummyData/Merchant/deleteTrademark.json @@ -0,0 +1,3 @@ +{ + "result": "1" +} \ No newline at end of file diff --git a/tests/dummyData/Merchant/deleteTrademarkError.json b/tests/dummyData/Merchant/deleteTrademarkError.json new file mode 100644 index 00000000..2b5df5d9 --- /dev/null +++ b/tests/dummyData/Merchant/deleteTrademarkError.json @@ -0,0 +1,4 @@ +{ + "status": "FALSE", + "error": "Trademark not found" +} \ No newline at end of file diff --git a/tests/dummyData/Merchant/info.json b/tests/dummyData/Merchant/info.json new file mode 100644 index 00000000..15528cb7 --- /dev/null +++ b/tests/dummyData/Merchant/info.json @@ -0,0 +1,58 @@ +{ + "request": { + "result": "1", + "errorId": "", + "errorMessage": "" + }, + "merchant": { + "merchantId": "M-0123-4567", + "name": "Demotour Pay.nl", + "publicName": "Classic Carparts", + "website": "http:\/\/www.demotour.pay.nl\/", + "type": "1", + "typeName": "BV (Besloten Vennootschap)", + "cocNumber": "012345678", + "vatNumber": "NL1234567B01", + "iban": "NL35RABO012345678", + "bic": "INGBNL2A", + "bankaccountHolder": "Demotour Pay.nl", + "postalAddress": { + "street": "Voorstraat", + "houseNumber": "2", + "zipCode": "3201BB", + "city": "Spijkenisse", + "countryCode": "NL", + "countryName": "Netherlands" + }, + "visitAddress": { + "street": "Voorstraat", + "houseNumber": "2", + "zipCode": "3201BB", + "city": "Spijkenisse", + "countryCode": "NL", + "countryName": "Netherlands" + }, + "tradeNames": [ + { + "id": "TM-3255-6540", + "name": "Classic Carparts" + }, + { + "id": "TM-6974-7140", + "name": "Demotour Pay.nl" + } + ], + "contactData": [ + { + "type": "email", + "value": "info@webshop.nl", + "description": "klantenservice" + }, + { + "type": "phone", + "value": "012345678", + "description": "algemeen" + } + ] + } +} \ No newline at end of file diff --git a/tests/dummyData/Merchant/infoError.json b/tests/dummyData/Merchant/infoError.json new file mode 100644 index 00000000..36df0a8e --- /dev/null +++ b/tests/dummyData/Merchant/infoError.json @@ -0,0 +1,7 @@ +{ + "request": { + "result": "0", + "errorId": "PAY-109", + "errorMessage": "Not allowed" + } +} \ No newline at end of file