Browse documentationOpen

byrcsc/laravel-payrex · 1.x

Customers.

Create and manage PayRex customers, saved payment methods, customer sessions, listings, and deleted-customer tombstones.

Customers group payment methods, payments, and billing statements around one payer.

Create a customer

use ByRcsc\LaravelPayrex\Enums\Currency;
use ByRcsc\LaravelPayrex\Facades\Payrex;

$customer = Payrex::customers()->create(
    name: 'Ada Lovelace',
    email: 'ada@example.com',
    currency: Currency::PHP,
    billingDetails: [
        'phone' => '+639171234567',
        'address' => [
            'line1' => '123 Example Street',
            'city' => 'Makati',
            'postal_code' => '1200',
            'country' => 'PH',
        ],
    ],
    billingStatementPrefix: 'ACME',
    nextBillingStatementSequenceNumber: '1001',
    metadata: ['account_id' => '1042'],
);

The complete signature is:

create(
    string $name,
    string $email,
    Currency $currency = Currency::PHP,
    ?array $billingDetails = null,
    ?string $billingStatementPrefix = null,
    ?string $nextBillingStatementSequenceNumber = null,
    ?array $metadata = null,
    array $options = [],
): Customer

Retrieve and update

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

$customer = Payrex::customers()->update(
    id: $customer->id,
    name: 'Ada Byron',
    email: 'ada@example.com',
    metadata: ['account_id' => '1042'],
);

update() accepts the same customer fields as create, except the ID is first and every other field is optional.

Delete a customer

$deleted = Payrex::customers()->delete('cus_...');

$deleted->id;
$deleted->deleted; // true

PayRex can return a deleted customer from retrieve() as an HTTP 200 tombstone instead of a 404:

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

if ($customer->isDeleted()) {
    // Remove or replace the stale local association.
}

Do not infer that any HTTP 200 customer is active without checking isDeleted() when deletion could have happened outside your application.

List customers

$page = Payrex::customers()->list(limit: 25);

foreach (Payrex::customers()->autoPaging(limit: 100) as $customer) {
    // ...
}

$customers = Payrex::customers()->paginate(perPage: 20);

See pagination for manual cursors and Laravel links.

Saved payment methods

$methods = Payrex::customers()->listPaymentMethods(
    id: $customer->id,
    limit: 25,
);

$deleted = Payrex::customers()->deletePaymentMethod(
    id: $customer->id,
    paymentMethodId: 'pm_...',
);

listPaymentMethods() also accepts before, after, and options.

Customer sessions

Create a short-lived session for PayRex embedded components:

$session = Payrex::customerSessions()->create($customer->id);

$session = Payrex::customerSessions()->retrieve($session->id);

Only the customer-session client secret and publishable key should be exposed to the browser. The server secret key must remain private.

Customer response

The Customer object includes:

$customer->id;
$customer->name;
$customer->email;
$customer->currency;
$customer->billing;
$customer->billingStatementPrefix;
$customer->nextBillingStatementSequenceNumber;
$customer->deleted;
$customer->livemode;
$customer->metadata;
$customer->createdAt;
$customer->updatedAt;
$customer->raw;

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

View source