Browse documentationOpen

byrcsc/laravel-payrex · 1.x

Client and resources.

Use the PayRex facade or injected client, understand resource methods and options, and reach newly introduced API endpoints.

Laravel PayRex exposes one PayrexClient singleton. Use the facade for concise calls or inject the client when explicit dependencies are preferable:

use ByRcsc\LaravelPayrex\Facades\Payrex;
use ByRcsc\LaravelPayrex\PayrexClient;

$fromFacade = Payrex::customers()->retrieve('cus_...');

final class CreatePayment
{
    public function __construct(
        private readonly PayrexClient $payrex,
    ) {}

    public function __invoke(): void
    {
        $customer = $this->payrex->customers()->retrieve('cus_...');
    }
}

Both forms resolve to the same client instance in the application container. Each resource object is also created once and reused by that client.

Resource catalog

AccessorSupported methods
paymentIntents()create, retrieve, update, cancel, capture, attach
checkoutSessions()create, retrieve, list, autoPaging, paginate, expire
setupIntents()create, retrieve, cancel
customers()create, retrieve, update, delete, list, autoPaging, paginate, listPaymentMethods, deletePaymentMethod
customerSessions()create, retrieve
payments()retrieve, update
refunds()create, update
payouts()listTransactions
billingStatements()create, retrieve, update, delete, list, autoPaging, paginate, finalize, send, void, markUncollectible
billingStatementLineItems()create, update, delete
webhooks()create, retrieve, update, delete, list, autoPaging, paginate, enable, disable

Methods mirror the operations PayRex documents. For example, payments are created through payment intents rather than payments()->create(), and PayRex does not provide a retrieve route for a billing statement line item.

Named and typed parameters

Resource methods use named, typed arguments:

$customer = Payrex::customers()->create(
    name: 'Ada Lovelace',
    email: 'ada@example.com',
    metadata: ['account_id' => 'acct_1042'],
);

Enum objects and their equivalent string values are accepted where the method signature allows both. Enum values are converted to their API strings during form encoding.

Null arguments are omitted from the request. Passing null does not send an empty value and generally means “leave unchanged” on update methods.

The options escape hatch

Every resource operation has a trailing $options array. It is merged over the named parameters immediately before the request is encoded:

$intent = Payrex::paymentIntents()->create(
    amount: 10_000,
    paymentMethods: ['card'],
    options: [
        'payment_method_options' => [
            'card' => ['capture_type' => 'manual'],
        ],
        'new_api_field' => 'supported-before-the-next-release',
    ],
);

Use named parameters for modeled fields. Use options for a PayRex field that is documented but not yet represented in the installed package version. Because options win during the merge, they can also override a named parameter.

Low-level requests

The client exposes get, post, put, and delete for confirmed PayRex routes that the package does not model yet:

$payload = Payrex::get('/new_resource/res_123', [
    'expand' => ['customer'],
]);

These methods return the decoded response as an array and still use package authentication, form encoding, timeouts, response metadata, typed exceptions, and safe GET retries.

Use pendingRequest() when you need the configured Laravel HTTP client itself:

$response = Payrex::pendingRequest()->get('/new_resource/res_123');

This is lower level: you are responsible for response decoding and error handling.

Response metadata

After any API response, inspect its status and headers:

$intent = Payrex::paymentIntents()->retrieve('pi_...');
$response = Payrex::lastResponse();

$response?->status;
$response?->successful();
$response?->header('Request-Id');
$response?->headerValues('Set-Cookie');

lastResponse() is null before the first call and after a connection failure that produced no HTTP response. The client is a singleton, so the value always describes whichever call most recently completed in the current process.

Credentials

publicKey() returns the configured publishable key:

$publicKey = Payrex::publicKey();

There is intentionally no secret-key accessor. The secret key should never leave the server, appear in logs, or enter a browser bundle.

Laravel PayRex is an unofficial community SDK and is not affiliated with PayRex.

View source