PHP code example of codearachnid / check-commerce-laravel-sdk

1. Go to this page and download the library: Download codearachnid/check-commerce-laravel-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

codearachnid / check-commerce-laravel-sdk example snippets


use CheckCommerce\CheckCommerceClient;

class CheckoutController
{
    public function __construct(private CheckCommerceClient $checkCommerce) {}

    public function store(Request $request)
    {
        $result = $this->checkCommerce->transactions->debit([...]);
    }
}

use CheckCommerce\Laravel\Facades\CheckCommerce;

CheckCommerce::transactions()->debit([...]);

use CheckCommerce\Enums\PaymentType;
use CheckCommerce\Laravel\Facades\CheckCommerce;

$result = CheckCommerce::transactions()->debit([
    'merchantNumber' => config('check-commerce.merchant_number'),
    'amount' => 42.50,
    'referenceNumber' => 'INV-1001',
    'consumerInfo' => [
        'name' => 'Jane Doe',
        'bankAccountNumber' => '1234567890',
        'bankRoutingNumber' => 121000248,
    ],
]);

$result->transactionId;     // 123456789
$result->status?->value;    // "Processed"

// Credits, voids and refunds work the same way:
$merchantNumber = config('check-commerce.merchant_number');

CheckCommerce::transactions()->credit([...]);
CheckCommerce::transactions()->void(['merchantNumber' => $merchantNumber, 'originalTransaction' => ['transactionId' => 123456789]]);
CheckCommerce::transactions()->refund(['merchantNumber' => $merchantNumber, 'originalTransaction' => ['referenceNumber' => 'INV-1001']]);

// Any rail, any transaction type:
CheckCommerce::transactions()->create([...], PaymentType::Rtp);

// Status lookups:
$status = CheckCommerce::transactions()->status(transactionId: 123456789);

if ($status->isDeclined()) {
    report(new RuntimeException($status->processingFailure?->detail ?? 'Declined'));
}

$created = CheckCommerce::consumers()->create([
    'name' => 'Jane Doe',
    'email' => '[email protected]',
    'bankAccountNumber' => '1234567890',
    'bankRoutingNumber' => 121000248,
]);

$consumer = CheckCommerce::consumers()->retrieve($created->consumerId);
CheckCommerce::consumers()->update($created->consumerId, ['phoneNumber' => '5125551234']);

// Charge a stored consumer:
CheckCommerce::transactions()->debit([
    'merchantNumber' => config('check-commerce.merchant_number'),
    'amount' => 42.50,
    'consumerInfo' => ['consumerId' => $created->consumerId],
]);

// List endpoints page lazily:
foreach (CheckCommerce::consumers()->list(['city' => 'Austin'])->autoPagingIterator() as $consumer) {
    // ...
}

use CheckCommerce\Enums\SubscriptionEndCode;
use CheckCommerce\Enums\SubscriptionStatus;
use CheckCommerce\Enums\TransactionType;

$created = CheckCommerce::subscriptions()->create([
    'startTime' => now()->addMonth()->startOfMonth()->toDateTimeImmutable(),
    'amount' => 25.00,
    'schCode' => 'Monthly:1',
    'endCode' => SubscriptionEndCode::Indefinite,
    'transactionType' => TransactionType::Debit,
    'status' => SubscriptionStatus::Active,
    'consumerInfo' => ['consumerId' => $consumerId],
]);

CheckCommerce::subscriptions()->update($created->subscriptionId, ['amount' => 30.00]);

$link = CheckCommerce::hostedPages()->createLink([
    'customer' => ['name' => 'Jane Doe', 'email' => '[email protected]'],
    'order' => [
        'subTotal' => 89.95,
        'tax' => 10.00,
        'total' => 99.95,
        'returnURL' => route('checkout.thanks'),
    ],
    'orderItems' => [
        ['name' => 'Widget', 'quantity' => 1, 'price' => 89.95],
    ],
]);

return redirect()->away($link->url);

use CheckCommerce\Enums\FileDelimiter;

$merchantNumber = config('check-commerce.merchant_number');

$batch = CheckCommerce::batches()->submit([
    ['merchantNumber' => $merchantNumber, 'transactionType' => 'Debit', 'amount' => 42.50, 'consumerInfo' => [...]],
    ['merchantNumber' => $merchantNumber, 'transactionType' => 'Debit', 'amount' => 19.99, 'consumerInfo' => [...]],
]);

// Or upload a file:
$batch = CheckCommerce::batches()->uploadFile(storage_path('app/batches/today.csv'), FileDelimiter::Comma);

CheckCommerce::batches()->status($batch->batchId)->status?->value;   // "Pending" | "Processing" | "Processed" | "Declined"

$result = CheckCommerce::boarding()->board(['merchants' => [/* boarding records */]]);

foreach ($result->boardingFailures as $failure) {
    Log::warning('Boarding failed', [
        'company' => $failure->companyName,
        'detail' => $failure->processingFailure?->detail,
    ]);
}

use CheckCommerce\Exception\ApiException;
use CheckCommerce\Exception\ValidationException;

try {
    CheckCommerce::transactions()->debit([...]);
} catch (ValidationException $e) {
    return back()->withErrors(collect($e->getValidationErrors())
        ->mapWithKeys(fn ($error) => [$error->property => $error->detail])
        ->all());
} catch (ApiException $e) {
    Log::error($e->getMessage(), [
        'status' => $e->getStatusCode(),
        'code' => $e->getErrorCode(),
        'correlation_id' => $e->getCorrelationId(),
    ]);

    throw $e;
}

'token_cache' => [
    'store' => env('CHECK_COMMERCE_TOKEN_CACHE_STORE'),   // null uses your default cache store
    'prefix' => env('CHECK_COMMERCE_TOKEN_CACHE_PREFIX', 'check-commerce'),
],

CheckCommerce::authenticate();

use CheckCommerce\Laravel\Facades\CheckCommerce;
use Psr\Http\Message\RequestInterface;

it('charges the customer at checkout', function () {
    $checkCommerce = CheckCommerce::fake();

    $checkCommerce->queueJson(200, [
        'transactionId' => 123456789,
        'status' => 'Processed',
    ]);

    $this->post('/checkout', ['amount' => 42.50])->assertRedirect();

    $checkCommerce->assertSentCount(1);
    $checkCommerce->assertSent(fn (RequestInterface $request): bool => json_decode(
        (string) $request->getBody(), true
    )['request']['amount'] === 42.50);
});

$this->app->bind(\Psr\Http\Client\ClientInterface::class, fn () => new \GuzzleHttp\Client([
    'handler' => $stackWithYourMiddleware,
]));
bash
php artisan vendor:publish --tag="check-commerce-laravel-sdk-config"