Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add subscription change preview #94

Merged
merged 1 commit into from
Jun 6, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

## Unreleased

* Added remaining billing cycles to subscriptions: `subscription.remaining_billing_cycles` [91](https://github.com/recurly/recurly-client-php/pull/91)
* Added remaining billing cycles to subscriptions: `subscription->remaining_billing_cycles` [91](https://github.com/recurly/recurly-client-php/pull/91)
* Add subscription change preview for existing subscriptions: `subscription->preview()` [94](https://github.com/recurly/recurly-client-php/pull/94)

## Version 2.3.0 (May 19th, 2014)

Expand Down
16 changes: 10 additions & 6 deletions Tests/Recurly/Subscription_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ public function testCreateSubscriptionWithBillingInfoTokenXml() {
);
}

public function testPreviewableSubscription() {
$this->client->addResponse('POST', '/subscriptions/preview', 'subscriptions/preview-200.xml');
public function testPreviewNewSubscription() {
$this->client->addResponse('POST', '/subscriptions/preview', 'subscriptions/preview-200-new.xml');

$subscription = new Recurly_Subscription(null, $this->client);
$subscription->plan_code = 'gold';
Expand All @@ -157,13 +157,17 @@ public function testPreviewableSubscription() {
$this->assertEquals('pending', $subscription->state);
}

/**
* @expectedException Recurly_Error
*/
public function testNonPreviewableSubscriptions() {
public function testPreviewChangeSubscription() {
$this->client->addResponse('GET', '/subscriptions/012345678901234567890123456789ab', 'subscriptions/show-200.xml');

$this->client->addResponse('POST', 'https://api.recurly.com/v2/subscriptions/012345678901234567890123456789ab/preview', 'subscriptions/preview-200-change.xml');

$subscription = Recurly_Subscription::get('012345678901234567890123456789ab', $this->client);
$subscription->plan_code = 'gold';

$subscription->preview();

$this->assertEquals('5000', $subscription->cost_in_cents);
$this->assertEquals('gold', $subscription->plan_code);
}
}
68 changes: 68 additions & 0 deletions Tests/fixtures/subscriptions/preview-200-change.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
HTTP/1.1 200 OK
Content-Type: application/xml; charset=utf-8

<?xml version="1.0" encoding="UTF-8"?>
<subscription href="https://api.recurly.com/v2/subscriptions/012345678901234567890123456789ab">
<account href="https://api.recurly.com/v2/accounts/account_code"/>
<plan href="https://api.recurly.com/v2/plans/gold">
<plan_code>gold</plan_code>
<name>gold</name>
</plan>
<uuid>012345678901234567890123456789ab</uuid>
<state>active</state>
<unit_amount_in_cents type="integer">1000</unit_amount_in_cents>
<currency>USD</currency>
<quantity type="integer">5</quantity>
<activated_at type="datetime">2014-05-20T16:29:59Z</activated_at>
<canceled_at nil="nil"></canceled_at>
<expires_at nil="nil"></expires_at>
<total_billing_cycles nil="nil"></total_billing_cycles>
<remaining_billing_cycles nil="nil"></remaining_billing_cycles>
<current_period_started_at type="datetime">2014-05-20T16:29:59Z</current_period_started_at>
<current_period_ends_at type="datetime">2014-06-20T16:29:59Z</current_period_ends_at>
<trial_started_at nil="nil"></trial_started_at>
<trial_ends_at nil="nil"></trial_ends_at>
<cost_in_cents type="integer">5000</cost_in_cents>
<subscription_add_ons type="array">
</subscription_add_ons>
<invoice href="">
<account href="https://api.recurly.com/v2/accounts/account_code"/>
<subscription href="https://api.recurly.com/v2/subscriptions/012345678901234567890123456789ab"/>
<uuid>abcdefg123</uuid>
<state>open</state>
<invoice_number nil="nil"></invoice_number>
<po_number nil="nil"></po_number>
<vat_number></vat_number>
<subtotal_in_cents type="integer">5000</subtotal_in_cents>
<tax_in_cents type="integer">0</tax_in_cents>
<total_in_cents type="integer">5000</total_in_cents>
<currency>USD</currency>
<created_at nil="nil"></created_at>
<closed_at nil="nil"></closed_at>
<line_items type="array">
<adjustment href="https://api.recurly.com/v2/adjustments/abcdefg111" type="charge">
<account href="https://api.recurly.com/v2/accounts/account_code"/>
<subscription href="https://api.recurly.com/v2/subscriptions/012345678901234567890123456789ab"/>
<uuid>abcdefg111</uuid>
<state>pending</state>
<description>gold</description>
<accounting_code></accounting_code>
<product_code>gold</product_code>
<origin>plan</origin>
<unit_amount_in_cents type="integer">5000</unit_amount_in_cents>
<quantity type="integer">5</quantity>
<discount_in_cents type="integer">0</discount_in_cents>
<tax_in_cents type="integer">0</tax_in_cents>
<total_in_cents type="integer">5000</total_in_cents>
<currency>USD</currency>
<taxable type="boolean">false</taxable>
<tax_exempt type="boolean">false</tax_exempt>
<start_date type="datetime">2014-05-20T18:14:08Z</start_date>
<end_date type="datetime">2014-06-20T16:29:59Z</end_date>
<created_at nil="nil"></created_at>
</adjustment>
</line_items>
<transactions type="array">
</transactions>
</invoice>
</subscription>
8 changes: 4 additions & 4 deletions lib/recurly/subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public function create() {
}

public function preview() {
if (!empty($this->uuid)) {
throw new Recurly_Error('Cannot preview an existing subscription');
if ($this->uuid) {
$this->_save(Recurly_Client::POST, $this->uri() . '/preview');
} else {
$this->_save(Recurly_Client::POST, Recurly_Client::PATH_SUBSCRIPTIONS . '/preview');
}

$this->_save(Recurly_Client::POST, Recurly_Client::PATH_SUBSCRIPTIONS . '/preview');
}

/**
Expand Down