-
Notifications
You must be signed in to change notification settings - Fork 853
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for Subscription Schedules
- Loading branch information
1 parent
fbbcc90
commit 528d54b
Showing
6 changed files
with
342 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
<?php | ||
|
||
namespace Stripe; | ||
|
||
/** | ||
* Class Subscription | ||
* | ||
* @property string $id | ||
* @property string $object | ||
* @property string $billing | ||
* @property mixed $billing_thresholds | ||
* @property int $canceled_at | ||
* @property int $completed_at | ||
* @property int $created | ||
* @property mixed $current_phase | ||
* @property string $customer | ||
* @property mixed $invoice_settings | ||
* @property boolean $livemode | ||
* @property StripeObject $metadata | ||
* @property mixed $phases | ||
* @property int $released_at | ||
* @property string $released_subscription | ||
* @property string $renewal_behavior | ||
* @property mixed $renewal_interval | ||
* @property string $revision | ||
* @property string $status | ||
* @property string $subscription | ||
* | ||
* @package Stripe | ||
*/ | ||
class SubscriptionSchedule extends ApiResource | ||
{ | ||
|
||
const OBJECT_NAME = "subscription_schedule"; | ||
|
||
use ApiOperations\All; | ||
use ApiOperations\Create; | ||
use ApiOperations\Retrieve; | ||
use ApiOperations\Update; | ||
use ApiOperations\NestedResource; | ||
|
||
const PATH_REVISIONS = '/revisions'; | ||
|
||
/** | ||
* @param array|null $params | ||
* @param array|string|null $opts | ||
* | ||
* @return SubscriptionSchedule The canceled subscription schedule. | ||
*/ | ||
public function cancel($params = null, $opts = null) | ||
{ | ||
$url = $this->instanceUrl() . '/cancel'; | ||
list($response, $opts) = $this->_request('post', $url, $params, $opts); | ||
$this->refreshFrom($response, $opts); | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param array|null $params | ||
* @param array|string|null $opts | ||
* | ||
* @return SubscriptionSchedule The released subscription schedule. | ||
*/ | ||
public function release($params = null, $opts = null) | ||
{ | ||
$url = $this->instanceUrl() . '/release'; | ||
list($response, $opts) = $this->_request('post', $url, $params, $opts); | ||
$this->refreshFrom($response, $opts); | ||
return $this; | ||
} | ||
|
||
/** | ||
* @param array|null $params | ||
* @param array|string|null $options | ||
* | ||
* @return Collection The list of subscription schedule revisions. | ||
*/ | ||
public function revisions($params = null, $options = null) | ||
{ | ||
$url = $this->instanceUrl() . '/revisions'; | ||
list($response, $opts) = $this->_request('get', $url, $params, $options); | ||
$obj = Util\Util::convertToStripeObject($response, $opts); | ||
$obj->setLastResponse($response); | ||
return $obj; | ||
} | ||
|
||
/** | ||
* @param array|null $id The ID of the subscription schedule to which the person belongs. | ||
* @param array|null $personId The ID of the person to retrieve. | ||
* @param array|null $params | ||
* @param array|string|null $opts | ||
* | ||
* @return Revision | ||
*/ | ||
public static function retrieveRevision($id, $personId, $params = null, $opts = null) | ||
{ | ||
return self::_retrieveNestedResource($id, static::PATH_REVISIONS, $personId, $params, $opts); | ||
} | ||
|
||
/** | ||
* @param array|null $id The ID of the subscription schedule on which to retrieve the persons. | ||
* @param array|null $params | ||
* @param array|string|null $opts | ||
* | ||
* @return Revision | ||
*/ | ||
public static function allRevisions($id, $params = null, $opts = null) | ||
{ | ||
return self::_allNestedResources($id, static::PATH_REVISIONS, $params, $opts); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?php | ||
|
||
namespace Stripe; | ||
|
||
/** | ||
* Class Subscription | ||
* | ||
* @property string $id | ||
* @property string $object | ||
* @property int $created | ||
* @property mixed $invoice_settings | ||
* @property boolean $livemode | ||
* @property mixed $phases | ||
* @property string $previous_revision | ||
* @property string $renewal_behavior | ||
* @property mixed $renewal_interval | ||
* @property string $chedule | ||
* | ||
* @package Stripe | ||
*/ | ||
class SubscriptionScheduleRevision extends ApiResource | ||
{ | ||
|
||
const OBJECT_NAME = "subscription_schedule_revision"; | ||
|
||
use ApiOperations\All; | ||
use ApiOperations\Retrieve; | ||
|
||
/** | ||
* @return string The API URL for this Subscription Schedule Revision. | ||
*/ | ||
public function instanceUrl() | ||
{ | ||
$id = $this['id']; | ||
$schedule = $this['schedule']; | ||
if (!$id) { | ||
throw new Error\InvalidRequest( | ||
"Could not determine which URL to request: " . | ||
"class instance has invalid ID: $id", | ||
null | ||
); | ||
} | ||
$id = Util\Util::utf8($id); | ||
$schedule = Util\Util::utf8($schedule); | ||
|
||
$base = SubscriptionSchedule::classUrl(); | ||
$scheduleExtn = urlencode($schedule); | ||
$extn = urlencode($id); | ||
return "$base/$scheduleExtn/revisions/$extn"; | ||
} | ||
|
||
/** | ||
* @param array|string $_id | ||
* @param array|string|null $_opts | ||
* | ||
* @throws \Stripe\Error\InvalidRequest | ||
*/ | ||
public static function retrieve($_id, $_opts = null) | ||
{ | ||
$msg = "Subscription Schedule Revisions cannot be accessed without a subscription schedule ID. " . | ||
"Retrieve one using \$schedule->retrieveRevision('revision_id') instead."; | ||
throw new Error\InvalidRequest($msg, null); | ||
} | ||
|
||
/** | ||
* @param array|string $_id | ||
* @param array|string|null $_opts | ||
* | ||
* @throws \Stripe\Error\InvalidRequest | ||
*/ | ||
public static function all($params = null, $opts = null) | ||
{ | ||
$msg = "Subscription Schedule Revisions cannot be listed without a subscription schedule ID. " . | ||
"List those using \$schedule->allRevisions('revision_id') instead."; | ||
throw new Error\InvalidRequest($msg, null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Stripe; | ||
|
||
class SubscriptionScheduleRevisionTest extends TestCase | ||
{ | ||
const TEST_SCHEDULE_ID = 'sub_sched_123'; | ||
const TEST_RESOURCE_ID = 'sub_sched_rev_123'; | ||
|
||
public function testHasCorrectUrl() | ||
{ | ||
$resource = \Stripe\SubscriptionSchedule::retrieveRevision(self::TEST_SCHEDULE_ID, self::TEST_RESOURCE_ID); | ||
$this->assertSame( | ||
"/v1/subscription_schedules/" . self::TEST_SCHEDULE_ID . "/revisions/" . self::TEST_RESOURCE_ID, | ||
$resource->instanceUrl() | ||
); | ||
} | ||
|
||
/** | ||
* @expectedException \Stripe\Error\InvalidRequest | ||
*/ | ||
public function testIsNotDirectlyRetrievable() | ||
{ | ||
SubscriptionScheduleRevision::retrieve(self::TEST_RESOURCE_ID); | ||
} | ||
|
||
/** | ||
* @expectedException \Stripe\Error\InvalidRequest | ||
*/ | ||
public function testIsNotDirectlyAll() | ||
{ | ||
SubscriptionScheduleRevision::all(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
<?php | ||
|
||
namespace Stripe; | ||
|
||
class SubscriptionScheduleTest extends TestCase | ||
{ | ||
const TEST_RESOURCE_ID = 'sub_sched_123'; | ||
const TEST_REVISION_ID = 'sub_sched_rev_123'; | ||
|
||
public function testIsListable() | ||
{ | ||
$this->expectsRequest( | ||
'get', | ||
'/v1/subscription_schedules' | ||
); | ||
$resources = SubscriptionSchedule::all(); | ||
$this->assertTrue(is_array($resources->data)); | ||
$this->assertInstanceOf("Stripe\\SubscriptionSchedule", $resources->data[0]); | ||
} | ||
|
||
public function testIsRetrievable() | ||
{ | ||
$this->expectsRequest( | ||
'get', | ||
'/v1/subscription_schedules/' . self::TEST_RESOURCE_ID | ||
); | ||
$resource = SubscriptionSchedule::retrieve(self::TEST_RESOURCE_ID); | ||
$this->assertInstanceOf("Stripe\\SubscriptionSchedule", $resource); | ||
} | ||
|
||
public function testIsCreatable() | ||
{ | ||
$this->expectsRequest( | ||
'post', | ||
'/v1/subscription_schedules' | ||
); | ||
$resource = SubscriptionSchedule::create([ | ||
"phases" => [ | ||
[ | ||
"plans" => [ | ||
[ "plan" => "plan_123", "quantity" => 2], | ||
], | ||
], | ||
], | ||
]); | ||
$this->assertInstanceOf("Stripe\\SubscriptionSchedule", $resource); | ||
} | ||
|
||
public function testIsSaveable() | ||
{ | ||
$resource = SubscriptionSchedule::retrieve(self::TEST_RESOURCE_ID); | ||
$resource->metadata["key"] = "value"; | ||
$this->expectsRequest( | ||
'post', | ||
'/v1/subscription_schedules/' . $resource->id | ||
); | ||
$resource->save(); | ||
$this->assertInstanceOf("Stripe\\SubscriptionSchedule", $resource); | ||
} | ||
|
||
public function testIsUpdatable() | ||
{ | ||
$this->expectsRequest( | ||
'post', | ||
'/v1/subscription_schedules/' . self::TEST_RESOURCE_ID | ||
); | ||
$resource = SubscriptionSchedule::update(self::TEST_RESOURCE_ID, [ | ||
"metadata" => ["key" => "value"], | ||
]); | ||
$this->assertInstanceOf("Stripe\\SubscriptionSchedule", $resource); | ||
} | ||
|
||
public function testIsCancelable() | ||
{ | ||
$resource = SubscriptionSchedule::retrieve(self::TEST_RESOURCE_ID); | ||
$this->expectsRequest( | ||
'post', | ||
'/v1/subscription_schedules/' . $resource->id . '/cancel', | ||
[] | ||
); | ||
$resource->cancel([]); | ||
$this->assertInstanceOf("Stripe\\SubscriptionSchedule", $resource); | ||
} | ||
|
||
public function testRevisions() | ||
{ | ||
$schedule = SubscriptionSchedule::retrieve(self::TEST_RESOURCE_ID); | ||
$this->expectsRequest( | ||
'get', | ||
'/v1/subscription_schedules/' . $schedule->id . '/revisions' | ||
); | ||
$revisions = $schedule->revisions(); | ||
$this->assertTrue(is_array($revisions->data)); | ||
$this->assertInstanceOf("Stripe\\SubscriptionScheduleRevision", $revisions->data[0]); | ||
} | ||
|
||
public function testCanRetrieveRevision() | ||
{ | ||
$this->expectsRequest( | ||
'get', | ||
'/v1/subscription_schedules/' . self::TEST_RESOURCE_ID . '/revisions/' . self::TEST_REVISION_ID | ||
); | ||
$resource = SubscriptionSchedule::retrieveRevision(self::TEST_RESOURCE_ID, self::TEST_REVISION_ID); | ||
$this->assertInstanceOf("Stripe\\SubscriptionScheduleRevision", $resource); | ||
} | ||
|
||
public function testCanListRevisions() | ||
{ | ||
$this->expectsRequest( | ||
'get', | ||
'/v1/subscription_schedules/' . self::TEST_RESOURCE_ID . '/revisions' | ||
); | ||
$resources = SubscriptionSchedule::allRevisions(self::TEST_RESOURCE_ID); | ||
$this->assertTrue(is_array($resources->data)); | ||
} | ||
} |