/
recurlysubscription.php
executable file
·201 lines (168 loc) · 8.05 KB
/
recurlysubscription.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
<?php
/**
* @category Recurly
* @package Recurly_Client_PHP
* @copyright Copyright (c) 2011 {@link http://recurly.com Recurly, Inc.}
*/
class RecurlySubscription
{
var $account; // User account information
var $plan_code; // Subscription plan's code
var $coupon_code; // Apply a coupon to a new subscription
var $unit_amount; // Defaults to plan's current price if not set
var $currency; // Subscription currency code (e.g. "USD")
var $quantity; // Defaults to 1
var $billing_info; // Account's billing information
var $add_ons; // Subscription's add ons
/* These values are populated by Recurly -- they do not need to be set by you. */
var $activated_at; // Date the subscription started
var $canceled_at; // If set, the date the subscriber canceled their subscription
var $expires_at; // If set, the subscription will expire on this date
var $current_period_started_at; // Date the current invoice period started
var $current_period_ends_at; // The subscription is paid until this date / Next Invoice date
var $trial_period_started_at; // Date the trial started, if the subscription has a trial
var $trial_period_ends_at; // Date the trial ends, if the subscription has/had a trial
var $pending_subscription; // Set if the subscription has a pending change
var $errors; // Set by the transparent post result
public function create()
{
$uri = RecurlyClient::PATH_ACCOUNTS . urlencode($this->account->account_code) . RecurlyClient::PATH_ACCOUNT_SUBSCRIPTION;
$data = $this->getXml();
$result = RecurlyClient::__sendRequest($uri, 'POST', $data);
if (preg_match("/^2..$/", $result->code)) {
return RecurlyClient::__parse_xml($result->response, 'subscription');
} else if (strpos($result->response, '<errors>') > 0 && $result->code == 422) {
throw new RecurlyValidationException($result->code, $result->response);
} else {
throw new RecurlyException("Could not create a subscription for {$this->account->account_code}: {$result->response} -- ({$result->code})");
}
}
public static function getSubscription($accountCode)
{
$uri = RecurlyClient::PATH_ACCOUNTS . urlencode($accountCode) . RecurlyClient::PATH_ACCOUNT_SUBSCRIPTION;
$result = RecurlyClient::__sendRequest($uri, 'GET');
if (preg_match("/^2..$/", $result->code)) {
return RecurlyClient::__parse_xml($result->response, 'subscription');
} else if ($result->code == '404') {
return null;
} else {
throw new RecurlyException("Could not get subscription for {$accountCode}: {$result->response} -- ({$result->code})");
}
}
public static function cancelSubscription($accountCode)
{
$uri = RecurlyClient::PATH_ACCOUNTS . urlencode($accountCode) . RecurlyClient::PATH_ACCOUNT_SUBSCRIPTION;
$result = RecurlyClient::__sendRequest($uri, 'DELETE');
if (preg_match("/^2..$/", $result->code)) {
return true;
} else if (strpos($result->response, '<errors>') > 0 && $result->code == 422) {
throw new RecurlyValidationException($result->code, $result->response);
} else {
throw new RecurlyException("Could not cancel the subscription for {$accountCode}: {$result->response} ({$result->code})");
}
}
public static function refundSubscription($accountCode, $refund_type = 'partial')
{
/* Support previous PHP library that accepted a boolean for refund type */
if (is_bool($refund_type)) { $refund_type = ($refund_type ? 'full' : 'partial'); }
/* Verify refund type is supported */
if (!in_array($refund_type, array('full', 'partial', 'none'))) { throw new RecurlyException("Invalid refund type"); }
$uri = RecurlyClient::PATH_ACCOUNTS . urlencode($accountCode) . RecurlyClient::PATH_ACCOUNT_SUBSCRIPTION;
$uri .= '?refund=' . $refund_type;
$result = RecurlyClient::__sendRequest($uri, 'DELETE');
if (preg_match("/^2..$/", $result->code)) {
return true;
} else if (strpos($result->response, '<errors>') > 0 && $result->code == 422) {
throw new RecurlyValidationException($result->code, $result->response);
} else {
if ($refund_type == 'none') {
throw new RecurlyException("Could not terminate the subscription for {$accountCode}: {$result->response} ({$result->code})");
} else {
throw new RecurlyException("Could not refund the subscription for {$accountCode}: {$result->response} ({$result->code})");
}
}
}
// Immediately end the subscription without a refund.
public static function terminateSubscription($accountCode)
{
return RecurlySubscription::refundSubscription($accountCode, 'none');
}
// Change the subscription 'now' or at 'renewal'.
// Set a value to change it. Leave it as null otherwise.
public static function changeSubscription($accountCode, $timeframe = 'now', $newPlanCode = null, $newQuantity = null, $newUnitAmount = null, $addOns = null)
{
error_log('<br>quant:' . $newQuantity);
if (!($timeframe == 'now' || $timeframe == 'renewal'))
throw new RecurlyException("The timeframe must be 'now' or 'renewal'.");
$uri = RecurlyClient::PATH_ACCOUNTS . urlencode($accountCode) . RecurlyClient::PATH_ACCOUNT_SUBSCRIPTION;
$data = RecurlySubscription::getChangeSubscriptionXml($timeframe, $newPlanCode, $newQuantity, $newUnitAmount, $addOns);
$result = RecurlyClient::__sendRequest($uri, 'PUT', $data);
if (preg_match("/^2..$/", $result->code)) {
return true;
} else if (strpos($result->response, '<errors>') > 0 && $result->code == 422) {
throw new RecurlyValidationException($result->code, $result->response);
} else {
throw new RecurlyException("Could not change the subscription for {$accountCode}: {$result->response} ({$result->code})");
}
}
public function getXml()
{
$doc = new DOMDocument("1.0");
$this->populateXmlDoc($doc);
return $doc->saveXML();
}
public function populateXmlDoc(&$doc)
{
$root = $doc->appendChild($doc->createElement("subscription"));
$root->appendChild($doc->createElement("plan_code", $this->plan_code));
if (isset($this->coupon_code))
$root->appendChild($doc->createElement("coupon_code", $this->coupon_code));
if (isset($this->trial_period_ends_at))
$root->appendChild($doc->createElement("trial_ends_at", $this->trial_period_ends_at));
if (isset($this->quantity))
$root->appendChild($doc->createElement("quantity", $this->quantity));
if (isset($this->unit_amount))
$root->appendChild($doc->createElement("unit_amount", $this->unit_amount));
if (isset($this->currency))
$root->appendChild($doc->createElement("currency", $this->currency));
if (isset($this->add_ons))
$root->appendChild(RecurlySubscription::getAddOnsXml($this->add_ons, $doc));
$account_node = $this->account->populateXmlDoc($doc, $root);
if(isset($this->billing_info)){
$this->billing_info->populateXmlDoc($doc, $account_node);
}
return $root;
}
public static function getChangeSubscriptionXml($timeframe, $newPlanCode, $newQuantity, $newUnitAmount, $add_ons)
{
$doc = new DOMDocument("1.0");
$root = $doc->appendChild($doc->createElement("subscription"));
$root->appendChild($doc->createElement("timeframe", $timeframe));
if ($newPlanCode != null)
$root->appendChild($doc->createElement("plan_code", $newPlanCode));
if ($newQuantity != null)
$root->appendChild($doc->createElement("quantity", $newQuantity));
if ($newUnitAmount != null)
$root->appendChild($doc->createElement("unit_amount", $newUnitAmount));
if (isset($add_ons))
$root->appendChild(RecurlySubscription::getAddOnsXml($add_ons, $doc));
return $doc->saveXML();
}
public static function getAddOnsXml($add_ons, $doc){
$add_ons_elem = $doc->createElement("add_ons");
foreach($add_ons as $add_on){
$add_on_elem = $doc->createElement('add_on');
$add_on_elem->appendChild($doc->createElement('add_on_code', $add_on->add_on_code));
$add_on_elem->appendChild($doc->createElement('quantity', $add_on->quantity));
$add_on_elem->appendChild($doc->createElement('unit_amount_in_cents', $add_on->unit_amount_in_cents));
$add_ons_elem->appendChild($add_on_elem);
}
return $add_ons_elem;
}
}
class RecurlyPendingSubscription
{
var $plan_code;
var $quantity;
var $activates_at;
}