Skip to content

Commit

Permalink
GET external_subscription by external_id - V2 PHP
Browse files Browse the repository at this point in the history
Added new method to get external_subscription by external_id.
  • Loading branch information
paulorbpinho-fullstacklabs committed Jun 14, 2024
1 parent f820d95 commit ad8b961
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Tests/Recurly/ExternalSubscription_List_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public function testGetAll() {
$this->assertEquals($external_subscription->state, 'active');
$this->assertEquals($external_subscription->auto_renew, false);
$this->assertEquals($external_subscription->in_grace_period, false);
$this->assertEquals($external_subscription->import, false);
$this->assertEquals($external_subscription->test, false);
$this->assertInstanceOf('DateTime', $external_subscription->canceled_at);
$this->assertInstanceOf('DateTime', $external_subscription->expires_at);
$this->assertInstanceOf('DateTime', $external_subscription->trial_started_at);
Expand Down
32 changes: 32 additions & 0 deletions Tests/Recurly/ExternalSubscription_Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,38 @@ public function testGetSubscription() {
$this->assertEquals($external_subscription->state, 'active');
$this->assertEquals($external_subscription->auto_renew, false);
$this->assertEquals($external_subscription->in_grace_period, false);
$this->assertEquals($external_subscription->import, false);
$this->assertEquals($external_subscription->test, false);
$this->assertInstanceOf('DateTime', $external_subscription->canceled_at);
$this->assertInstanceOf('DateTime', $external_subscription->expires_at);
$this->assertInstanceOf('DateTime', $external_subscription->trial_started_at);
$this->assertInstanceOf('DateTime', $external_subscription->trial_ends_at);
$external_product_reference = $external_subscription->external_product_reference;
$this->assertEquals($external_product_reference->id, 'rauqpcdmxc4a');
$this->assertEquals($external_product_reference->reference_code, '1234');
$this->assertEquals($external_product_reference->external_connection_type, 'apple_app_store');
$this->assertInstanceOf('DateTime', $external_product_reference->created_at);
$this->assertInstanceOf('DateTime', $external_product_reference->updated_at);
}

public function testGetSubscriptionByExternalId() {
$this->client->addResponse('GET', '/external_subscriptions/external-id-1_ext_id', 'external_subscriptions/show-200.xml');

$external_subscription = Recurly_ExternalSubscription::getByExternalId('1_ext_id', $this->client);
$this->assertInstanceOf('Recurly_ExternalSubscription', $external_subscription);
$this->assertInstanceOf('Recurly_Stub', $external_subscription->account);
$this->assertEquals($external_subscription->account->getHref(), 'https://api.recurly.com/v2/accounts/1');
$this->assertEquals('https://api.recurly.com/v2/external_subscriptions/rjx71rx8gs2m/external_invoices', $external_subscription->external_invoices->getHref());
$this->assertEquals('https://api.recurly.com/v2/external_subscriptions/rjx71rx8gs2m/external_payment_phases', $external_subscription->external_payment_phases->getHref());
$this->assertInstanceOf('DateTime', $external_subscription->created_at);
$this->assertInstanceOf('DateTime', $external_subscription->updated_at);
$this->assertEquals($external_subscription->quantity, 18);
$this->assertEquals($external_subscription->external_id, '1_ext_id');
$this->assertEquals($external_subscription->state, 'active');
$this->assertEquals($external_subscription->auto_renew, false);
$this->assertEquals($external_subscription->in_grace_period, false);
$this->assertEquals($external_subscription->import, false);
$this->assertEquals($external_subscription->test, false);
$this->assertInstanceOf('DateTime', $external_subscription->canceled_at);
$this->assertInstanceOf('DateTime', $external_subscription->expires_at);
$this->assertInstanceOf('DateTime', $external_subscription->trial_started_at);
Expand Down
4 changes: 4 additions & 0 deletions Tests/fixtures/external_subscriptions/index-200.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Content-Type: application/xml; charset=utf-8
<last_purchased nil="nil"></last_purchased>
<auto_renew type="boolean">false</auto_renew>
<in_grace_period type="boolean">false</in_grace_period>
<import type="boolean">false</import>
<test type="boolean">false</test>
<app_identifier nil="nil"></app_identifier>
<quantity type="integer">18</quantity>
<external_id>1_ext_id</external_id>
Expand All @@ -43,6 +45,8 @@ Content-Type: application/xml; charset=utf-8
<last_purchased nil="nil"></last_purchased>
<auto_renew type="boolean">false</auto_renew>
<in_grace_period type="boolean">false</in_grace_period>
<import type="boolean">false</import>
<test type="boolean">false</test>
<app_identifier nil="nil"></app_identifier>
<quantity type="integer">12</quantity>
<external_id>2_ext_id</external_id>
Expand Down
2 changes: 2 additions & 0 deletions Tests/fixtures/external_subscriptions/show-200.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ Content-Type: application/xml; charset=utf-8
<last_purchased nil="nil"></last_purchased>
<auto_renew type="boolean">false</auto_renew>
<in_grace_period type="boolean">false</in_grace_period>
<import type="boolean">false</import>
<test type="boolean">false</test>
<app_identifier nil="nil"></app_identifier>
<quantity type="integer">18</quantity>
<external_id>1_ext_id</external_id>
Expand Down
16 changes: 16 additions & 0 deletions lib/recurly/external_subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* @property DateTime $last_purchased
* @property boolean $auto_renew
* @property boolean $in_grace_period
* @property boolean $import
* @property boolean $test
* @property string $app_identifier
* @property integer $quantity
* @property string $external_id
Expand All @@ -31,6 +33,16 @@ public static function get($uuid, $client = null) {
return Recurly_Base::_get(Recurly_ExternalSubscription::uriForExternalSubscription($uuid), $client);
}

/**
* @param $external_id
* @param Recurly_Client $client Optional client for the request, useful for mocking the client
* @return Recurly_ExternalSubscription|null
* @throws Recurly_Error
*/
public static function getByExternalId($external_id, $client = null) {
return Recurly_Base::_get(Recurly_ExternalSubscription::uriForExternalSubscriptionExternalId($external_id), $client);
}

public function getExternalPaymentPhase($external_payment_phase_uuid, $client = null) {
return Recurly_Base::_get($this->uriForExternalPaymentPhase() . '/' . $external_payment_phase_uuid, $client);
}
Expand All @@ -46,6 +58,10 @@ protected static function uriForExternalSubscription($uuid) {
return self::_safeUri(Recurly_Client::PATH_EXTERNAL_SUBSCRIPTIONS, $uuid);
}

protected static function uriForExternalSubscriptionExternalId($external_id) {
return self::_safeUri(Recurly_Client::PATH_EXTERNAL_SUBSCRIPTIONS, "external-id-$external_id");
}

protected function uriForExternalPaymentPhase() {
return $this->uri() . '/' . Recurly_Client::PATH_EXTERNAL_PAYMENT_PHASES;
}
Expand Down

0 comments on commit ad8b961

Please sign in to comment.