876b408908cca5b371f08413c6dfd074bb361cae
[WP-Plugins/glm-member-db.git] /
1 <?php
2 namespace PayPal\Core;
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;
9
10 class PPAPIService {
11
12         public $endpoint;
13         public $config;
14         public $options = array();
15         public $serviceName;
16         private $logger;
17         private $handlers = array();
18         private $serviceBinding;
19         private $port;
20         private $apiMethod;
21         public function __construct($port, $serviceName, $serviceBinding, $handlers=array(), $config) {
22                 
23                 $this->config = $config;
24                 $this->serviceName = $serviceName;
25                 $this->port = $port;
26
27                 $this->logger = new PPLoggingManager(__CLASS__, $this->config);
28                 $this->handlers = $handlers;
29                 $this->serviceBinding = $serviceBinding;
30                 
31         }
32
33         public function setServiceName($serviceName) {
34                 $this->serviceName = $serviceName;
35         }
36
37         public function addHandler($handler) {
38                 $this->handlers[] = $handler;
39         }
40
41         public function makeRequest($apiMethod, $params, $apiUsername = null) {
42                 
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 ));
48                 } else {
49                         $apiCredential = $apiUsername; //TODO: Aargh
50                 }
51             if((isset($this->config['accessToken']) && isset($this->config['tokenSecret']))) {
52                         $apiCredential->setThirdPartyAuthorization(
53                                         new PPTokenAuthorization($this->config['accessToken'], $this->config['tokenSecret']));
54                 }
55
56
57                 $request = new PPRequest($params, $this->serviceBinding);
58                 $request->setCredential($apiCredential);
59                 $httpConfig = new PPHttpConfig(null, PPHttpConfig::HTTP_POST);
60                 $this->runHandlers($httpConfig, $request);
61
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");
68
69                 return array('request' => $payload, 'response' => $response);
70         }
71
72         private function runHandlers($httpConfig, $request) {
73         
74                 $this->getOptions();
75                 
76                 foreach($this->handlers as $handlerClass) {
77                         $handler = new $handlerClass();
78                         $handler->handle($httpConfig, $request, $this->options);
79                 }
80                 $handler = new PPAuthenticationHandler();
81                 $handler->handle($httpConfig, $request, $this->options);
82         }
83         
84         private function getOptions()
85         {
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;
91         }       
92 }