mirror of
https://github.com/anikeen-com/id.git
synced 2026-11-07 16:18:33 +00:00
trial_payment_method (TrialPaymentMethod::REQUIRED, the default, or ::OPTIONAL) on app subscriptions, subscriptions and checkout sessions: an optional trial can be confirmed without a payment method and is cancelled at its end if there is still none (cancellation_reason and subscription.cancelled data.reason CancellationReason::TRIAL_ENDED_WITHOUT_PAYMENT_METHOD). subscription.trial_will_end carries data.payment_method_missing.
125 lines
4.4 KiB
PHP
125 lines
4.4 KiB
PHP
<?php
|
|
|
|
namespace Anikeen\Id\Resources;
|
|
|
|
use Anikeen\Id\Concerns\HasBillable;
|
|
use Anikeen\Id\Concerns\HasConfirmationUrl;
|
|
use Anikeen\Id\Concerns\HasSubscriptionStatus;
|
|
use Anikeen\Id\Contracts\AppTokenRepository;
|
|
use Throwable;
|
|
|
|
/**
|
|
* @property string $id
|
|
* @property string $name
|
|
* @property string $description
|
|
* @property string $unit
|
|
* @property float $price
|
|
* @property ?float $monthly_cap net, at most this per calendar month (hour and day units)
|
|
* @property ?float $expected_monthly_price gross estimate for a month (720 hours, at most the monthly cap)
|
|
* @property ?string $predecessor_id the subscription this one replaced (a resize); they share the monthly cap
|
|
* @property float $vat_rate
|
|
* @property array $payload
|
|
* @property bool $active
|
|
* @property bool $autorenew
|
|
* @property bool $cancelled
|
|
* @property ?string $status draft, trial, active, paused, cancelling, expired or cancelled, see status()
|
|
* @property ?string $starts_at
|
|
* @property string $ends_at
|
|
* @property ?string $cancels_at when a scheduled cancellation takes effect
|
|
* @property ?string $trial_ends_at
|
|
* @property ?string $trial_payment_method required or optional with a trial (TrialPaymentMethod), null without
|
|
* @property ?string $cancellation_reason why the billing cancelled it (CancellationReason), null otherwise
|
|
* @property ?object $discount {label, percent_off, amount_off, duration, duration_in_periods, periods_remaining}; periods_remaining is null for `forever`, 0 once it ended
|
|
* @property null|string $confirmation_url where the user confirms it (third-party apps)
|
|
*/
|
|
class Subscription extends BaseResource
|
|
{
|
|
use HasBillable;
|
|
use HasConfirmationUrl;
|
|
use HasSubscriptionStatus;
|
|
|
|
/**
|
|
* Update a given subscription from the current user.
|
|
*
|
|
* @param array{
|
|
* group: string,
|
|
* name: string,
|
|
* description: null|string,
|
|
* unit: string,
|
|
* price: float,
|
|
* monthly_cap?: null|float,
|
|
* vat_rate: float,
|
|
* payload: null|array,
|
|
* ends_at: null|string
|
|
* } $attributes The subscription data:
|
|
* - group: The group (optional)
|
|
* - name: The name (required when set)
|
|
* - description: The description (optional)
|
|
* - unit: The unit (required when set, e.g. "hour", "day", "week", "month", "year")
|
|
* - price: The price per unit (required when set)
|
|
* - monthly_cap: Net, at most this per calendar month (hour and day units; optional)
|
|
* - vat_rate: The VAT rate (required when set)
|
|
* - payload: The payload (optional)
|
|
* - ends_at: The end date (optional)
|
|
* @throws Throwable
|
|
*/
|
|
public function update(array $attributes): self
|
|
{
|
|
return (new self(fn() => $this->billable->anikeenId()
|
|
->request('PUT', sprintf('v1/subscriptions/%s', $this->id), $attributes)))
|
|
->setBillable($this->billable);
|
|
}
|
|
|
|
/**
|
|
* Force given subscription to check out (trusted apps only).
|
|
*
|
|
* @throws Throwable
|
|
*/
|
|
public function checkout(): self
|
|
{
|
|
return (new self(fn() => $this->billable->anikeenId()
|
|
->request('PUT', sprintf('v1/subscriptions/%s/checkout', $this->id))))
|
|
->setBillable($this->billable);
|
|
}
|
|
|
|
/**
|
|
* Revoke a given running subscription from the current user.
|
|
*
|
|
* @throws Throwable
|
|
*/
|
|
public function revoke(bool $refund = false): self
|
|
{
|
|
$attributes = [
|
|
'refund' => $refund,
|
|
];
|
|
|
|
return (new self(fn() => $this->billable->anikeenId()
|
|
->request('PUT', sprintf('v1/subscriptions/%s/revoke', $this->id), $attributes)))
|
|
->setBillable($this->billable);
|
|
}
|
|
|
|
/**
|
|
* Pause a given running subscription from the current user.
|
|
*
|
|
* @throws Throwable
|
|
*/
|
|
public function pause(): self
|
|
{
|
|
return (new self(fn() => $this->billable->anikeenId()
|
|
->request('PUT', sprintf('v1/subscriptions/%s/pause', $this->id))))
|
|
->setBillable($this->billable);
|
|
}
|
|
|
|
/**
|
|
* Resume a given running subscription from the current user.
|
|
*
|
|
* @throws Throwable
|
|
*/
|
|
public function resume(): self
|
|
{
|
|
return (new self(fn() => $this->billable->anikeenId()
|
|
->request('PUT', sprintf('v1/subscriptions/%s/resume', $this->id))))
|
|
->setBillable($this->billable);
|
|
}
|
|
}
|