Skip to content

Commit

Permalink
feat: [sc-39698] Add Subscription Events for chartmogul PHP library (#99
Browse files Browse the repository at this point in the history
)

* Add Subscription Events for chartmogul PHP [sc-39698]
  • Loading branch information
dina920 authored Aug 16, 2022
1 parent 9519936 commit 86b11c0
Show file tree
Hide file tree
Showing 11 changed files with 515 additions and 21 deletions.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class Client implements ClientInterface
/**
* @var string
*/
private $apiVersion = '5.0.0';
private $apiVersion = '5.0.1';

/**
* @var string
Expand Down
76 changes: 58 additions & 18 deletions src/Resource/AbstractResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
29 changes: 29 additions & 0 deletions src/Resource/MetaCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace ChartMogul\Resource;

use Doctrine\Common\Collections\ArrayCollection;

class MetaCollection extends ArrayCollection
{
/**
* @var int
*/
public $next_key;
/**
* @var int
*/
public $prev_key;
/**
* @var int
*/
public $page;
/**
* @var int
*/
public $total_pages;
/**
* @var string
*/
public $before_key;
}
13 changes: 13 additions & 0 deletions src/Resource/SubscriptionEventCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace ChartMogul\Resource;

use Doctrine\Common\Collections\ArrayCollection;

class SubscriptionEventCollection extends ArrayCollection
{
/**
* @array
*/
public $meta;
}
23 changes: 23 additions & 0 deletions src/Service/DestroyWithParamsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace ChartMogul\Service;

use ChartMogul\Http\ClientInterface;

/**
* @codeCoverageIgnore
*/
trait DestroyWithParamsTrait
{
/**
* Delete a resource
* @return boolean
*/
public static function destroyWithParams(array $params = [], ClientInterface $client = null)
{
return (new RequestService($client))
->setResourceClass(static::class)
->setResourcePath(static::RESOURCE_PATH)
->destroyWithParams($params);
}
}
47 changes: 46 additions & 1 deletion src/Service/RequestService.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use ChartMogul\Http\ClientInterface;
use ChartMogul\Resource\AbstractResource;
use ReflectionClass;
use ChartMogul\Exceptions\ChartMogulException;

class RequestService
{
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
25 changes: 25 additions & 0 deletions src/Service/UpdateWithParamsTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace ChartMogul\Service;

use ChartMogul\Http\ClientInterface;

/**
* @codeCoverageIgnore
*/
trait UpdateWithParamsTrait
{
/**
* Update a Resource
* @param array $params
* @param ClientInterface|null $client
* @return self
*/
public static function updateWithParams(array $params = [], ClientInterface $client = null)
{
return (new RequestService($client))
->setResourceClass(static::class)
->setResourcePath(static::RESOURCE_PATH)
->updateWithParams($params);
}
}
48 changes: 48 additions & 0 deletions src/SubscriptionEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace ChartMogul;

use ChartMogul\Http\ClientInterface;
use ChartMogul\Resource\AbstractResource;
use ChartMogul\Service\AllTrait;
use ChartMogul\Service\CreateTrait;
use ChartMogul\Service\UpdateWithParamsTrait;
use ChartMogul\Service\DestroyWithParamsTrait;
use ChartMogul\Resource\Collection;
use ChartMogul\Resource\SubscriptionEventCollection;
use ChartMogul\Resource\MetaCollection;

class SubscriptionEvent extends AbstractResource
{
use AllTrait;
use CreateTrait;
use UpdateWithParamsTrait;
use DestroyWithParamsTrait;

/**
* @ignore
*/
public const RESOURCE_PATH = '/v1/subscription_events';

/**
* @ignore
*/
public const RESOURCE_NAME = 'SubscriptionEvent';

/**
* @ignore
*/
public const ROOT_KEY = 'subscription_events';

protected $id;
protected $external_id;
protected $data_source_uuid;
protected $customer_external_id;
protected $event_type;
protected $event_date;
protected $effective_date;
protected $currency;
protected $amount_in_cents;
protected $subscription_external_id;
protected $plan_external_id;
}
2 changes: 1 addition & 1 deletion tests/Unit/Http/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function testGetUserAgent()
->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()
Expand Down
Loading

0 comments on commit 86b11c0

Please sign in to comment.