PayPalHandlerRestHandler

Class RestHandler.

Defined (1)

The class is defined in the following location(s).

/classes/lib/PayPal/Handler/RestHandler.php  
  1. class RestHandler implements IPayPalHandler 
  2. /** 
  3. * Private Variable 
  4. * @var \Paypal\Rest\ApiContext $apiContext 
  5. */ 
  6. private $apiContext; 
  7.  
  8. /** 
  9. * Construct 
  10. * @param \Paypal\Rest\ApiContext $apiContext 
  11. */ 
  12. public function __construct($apiContext) 
  13. $this->apiContext = $apiContext; 
  14.  
  15. /** 
  16. * @param PayPalHttpConfig $httpConfig 
  17. * @param string $request 
  18. * @param mixed $options 
  19. * @return mixed|void 
  20. * @throws PayPalConfigurationException 
  21. * @throws PayPalInvalidCredentialException 
  22. * @throws PayPalMissingCredentialException 
  23. */ 
  24. public function handle($httpConfig, $request, $options) 
  25.  
  26. $credential = $this->apiContext->getCredential(); 
  27. $config = $this->apiContext->getConfig(); 
  28.  
  29. if ($credential == null) { 
  30. // Try picking credentials from the config file 
  31. $credMgr = PayPalCredentialManager::getInstance($config); 
  32. $credValues = $credMgr->getCredentialObject(); 
  33.  
  34. if (!is_array($credValues)) { 
  35. throw new PayPalMissingCredentialException("Empty or invalid credentials passed"); 
  36.  
  37. $credential = new OAuthTokenCredential($credValues['clientId'], $credValues['clientSecret']); 
  38.  
  39. if ($credential == null || !($credential instanceof OAuthTokenCredential)) { 
  40. throw new PayPalInvalidCredentialException("Invalid credentials passed"); 
  41.  
  42. $httpConfig->setUrl( 
  43. rtrim(trim($this->_getEndpoint($config)), '/') . 
  44. (isset($options['path']) ? $options['path'] : '') 
  45. ); 
  46.  
  47. // Overwrite Expect Header to disable 100 Continue Issue 
  48. $httpConfig->addHeader("Expect", null); 
  49.  
  50. if (!array_key_exists("User-Agent", $httpConfig->getHeaders())) { 
  51. $httpConfig->addHeader("User-Agent", PayPalUserAgent::getValue(PayPalConstants::SDK_NAME, PayPalConstants::SDK_VERSION)); 
  52.  
  53. if (!is_null($credential) && $credential instanceof OAuthTokenCredential && is_null($httpConfig->getHeader('Authorization'))) { 
  54. $httpConfig->addHeader('Authorization', "Bearer " . $credential->getAccessToken($config), false); 
  55.  
  56. if ($httpConfig->getMethod() == 'POST' || $httpConfig->getMethod() == 'PUT') { 
  57. $httpConfig->addHeader('PayPal-Request-Id', $this->apiContext->getRequestId()); 
  58. // Add any additional Headers that they may have provided 
  59. $headers = $this->apiContext->getRequestHeaders(); 
  60. foreach ($headers as $key => $value) { 
  61. $httpConfig->addHeader($key, $value); 
  62.  
  63. /** 
  64. * End Point 
  65. * @param array $config 
  66. * @return string 
  67. * @throws \PayPal\Exception\PayPalConfigurationException 
  68. */ 
  69. private function _getEndpoint($config) 
  70. if (isset($config['service.EndPoint'])) { 
  71. return $config['service.EndPoint']; 
  72. } else if (isset($config['mode'])) { 
  73. switch (strtoupper($config['mode'])) { 
  74. case 'SANDBOX': 
  75. return PayPalConstants::REST_SANDBOX_ENDPOINT; 
  76. break; 
  77. case 'LIVE': 
  78. return PayPalConstants::REST_LIVE_ENDPOINT; 
  79. break; 
  80. default: 
  81. throw new PayPalConfigurationException('The mode config parameter must be set to either sandbox/live'); 
  82. break; 
  83. } else { 
  84. // Defaulting to Sandbox 
  85. return PayPalConstants::REST_SANDBOX_ENDPOINT;