PayPalRestApiContext

Class ApiContext.

Defined (1)

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

/classes/lib/PayPal/Rest/ApiContext.php  
  1. class ApiContext 
  2.  
  3. /** 
  4. * Unique request id to be used for this call 
  5. * The user can either generate one as per application 
  6. * needs or let the SDK generate one 
  7. * @var null|string $requestId 
  8. */ 
  9. private $requestId; 
  10.  
  11. /** 
  12. * This is a placeholder for holding credential for the request 
  13. * If the value is not set, it would get the value from @\PayPal\Core\PayPalCredentialManager 
  14. * @var \Paypal\Auth\OAuthTokenCredential 
  15. */ 
  16. private $credential; 
  17.  
  18.  
  19. /** 
  20. * Construct 
  21. * @param \PayPal\Auth\OAuthTokenCredential $credential 
  22. * @param string|null $requestId 
  23. */ 
  24. public function __construct($credential = null, $requestId = null) 
  25. $this->requestId = $requestId; 
  26. $this->credential = $credential; 
  27.  
  28. /** 
  29. * Get Credential 
  30. * @return \PayPal\Auth\OAuthTokenCredential 
  31. */ 
  32. public function getCredential() 
  33. if ($this->credential == null) { 
  34. return PayPalCredentialManager::getInstance()->getCredentialObject(); 
  35. return $this->credential; 
  36.  
  37. public function getRequestHeaders() 
  38. $result = PayPalConfigManager::getInstance()->get('http.headers'); 
  39. $headers = array(); 
  40. foreach ($result as $header => $value) { 
  41. $headerName = ltrim($header, 'http.headers'); 
  42. $headers[$headerName] = $value; 
  43. return $headers; 
  44.  
  45. public function addRequestHeader($name, $value) 
  46. // Determine if the name already has a 'http.headers' prefix. If not, add one. 
  47. if (!(substr($name, 0, strlen('http.headers')) === 'http.headers')) { 
  48. $name = 'http.headers.' . $name; 
  49. PayPalConfigManager::getInstance()->addConfigs(array($name => $value)); 
  50.  
  51. /** 
  52. * Get Request ID 
  53. * @return string 
  54. */ 
  55. public function getRequestId() 
  56. if ($this->requestId == null) { 
  57. $this->requestId = $this->generateRequestId(); 
  58.  
  59. return $this->requestId; 
  60.  
  61. /** 
  62. * Resets the requestId that can be used to set the PayPal-request-id 
  63. * header used for idempotency. In cases where you need to make multiple create calls 
  64. * using the same ApiContext object, you need to reset request Id. 
  65. * @return string 
  66. */ 
  67. public function resetRequestId() 
  68. $this->requestId = $this->generateRequestId(); 
  69. return $this->getRequestId(); 
  70.  
  71. /** 
  72. * Sets Config 
  73. * @param array $config SDK configuration parameters 
  74. */ 
  75. public function setConfig(array $config) 
  76. PayPalConfigManager::getInstance()->addConfigs($config); 
  77.  
  78. /** 
  79. * Gets Configurations 
  80. * @return array 
  81. */ 
  82. public function getConfig() 
  83. return PayPalConfigManager::getInstance()->getConfigHashmap(); 
  84.  
  85. /** 
  86. * Gets a specific configuration from key 
  87. * @param $searchKey 
  88. * @return mixed 
  89. */ 
  90. public function get($searchKey) 
  91. return PayPalConfigManager::getInstance()->get($searchKey); 
  92.  
  93. /** 
  94. * Generates a unique per request id that 
  95. * can be used to set the PayPal-Request-Id header 
  96. * that is used for idempotency 
  97. * @return string 
  98. */ 
  99. private function generateRequestId() 
  100. static $pid = -1; 
  101. static $addr = -1; 
  102.  
  103. if ($pid == -1) { 
  104. $pid = getmypid(); 
  105.  
  106. if ($addr == -1) { 
  107. if (array_key_exists('SERVER_ADDR', $_SERVER)) { 
  108. $addr = ip2long($_SERVER['SERVER_ADDR']); 
  109. } else { 
  110. $addr = php_uname('n'); 
  111.  
  112. return $addr . $pid . $_SERVER['REQUEST_TIME'] . mt_rand(0, 0xffff);