b66ec5ee1958c2b153dcb57a6e3e471ddebc906e
[WP-Plugins/glm-member-db.git] /
1 <?php
2 namespace PayPal\Handler;
3 use PayPal\Exception\PPConfigurationException;
4 use PayPal\Core\PPConstants;
5 class PPOpenIdHandler implements IPPHandler {
6         
7         private $apiContext;
8         
9         private static $sdkName = "openid-sdk-php";     
10         private static $sdkVersion = "1.4.0";
11         
12         public function __construct($apiContext) {
13                 $this->apiContext = $apiContext;
14         }
15
16         public function handle($httpConfig, $request, $options) {
17
18                 $config = $this->apiContext->getConfig();
19                 $httpConfig->setUrl(
20                         rtrim(trim($this->_getEndpoint($config)), '/') . 
21                                 (isset($options['path']) ? $options['path'] : '')
22                 );
23                 
24                 if(!array_key_exists("Authorization", $httpConfig->getHeaders())) {                     
25                         $auth = base64_encode($config['acct1.ClientId'] . ':' . $config['acct1.ClientSecret']);
26                         $httpConfig->addHeader("Authorization", "Basic $auth");
27                 }
28                 if(!array_key_exists("User-Agent", $httpConfig->getHeaders())) {
29                         $httpConfig->addHeader("User-Agent", PPUserAgent::getValue(self::$sdkName, self::$sdkVersion));
30                 }
31         }
32         
33         private function _getEndpoint($config) {
34                 if (isset($config['openid.EndPoint'])) {
35                         return $config['openid.EndPoint'];
36                 } else if (isset($config['service.EndPoint'])) {
37                         return $config['service.EndPoint'];
38                 } else if (isset($config['mode'])) {
39                         switch (strtoupper($config['mode'])) {
40                                 case 'SANDBOX':
41                                         return PPConstants::REST_SANDBOX_ENDPOINT;
42                                 case 'LIVE':
43                                         return PPConstants::REST_LIVE_ENDPOINT;
44                                 default:
45                                         throw new PPConfigurationException('The mode config parameter must be set to either sandbox/live');
46                         }
47                 } else {
48                         throw new PPConfigurationException('You must set one of service.endpoint or mode parameters in your configuration');
49                 }
50         }
51 }