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

Usage Based Billing #227

Merged
merged 1 commit into from
Jun 6, 2016
Merged

Usage Based Billing #227

merged 1 commit into from
Jun 6, 2016

Conversation

bhelx
Copy link
Contributor

@bhelx bhelx commented May 19, 2016

This is a feature that is not yet enabled on all sites and this PR is still a WIP.

This introduces some new resources and updates to current resources to
support the usage based billing (metered billing) feature.

https://docs.recurly.com/docs/usage-based-billing

Merged Ruby Version: recurly/recurly-client-ruby#237

Testing

$units = array();
foreach(Recurly_MeasuredUnitList::get() as $unit) {
  $units[$unit->name] = $unit;
}

if (isset($units['email'])) {
  $unit = $units['email'];
}
else {
  $unit = new Recurly_MeasuredUnit();
  $unit->name = 'email';
  $unit->display_name = 'email';
  $unit->description = 'An email';
  $unit->create();
}

$plan_code = uniqid();

$plan = new Recurly_Plan();
$plan->plan_code = $plan_code;
$plan->name = $plan_code;
$plan->unit_amount_in_cents->addCurrency('USD', 1000); // USD 10.00 month
$plan->plan_interveral_length = 1;
$plan->plan_interveral_unit = 'months';
$plan->create();

print "Plan: $plan\n\n";

$add_on = new Recurly_Addon();
$add_on->plan_code = $plan_code;
$add_on->add_on_code = 'marketing_emails';
$add_on->name = 'Marketing Emails';
$add_on->unit_amount_in_cents->addCurrency('USD', 5);
$add_on->measured_unit_id = $unit->id;
$add_on->add_on_type = 'usage';
$add_on->usage_type = 'price';
$add_on->optional = true;

$add_on->create();

print "AddOn: $add_on\n\n";

$subscription = new Recurly_Subscription();
$subscription->currency = 'USD';
$subscription->plan_code = $plan_code;
$subscription->collection_method = 'manual';

$account = new Recurly_Account();
$account->account_code = 'usageaccount';
$subscription->account = $account;

$addon = new Recurly_SubscriptionAddOn();
$addon->add_on_code = 'marketing_emails';

$subscription->subscription_add_ons = array($addon);

$subscription->create();

print "Subscription: $subscription\n\n";

sleep(1);

$usage = $subscription->buildUsage('marketing_emails');

print $usage;

$usage->amount = 100;
$usage->merchant_tag = "100 emails";
$usage->recording_timestamp = new DateTime();
sleep(3);
$usage->usage_timestamp = new DateTime();

$usage->create();

$usages = $subscription->usages('marketing_emails');

print "Usages: \n";

print count($usages);
foreach ($usages as $usage) {
  print $usage->merchant_tag . "\n";
}

// usages optionally takes params

// billing_status ["all" || "unbilled" || "billed"]
// datetime_type ["usage" || "recording"]
$params = array(
  "billing_status" => "all",
  "datetime_type" => "usage",
  #start_datetime: datetime,
  #end_datetime: datetime,
);

$usages = $subscription->usages('marketing_emails', $params);

print count($usages);
foreach ($usages as $usage) {
  print $usage->merchant_tag . "\n";
}

public static function init()
{
Recurly_Addon::$_writeableAttributes = array(
'id','name','display_name','description'
Copy link

Choose a reason for hiding this comment

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

They can set the id?

@bhelx bhelx force-pushed the usage_based branch 3 times, most recently from 3cc7af9 to 1a637c3 Compare May 25, 2016 18:12
protected function getNodeName() {
return 'coupons';
}
}
Copy link

Choose a reason for hiding this comment

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

Should this be getting deleted?

@bhelx bhelx removed the WIP label May 26, 2016
@bhelx bhelx force-pushed the usage_based branch 2 times, most recently from 9746223 to b029d47 Compare May 26, 2016 20:23
$measured_unit = Recurly_MeasuredUnit::get(123456, $this->client);

$this->assertInstanceOf('Recurly_MeasuredUnit', $measured_unit);
$this->assertEquals(123456, $measured_unit->id);
Copy link

Choose a reason for hiding this comment

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

Let's go ahead and check the name, display_name and description values as well.

<created_at type="date">2016-03-15T23:18:12+00:00</created_at>
<updated_at nil="nil"></updated_at>
<billed_at nil="nil"></billed_at>
<usage_type>PERCENTAGE</usage_type>
Copy link

Choose a reason for hiding this comment

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

Now that this is getting switched to lowecase you want to go ahead and down case it here too?

@drewish
Copy link

drewish commented Jun 6, 2016

So aside from that case change this looks good. We can do that here or in a followup.

@bhelx
Copy link
Contributor Author

bhelx commented Jun 6, 2016

Okay, updated

<created_at type="date">2016-03-15T23:18:12+00:00</created_at>
<updated_at nil="nil"></updated_at>
<billed_at nil="nil"></billed_at>
<usage_type>PERCENTAGE</usage_type>
Copy link

Choose a reason for hiding this comment

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

sorry should have called this one out too

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, missed that too

@drewish
Copy link

drewish commented Jun 6, 2016

+1

@drewish drewish merged commit 8154782 into master Jun 6, 2016
@drewish drewish deleted the usage_based branch June 6, 2016 18:16
@bhelx bhelx mentioned this pull request Jun 7, 2016
@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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants