├── public └── .gitkeep ├── src ├── lang │ └── .gitkeep ├── views │ └── .gitkeep ├── config │ ├── .gitkeep │ └── config.php ├── migrations │ └── .gitkeep ├── controllers │ └── .gitkeep └── Tinyrocket │ └── Magento │ ├── Objects │ ├── Exceptions.php │ ├── MagentoObjectCollection.php │ └── MagentoObject.php │ ├── Facades │ ├── Magento.php │ ├── MagentoSoapClient.php │ └── MagentoSoapStorage.php │ ├── Connections │ ├── Exceptions.php │ ├── MagentoSoapStorage.php │ └── MagentoSoapClient.php │ ├── MagentoServiceProvider.php │ └── Magento.php ├── tests └── .gitkeep ├── .gitignore ├── .travis.yml ├── phpunit.xml ├── composer.json └── README.md /public/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/lang/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/views/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /tests/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/config/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/migrations/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/controllers/.gitkeep: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /vendor 2 | composer.phar 3 | composer.lock 4 | .DS_Store 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.4 5 | - 5.5 6 | - 5.6 7 | - hhvm 8 | 9 | before_script: 10 | - travis_retry composer self-update 11 | - travis_retry composer install --prefer-source --no-interaction --dev 12 | 13 | script: phpunit 14 | -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 15 | ./tests/ 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "tinyrocket/magento", 3 | "description": "A simple Magento Integration using SOAP v1 and v2", 4 | "version": "1.0.0", 5 | "keywords": ["laravel", "magento", "soap"], 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Michael Kyle Martin", 10 | "email": "michael@tinyrocket.co" 11 | } 12 | ], 13 | "require": { 14 | "php": ">=5.4.0", 15 | "illuminate/support": "4.2.*" 16 | }, 17 | "autoload": { 18 | "classmap": [ 19 | "src/migrations", 20 | "src/Tinyrocket/Magento/Connections/Exceptions.php", 21 | "src/Tinyrocket/Magento/Objects/Exceptions.php" 22 | ], 23 | "psr-0": { 24 | "Tinyrocket\\Magento\\": "src/" 25 | } 26 | }, 27 | "extra": { 28 | "branch-alias": { 29 | "dev-master": "1.0.x-dev" 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /src/config/config.php: -------------------------------------------------------------------------------- 1 | [ 20 | 21 | // Default connection 22 | 'default' => [ 23 | 'site_url' => 'http://magentohost', 24 | 'user' => '', 25 | 'key' => '', 26 | 'version' => 'v2' 27 | ], 28 | 29 | // You can add as many connections as you'd like, as long 30 | // as the name (e.g. secondary) is unique 31 | 'secondary' => [ 32 | 'site_url' => 'http://magentohost', 33 | 'user' => '', 34 | 'key' => '', 35 | 'version' => 'v2' 36 | ], 37 | ], 38 | 39 | // enable to see SOAP stack 40 | 'show_stack' => false, 41 | ); -------------------------------------------------------------------------------- /src/Tinyrocket/Magento/Objects/Exceptions.php: -------------------------------------------------------------------------------- 1 | 31 | * @copyright 2014 TinyRocket 32 | * 33 | */ 34 | class MagentoObjectCollectionException extends \InvalidArgumentException {} 35 | class MagentoObjectException extends \InvalidArgumentException {} -------------------------------------------------------------------------------- /src/Tinyrocket/Magento/Facades/Magento.php: -------------------------------------------------------------------------------- 1 | 33 | * @copyright 2014 TinyRocket 34 | * 35 | */ 36 | 37 | class Magento extends Facade { 38 | 39 | /** 40 | * Get the registered name of the component. 41 | * 42 | * @return string 43 | */ 44 | protected static function getFacadeAccessor() 45 | { 46 | return 'magento'; 47 | } 48 | } -------------------------------------------------------------------------------- /src/Tinyrocket/Magento/Connections/Exceptions.php: -------------------------------------------------------------------------------- 1 | 31 | * @copyright 2014 TinyRocket 32 | * 33 | */ 34 | 35 | class ConnectionNotProvidedException extends \InvalidArgumentException {} 36 | 37 | class InvalidConnectionException extends \InvalidArgumentException {} 38 | 39 | class MagentoSoapClientException extends \InvalidArgumentException {} 40 | 41 | class MagentoSoapConfigurationException extends \InvalidArgumentException {} -------------------------------------------------------------------------------- /src/Tinyrocket/Magento/Facades/MagentoSoapClient.php: -------------------------------------------------------------------------------- 1 | 33 | * @copyright 2014 TinyRocket 34 | * 35 | */ 36 | 37 | class MagentoSoapClient extends Facade { 38 | 39 | /** 40 | * Get the registered name of the component. 41 | * 42 | * @return string 43 | */ 44 | protected static function getFacadeAccessor() 45 | { 46 | return 'magento_soap_client'; 47 | } 48 | } -------------------------------------------------------------------------------- /src/Tinyrocket/Magento/Facades/MagentoSoapStorage.php: -------------------------------------------------------------------------------- 1 | 33 | * @copyright 2014 TinyRocket 34 | * 35 | */ 36 | 37 | class MagentoSoapStorage extends Facade { 38 | 39 | /** 40 | * Get the registered name of the component. 41 | * 42 | * @return string 43 | */ 44 | protected static function getFacadeAccessor() 45 | { 46 | return 'magento_soap_storage'; 47 | } 48 | } -------------------------------------------------------------------------------- /src/Tinyrocket/Magento/Connections/MagentoSoapStorage.php: -------------------------------------------------------------------------------- 1 | 35 | * @copyright 2014 TinyRocket 36 | * 37 | */ 38 | 39 | class MagentoSoapStorage { 40 | 41 | /** 42 | * @var services 43 | */ 44 | protected $services; 45 | 46 | /** 47 | * @var primary 48 | */ 49 | protected $primary; 50 | 51 | /** 52 | * Create an instance of available services and primary connection 53 | * 54 | * @return void 55 | */ 56 | public function __construct() 57 | { 58 | $this->primary = \Cache::has('magento_soap_primary') ? \Cache::get('magento_soap_primary') : 'default'; 59 | $this->services = \Cache::has('magento_soap_services') ? \Cache::get('magento_soap_services') : array(); 60 | } 61 | 62 | /** 63 | * Register Connection 64 | * 65 | * @return Tinyrocket\Magento\Connections\MagentoSoapClient 66 | */ 67 | public function add($connection) 68 | { 69 | if ( !array_key_exists(key($connection), $this->services) ) { 70 | $this->services[key($connection)] = $connection[key($connection)]; 71 | \Cache::forever('magento_soap_services', $connection); 72 | } 73 | return $connection; 74 | } 75 | 76 | /** 77 | * Remove Connection 78 | * 79 | * @return Tinyrocket\Magento\Connections\MagentoSoapClient 80 | */ 81 | public function remove($connection) 82 | { 83 | if ( array_key_exists(key($connection), $this->services) ) { 84 | unset($this->services[key($connection)]); 85 | \Cache::forever('magento_soap_services', $this->services); 86 | } 87 | return $connection; 88 | } 89 | 90 | /** 91 | * Serve available connections 92 | * 93 | * @return array 94 | */ 95 | public function services() 96 | { 97 | return $this->services; 98 | } 99 | 100 | /** 101 | * Serve primary connection 102 | * 103 | * @return array 104 | */ 105 | public function primary($name = null) 106 | { 107 | if ( !is_null($name) ) { 108 | \Cache::forever('magento_soap_primary', $name); 109 | $this->primary = \Cache::get('magento_soap_primary'); 110 | } 111 | return $this->primary; 112 | } 113 | } -------------------------------------------------------------------------------- /src/Tinyrocket/Magento/Objects/MagentoObjectCollection.php: -------------------------------------------------------------------------------- 1 | 34 | * @copyright 2014 TinyRocket 35 | * 36 | */ 37 | class MagentoObjectCollection { 38 | 39 | /** 40 | * @var collection - stores items 41 | */ 42 | protected $collection; 43 | 44 | /** 45 | * @var count - total number of collection items 46 | */ 47 | protected $count; 48 | 49 | /** 50 | * Constructor 51 | * 52 | * @see Varien_Object 53 | */ 54 | public function __construct() 55 | { 56 | $args = func_get_args(); 57 | if (empty($args[0])) { 58 | $args[0] = array(); 59 | } 60 | $data = $args[0]; 61 | 62 | if ( is_array($data) ) { 63 | foreach ($data as $item) { 64 | $object = new MagentoObject($item); 65 | $this->collection[] = $object; 66 | } 67 | } 68 | 69 | $this->count = count($this->collection); 70 | } 71 | 72 | /** 73 | * Get Collection 74 | * 75 | * @return Tinyrocket\Magento\Objects\MagentoObjectCollection 76 | */ 77 | public function getCollection() 78 | { 79 | return $this->collection; 80 | } 81 | 82 | /** 83 | * Get First Item 84 | * 85 | * @return MagentoObject 86 | */ 87 | public function getFirstItem() 88 | { 89 | if ( count($this->collection) ) { 90 | return current($this->collection); 91 | } 92 | } 93 | 94 | /** 95 | * Get Functions 96 | * 97 | * Generates a list of available functions for a given object 98 | * based on the keys in it's data collection. 99 | * 100 | * @return mixed 101 | */ 102 | public function getFunctions() 103 | { 104 | if ( $this->getFirstItem() instanceof MagentoObject ) { 105 | return $this->getFirstItem()->getFunctions(); 106 | } 107 | } 108 | 109 | 110 | /** 111 | * Get Count 112 | * 113 | * @return int 114 | */ 115 | public function getCount() 116 | { 117 | return $this->count; 118 | } 119 | } -------------------------------------------------------------------------------- /src/Tinyrocket/Magento/MagentoServiceProvider.php: -------------------------------------------------------------------------------- 1 | 33 | * @copyright 2014 TinyRocket 34 | * 35 | */ 36 | 37 | class MagentoServiceProvider extends ServiceProvider { 38 | 39 | /** 40 | * Indicates if loading of the provider is deferred. 41 | * 42 | * @var bool 43 | */ 44 | protected $defer = false; 45 | 46 | /** 47 | * Bootstrap the application events. 48 | * 49 | * @return void 50 | */ 51 | public function boot() 52 | { 53 | $this->package('tinyrocket/magento'); 54 | 55 | } 56 | 57 | /** 58 | * Register the service provider. 59 | * 60 | * @return void 61 | */ 62 | public function register() 63 | { 64 | $this->registerMagento(); 65 | $this->registerSoapClient(); 66 | $this->registerSoapStorage(); 67 | } 68 | 69 | /** 70 | * Register the Magento Facacde Accessor 71 | * 72 | * @return void 73 | */ 74 | public function registerMagento() 75 | { 76 | $this->app['magento'] = $this->app->share(function($app) 77 | { 78 | return new Magento($app['config']); 79 | }); 80 | } 81 | 82 | /** 83 | * Register the Magento Soap Client 84 | * 85 | * @return void 86 | */ 87 | public function registerSoapClient() 88 | { 89 | $this->app['magento_soap_client'] = $this->app->share(function($app) 90 | { 91 | return new Connections\MagentoSoapClient; 92 | }); 93 | 94 | $this->app->booting(function() 95 | { 96 | $loader = \Illuminate\Foundation\AliasLoader::getInstance(); 97 | $loader->alias('MagentoSoapClient', 'Tinyrocket\Magento\Facades\MagentoSoapClient'); 98 | }); 99 | } 100 | 101 | /** 102 | * Register the Magento Soap Storage 103 | * 104 | * @return void 105 | */ 106 | public function registerSoapStorage() 107 | { 108 | $this->app['magento_soap_storage'] = $this->app->share(function($app) 109 | { 110 | return new Connections\MagentoSoapStorage; 111 | }); 112 | 113 | $this->app->booting(function() 114 | { 115 | $loader = \Illuminate\Foundation\AliasLoader::getInstance(); 116 | $loader->alias('MagentoSoapStorage', 'Tinyrocket\Magento\Facades\MagentoSoapStorage'); 117 | }); 118 | } 119 | 120 | /** 121 | * Get the services provided by the provider. 122 | * 123 | * @return array 124 | */ 125 | public function provides() 126 | { 127 | return array(); 128 | } 129 | 130 | } 131 | -------------------------------------------------------------------------------- /src/Tinyrocket/Magento/Objects/MagentoObject.php: -------------------------------------------------------------------------------- 1 | 33 | * @copyright 2014 TinyRocket 34 | * 35 | */ 36 | class MagentoObject { 37 | 38 | 39 | 40 | /** 41 | * Object attributes 42 | * 43 | * @var array 44 | */ 45 | protected $data = array(); 46 | 47 | /** 48 | * Construct 49 | * 50 | * Set data for the object, if applicable 51 | * 52 | * @return void 53 | */ 54 | public function __construct() 55 | { 56 | $args = func_get_args(); 57 | if (empty($args[0])) { 58 | $args[0] = array(); 59 | } 60 | $this->data = (array)$args[0]; 61 | } 62 | 63 | /** 64 | * Retrieves data from the object 65 | * 66 | * @see Varien_Object 67 | * @return mixed 68 | */ 69 | public function getData($key = null) 70 | { 71 | // Return everything 72 | if (is_null($key)) { 73 | return $this->data; 74 | } 75 | 76 | // Return key 77 | if (isset($this->data[$key])) { 78 | return $this->data[$key]; 79 | }; 80 | 81 | throw new MagentoObjectException("Key [$key] not found for this item"); 82 | } 83 | 84 | /** 85 | * Get Item Id 86 | * 87 | * This assumes that the first item in the array is the PK 88 | * 89 | * @return int 90 | */ 91 | public function getId() 92 | { 93 | if ( isset($this->data) ) { 94 | return current($this->data); 95 | } 96 | } 97 | 98 | /** 99 | * Get Functions 100 | * 101 | * Generates a list of available functions for a given object 102 | * based on the keys in it's data collection. 103 | * 104 | * @return mixed 105 | */ 106 | public function getFunctions() 107 | { 108 | $functions = array(); 109 | if ( is_array($this->getData()) ) { 110 | foreach ( $this->getData() as $key => $value ) { 111 | $functions[] = $this->camelize($key); 112 | } 113 | echo '
';
114 |             print_r($functions);
115 |             echo '
'; 116 | return; 117 | } 118 | } 119 | 120 | /** 121 | * Camelize 122 | * 123 | * @return string 124 | */ 125 | public function camelize($key) 126 | { 127 | return 'get' . implode('', array_map('ucfirst', array_map('strtolower', explode('_', $key)))); 128 | } 129 | 130 | /** 131 | * Set/Get attribute wrapper 132 | * 133 | * @see Varien_Object 134 | * @return mixed 135 | */ 136 | public function __call($method, $args) 137 | { 138 | if (substr($method, 0, 3) == 'get') { 139 | $key = $this->underscore(substr($method,3)); 140 | $data = $this->getData($key, isset($args[0]) ? $args[0] : null); 141 | return $data; 142 | } 143 | } 144 | 145 | /** 146 | * Converts field names for setters and geters 147 | * 148 | * @see Varien_Object 149 | * @return string 150 | */ 151 | protected function underscore($name) 152 | { 153 | return strtolower(preg_replace('/(.)([A-Z])/', "$1_$2", $name)); 154 | } 155 | } 156 | -------------------------------------------------------------------------------- /src/Tinyrocket/Magento/Connections/MagentoSoapClient.php: -------------------------------------------------------------------------------- 1 | 38 | * @copyright 2014 TinyRocket 39 | * 40 | */ 41 | class MagentoSoapClient extends \SoapClient { 42 | 43 | /** 44 | * @var wsdl 45 | */ 46 | protected $wsdl; 47 | 48 | /** 49 | * @var connection 50 | */ 51 | protected $connection; 52 | 53 | /** 54 | * @var client 55 | */ 56 | protected $session; 57 | 58 | /** 59 | * @var results 60 | */ 61 | protected $results; 62 | 63 | /** 64 | * @var debugTrace 65 | */ 66 | protected $debugTrace; 67 | 68 | /** 69 | * Constructor 70 | * 71 | * @return void 72 | */ 73 | public function __construct($connection = null, $options = array('trace' => 1)) 74 | { 75 | if ( !is_null($connection) ) { 76 | try { 77 | $this->connection = $connection[key($connection)]; 78 | $this->wsdl = $this->getConstructedUrl(); 79 | 80 | parent::__construct($this->wsdl, $options); 81 | 82 | $this->session = $this->login($this->connection['user'], $this->connection['key']); 83 | } catch (Exception $e) { 84 | throw new MagentoSoapClientException($e->getMessage()); 85 | } 86 | 87 | } 88 | 89 | } 90 | 91 | /** 92 | * Capture and format SOAP calls for both 93 | * v1 and v2 of the Magento api. 94 | */ 95 | public function __call($method, $args) 96 | { 97 | if ( is_array($args) && count($args) == 1 ) { 98 | $args = current($args); 99 | } 100 | 101 | try { 102 | if ( $method == 'login' ) { 103 | $this->results = $this->__soapCall($method, $args); 104 | } elseif ($method == 'call') { 105 | $this->results = $this->__soapCall($method, $args); 106 | } else { 107 | $this->results = $this->__soapCall($method, array($this->session, $args)); 108 | } 109 | 110 | return $this->getResultsCollections(); 111 | } catch ( \Exception $e) { 112 | throw new MagentoSoapClientException($e->getMessage()); 113 | } 114 | } 115 | 116 | public function call($method, $params) 117 | { 118 | try { 119 | $data = array_merge(array($this->session, $method), array($params)); 120 | return parent::call($data); 121 | } catch (Exception $e) { 122 | throw new MagentoSoapClientException($e->getMessage()); 123 | } 124 | } 125 | 126 | /** 127 | * Get Results Collection 128 | * 129 | * Determines whether to return a formatted collection for 130 | * array returns, a single object for single array returns 131 | * or simply the response, for single var/int returns 132 | * 133 | * @return mixed 134 | */ 135 | public function getResultsCollections() 136 | { 137 | if ( is_array($this->results) && $this->isMulti() ) { 138 | 139 | return new MagentoObjectCollection($this->results); 140 | 141 | } elseif( is_array($this->results) && !$this->isMulti() ) { 142 | 143 | return new MagentoObject($this->results); 144 | 145 | } elseif( is_object($this->results) ) { 146 | 147 | return new MagentoObject($this->results); 148 | 149 | } else { 150 | 151 | return $this->results; 152 | } 153 | } 154 | 155 | /** 156 | * Get Functions 157 | * 158 | * Extension of the __getFunctions method core to SoapClient 159 | * 160 | * @return array 161 | */ 162 | public function getFunctions() 163 | { 164 | return $this->__getFunctions(); 165 | } 166 | 167 | /** 168 | * Get Last Response 169 | * 170 | * Extension of the __getLastResponse method core to SoapClient 171 | * 172 | * @return array 173 | */ 174 | public function getLastResponse() 175 | { 176 | return $this->__getLastResponse(); 177 | } 178 | 179 | /** 180 | * Get Soap Version 181 | * 182 | * Returns the selected version of the SOAP API 183 | * 184 | * @return string 185 | */ 186 | public function getSoapVersion() 187 | { 188 | 189 | $version = isset($this->connection['version']) ? $this->connection['version'] : ''; 190 | 191 | if ( isset($this->connection['version']) && in_array(strtolower($this->connection['version']), $this->getAvailableVersions()) ) { 192 | 193 | // Return the requested version 194 | return strtolower($this->connection['version']); 195 | 196 | } elseif ( !isset($this->connection['version']) || !strlen($this->connection['version']) ) { 197 | 198 | // Fallback to v2 if no version is supplied 199 | return end($this->getAvailableVersions()); 200 | } 201 | throw new MagentoSoapConfigurationException("The supplied version [$version] is invalid. Please check your configuration."); 202 | } 203 | 204 | /** 205 | * Get Applicable Soap Version 206 | * 207 | * Returns an array of possible API versoins 208 | * 209 | * @return string 210 | * @todo add support for XML responses 211 | */ 212 | public function getAvailableVersions() 213 | { 214 | return array('v1', 'v2'); 215 | } 216 | 217 | /** 218 | * Is Multi 219 | * 220 | * Check whether the returned result is a multi-level array 221 | * 222 | * @return bool 223 | */ 224 | protected function isMulti() 225 | { 226 | return isset($this->results[0]); 227 | } 228 | 229 | /** 230 | * Test SOAP Connection 231 | * 232 | * Returns either a boolean reponse or response headers, depending 233 | * on whether $showHeaders is set to true. 234 | * 235 | * @return mixed 236 | */ 237 | public function testConnection($connection, $showHeaders) 238 | { 239 | $testUrl = $this->getConstructedUrl($connection); 240 | try { 241 | file_get_contents($testUrl); 242 | if ( $showHeaders === true ) { 243 | return $http_response_header; 244 | } 245 | $responseCode = array_values($http_response_header)[0]; 246 | return strpos($responseCode, '200 OK') === false ? false : true; 247 | 248 | // No Response returned 249 | } catch ( \Exception $e ) { 250 | if ( $showHeaders === true ) { 251 | return "No response headers. Test failed."; 252 | } 253 | 254 | return false; 255 | } 256 | } 257 | 258 | /** 259 | * Get Magento Version 260 | * 261 | * Returns the Magento build version for either the default 262 | * connection or the connection passed through to the function 263 | * 264 | * @return string 265 | * @example 266 | */ 267 | public function getMagentoVersion() 268 | { 269 | $version = $this->getSoapVersion(); 270 | switch ($version) { 271 | case 'v1': 272 | $response = $this->call('core_magento.info', array()); 273 | break; 274 | 275 | case 'v2': 276 | $response = $this->__call('magentoInfo', array()); 277 | break; 278 | } 279 | 280 | if ( isset($response) ) { 281 | return sprintf('%s %s', $response->getMagentoEdition(), $response->getMagentoVersion()); 282 | } 283 | } 284 | 285 | /** 286 | * Construct URL 287 | * 288 | * Used to created either a Soap V1 or V2 URL for the WSDL 289 | * based on the configuration for the primary connection. 290 | * 291 | * @return string 292 | * @todo modify to work with rewrites 293 | */ 294 | private function getConstructedUrl($connection = null) 295 | { 296 | $url = is_null($connection) ? rtrim($this->connection['site_url'], '/') : rtrim($connection[key($connection)]['site_url'], '/'); 297 | $version = is_null($connection) ? strtolower($this->connection['version']) : strtolower($connection[key($connection)]['version']); 298 | 299 | switch ($version) { 300 | case 'v1': 301 | return sprintf("%s/api/soap/?wsdl", $url); 302 | break; 303 | 304 | case 'v2': 305 | return sprintf("%s/api/v2_soap?wsdl", $url); 306 | break; 307 | 308 | default: 309 | return sprintf("%s/api/v2_soap?wsdl", $url); 310 | break; 311 | } 312 | } 313 | 314 | public function __destruct() 315 | { 316 | 317 | } 318 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Magento SOAP Integration for Laravel 2 | =========================== 3 | 4 | A simple and somewhat intuitive package for managing and interacting with the Magento SOAP Api. Compatible with Laravel 4 and Magneto SOAP v1 & v2. 5 | 6 | [![Build Status](https://travis-ci.org/TinyRocket/laravel-magento-integration.svg?branch=master)](https://travis-ci.org/TinyRocket/laravel-magento-integration) [![Latest Stable Version](https://poser.pugx.org/tinyrocket/magento/v/stable.svg)](https://packagist.org/packages/tinyrocket/magento) [![Total Downloads](https://poser.pugx.org/tinyrocket/magento/downloads.svg)](https://packagist.org/packages/tinyrocket/magento) [![Latest Unstable Version](https://poser.pugx.org/tinyrocket/magento/v/unstable.svg)](https://packagist.org/packages/tinyrocket/magento) [![License](https://poser.pugx.org/tinyrocket/magento/license.svg)](https://packagist.org/packages/tinyrocket/magento) 7 | 8 | > **Note:** This package is still in the beta phase of development. I'd advise against using to test against a production Magento application. This package is for Laravel 4, if you're looking for a Laravel 3 version of this package, checkout [MatteoCastiglioni's bundle](https://github.com/MatteoCastiglioni/magentoapi) 9 | 10 | 11 | ### Installation 12 | 13 | To install via composer, add the following to your requirements 14 | 15 | "require": { 16 | ... 17 | "tinyrocket/magento": "1.0.*" 18 | ... 19 | }, 20 | **Note:** You may need to change your **minimum-stability** to **dev** 21 | 22 | ### Configuration 23 | 24 | Add the following to your Laravel Application configuration (app/config/app.php) 25 | 26 | To your **providers** array 27 | 28 | 29 | 'providers' => array( 30 | ... 31 | 'Tinyrocket\Magento\MagentoServiceProvider', 32 | ... 33 | ), 34 | 35 | and to your **aliases** array 36 | 37 | 38 | 'aliases' => array( 39 | ... 40 | 'Magento' => 'Tinyrocket\Magento\Facades\Magento', 41 | ... 42 | ), 43 | 44 | Publish the the package configuration file by running the following in CLI 45 | 46 | php artisan config:publish tinyrocket/magento 47 | 48 | #### Setting up the SOAP connections 49 | 50 | The quickest way to get started with Magento integration is to add your connection(s) to the newly published configuration file. The file can be found in 51 | 52 | app/config/packages/tinyrocket/magento/config.php 53 | 54 | There is no limit to the amount of connections that you can save, but you should set a default configuration to handle fallbacks. Inside of the configuration file, set the following with your Magento SOAP information. 55 | 56 | **Note:** Use your store's base URL, not the URL for the SOAP endpoint. (e.g. http://magentostore.com rather than http://magentostore.com/api/soap?wsdl) 57 | 58 | 'connections' => [ 59 | ... 60 | 'default' => [ 61 | 'site_url' => 'http://magentohost', 62 | 'user' => '', 63 | 'key' => '', 64 | 'version' => 'v2' 65 | ], 66 | ... 67 | ] 68 | 69 | The first parameter defines the name of the connection and should be unique. The API version is optional and will default to v2 for all connections, unless set otherwise. 70 | 71 | ##Usage 72 | 73 | An exhaustive list of possible methods is available on the [Magento Website](http://www.magentocommerce.com/api/soap/) 74 | 75 | There are two basic methods used to interact with the SOAP Api 76 | 77 | For **SOAP v2** 78 | 79 | Magento::call()->someSoapRequest($requestParams) 80 | 81 | // Example 82 | Magento::call()->customerCustomerList() 83 | 84 | For **SOAP v1** 85 | 86 | Magento::get('some.method', array($requestParams)) 87 | 88 | //Example 89 | Magento::get('customer.list') 90 | 91 | Alternatively, you can call methods directly for **SOAP v2** requests 92 | 93 | Magento::someSoapRequest($requestParams) 94 | 95 | // Example 96 | $customerParams = array( 97 | 'email' => 'customer@example.org', 98 | 'firstname' => 'Dough', 99 | 'lastname' => 'Deeks', 100 | 'password' => 'password', 101 | 'website_id' => 1, 102 | 'store_id' => 1, 103 | 'group_id' => 1 104 | ); 105 | $customerId = Magento::customerCustomerCreate($customerParams) 106 | 107 | 108 | ###Working with the results 109 | 110 | To make working with the SOAP API slightly more intuitive, some request results are returned as data objects and collections, inspired by **Varien_Object** and **Varien_Object_Collection** classes in Magento. These classes allow for the calling of information with some basic methods. 111 | 112 | ####Objects Collections 113 | For SOAP responses that return a group of items, results are returned as object collections containing individual objects. These collections have four basic methods. 114 | 115 | **getCollection()** - Returns all items as a group of objects 116 | 117 | **getCount()** - Returns number of items in the collection 118 | 119 | **getFirstItem()** - Returns the first response item 120 | 121 | **getLastItem()** - Returns the last response item 122 | 123 | foreach ( Magento::salesOrderList()->getCollection() as $order ) { 124 | // Do stuff 125 | } 126 | --- 127 | ####Objects 128 | For SOAP responses that return a single array item, or when iterating through a response collection, the MagnetoObject is used. This object comes with a couple methods that should be familiar to a Magento developer 129 | 130 | **getData(optional $key)** - Either returns all of an objects values or a single value that matches the provided key. 131 | 132 | **getId()** - Returns the primary key of a given response object 133 | 134 | Like a **Varien_Object** you can also use a magic getter to pull information from an object. For example, you can use the following two methods to return the incrementId of an order object 135 | 136 | foreach ( Magento::salesOrderList()->getCollection() as $order ) { 137 | 138 | // with data 139 | echo $order->getData('increment_id') 140 | 141 | // with magic getter 142 | echo $order->getIncrementId() 143 | } 144 | 145 | --- 146 | ####Single Results 147 | For SOAP responses that return a single value or boolean, results are returned as a string/integer/boolean 148 | 149 | // will return an integer 150 | echo Magento::customerCustomerCreate($customerParams) 151 | 152 | ###Working multiple connections 153 | You have the option to use connections to multiple Magento websites. This can be done by either adding a secondary connection to the package configuration or by creating a connection on the fly. Connections are stored, so once registered, you can continue to use that connection by referencing it's unique identifier or the newly created connection object. 154 | 155 | To create a connection on the fly 156 | 157 | $connection = Magento::createConnection($name, $url, $user, $key, $version) 158 | 159 | To use a stored connection 160 | 161 | // SOAP v2 162 | $orders = Magento::call($connection)->salesOrderList(); 163 | $orders = Magento::call('unique_identifier')->salesOrderList(); 164 | 165 | // SOAP v1 166 | $customers = Magento::get('customers.list', null, $connection) 167 | $customers = Magento::get('customers.list', null, 'unique_identifier') 168 | 169 | To register a connection programmatically 170 | 171 | Magento::createAndRegisterConnection($name, $url, $user, $key, $version) 172 | 173 | To see a list of available connections 174 | 175 | print_r( Magento::getAvailableConnections() ) 176 | 177 | **Setting a primary connection** 178 | Inherently, unless explicitly passed to a SOAP call, the default connection found in the configuration file is used when making all calls. To change which connection is used by default, you can use 179 | 180 | Magento::setPrimaryConnection('unique_identifier') 181 | 182 | // Then use for subsequent calls 183 | Magento::salesOrderList() 184 | 185 | To see the currently primary connection 186 | 187 | echo Magento::getPrimaryConnection() 188 | 189 | To remove a connection from memory 190 | 191 | Magento::unregister('unique_identifier') 192 | 193 | To create a temporary connection 194 | 195 | $connection = Magento::createAndForgetConnection($name, $url, $user, $key, $version) 196 | 197 | // Then reference it in the call 198 | $orders = Magento::call($connection)->salesOrderList() 199 | 200 | ###Helpers 201 | 202 | **Getting Magic Getters for an object/collection** 203 | Though used just to return data, you can use the following to get a list of available functions for a given object or collection 204 | 205 | // For a collection 206 | echo Magento::call()->customerCustomerList()->getFunctions(); 207 | 208 | // For an object 209 | $customer->getFunctions(); 210 | 211 | This will return something like 212 | 213 | Array 214 | ( 215 | [0] => getCustomerId 216 | [1] => getCreatedAt 217 | [2] => getUpdatedAt 218 | [3] => getStoreId 219 | [4] => getWebsiteId 220 | [5] => getCreatedIn 221 | [6] => getEmail 222 | [7] => getFirstname 223 | [8] => getLastname 224 | [9] => getGroupId 225 | [10] => getDob 226 | [11] => getPasswordHash 227 | ) 228 | 229 | **Test your SOAP connection** 230 | Returns a boolean by default, but can return headers by flagging the second parameter as true. 231 | 232 | var_dump( Magento::testConnection('unique_identifier', $returnHeaders = false) ) 233 | 234 | **Get Magento Version** 235 | Returns the build version of either the default connection or the one passed 236 | 237 | // Example return Community 1.9.0.0 238 | var_dump( Magento::getMagentoVersion(optional $connection) ) 239 | 240 | ###XML Support 241 | Currently, there is no support of XML request and responses. But, it's planned for future releases. 242 | 243 | ###WS-I Compliance Mode 244 | Currently, there is no way enforce WS-I Compliance Mode. 245 | -------------------------------------------------------------------------------- /src/Tinyrocket/Magento/Magento.php: -------------------------------------------------------------------------------- 1 | 41 | * @copyright 2014 TinyRocket 42 | * 43 | */ 44 | 45 | class Magento { 46 | 47 | /** 48 | * @var connections 49 | */ 50 | private $connections; 51 | 52 | /** 53 | * @var client 54 | */ 55 | protected $client; 56 | 57 | /** 58 | * @var forgets 59 | */ 60 | protected $forgets; 61 | 62 | /** 63 | * Construct Magento Instance 64 | * 65 | * @return void 66 | */ 67 | public function __construct(Repository $config) 68 | { 69 | $this->connections = $config->get('magento::connections'); 70 | if ( is_array($this->connections) ) { 71 | $this->batchRegister($this->connections); 72 | } 73 | } 74 | 75 | /** 76 | * __call Magic Method 77 | * 78 | * Allows for SOAP v2 request to be ran directly against 79 | * the class rather than using the 'call' method 80 | * 81 | * @return void 82 | */ 83 | public function __call($method, $args) 84 | { 85 | if ( in_array($method, get_class_methods($this)) ) { 86 | call_user_func($method, $args); 87 | } else { 88 | $clientExecutable = $this->call(); 89 | return call_user_func_array(array($clientExecutable, $method), $args); 90 | } 91 | } 92 | 93 | /** 94 | * Call SOAP V2 Method 95 | */ 96 | public function call($connection = null) 97 | { 98 | if ( !is_array($connection) or is_null($connection) ) { 99 | $connection = !is_null($connection) ? $this->getConnection($connection) : $this->getPrimaryConnection(); 100 | } 101 | return new MagentoSoapClient($connection); 102 | } 103 | 104 | /** 105 | * Get Soap V1 Method 106 | */ 107 | public function get($method, $params = array(), $connection = null) 108 | { 109 | $connection = !is_null($connection) ? $this->getConnection($connection) : $this->getPrimaryConnection(); 110 | $soap = new MagentoSoapClient($connection); 111 | if ( (isset($connection[key($connection)]['version']) && (strtolower($connection[key($connection)]['version']) == 'v1') ) ) { 112 | return $soap->call($method, $params); 113 | } else { 114 | return $soap->__call($method, $params); 115 | } 116 | } 117 | 118 | /** 119 | * Use SOAP Method 120 | */ 121 | public function connection($connection = null) 122 | { 123 | if ( !is_null($connection) ) { 124 | return new MagentoSoapClient($this->getConnection($connection)); 125 | } 126 | throw new MagentoSoapClientException("This [connection] paramenter cannot be left blank"); 127 | } 128 | 129 | /** 130 | * Create Connection 131 | * 132 | * Allows a user to create a connection on the fly 133 | * 134 | * @return array 135 | */ 136 | public function createConnection($name, $url, $user, $key, $version = null) 137 | { 138 | if ( !in_array($name, $this->getConnections()) ) { 139 | return array($name => array( 140 | 'site_url' => $url, 141 | 'user' => $user, 142 | 'key' => $key, 143 | 'version' => $version, 144 | )); 145 | } 146 | throw new MagentoSoapClientException("Connection [$name] already exists. Please choose a different identifier"); 147 | } 148 | 149 | /** 150 | * Create And Register Connection 151 | * 152 | * Allows a user to create and register a connection on the fly 153 | * 154 | * @return array 155 | */ 156 | public function createAndRegisterConnection($name, $url, $user, $key, $version = null) 157 | { 158 | return $this->register($this->createConnection($name, $url, $user, $key, $version), false); 159 | } 160 | 161 | /** 162 | * Create a temporary connection 163 | * 164 | * Allows a user to create a temporary connection on the fly 165 | * 166 | * @return array 167 | */ 168 | public function createAndForgetConnection($name, $url, $user, $key, $version = null) 169 | { 170 | if ( $this->forgets[] = $name ) { 171 | return $this->register($this->createConnection($name, $url, $user, $key, $version), false); 172 | } 173 | } 174 | 175 | /** 176 | * Get Connections 177 | * 178 | * @return array 179 | */ 180 | public function getConnections() 181 | { 182 | return $this->connections; 183 | } 184 | 185 | /** 186 | * Get Services 187 | */ 188 | public function getAvailableConnections() 189 | { 190 | return \MagentoSoapStorage::services(); 191 | } 192 | 193 | /** 194 | * Get Primary Connection 195 | * 196 | * @return Tinyrocket\Magento\Connections\MagentoSoapClient 197 | */ 198 | public function getPrimaryConnection() 199 | { 200 | return $this->getConnection(\MagentoSoapStorage::primary()); 201 | } 202 | 203 | /** 204 | * Test SOAP Connection 205 | * 206 | * Returns either a boolean reponse or response headers, depending 207 | * on whether $showHeaders is set to true. 208 | * 209 | * @return mixed 210 | */ 211 | public function testConnection($connection = null, $showHeaders = false) 212 | { 213 | if ( !is_null($connection) ) { 214 | $connection = is_array($connection) ? $connection : $this->getConnection($connection, false); 215 | return \MagentoSoapClient::testConnection($connection, $showHeaders); 216 | } 217 | throw new InvalidConnectionException("No connection provided to test. Please provide a connection object or identifier"); 218 | } 219 | 220 | /** 221 | * Get Magento Version 222 | * 223 | * Returns the Magento build version for either the default 224 | * connection or the connection passed through to the function 225 | * 226 | * @return string 227 | * @example 228 | */ 229 | public function getMagentoVersion($connection = null) 230 | { 231 | $connection = is_array($connection) ? $connection : $this->getConnection($connection); 232 | $temporaryClient = new MagentoSoapClient($connection); 233 | return $temporaryClient->getMagentoVersion(); 234 | } 235 | 236 | /** 237 | * Get Primary Connection 238 | * 239 | * @return Tinyrocket\Magento\Connections\MagentoSoapClient 240 | */ 241 | public function setPrimaryConnection($name) 242 | { 243 | return $this->getConnection(\MagentoSoapStorage::primary($name)); 244 | } 245 | 246 | /** 247 | * Get Connection 248 | * 249 | * @return connection array 250 | */ 251 | public function getConnection($identifier, $useDefault = true) 252 | { 253 | if ( is_array($identifier) ) { 254 | $identifier = key($identifier); 255 | } 256 | 257 | // Fallback if applicable 258 | if ( $useDefault ) { 259 | $identifier = array_key_exists($identifier, $this->connections) ? $identifier : 'default'; 260 | } 261 | 262 | // Return Connection 263 | if ( isset($this->connections[$identifier]) ) { 264 | return array($identifier => $this->connections[$identifier]); 265 | } 266 | throw new InvalidConnectionException("Connection [$identifier] not found. No default configuration found."); 267 | } 268 | 269 | /** 270 | * Register Connection 271 | * 272 | * @return Tinyrocket\Magento\Connections\MagentoSoapClient 273 | */ 274 | public function register($connection, $return = true, $forget = false) 275 | { 276 | try { 277 | $connection = is_array($connection) ? $connection : $this->getConnection($connection); 278 | \MagentoSoapStorage::add($connection); 279 | 280 | if ( true === $forget ) { 281 | $this->forgets[] = key($connection); 282 | } 283 | 284 | return $return ? $this->call($connection) : null; 285 | } catch (Exception $e) { 286 | throw new MagentoSoapClientException($e->getMessage()); 287 | } 288 | } 289 | 290 | /** 291 | * Register Connection 292 | * 293 | * @return Tinyrocket\Magento\Connections\MagentoSoapClient 294 | */ 295 | public function batchRegister($connections) 296 | { 297 | try { 298 | foreach ( $connections as $connection => $data ) { 299 | \MagentoSoapStorage::add(array($connection => $data)); 300 | } 301 | } catch (Exception $e) { 302 | throw new MagentoSoapClientException($e->getMessage()); 303 | } 304 | } 305 | 306 | /** 307 | * Get Functions 308 | * 309 | * Extension of the __getFunctions method core to SoapClient 310 | * 311 | * @return array 312 | */ 313 | public function getFunctions($connection = null) 314 | { 315 | return $this->call()->getFunctions(); 316 | } 317 | 318 | /** 319 | * Unregister Connection 320 | * 321 | * @return void 322 | */ 323 | public function unregister($connection) 324 | { 325 | $connection = is_array($connection) ? $connection : $this->getConnection($connection); 326 | \MagentoSoapStorage::remove($this->getConnection($connection)); 327 | } 328 | 329 | 330 | /** 331 | * Has Default Connection 332 | * 333 | * @return bool 334 | */ 335 | public function hasDefaultConnection() 336 | { 337 | return array_key_exists('default', $this->getConnections()); 338 | } 339 | 340 | /** 341 | * Deconstructor 342 | * 343 | * @return void 344 | */ 345 | public function __destruct() { 346 | if ( !is_null($this->forgets) && is_array($this->forgets) ) { 347 | foreach ( $this->forgets as $removeKey ) { 348 | $this->unregister($removeKey); 349 | } 350 | 351 | $this->forgets = null; 352 | } 353 | } 354 | } --------------------------------------------------------------------------------