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

Flexible/Metered Billing API support #460

Merged
merged 1 commit into from
Apr 11, 2018
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
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'
]);
}
}