Open Journal Systems  3.3.0
vendor/omnipay/paypal/src/Message/AbstractRestRequest.php
1 <?php
6 namespace Omnipay\PayPal\Message;
7 
9 
31 abstract class AbstractRestRequest extends \Omnipay\Common\Message\AbstractRequest
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 
126  // Guzzle HTTP Client createRequest does funny things when a GET request
127  // has attached data, so don't send the data if the method is GET.
128  if ($this->getHttpMethod() == 'GET') {
129  $requestUrl = $this->getEndpoint() . '?' . http_build_query($data);
130  $body = null;
131  } else {
132  $body = $this->toJSON($data);
133  $requestUrl = $this->getEndpoint();
134  }
135 
136  // Might be useful to have some debug code here, PayPal especially can be
137  // a bit fussy about data formats and ordering. Perhaps hook to whatever
138  // logging engine is being used.
139  // echo "Data == " . json_encode($data) . "\n";
140 
141  try {
142  $httpResponse = $this->httpClient->request(
143  $this->getHttpMethod(),
144  $this->getEndpoint(),
145  array(
146  'Accept' => 'application/json',
147  'Authorization' => 'Bearer ' . $this->getToken(),
148  'Content-type' => 'application/json',
149  ),
150  $body
151  );
152  // Empty response body should be parsed also as and empty array
153  $body = (string) $httpResponse->getBody()->getContents();
154  $jsonToArrayResponse = !empty($body) ? json_decode($body, true) : array();
155  return $this->response = $this->createResponse($jsonToArrayResponse, $httpResponse->getStatusCode());
156  } catch (\Exception $e) {
157  throw new InvalidResponseException(
158  'Error communicating with payment gateway: ' . $e->getMessage(),
159  $e->getCode()
160  );
161  }
162  }
163 
174  public function toJSON($data, $options = 0)
175  {
176  // Because of PHP Version 5.3, we cannot use JSON_UNESCAPED_SLASHES option
177  // Instead we would use the str_replace command for now.
178  // TODO: Replace this code with return json_encode($this->toArray(), $options | 64); once we support PHP >= 5.4
179  if (version_compare(phpversion(), '5.4.0', '>=') === true) {
180  return json_encode($data, $options | 64);
181  }
182  return str_replace('\\/', '/', json_encode($data, $options));
183  }
184 
185  protected function createResponse($data, $statusCode)
186  {
187  return $this->response = new RestResponse($this, $data, $statusCode);
188  }
189 }
Omnipay\PayPal\Message\AbstractRestRequest\getPayerId
getPayerId()
Definition: 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: vendor/omnipay/paypal/src/Message/AbstractRestRequest.php:84
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: vendor/omnipay/paypal/src/Message/AbstractRestRequest.php:74
Omnipay\PayPal\Message\AbstractRestRequest\getHttpMethod
getHttpMethod()
Definition: 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: vendor/omnipay/paypal/src/Message/AbstractRestRequest.php:89
Omnipay\PayPal\Message\AbstractRestRequest\setToken
setToken($value)
Definition: 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: vendor/omnipay/paypal/src/Message/AbstractRestRequest.php:126
Omnipay\PayPal\Message\AbstractRestRequest\setClientId
setClientId($value)
Definition: vendor/omnipay/paypal/src/Message/AbstractRestRequest.php:79
Omnipay\PayPal\Message\AbstractRestRequest\getToken
getToken()
Definition: 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: vendor/omnipay/paypal/src/Message/AbstractRestRequest.php:132
Omnipay\PayPal\Message\AbstractRestRequest\setPayerId
setPayerId($value)
Definition: vendor/omnipay/paypal/src/Message/AbstractRestRequest.php:109
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