diff --git a/README.md b/README.md index 0d695a8..c86b152 100644 --- a/README.md +++ b/README.md @@ -345,6 +345,51 @@ $plan = ChartMogul\Plan::update(["plan_uuid" => $plan->uuid], [ ]); ``` +### Subscription Events + +**List Subscriptions Events** + +```php +$subscription_events = ChartMogul\SubscriptionEvent::all(); +``` + +**Create Subscription Event** + +```php +ChartMogul\SubscriptionEvent::create(['subscription_event' => [ + "external_id" => "evnt_026", + "customer_external_id" => "cus_0003", + "data_source_uuid" => $ds->uuid, + "event_type" => "subscription_start_scheduled", + "event_date" => "2022-03-30", + "effective_date" => "2022-04-01", + "subscription_external_id" => "sub_0001", + "plan_external_id" => "plan_0001", + "currency" => "USD", + "amount_in_cents" => 1000 + "quantity" => 1 +]]); +``` + +**Delete Subscription Event** + +```php +ChartMogul\SubscriptionEvent::destroyWithParams(['subscription_event' => [ + "id" => "some_event_id" +]]); +``` + +**Update Subscription Event** + +```php +ChartMogul\SubscriptionEvent::updateWithParams(['subscription_event' => [ + "id" => "some_event_id", + "currency" => "EUR", + "amount_in_cents" => "100" +]]); +``` + + ### Plan Groups **Create a Plan Group** diff --git a/src/Http/Client.php b/src/Http/Client.php index 4c377cb..44b3d15 100644 --- a/src/Http/Client.php +++ b/src/Http/Client.php @@ -30,7 +30,7 @@ class Client implements ClientInterface /** * @var string */ - private $apiVersion = '5.0.0'; + private $apiVersion = '5.0.1'; /** * @var string diff --git a/src/Resource/AbstractResource.php b/src/Resource/AbstractResource.php index c83cc5a..6222bef 100644 --- a/src/Resource/AbstractResource.php +++ b/src/Resource/AbstractResource.php @@ -72,28 +72,68 @@ public function setClient(ClientInterface $client) public static function fromArray(array $data, ClientInterface $client = null) { if (isset($data[static::ROOT_KEY])) { - $array = new Collection(array_map(function ($data) use ($client) { - return static::fromArray($data, $client); - }, $data[static::ROOT_KEY])); - // The following are subject to change soon, so they are optional. - if (isset($data["current_page"])) { - $array->current_page = $data["current_page"]; - } - if (isset($data["total_pages"])) { - $array->total_pages = $data["total_pages"]; - } - if (isset($data["has_more"])) { - $array->has_more = $data["has_more"]; - } - if (isset($data["per_page"])) { - $array->per_page = $data["per_page"]; - } - if (isset($data["page"])) { - $array->page = $data["page"]; + if (static::ROOT_KEY != "subscription_events") { + $array = new Collection(array_map(function ($data) use ($client) { + return static::fromArray($data, $client); + }, $data[static::ROOT_KEY])); + // The following are subject to change soon, so they are optional. + $array = static::allData($data, $array); + } else { + $array = new SubscriptionEventCollection(array_map(function ($data) use ($client) { + return static::fromArray($data, $client); + }, $data[static::ROOT_KEY])); + + if (isset($data["meta"])) { + $meta = $data['meta']; + $array->meta = static::metaData($meta); + } } return $array; } return new static($data, $client); } + + public static function allData(array $data, Collection $array) + { + if (isset($data["current_page"])) { + $array->current_page = $data["current_page"]; + } + if (isset($data["total_pages"])) { + $array->total_pages = $data["total_pages"]; + } + if (isset($data["has_more"])) { + $array->has_more = $data["has_more"]; + } + if (isset($data["per_page"])) { + $array->per_page = $data["per_page"]; + } + if (isset($data["page"])) { + $array->page = $data["page"]; + } + + return $array; + } + + public static function metaData(array $meta) + { + $array_meta = new MetaCollection(); + if (isset($meta['page'])) { + $array_meta->page = $meta["page"]; + } + if (isset($meta['next_key'])) { + $array_meta->next_key = $meta["next_key"]; + } + if (isset($meta['prev_key'])) { + $array_meta->prev_key = $meta["prev_key"]; + } + if (isset($meta['before_key'])) { + $array_meta->before_key = $meta["before_key"]; + } + if (isset($meta['total_pages'])) { + $array_meta->total_pages = $meta["total_pages"]; + } + + return $array_meta; + } } diff --git a/src/Resource/MetaCollection.php b/src/Resource/MetaCollection.php new file mode 100644 index 0000000..9f406a3 --- /dev/null +++ b/src/Resource/MetaCollection.php @@ -0,0 +1,29 @@ +setResourceClass(static::class) + ->setResourcePath(static::RESOURCE_PATH) + ->destroyWithParams($params); + } +} diff --git a/src/Service/RequestService.php b/src/Service/RequestService.php index cee026f..ff6b318 100644 --- a/src/Service/RequestService.php +++ b/src/Service/RequestService.php @@ -6,6 +6,7 @@ use ChartMogul\Http\ClientInterface; use ChartMogul\Resource\AbstractResource; use ReflectionClass; +use ChartMogul\Exceptions\ChartMogulException; class RequestService { @@ -107,7 +108,6 @@ public function update(array $id, array $data) { $class = $this->resourceClass; - $response = $this->client ->setResourceKey($class::RESOURCE_NAME) ->send($this->applyResourcePath($id), 'PATCH', $data); @@ -138,6 +138,51 @@ public function destroy() return true; } + public function updateWithParams(array $params) + { + $client = $this->client; + + if (!(array_key_exists('subscription_event', $params))) { + throw new \ChartMogul\Exceptions\SchemaInvalidException("Data is not in the good format, 'subscription_event' is missing."); + } + + $sub_ev = $params['subscription_event']; + + if (!(array_key_exists('id', $sub_ev) || (array_key_exists('data_source_uuid', $sub_ev) && array_key_exists('external_id', $sub_ev)))) { + throw new \ChartMogul\Exceptions\SchemaInvalidException("Param id or params external_id and data_source_uuid required."); + } + + $class = $this->resourceClass; + $response = $client->setResourceKey($class::RESOURCE_NAME) + ->send($this->applyResourcePath($id), 'PATCH', $params); + + return $class::fromArray($response, $this->client); + } + + /** + * @return boolean + */ + public function destroyWithParams(array $params) + { + $client = $this->client; + + if (!(array_key_exists('subscription_event', $params))) { + throw new \ChartMogul\Exceptions\SchemaInvalidException("Data is not in the good format, 'subscription_event' is missing."); + } + + $sub_ev = $params['subscription_event']; + + if (!(array_key_exists('id', $sub_ev) || (array_key_exists('data_source_uuid', $sub_ev) && array_key_exists('external_id', $sub_ev)))) { + throw new \ChartMogul\Exceptions\SchemaInvalidException("Param id or params external_id and data_source_uuid required."); + } + + $class = $this->resourceClass; + $response = $client->setResourceKey($class::RESOURCE_NAME) + ->send($this->applyResourcePath($id), 'DELETE', $params); + + return true; + } + public function get($uuid = null) { $class = $this->resourceClass; diff --git a/src/Service/UpdateWithParamsTrait.php b/src/Service/UpdateWithParamsTrait.php new file mode 100644 index 0000000..6c34cb6 --- /dev/null +++ b/src/Service/UpdateWithParamsTrait.php @@ -0,0 +1,25 @@ +setResourceClass(static::class) + ->setResourcePath(static::RESOURCE_PATH) + ->updateWithParams($params); + } +} diff --git a/src/SubscriptionEvent.php b/src/SubscriptionEvent.php new file mode 100644 index 0000000..cbc3c71 --- /dev/null +++ b/src/SubscriptionEvent.php @@ -0,0 +1,48 @@ +setMethods(null) ->getMock(); - $this->assertEquals("chartmogul-php/5.0.0/PHP-".PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION, $mock->getUserAgent()); + $this->assertEquals("chartmogul-php/5.0.1/PHP-".PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION, $mock->getUserAgent()); } public function testGetBasicAuthHeader() diff --git a/tests/Unit/SubscriptionEventTest.php b/tests/Unit/SubscriptionEventTest.php new file mode 100644 index 0000000..6224790 --- /dev/null +++ b/tests/Unit/SubscriptionEventTest.php @@ -0,0 +1,226 @@ +getMockClient(0, [200], $stream); + + $result = SubscriptionEvent::all([],$cmClient); + $request = $mockClient->getRequests()[0]; + + $this->assertEquals("GET", $request->getMethod()); + $uri = $request->getUri(); + $this->assertEquals("/v1/subscription_events", $uri->getPath()); + + $this->assertEquals(2, sizeof($result)); + $this->assertTrue($result[0] instanceof SubscriptionEvent); + $this->assertEquals("ds_1fm3eaac-62d0-31ec-clf4-4bf0mbe81aba", $result[0]->data_source_uuid); + $this->assertEquals("scus_023", $result[0]->customer_external_id); + $this->assertEquals(166, $result->meta->total_pages); + $this->assertEquals(1, $result->meta->page); + $this->assertEquals("2022-04-10T22:27:35.834Z", $result->meta->before_key); + } + + public function testCreateSubscriptionEvent() + { + $stream = Psr7\stream_for(SubscriptionEventTest::RETRIEVE_SUBSCRIPTION_EVENT); + list($cmClient, $mockClient) = $this->getMockClient(0, [200], $stream); + + $customer_external_id = 'scus_023'; + $data_source_uuid = "ds_1fm3eaac-62d0-31ec-clf4-4bf0mbe81aba"; + $event_type = 'subscription_start_scheduled'; + $event_date = '2022-03-30'; + $effective_date = '2022-04-01'; + + $result = SubscriptionEvent::create([ + "customer_external_id" => $customer_external_id, + "data_source_uuid" => $data_source_uuid, + "event_type" => $event_type, + "event_date" => $event_date, + "effective_date" => $effective_date, + ], + $cmClient + ); + + $request = $mockClient->getRequests()[0]; + + $this->assertEquals("POST", $request->getMethod()); + $uri = $request->getUri(); + $this->assertEquals("", $uri->getQuery()); + $this->assertEquals("/v1/subscription_events", $uri->getPath()); + + $this->assertTrue($result instanceof SubscriptionEvent); + $this->assertEquals("scus_023", $result->customer_external_id); + } + + public function testUpdateSubscriptionEventWithId() + { + $stream = Psr7\stream_for(SubscriptionEventTest::UPDATE_SUBSCRIPTION_EVENT); + list($cmClient, $mockClient) = $this->getMockClient(0, [200], $stream); + + $id = 73966836; + $new_amount = 100; + + $result = SubscriptionEvent::updateWithParams(['subscription_event' => [ + "id" => $id, 'amount_in_cents' => $new_amount + ]], + $cmClient + ); + + $request = $mockClient->getRequests()[0]; + $this->assertEquals("PATCH", $request->getMethod()); + $uri = $request->getUri(); + $this->assertEquals("", $uri->getQuery()); + $this->assertEquals("/v1/subscription_events", $uri->getPath()); + $this->assertTrue($result instanceof SubscriptionEvent); + $this->assertEquals($result->amount_in_cents, $new_amount); + } + + public function testUpdateSubscriptionEventWithDataSourceUuidAndExternalId() + { + $stream = Psr7\stream_for(SubscriptionEventTest::UPDATE_SUBSCRIPTION_EVENT); + list($cmClient, $mockClient) = $this->getMockClient(0, [200], $stream); + + $data_source_uuid = "ds_1fm3eaac-62d0-31ec-clf4-4bf0mbe81aba"; + $external_id = "ex_id_1"; + $new_amount = 100; + + $result = SubscriptionEvent::updateWithParams(['subscription_event' => [ + "data_source_uuid" => $data_source_uuid, "external_id" => $external_id, 'amount_in_cents' => $new_amount + ]],$cmClient + ); + + $request = $mockClient->getRequests()[0]; + $this->assertEquals("PATCH", $request->getMethod()); + $uri = $request->getUri(); + $this->assertEquals("", $uri->getQuery()); + $this->assertEquals("/v1/subscription_events", $uri->getPath()); + $this->assertTrue($result instanceof SubscriptionEvent); + $this->assertEquals($result->amount_in_cents, $new_amount); + } + + public function testDestroySubscriptionEventWithId() + { + list($cmClient, $mockClient) = $this->getMockClient(0, [204]); + $id = 73966836; + + $result = SubscriptionEvent::destroyWithParams(['subscription_event' => ["id" => $id]], $cmClient); + + $request = $mockClient->getRequests()[0]; + $this->assertEquals("DELETE", $request->getMethod()); + $uri = $request->getUri(); + $this->assertEquals("", $uri->getQuery()); + $this->assertEquals("/v1/subscription_events", $uri->getPath()); + $this->assertEquals($result, true); + } + + public function testDestroySubscriptionEventWithDataSourceUuidAndExternalId() + { + list($cmClient, $mockClient) = $this->getMockClient(0, [204]); + $data_source_uuid = "ds_1fm3eaac-62d0-31ec-clf4-4bf0mbe81aba"; + $external_id = "ex_id_1"; + + $result = SubscriptionEvent::destroyWithParams(['subscription_event' => [ + "data_source_uuid" => $data_source_uuid, "external_id" => $external_id] + ],$cmClient); + $request = $mockClient->getRequests()[0]; + $this->assertEquals("DELETE", $request->getMethod()); + $uri = $request->getUri(); + $this->assertEquals("", $uri->getQuery()); + $this->assertEquals("/v1/subscription_events", $uri->getPath()); + $this->assertEquals($result, true); + } +}