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 support for account hierarchy #393

Merged
merged 1 commit into from
Jan 11, 2019
Merged

Add support for account hierarchy #393

merged 1 commit into from
Jan 11, 2019

Conversation

aaron-junot
Copy link

This adds support for account hierarchy. Tests only include operations on the account object because there is no change in the response to create subscription and create purchase endpoints. Examples of this usage are included below.

Create an account with a parent account:

# Assumes 'example-124' exists but 'example-123' does not exist
try {
    $account = new Recurly_Account('example-123');
    $account->parent_account_code = 'example-124';
    $account->create();
  } catch (Recurly_ValidationError $e) {
    print "Invalid Account: $e";
  }

Get a parent account:

# Assumes 'example-123' has a parent account
$accountCode = 'example-123';
try
{
    $account = Recurly_Account::get($accountCode);
    $parent = $account->parent_account->get();
    print($parent);
}
catch(Recurly_Error $e)
{
    print("Caught an error:\n");
    print($e);
}
print("\n");

Get child accounts:

# Assumes 'example-124' has a child accounts
$accountCode = 'example-124';
try
{
    $account = Recurly_Account::get($accountCode);
    $children = $account->child_accounts->get();
    foreach ($children as $child) {
        print($child->account_code . "\n");
    }
}
catch(Recurly_Error $e)
{
    print("Caught an error:\n");
    print($e);
}

Remove a parent account:

# Assumes 'example-123' has a parent
$account = Recurly_Account::get('example-123');
$account->parent_account_code = '';

try {
    $account->update();
}
catch (Recurly_ValidationError $e) {
    print($e->getMessage());
}

Associate an account with a parent account on a subscription:

  # Assumes plan code 'silver' exists
  # Assumes account 'example-124' exists
  $subscription = new Recurly_Subscription();
  $subscription->plan_code = 'silver';
  $subscription->currency = 'USD';

  $account = new Recurly_Account();
  $account->account_code = 'example-123';
  $account->parent_account_code = 'example-124';
  $subscription->account = $account;

  $subscription->create();

  print "Subscription: $subscription\n";

Remove a parent by creating a subscription:

  # Assumes plan code 'silver' exists
  # Assumes account 'example-123' exists and has a parent
  $subscription = new Recurly_Subscription();
  $subscription->plan_code = 'silver';
  $subscription->currency = 'USD';

  $account = new Recurly_Account();
  $account->account_code = 'example-123';
  $account->parent_account_code = '';
  $subscription->account = $account;

  $subscription->create();

  print "Subscription: $subscription\n";

Associate an account with a parent by creating a purchase:

$purchase = new Recurly_Purchase();
$purchase->currency = 'USD';
$purchase->collection_method = 'automatic';
$purchase->account = new Recurly_Account();
$purchase->account->account_code = 'example-123';
$purchase->account->parent_account_code = 'example-124';
$subscription = new Recurly_Subscription();
$subscription->plan_code = 'silver';
$purchase->subscriptions = array($subscription);

// Create an invoice collection
try {
  $collection = Recurly_Purchase::invoice($purchase);
} catch (Recurly_Error $e) {
    print("There was an error!\n");
    print($e);
}

Remove a parent account by creating a purchase:

$purchase = new Recurly_Purchase();
$purchase->currency = 'USD';
$purchase->collection_method = 'automatic';
$purchase->account = new Recurly_Account();
$purchase->account->account_code = 'example-123';
$purchase->account->parent_account_code = '';
$subscription = new Recurly_Subscription();
$subscription->plan_code = 'silver';
$purchase->subscriptions = array($subscription);

// Create an invoice collection
try {
  $collection = Recurly_Purchase::invoice($purchase);
} catch (Recurly_Error $e) {
    print("There was an error!\n");
    print($e);
}

@aaron-junot aaron-junot changed the base branch from master to api_version_2_18 January 8, 2019 20:25
* Tests regarding account hierarchy.
*/
public function testAccountHierarchy() {
$this->client->addResponse('POST', '/accounts', 'accounts/hierarchy/create-201.xml');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason we need to create new fixtures for all these? Could we possibly re-use fixtures? I'm open to either but I find adding fixtures to be cumbersome since they are managed by hand. They tend to go stale quick.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm okay with it 👍

Copy link
Contributor

@bhelx bhelx left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@bhelx bhelx merged commit 39c2b02 into api_version_2_18 Jan 11, 2019
@bhelx bhelx deleted the parent-account branch January 11, 2019 22:03
aaron-junot pushed a commit that referenced this pull request Jan 18, 2019
Add support for account hierarchy
@bhelx bhelx added the V2 V2 Client label Mar 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
V2 V2 Client
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants