/
recurlyclient.php
executable file
·300 lines (259 loc) · 9.12 KB
/
recurlyclient.php
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<?php
/**
* RecurlyClient provides methods for interacting with the {@link http://support.recurly.com/faqs/api Recurly} API.
*
* @category Recurly
* @package Recurly_Client_PHP
* @copyright Copyright (c) 2011 {@link http://recurly.com Recurly, Inc.}
*/
class RecurlyClient
{
const API_CLIENT_VERSION = '0.2.1';
const API_PRODUCTION_URL = 'https://api-production.recurly.com';
const API_SANDBOX_URL = 'https://api-sandbox.recurly.com';
const API_DEVELOPMENT_URL = 'http://api-sandbox.recurly.local:3000';
const DEFAULT_ENCODING = 'UTF-8';
const PATH_ACCOUNTS = '/accounts/';
const PATH_BILLING_INFO = '/billing_info';
const PATH_ACCOUNT_COUPON = '/coupon';
const PATH_ACCOUNT_CHARGES = '/charges';
const PATH_ACCOUNT_CREDITS = '/credits';
const PATH_ACCOUNT_INVOICES = '/invoices';
const PATH_ACCOUNT_SUBSCRIPTION = '/subscription';
const PATH_TRANSACTIONS = '/transactions/';
const PATH_INVOICES = '/invoices/';
const PATH_PLANS = '/company/plans/';
const PATH_TRANSPARENT = '/transparent/';
const PATH_TRANSPARENT_SUBSCRIPTION = '/subscription';
const PATH_TRANSPARENT_TRANSACTION = '/transaction';
const PATH_TRANSPARENT_BILLING_INFO = '/billing_info';
const PATH_TRANSPARENT_RESULTS = 'results/';
static $class_map = array(
'account' => 'RecurlyAccount',
'billing_info' => 'RecurlyBillingInfo',
'charge' => 'RecurlyAccountCharge',
'coupon' => 'RecurlyCoupon',
'credit' => 'RecurlyAccountCredit',
'credit_card' => 'RecurlyCreditCard',
'error' => 'RecurlyError',
'errors' => 'array',
'invoice' => 'RecurlyInvoice',
'latest_version' => '', // Depreciated -- ignore
'line_item' => 'RecurlyLineItem',
'line_items' => 'array',
'plan' => 'RecurlyPlan',
'plan_version' => '', // Depreciated -- ignore
'payment' => 'RecurlyTransaction',
'payments' => 'array',
'pending_subscription' => 'RecurlyPendingSubscription',
'redemption' => 'RecurlyCouponRedemption',
'subscription' => 'RecurlySubscription',
'transaction' => 'RecurlyTransaction',
'add_ons' => 'array',
'add_on' => 'RecurlyAddOn');
/**
* Recurly API username
*
* @var string
*/
static $username = '';
/**
* Recurly API password
*
* @var string
*/
static $password = '';
/**
* Recurly API private key
*
* @var string
*/
static $private_key = '';
/**
* Recurly account subdomain
*
* @var string
*/
static $subdomain = '';
/**
* Recurly environment: 'production' or 'sandbox'
*
* @var string
*/
static $environment = '';
/**
* Set Recurly username and password.
*
* @param string $username Recurly username
* @param string $password Recurly password
*/
public static function SetAuth($username, $password, $subdomain, $environment = 'production', $private_key = null)
{
if (!in_array($environment, array('production','sandbox','development'))) {
throw new RecurlyException("Environment must be production or sandbox.");
}
self::$username = $username;
self::$password = $password;
self::$subdomain = $subdomain;
self::$environment = $environment;
self::$private_key = $private_key;
}
public static function __recurlyBaseUrl() {
if (self::$environment == 'production') {
return RecurlyClient::API_PRODUCTION_URL;
} elseif (self::$environment == 'development') {
return RecurlyClient::API_DEVELOPMENT_URL;
} else {
return RecurlyClient::API_SANDBOX_URL;
}
}
/**
* Sends an HTTP request to the Recurly API with Basic authentication.
*
* @param string $uri Target URI for this request (relative to the API root)
* @param string $method Specifies the HTTP method to be used for this request
* @param mixed $data x-www-form-urlencoded data (or array) to be sent in a POST request body
*
* @return RecurlyResponse
* @throws RecurlyException
*/
public static function __sendRequest($uri, $method = 'GET', $data = '')
{
if(function_exists('mb_internal_encoding'))
{
mb_internal_encoding(self::DEFAULT_ENCODING);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, self::__recurlyBaseUrl() . $uri);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_MAXREDIRS, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/xml; charset=utf-8',
'Accept: application/xml',
"User-Agent: Recurly PHP Client v" . self::API_CLIENT_VERSION
));
curl_setopt($ch, CURLOPT_USERPWD, self::$username . ':' . self::$password);
if('POST' == ($method = strtoupper($method)))
{
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
else if ('PUT' == $method)
{
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
else if('GET' != $method)
{
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
}
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 90);
$result = new StdClass();
$result->response = curl_exec($ch);
$result->code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$result->meta = curl_getinfo($ch);
$curl_error = ($result->code > 0 ? null : curl_error($ch) . ' (' . curl_errno($ch) . ')');
curl_close($ch);
if ($result->code == 0)
throw new RecurlyConnectionException('An error occurred while connecting to Recurly: ' . $curl_error);
if ($result->code == 401)
throw new RecurlyUnauthorizedException('Your API user is not authorized to connect to Recurly.');
return $result;
}
public static function __parse_xml($xml, $node_name, $parse_attributes = false) {
$dom = @DOMDocument::loadXML($xml);
if (!$dom) return null;
$childNodes = $dom->getElementsByTagName($node_name);
$list = array();
for ($i=0; $i < $childNodes->length; $i++) {
$node = $childNodes->item($i)->firstChild;
$node_class = RecurlyClient::$class_map[$node_name];
$list[] = RecurlyClient::__parseXmlToObject($node, $node_class, $parse_attributes);
}
if (count($list) == 0)
return null;
else if (count($list) == 1)
return $list[0];
else
return $list;
}
protected static function __parseXmlToObject($node, $node_class, $parse_attributes) {
if ($node_class != null)
{
if ($node_class == 'array')
$obj = array();
else
$obj = new $node_class();
}
else
$obj = new RecurlyObject();
while ($node) {
if ($node->nodeType == XML_TEXT_NODE) {
if ($node->wholeText != null) {
$text = trim($node->wholeText);
if (strlen($text) > 0)
$obj->message = $text;
}
} else if ($node->nodeType == XML_ELEMENT_NODE) {
$nodeName = str_replace("-", "_", $node->nodeName);
if (is_array($obj)) {
$child_node_class = RecurlyClient::$class_map[$nodeName];
$new_obj = RecurlyClient::__parseXmlToObject($node->childNodes->item(0), $child_node_class, $parse_attributes);
if ($parse_attributes) {
foreach ($node->attributes as $attrName => $attrNode) {
$nodeName = str_replace("-", "_", $attrName);
$nodeValue = $attrNode->nodeValue;
if (!is_numeric($nodeValue) && $tmp = strtotime($nodeValue))
$new_obj->$nodeName = $tmp;
else if ($nodeValue == "false")
$new_obj->$nodeName = false;
else if ($nodeValue == "true")
$new_obj->$nodeName = true;
else
$new_obj->$nodeName = $nodeValue;
}
}
$obj[] = $new_obj;
$node = $node->nextSibling;
continue;
}
if (!is_numeric($node->nodeValue) && $tmp = strtotime($node->nodeValue))
$obj->$nodeName = $tmp;
else if ($node->nodeValue == "false")
$obj->$nodeName = false;
else if ($node->nodeValue == "true")
$obj->$nodeName = true;
else
$obj->$nodeName = $node->nodeValue;
if ($node->childNodes->length > 1) {
$child_node_class = RecurlyClient::$class_map[$nodeName];
if ($child_node_class != '') {
$obj->$nodeName = RecurlyClient::__parseXmlToObject($node->childNodes->item(0), $child_node_class, $parse_attributes);
}
}
if ($parse_attributes) {
foreach ($node->attributes as $attrName => $attrNode) {
$nodeName = str_replace("-", "_", $attrName);
$nodeValue = $attrNode->nodeValue;
if (!is_numeric($nodeValue) && $tmp = strtotime($nodeValue))
$obj->$nodeName = $tmp;
else if ($nodeValue == "false")
$obj->$nodeName = false;
else if ($nodeValue == "true")
$obj->$nodeName = true;
else
$obj->$nodeName = $nodeValue;
}
}
}
$node = $node->nextSibling;
}
return $obj;
}
}
// In case node_class is not specified.
class RecurlyObject {}