PHP code example of spart / sdk

1. Go to this page and download the library: Download spart/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/ */

    

spart / sdk example snippets



use Spart\Sdk\SpartClient;
use Spart\Sdk\SpartClientConfig;

// baseUrl defaults to https://api.spartpay.com; override it only for
// non-production environments.
$config = new SpartClientConfig(
    apiKey: 'sk_live_your_api_key_here',
);
$client = new SpartClient($config);


use Spart\Sdk\Dtos\CreateIntentRequest;
use Spart\Sdk\Models\Contact;
use Spart\Sdk\Models\LineItem;
use Spart\Sdk\Models\Money;
use Spart\Sdk\Models\OrderOptions;

$request = new CreateIntentRequest(
    total: Money::fromMinorUnits(11000, decimals: 2, currency: 'EUR'),  // 110.00 EUR
    lineItems: [
        new LineItem(
            name: 'Product A',
            quantity: 1,
            description: 'Optional description',
        ),
        new LineItem(
            name: 'Product B',
            quantity: 2,
            imageUri: 'https://merchant.example/img/b.png',
        ),
    ],
    sparter: new Contact(
        email: '[email protected]',
        firstName: 'Jane',
        lastName: 'Doe',
    ),
    // sessionId is OPTIONAL: when supplied, retrying the same payload
    // returns the existing intent (HTTP 200) instead of creating a duplicate.
    sessionId: 'cart_abc123',
    // options is OPTIONAL: when omitted, the server applies its defaults.
    // When provided, maxDuration is REQUIRED.
    options: new OrderOptions(
        maxDuration: new \DateInterval('P1D'),
        returnUri: 'https://merchant.example/checkout/return',
        cancelUri: 'https://merchant.example/checkout/cancel',
    ),
);

$result = $client->intents()->create($request);
echo $result->intentShortId . PHP_EOL;       // Short ID for URLs / follow-up reads
echo $result->checkoutUrl . PHP_EOL;         // Checkout URL to redirect the customer to
echo ($result->wasIdempotentReplay ? 'replay' : 'new') . PHP_EOL;


use Spart\Sdk\Webhooks\EventType;
use Spart\Sdk\Webhooks\SignatureVerifier;

$verifier = new SignatureVerifier('whsec_your_signing_secret_here');
$event = $verifier->verifyAndParse(
    rawBody: $rawRequestBody,
    headerValue: $_SERVER['HTTP_X_SPART_SIGNATURE'] ?? '',
    deliveryId: $_SERVER['HTTP_X_SPART_DELIVERY_ID'] ?? '',
    attempt: (int) ($_SERVER['HTTP_X_SPART_WEBHOOK_ATTEMPT'] ?? 1),
);

switch ($event->knownType) {
    case EventType::IntentCreated:
        // Handle intent created
        break;
    case EventType::PaymentAuthorized:
        // Handle a payment part being authorized
        break;
    case EventType::OrderCompleted:
        // Handle order completed (all parts authorized)
        break;
    case EventType::OrderCanceled:
        // Handle order canceled
        break;
    case EventType::OrderExpired:
        // Handle order expired (intent timed out before completion)
        break;
    case EventType::WebhookTest:
        // Handle merchant-initiated test ping
        break;
    default:
        // Unknown / forward-compat event type — ignore or log
        break;
}