# Extension and action examples

<!-- Maintained by scripts/generate-package-readmes.php -->

Use the action functions with records and Data objects supplied by your application.
They pass each argument to the package operation and return its result.

These adapters show container registration. Use the owning package registry when
a contract requires contributor discovery.

Contract adapters wrap an existing implementation. Call their registration function
from your service provider with that implementation; tagged contracts keep their declared tag.
Resolve the backend by its concrete class before registration so the replacement contract
does not resolve itself. Static contract metadata uses one backend class per adapter.

## Contract `Capell\Payments\Contracts\PaymentFulfillmentHandler`

<!-- example: contract Capell\Payments\Contracts\PaymentFulfillmentHandler -->

```php
<?php
declare(strict_types=1);

namespace App\CapellExamples\Payments;

final class PaymentFulfillmentHandlerAdapter implements \Capell\Payments\Contracts\PaymentFulfillmentHandler
{
    public function __construct(private readonly \Capell\Payments\Contracts\PaymentFulfillmentHandler $backend) {}

    #[\Override]
    public function fulfill(\Capell\Payments\Models\CheckoutSession $checkoutSession): \Capell\Payments\Data\PaymentFulfillmentResultData
    {
        return $this->backend->fulfill($checkoutSession);
    }

    #[\Override]
    public function key(): string
    {
        return $this->backend->key();
    }

    #[\Override]
    public function supports(\Capell\Payments\Models\CheckoutSession $checkoutSession): bool
    {
        return $this->backend->supports($checkoutSession);
    }
}

function registerPaymentFulfillmentHandlerAdapter(\Capell\Payments\Contracts\PaymentFulfillmentHandler $backend): void
{
    app()->bind(PaymentFulfillmentHandlerAdapter::class, static fn (): PaymentFulfillmentHandlerAdapter => new PaymentFulfillmentHandlerAdapter($backend));
    app()->bind(\Capell\Payments\Contracts\PaymentFulfillmentHandler::class, PaymentFulfillmentHandlerAdapter::class);
    app()->tag([PaymentFulfillmentHandlerAdapter::class], \Capell\Payments\Contracts\PaymentFulfillmentHandler::TAG);
}
```

## Contract `Capell\Payments\Contracts\PaymentGateway`

<!-- example: contract Capell\Payments\Contracts\PaymentGateway -->

```php
<?php
declare(strict_types=1);

namespace App\CapellExamples\Payments;

final class PaymentGatewayAdapter implements \Capell\Payments\Contracts\PaymentGateway
{
    public function __construct(private readonly \Capell\Payments\Contracts\PaymentGateway $backend) {}

    #[\Override]
    public function createCheckoutSession(\Capell\Payments\Data\CreateCheckoutSessionData $data): \Capell\Payments\Data\CheckoutSessionData
    {
        return $this->backend->createCheckoutSession($data);
    }
}

function registerPaymentGatewayAdapter(\Capell\Payments\Contracts\PaymentGateway $backend): void
{
    app()->bind(PaymentGatewayAdapter::class, static fn (): PaymentGatewayAdapter => new PaymentGatewayAdapter($backend));
    app()->bind(\Capell\Payments\Contracts\PaymentGateway::class, PaymentGatewayAdapter::class);
}
```

## Action `billingPortal`

<!-- example: action billingPortal -->

```php
<?php
declare(strict_types=1);

namespace App\CapellExamples\Payments;

function runBillingPortal(\Capell\Payments\Data\CreateBillingPortalSessionData $data): \Capell\Payments\Data\BillingPortalSessionData
{
    return \Capell\Payments\Actions\CreateBillingPortalSessionAction::run($data);
}
```

## Action `checkout`

<!-- example: action checkout -->

```php
<?php
declare(strict_types=1);

namespace App\CapellExamples\Payments;

function runCheckout(\Capell\Payments\Data\CreateCheckoutSessionData $data): \Capell\Payments\Models\CheckoutSession
{
    return \Capell\Payments\Actions\CreateCheckoutSessionAction::run($data);
}
```

## Action `paidDownloadUrl`

<!-- example: action paidDownloadUrl -->

```php
<?php
declare(strict_types=1);

namespace App\CapellExamples\Payments;

function runPaidDownloadUrl(\Capell\Payments\Models\PaymentDownloadEntitlement $entitlement, ?int $ttlMinutes = null): string
{
    return \Capell\Payments\Actions\CreatePaidDownloadUrlAction::run($entitlement, $ttlMinutes);
}
```

## Action `subscriptionEntitlement`

<!-- example: action subscriptionEntitlement -->

```php
<?php
declare(strict_types=1);

namespace App\CapellExamples\Payments;

function runSubscriptionEntitlement(?string $providerCustomerId = null, ?string $billableType = null, ?string $billableId = null, \Capell\Payments\Enums\PaymentProvider $provider = \Capell\Payments\Enums\PaymentProvider::Stripe, ?\Carbon\CarbonInterface $now = null): \Capell\Payments\Data\SubscriptionEntitlementData
{
    return \Capell\Payments\Actions\ResolveSubscriptionEntitlementAction::run($providerCustomerId, $billableType, $billableId, $provider, $now);
}
```