Meter.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. // File generated from our OpenAPI spec
  3. namespace Stripe\Billing;
  4. /**
  5. * A billing meter is a resource that allows you to track usage of a particular event. For example, you might create a billing meter to track the number of API calls made by a particular user. You can then attach the billing meter to a price and attach the price to a subscription to charge the user for the number of API calls they make.
  6. *
  7. * @property string $id Unique identifier for the object.
  8. * @property string $object String representing the object's type. Objects of the same type share the same value.
  9. * @property int $created Time at which the object was created. Measured in seconds since the Unix epoch.
  10. * @property \Stripe\StripeObject $customer_mapping
  11. * @property \Stripe\StripeObject $default_aggregation
  12. * @property string $display_name The meter's name.
  13. * @property string $event_name The name of the meter event to record usage for. Corresponds with the <code>event_name</code> field on meter events.
  14. * @property null|string $event_time_window The time window to pre-aggregate meter events for, if any.
  15. * @property bool $livemode Has the value <code>true</code> if the object exists in live mode or the value <code>false</code> if the object exists in test mode.
  16. * @property string $status The meter's status.
  17. * @property \Stripe\StripeObject $status_transitions
  18. * @property int $updated Time at which the object was last updated. Measured in seconds since the Unix epoch.
  19. * @property \Stripe\StripeObject $value_settings
  20. */
  21. class Meter extends \Stripe\ApiResource
  22. {
  23. const OBJECT_NAME = 'billing.meter';
  24. use \Stripe\ApiOperations\NestedResource;
  25. use \Stripe\ApiOperations\Update;
  26. const EVENT_TIME_WINDOW_DAY = 'day';
  27. const EVENT_TIME_WINDOW_HOUR = 'hour';
  28. const STATUS_ACTIVE = 'active';
  29. const STATUS_INACTIVE = 'inactive';
  30. /**
  31. * Creates a billing meter.
  32. *
  33. * @param null|array $params
  34. * @param null|array|string $options
  35. *
  36. * @throws \Stripe\Exception\ApiErrorException if the request fails
  37. *
  38. * @return \Stripe\Billing\Meter the created resource
  39. */
  40. public static function create($params = null, $options = null)
  41. {
  42. self::_validateParams($params);
  43. $url = static::classUrl();
  44. list($response, $opts) = static::_staticRequest('post', $url, $params, $options);
  45. $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
  46. $obj->setLastResponse($response);
  47. return $obj;
  48. }
  49. /**
  50. * Retrieve a list of billing meters.
  51. *
  52. * @param null|array $params
  53. * @param null|array|string $opts
  54. *
  55. * @throws \Stripe\Exception\ApiErrorException if the request fails
  56. *
  57. * @return \Stripe\Collection<\Stripe\Billing\Meter> of ApiResources
  58. */
  59. public static function all($params = null, $opts = null)
  60. {
  61. $url = static::classUrl();
  62. return static::_requestPage($url, \Stripe\Collection::class, $params, $opts);
  63. }
  64. /**
  65. * Retrieves a billing meter given an ID.
  66. *
  67. * @param array|string $id the ID of the API resource to retrieve, or an options array containing an `id` key
  68. * @param null|array|string $opts
  69. *
  70. * @throws \Stripe\Exception\ApiErrorException if the request fails
  71. *
  72. * @return \Stripe\Billing\Meter
  73. */
  74. public static function retrieve($id, $opts = null)
  75. {
  76. $opts = \Stripe\Util\RequestOptions::parse($opts);
  77. $instance = new static($id, $opts);
  78. $instance->refresh();
  79. return $instance;
  80. }
  81. /**
  82. * Updates a billing meter.
  83. *
  84. * @param string $id the ID of the resource to update
  85. * @param null|array $params
  86. * @param null|array|string $opts
  87. *
  88. * @throws \Stripe\Exception\ApiErrorException if the request fails
  89. *
  90. * @return \Stripe\Billing\Meter the updated resource
  91. */
  92. public static function update($id, $params = null, $opts = null)
  93. {
  94. self::_validateParams($params);
  95. $url = static::resourceUrl($id);
  96. list($response, $opts) = static::_staticRequest('post', $url, $params, $opts);
  97. $obj = \Stripe\Util\Util::convertToStripeObject($response->json, $opts);
  98. $obj->setLastResponse($response);
  99. return $obj;
  100. }
  101. /**
  102. * @param null|array $params
  103. * @param null|array|string $opts
  104. *
  105. * @throws \Stripe\Exception\ApiErrorException if the request fails
  106. *
  107. * @return \Stripe\Billing\Meter the deactivated meter
  108. */
  109. public function deactivate($params = null, $opts = null)
  110. {
  111. $url = $this->instanceUrl() . '/deactivate';
  112. list($response, $opts) = $this->_request('post', $url, $params, $opts);
  113. $this->refreshFrom($response, $opts);
  114. return $this;
  115. }
  116. /**
  117. * @param null|array $params
  118. * @param null|array|string $opts
  119. *
  120. * @throws \Stripe\Exception\ApiErrorException if the request fails
  121. *
  122. * @return \Stripe\Billing\Meter the reactivated meter
  123. */
  124. public function reactivate($params = null, $opts = null)
  125. {
  126. $url = $this->instanceUrl() . '/reactivate';
  127. list($response, $opts) = $this->_request('post', $url, $params, $opts);
  128. $this->refreshFrom($response, $opts);
  129. return $this;
  130. }
  131. const PATH_EVENT_SUMMARIES = '/event_summaries';
  132. /**
  133. * @param string $id the ID of the meter on which to retrieve the meter event summaries
  134. * @param null|array $params
  135. * @param null|array|string $opts
  136. *
  137. * @throws \Stripe\Exception\ApiErrorException if the request fails
  138. *
  139. * @return \Stripe\Collection<\Stripe\Billing\MeterEventSummary> the list of meter event summaries
  140. */
  141. public static function allEventSummaries($id, $params = null, $opts = null)
  142. {
  143. return self::_allNestedResources($id, static::PATH_EVENT_SUMMARIES, $params, $opts);
  144. }
  145. }