-
Notifications
You must be signed in to change notification settings - Fork 179
Expand file tree
/
Copy pathAbstractRestRequest.php
More file actions
189 lines (167 loc) · 5.8 KB
/
AbstractRestRequest.php
File metadata and controls
189 lines (167 loc) · 5.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
<?php
/**
* PayPal Abstract REST Request
*/
namespace Omnipay\PayPal\Message;
use Omnipay\Common\Exception\InvalidResponseException;
/**
* PayPal Abstract REST Request
*
* This class forms the base class for PayPal REST requests via the PayPal REST APIs.
*
* A complete REST operation is formed by combining an HTTP method (or “verb”) with
* the full URI to the resource you’re addressing. For example, here is the operation
* to create a new payment:
*
* <code>
* POST https://api.paypal.com/v1/payments/payment
* </code>
*
* To create a complete request, combine the operation with the appropriate HTTP headers
* and any required JSON payload.
*
* @link https://developer.paypal.com/docs/api/
* @link https://devtools-paypal.com/integrationwizard/
* @link http://paypal.github.io/sdk/
* @see Omnipay\PayPal\RestGateway
*/
abstract class AbstractRestRequest extends \Omnipay\Common\Message\AbstractRequest
{
const API_VERSION = 'v1';
/**
* Sandbox Endpoint URL
*
* The PayPal REST APIs are supported in two environments. Use the Sandbox environment
* for testing purposes, then move to the live environment for production processing.
* When testing, generate an access token with your test credentials to make calls to
* the Sandbox URIs. When you’re set to go live, use the live credentials assigned to
* your app to generate a new access token to be used with the live URIs.
*
* @var string URL
*/
protected $testEndpoint = 'https://api.sandbox.paypal.com';
/**
* Live Endpoint URL
*
* When you’re set to go live, use the live credentials assigned to
* your app to generate a new access token to be used with the live URIs.
*
* @var string URL
*/
protected $liveEndpoint = 'https://api.paypal.com';
/**
* PayPal Payer ID
*
* @var string PayerID
*/
protected $payerId = null;
public function getClientId()
{
return $this->getParameter('clientId');
}
public function setClientId($value)
{
return $this->setParameter('clientId', $value);
}
public function getSecret()
{
return $this->getParameter('secret');
}
public function setSecret($value)
{
return $this->setParameter('secret', $value);
}
public function getToken()
{
return $this->getParameter('token');
}
public function setToken($value)
{
return $this->setParameter('token', $value);
}
public function getPayerId()
{
return $this->getParameter('payerId');
}
public function setPayerId($value)
{
return $this->setParameter('payerId', $value);
}
/**
* Get HTTP Method.
*
* This is nearly always POST but can be over-ridden in sub classes.
*
* @return string
*/
protected function getHttpMethod()
{
return 'POST';
}
protected function getEndpoint()
{
$base = $this->getTestMode() ? $this->testEndpoint : $this->liveEndpoint;
return $base . '/' . self::API_VERSION;
}
public function sendData($data)
{
// Guzzle HTTP Client createRequest does funny things when a GET request
// has attached data, so don't send the data if the method is GET.
if ($this->getHttpMethod() == 'GET') {
$requestUrl = $this->getEndpoint() . '?' . http_build_query($data);
$body = null;
} else {
$body = $this->toJSON($data);
$requestUrl = $this->getEndpoint();
}
// Might be useful to have some debug code here, PayPal especially can be
// a bit fussy about data formats and ordering. Perhaps hook to whatever
// logging engine is being used.
// echo "Data == " . json_encode($data) . "\n";
try {
$httpResponse = $this->httpClient->request(
$this->getHttpMethod(),
$requestUrl,
array(
'Accept' => 'application/json',
'Authorization' => 'Bearer ' . $this->getToken(),
'Content-type' => 'application/json',
),
$body
);
// Empty response body should be parsed also as and empty array
$body = (string) $httpResponse->getBody()->getContents();
$jsonToArrayResponse = !empty($body) ? json_decode($body, true) : array();
return $this->response = $this->createResponse($jsonToArrayResponse, $httpResponse->getStatusCode());
} catch (\Exception $e) {
throw new InvalidResponseException(
'Error communicating with payment gateway: ' . $e->getMessage(),
$e->getCode()
);
}
}
/**
* Returns object JSON representation required by PayPal.
* The PayPal REST API requires the use of JSON_UNESCAPED_SLASHES.
*
* Adapted from the official PayPal REST API PHP SDK.
* (https://github.com/paypal/PayPal-PHP-SDK/blob/master/lib/PayPal/Common/PayPalModel.php)
*
* @param int $options http://php.net/manual/en/json.constants.php
* @return string
*/
public function toJSON($data, $options = 0)
{
// Because of PHP Version 5.3, we cannot use JSON_UNESCAPED_SLASHES option
// Instead we would use the str_replace command for now.
// TODO: Replace this code with return json_encode($this->toArray(), $options | 64); once we support PHP >= 5.4
if (version_compare(phpversion(), '5.4.0', '>=') === true) {
return json_encode($data, $options | 64);
}
return str_replace('\\/', '/', json_encode($data, $options));
}
protected function createResponse($data, $statusCode)
{
return $this->response = new RestResponse($this, $data, $statusCode);
}
}