Open Journal Systems  3.3.0
lib/vendor/omnipay/paypal/src/Message/AbstractRestRequest.php
1 <?php
6 namespace Omnipay\PayPal\Message;
7 
9 
32 {
33  const API_VERSION = 'v1';
34 
46  protected $testEndpoint = 'https://api.sandbox.paypal.com';
47 
56  protected $liveEndpoint = 'https://api.paypal.com';
57 
63  protected $payerId = null;
64 
65  public function getClientId()
66  {
67  return $this->getParameter('clientId');
68  }
69 
70  public function setClientId($value)
71  {
72  return $this->setParameter('clientId', $value);
73  }
74 
75  public function getSecret()
76  {
77  return $this->getParameter('secret');
78  }
79 
80  public function setSecret($value)
81  {
82  return $this->setParameter('secret', $value);
83  }
84 
85  public function getToken()
86  {
87  return $this->getParameter('token');
88  }
89 
90  public function setToken($value)
91  {
92  return $this->setParameter('token', $value);
93  }
94 
95  public function getPayerId()
96  {
97  return $this->getParameter('payerId');
98  }
99 
100  public function setPayerId($value)
101  {
102  return $this->setParameter('payerId', $value);
103  }
104 
112  protected function getHttpMethod()
113  {
114  return 'POST';
115  }
116 
117  protected function getEndpoint()
118  {
119  $base = $this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint;
120  return $base . '/' . self::API_VERSION;
121  }
122 
123  public function sendData($data)
124  {
125  // don't throw exceptions for 4xx errors
126  $this->httpClient->getEventDispatcher()->addListener(
127  'request.error',
128  function ($event) {
129  if ($event['response']->isClientError()) {
130  $event->stopPropagation();
131  }
132  }
133  );
134 
135  // Guzzle HTTP Client createRequest does funny things when a GET request
136  // has attached data, so don't send the data if the method is GET.
137  if ($this->getHttpMethod() == 'GET') {
138  $httpRequest = $this->httpClient->createRequest(
139  $this->getHttpMethod(),
140  $this->getEndpoint() . '?' . http_build_query($data),
141  array(
142  'Accept' => 'application/json',
143  'Authorization' => 'Bearer ' . $this->getToken(),
144  'Content-type' => 'application/json',
145  )
146  );
147  } else {
148  $httpRequest = $this->httpClient->createRequest(
149  $this->getHttpMethod(),
150  $this->getEndpoint(),
151  array(
152  'Accept' => 'application/json',
153  'Authorization' => 'Bearer ' . $this->getToken(),
154  'Content-type' => 'application/json',
155  ),
156  $this->toJSON($data)
157  );
158  }
159 
160  // Might be useful to have some debug code here, PayPal especially can be
161  // a bit fussy about data formats and ordering. Perhaps hook to whatever
162  // logging engine is being used.
163  // echo "Data == " . json_encode($data) . "\n";
164 
165  try {
166  $httpRequest->getCurlOptions()->set(CURLOPT_SSLVERSION, 6); // CURL_SSLVERSION_TLSv1_2 for libcurl < 7.35
167  $httpResponse = $httpRequest->send();
168  // Empty response body should be parsed also as and empty array
169  $body = $httpResponse->getBody(true);
170  $jsonToArrayResponse = !empty($body) ? $httpResponse->json() : array();
171  return $this->response = $this->createResponse($jsonToArrayResponse, $httpResponse->getStatusCode());
172  } catch (\Exception $e) {
173  throw new InvalidResponseException(
174  'Error communicating with payment gateway: ' . $e->getMessage(),
175  $e->getCode()
176  );
177  }
178  }
179 
190  public function toJSON($data, $options = 0)
191  {
192  // Because of PHP Version 5.3, we cannot use JSON_UNESCAPED_SLASHES option
193  // Instead we would use the str_replace command for now.
194  // TODO: Replace this code with return json_encode($this->toArray(), $options | 64); once we support PHP >= 5.4
195  if (version_compare(phpversion(), '5.4.0', '>=') === true) {
196  return json_encode($data, $options | 64);
197  }
198  return str_replace('\\/', '/', json_encode($data, $options));
199  }
200 
201  protected function createResponse($data, $statusCode)
202  {
203  return $this->response = new RestResponse($this, $data, $statusCode);
204  }
205 }
Omnipay\PayPal\Message\AbstractRestRequest\getPayerId
getPayerId()
Definition: lib/vendor/omnipay/paypal/src/Message/AbstractRestRequest.php:104
Omnipay\Common\Message\AbstractRequest\getTestMode
getTestMode()
Definition: lib/vendor/omnipay/common/src/Omnipay/Common/Message/AbstractRequest.php:201
Omnipay\PayPal\Message\AbstractRestRequest\getSecret
getSecret()
Definition: lib/vendor/omnipay/paypal/src/Message/AbstractRestRequest.php:84
Omnipay\Common\Message\AbstractRequest\$httpRequest
$httpRequest
Definition: lib/vendor/omnipay/common/src/Omnipay/Common/Message/AbstractRequest.php:92
Omnipay\PayPal\Message\AbstractRestRequest\$testEndpoint
$testEndpoint
Definition: lib/vendor/omnipay/paypal/src/Message/AbstractRestRequest.php:49
Omnipay\Common\Message\AbstractRequest\getParameter
getParameter($key)
Definition: lib/vendor/omnipay/common/src/Omnipay/Common/Message/AbstractRequest.php:172
Omnipay\Common\Message\AbstractRequest\setParameter
setParameter($key, $value)
Definition: lib/vendor/omnipay/common/src/Omnipay/Common/Message/AbstractRequest.php:185
Omnipay\Common\Message\AbstractRequest
Definition: lib/vendor/omnipay/common/src/Omnipay/Common/Message/AbstractRequest.php:62
Omnipay\PayPal\Message\AbstractRestRequest\$liveEndpoint
$liveEndpoint
Definition: lib/vendor/omnipay/paypal/src/Message/AbstractRestRequest.php:62
Omnipay\PayPal\Message\AbstractRestRequest\getClientId
getClientId()
Definition: lib/vendor/omnipay/paypal/src/Message/AbstractRestRequest.php:74
Omnipay\PayPal\Message\AbstractRestRequest\getHttpMethod
getHttpMethod()
Definition: lib/vendor/omnipay/paypal/src/Message/AbstractRestRequest.php:121
Omnipay\PayPal\Message\AbstractRestRequest\createResponse
createResponse($data, $statusCode)
Definition: lib/vendor/omnipay/paypal/src/Message/AbstractRestRequest.php:210
Omnipay\Common\Exception\InvalidResponseException
Definition: lib/vendor/omnipay/common/src/Omnipay/Common/Exception/InvalidResponseException.php:10
Omnipay\PayPal\Message\AbstractRestRequest\$payerId
$payerId
Definition: lib/vendor/omnipay/paypal/src/Message/AbstractRestRequest.php:72
Omnipay\PayPal\Message\AbstractRestRequest\setSecret
setSecret($value)
Definition: lib/vendor/omnipay/paypal/src/Message/AbstractRestRequest.php:89
Omnipay\PayPal\Message\AbstractRestRequest\setToken
setToken($value)
Definition: lib/vendor/omnipay/paypal/src/Message/AbstractRestRequest.php:99
Omnipay\PayPal\Message\AbstractRestRequest\API_VERSION
const API_VERSION
Definition: lib/vendor/omnipay/paypal/src/Message/AbstractRestRequest.php:33
Omnipay\PayPal\Message\RestResponse
Definition: lib/vendor/omnipay/paypal/src/Message/RestResponse.php:14
Omnipay\PayPal\Message\AbstractRestRequest\getEndpoint
getEndpoint()
Definition: lib/vendor/omnipay/paypal/src/Message/AbstractRestRequest.php:126
Omnipay\PayPal\Message\AbstractRestRequest\setClientId
setClientId($value)
Definition: lib/vendor/omnipay/paypal/src/Message/AbstractRestRequest.php:79
Omnipay\PayPal\Message\AbstractRestRequest\getToken
getToken()
Definition: lib/vendor/omnipay/paypal/src/Message/AbstractRestRequest.php:94
Omnipay\PayPal\Message\AbstractRestRequest\toJSON
toJSON($data, $options=0)
Definition: lib/vendor/omnipay/paypal/src/Message/AbstractRestRequest.php:199
Omnipay\PayPal\Message\AbstractRestRequest\sendData
sendData($data)
Definition: lib/vendor/omnipay/paypal/src/Message/AbstractRestRequest.php:132
Omnipay\PayPal\Message\AbstractRestRequest\setPayerId
setPayerId($value)
Definition: lib/vendor/omnipay/paypal/src/Message/AbstractRestRequest.php:109
Omnipay\PayPal\Message\AbstractRestRequest
Definition: lib/vendor/omnipay/paypal/src/Message/AbstractRestRequest.php:31
GuzzleHttp\json_encode
json_encode($value, $options=0, $depth=512)
Definition: guzzlehttp/guzzle/src/functions.php:324
Omnipay\PayPal\Message
Definition: lib/vendor/omnipay/paypal/src/Message/AbstractRequest.php:6