Skip to content

Commit

Permalink
Merge pull request #460 from stripe/alexander/flexible-billing
Browse files Browse the repository at this point in the history
Flexible/Metered Billing API support
  • Loading branch information
brandur-stripe authored Apr 11, 2018
2 parents 82e5743 + 67e8c4f commit 1a8adb9
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions init.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
require(dirname(__FILE__) . '/lib/Topup.php');
require(dirname(__FILE__) . '/lib/Transfer.php');
require(dirname(__FILE__) . '/lib/TransferReversal.php');
require(dirname(__FILE__) . '/lib/UsageRecord.php');

// OAuth
require(dirname(__FILE__) . '/lib/OAuth.php');
Expand Down
4 changes: 4 additions & 0 deletions lib/Plan.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
* @property string $id
* @property string $object
* @property int $amount
* @property string $billing_scheme
* @property int $created
* @property string $currency
* @property string $interval
Expand All @@ -18,7 +19,10 @@
* @property StripeObject $metadata
* @property string $nickname
* @property string $product
* @property array $tiers
* @property string $tiers_mode
* @property int $trial_period_days
* @property string $usage_type
*/
class Plan extends ApiResource
{
Expand Down
41 changes: 41 additions & 0 deletions lib/UsageRecord.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Stripe;

/**
* Class UsageRecord
*
* @package Stripe
*
* @property string $id
* @property string $object
* @property bool $livemode
* @property int $quantity
* @property string $subscription_item
* @property int $timestamp
*/
class UsageRecord extends ApiResource
{
/**
* @param array|null $params
* @param array|string|null $options
*
* @return \Stripe\ApiResource The created resource.
*/
public static function create($params = null, $options = null)
{
self::_validateParams($params);
if (!array_key_exists('subscription_item', $params)) {
throw new Error\InvalidRequest("Missing subscription_item param in request", null);
}
$subscription_item = $params['subscription_item'];
$url = "/v1/subscription_items/$subscription_item/usage_records";
$request_params = $params;
unset($request_params['subscription_item']);

list($response, $opts) = static::_staticRequest('post', $url, $request_params, $options);
$obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
$obj->setLastResponse($response);
return $obj;
}
}
1 change: 1 addition & 0 deletions lib/Util/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public static function convertToStripeObject($resp, $opts)
'topup' => 'Stripe\\Topup',
'transfer' => 'Stripe\\Transfer',
'transfer_reversal' => 'Stripe\\TransferReversal',
'usage_record' => 'Stripe\\UsageRecord',
];
if (self::isList($resp)) {
$mapped = [];
Expand Down
35 changes: 35 additions & 0 deletions tests/Stripe/UsageRecordTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Stripe;

class UsageRecordTest extends TestCase
{
const TEST_RESOURCE_ID = 'usage_record';

public function testIsCreatable()
{
$this->expectsRequest(
'post',
'/v1/subscription_items/si_123/usage_records'
);
$resource = UsageRecord::create([
'subscription_item' => 'si_123',
'quantity' => 100,
'timestamp' => 12341234,
'action' => 'set'
]);
$this->assertInstanceOf("Stripe\\UsageRecord", $resource);
}

/**
* @expectedException \Stripe\Error\InvalidRequest
*/
public function testThrowsIfSubscriptionItemIsMissing()
{
UsageRecord::create([
'quantity' => 100,
'timestamp' => 12341234,
'action' => 'set'
]);
}
}

0 comments on commit 1a8adb9

Please sign in to comment.