3 use PayPal\Core\PPLoggingManager;
4 use PayPal\Formatter\FormatterFactory;
5 use PayPal\Core\PPRequest;
6 use PayPal\Core\PPHttpConfig;
7 use PayPal\Handler\PPAuthenticationHandler;
8 use PayPal\Auth\PPTokenAuthorization;
14 public $options = array();
17 private $handlers = array();
18 private $serviceBinding;
21 public function __construct($port, $serviceName, $serviceBinding, $handlers=array(), $config) {
23 $this->config = $config;
24 $this->serviceName = $serviceName;
27 $this->logger = new PPLoggingManager(__CLASS__, $this->config);
28 $this->handlers = $handlers;
29 $this->serviceBinding = $serviceBinding;
33 public function setServiceName($serviceName) {
34 $this->serviceName = $serviceName;
37 public function addHandler($handler) {
38 $this->handlers[] = $handler;
41 public function makeRequest($apiMethod, $params, $apiUsername = null) {
43 $this->apiMethod = $apiMethod;
44 if(is_string($apiUsername) || is_null($apiUsername)) {
45 // $apiUsername is optional, if null the default account in config file is taken
46 $credMgr = PPCredentialManager::getInstance($this->config);
47 $apiCredential = clone($credMgr->getCredentialObject($apiUsername ));
49 $apiCredential = $apiUsername; //TODO: Aargh
51 if((isset($this->config['accessToken']) && isset($this->config['tokenSecret']))) {
52 $apiCredential->setThirdPartyAuthorization(
53 new PPTokenAuthorization($this->config['accessToken'], $this->config['tokenSecret']));
57 $request = new PPRequest($params, $this->serviceBinding);
58 $request->setCredential($apiCredential);
59 $httpConfig = new PPHttpConfig(null, PPHttpConfig::HTTP_POST);
60 $this->runHandlers($httpConfig, $request);
62 $formatter = FormatterFactory::factory($this->serviceBinding);
63 $payload = $formatter->toString($request);
64 $connection = PPConnectionManager::getInstance()->getConnection($httpConfig, $this->config);
65 $this->logger->info("Request: $payload");
66 $response = $connection->execute($payload);
67 $this->logger->info("Response: $response");
69 return array('request' => $payload, 'response' => $response);
72 private function runHandlers($httpConfig, $request) {
76 foreach($this->handlers as $handlerClass) {
77 $handler = new $handlerClass();
78 $handler->handle($httpConfig, $request, $this->options);
80 $handler = new PPAuthenticationHandler();
81 $handler->handle($httpConfig, $request, $this->options);
84 private function getOptions()
86 $this->options['port'] = $this->port;
87 $this->options['serviceName'] = $this->serviceName;
88 $this->options['serviceBinding'] = $this->serviceBinding;
89 $this->options['config'] = $this->config;
90 $this->options['apiMethod'] = $this->apiMethod;