From 67e8c4fbf6139f48416ac67ffb70471abddc85d6 Mon Sep 17 00:00:00 2001 From: Alexander Thiemann Date: Wed, 4 Apr 2018 12:01:20 -0700 Subject: [PATCH] flexible and metered billing support --- init.php | 1 + lib/Plan.php | 4 ++++ lib/UsageRecord.php | 41 ++++++++++++++++++++++++++++++++ lib/Util/Util.php | 1 + tests/Stripe/UsageRecordTest.php | 35 +++++++++++++++++++++++++++ 5 files changed, 82 insertions(+) create mode 100644 lib/UsageRecord.php create mode 100644 tests/Stripe/UsageRecordTest.php diff --git a/init.php b/init.php index 79cf657a7..356c8f5e3 100644 --- a/init.php +++ b/init.php @@ -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'); diff --git a/lib/Plan.php b/lib/Plan.php index 8b0126b6f..6b670182d 100644 --- a/lib/Plan.php +++ b/lib/Plan.php @@ -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 @@ -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 { diff --git a/lib/UsageRecord.php b/lib/UsageRecord.php new file mode 100644 index 000000000..9afbb3e1e --- /dev/null +++ b/lib/UsageRecord.php @@ -0,0 +1,41 @@ +json, $opts); + $obj->setLastResponse($response); + return $obj; + } +} diff --git a/lib/Util/Util.php b/lib/Util/Util.php index 82fd337dd..e55a35cdd 100644 --- a/lib/Util/Util.php +++ b/lib/Util/Util.php @@ -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 = []; diff --git a/tests/Stripe/UsageRecordTest.php b/tests/Stripe/UsageRecordTest.php new file mode 100644 index 000000000..c5fe37306 --- /dev/null +++ b/tests/Stripe/UsageRecordTest.php @@ -0,0 +1,35 @@ +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' + ]); + } +}