Skip to content

Commit

Permalink
Merge 5fe9f38 into e6c8951
Browse files Browse the repository at this point in the history
  • Loading branch information
XaosSintez committed Sep 22, 2021
2 parents e6c8951 + 5fe9f38 commit 7887e82
Show file tree
Hide file tree
Showing 7 changed files with 183 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Entities/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ public function __construct()
'subscriptions/{subscriptionId}/change-plan' => function (array $content) {
return new Subscription($content);
},
'subscriptions/{subscriptionId}/change-items' => function (array $content) {
return new Subscription($content);
},
'subscriptions/{subscriptionId}/interim-invoice' => function (array $content) {
return new Invoice($content);
},
Expand Down
150 changes: 150 additions & 0 deletions src/Entities/SubscriptionChangeItems.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php
/**
* This source file is proprietary and part of Rebilly.
*
* (c) Rebilly SRL
* Rebilly Ltd.
* Rebilly Inc.
*
* @see https://www.rebilly.com
*/

namespace Rebilly\Entities;

use DomainException;
use Rebilly\Entities\Subscriptions\PlanItem;
use Rebilly\Rest\Resource;

class SubscriptionChangeItems extends Resource
{
public const UNEXPECTED_RENEWAL_POLICY = 'Unexpected renewalPolicy. Only %s are supported';

public const RENEWAL_POLICY_RESET = 'reset';

public const RENEWAL_POLICY_RETAIN = 'retain';

/**
* @return array
*/
public static function renewalPolicies()
{
return [
self::RENEWAL_POLICY_RESET,
self::RENEWAL_POLICY_RETAIN,
];
}

/**
* @param array $value
*
* @return $this
*/
public function setItems(array $value)
{
return $this->setAttribute('items', $value);
}

/**
* @param array $data
*
* @return array|Subscriptions\PlanItem[]
*/
public function createItems(array $data)
{
return array_map(
function ($item) {
if ($item instanceof PlanItem) {
return $item;
}

return new Subscriptions\PlanItem($item);
},
$data
);
}

/**
* @param $value
*
* @throws DomainException
*/
public function setRenewalPolicy($value)
{
if (!in_array($value, self::renewalPolicies(), true)) {
throw new DomainException(sprintf(self::UNEXPECTED_RENEWAL_POLICY, implode(', ', self::renewalPolicies())));
}
$this->setAttribute('renewalPolicy', $value);
}

/**
* @param $value
*/
public function setProrated($value)
{
$this->setAttribute('prorated', $value);
}

/**
* @param $value
*/
public function setEffectiveTime($value)
{
$this->setAttribute('effectiveTime', $value);
}

/**
* @param $value
*/
public function setPreview($value)
{
$this->setAttribute('preview', $value);
}

/**
* @param $value
*/
public function setKeepTrial($value)
{
$this->setAttribute('keepTrial', $value);
}

/**
* @return bool
*/
public function getPreview()
{
return $this->getAttribute('preview');
}

/**
* @return bool
*/
public function getProrated()
{
return $this->getAttribute('prorated');
}

/**
* @return string
*/
public function getRenewalPolicy()
{
return $this->getAttribute('renewalPolicy');
}

/**
* @return string
*/
public function getEffectiveTime()
{
return $this->getAttribute('effectiveTime');
}

/**
* @return bool
*/
public function getKeepTrial()
{
return $this->getAttribute('keepTrial');
}
}
4 changes: 4 additions & 0 deletions src/Entities/SubscriptionChangePlan.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
use DomainException;
use Rebilly\Rest\Resource;

/**
* @deprecated
* @see SubscriptionChangeItems
*/
class SubscriptionChangePlan extends Resource
{
public const UNEXPECTED_RENEWAL_POLICY = 'Unexpected renewalPolicy. Only %s are supported';
Expand Down
21 changes: 21 additions & 0 deletions src/Services/SubscriptionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Rebilly\Entities\Invoice;
use Rebilly\Entities\Subscription;
use Rebilly\Entities\SubscriptionCancel;
use Rebilly\Entities\SubscriptionChangeItems;
use Rebilly\Entities\SubscriptionChangePlan;
use Rebilly\Entities\SubscriptionInterimInvoice;
use Rebilly\Http\Exception\DataValidationException;
Expand Down Expand Up @@ -113,6 +114,9 @@ public function cancel($subscriptionId, $data)
}

/**
* @deprecated
* @see changeItems
*
* @param string $subscriptionId
* @param array|JsonSerializable|SubscriptionChangePlan $data
*
Expand Down Expand Up @@ -176,4 +180,21 @@ public function issueUpcomingInvoice($subscriptionId, $invoiceId)
]
);
}

/**
* @param string $subscriptionId
* @param array|JsonSerializable|SubscriptionChangeItems $data
*
* @throws DataValidationException The input data does not valid
*
* @return Subscription
*/
public function changeItems($subscriptionId, $data)
{
return $this->client()->post(
$data,
'subscriptions/{subscriptionId}/change-items',
['subscriptionId' => $subscriptionId]
);
}
}
1 change: 1 addition & 0 deletions tests/Api/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ public function provideEntityClasses(): iterable
[Entities\RulesEngine\Actions\TriggerWebhook::class],
[Entities\RulesEngine\Actions\GatewayAccountPick\AcquirerWeights::class],
[Entities\RulesEngine\Actions\GatewayAccountPick\AccountWeights::class],
[Entities\SubscriptionChangeItems::class],
];

foreach ($cases as $case) {
Expand Down
3 changes: 3 additions & 0 deletions tests/Api/ServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,9 @@ public function subscriptionService()

$result = $service->issueUpcomingInvoice('dummy', 'invoice-1');
self::assertInstanceOf(Entities\Subscription::class, $result);

$result = $service->changeItems('dummy', []);
self::assertInstanceOf(Entities\Subscription::class, $result);
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ protected function getFakeValue($attribute, $class)
case 'items':
switch ($class) {
case Entities\Subscription::class:
case Entities\SubscriptionChangeItems::class:
return [
['planId' => 'plan-1', 'quantity' => 1],
['planId' => 'plan-2', 'quantity' => null],
Expand Down

0 comments on commit 7887e82

Please sign in to comment.