PHP code example of lacasoft / coatipay-sdk

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

    

lacasoft / coatipay-sdk example snippets




oatiPay\CoatiPay;

// Use a SECRET key, server-side only — never ship it to a client.
$relay = new CoatiPay('sk_live_...');

$intent = $relay->paymentIntents->create(
    amount:   10_000_000,           // 10.00 USDC (6 decimals → 1 USDC = 1_000_000)
    currency: 'usdc',
    chain:    'base',
    metadata: ['order_id' => '123'],
    // Safe to retry: the same key with the same parameters returns the same intent.
    idempotencyKey: 'order_123',
);

echo $intent['id'], ' ', $intent['status'];  // "pi_…", "created"

use CoatiPay\Eip712;

$auth = Eip712::signAuthorization(
    payer:    '0xPayerAddress...',
    amount:   1_000_000,                       // 1.00 USDC
    settlementHub: '0xSettlementHubAddress...',
    chain:    'base',
    intentId: $intent['id'],                    // the "pi_…" the API returned — becomes the nonce
    privateKey: '0x...',                        // payer private key — server-side demo only
);

$relay->paymentIntents->submitAuthorization($intent['id'], $auth);

$nonce = Eip712::intentIdToBytes32('pi_abc123'); // 0x… (32 bytes)

use CoatiPay\X402\X402Middleware;

$relay = new CoatiPay('sk_live_...', merchantWallet: '0xMerchantWallet...');

$app->add(new X402Middleware($relay, [
    'price'       => 1_000,     // 0.001 USDC
    'currency'    => 'usdc',
    'chain'       => 'base',
    'description' => 'Premium API access',
]));

use CoatiPay\Errors\WebhookSignatureError;

try {
    $event = $relay->webhooks->verify(
        file_get_contents('php://input'),       // the RAW request body
        $_SERVER['HTTP_X_SIGNATURE'] ?? '',     // X-Signature header
        'whsec_...',
    );
} catch (WebhookSignatureError $e) {
    http_response_code(400);
    exit($e->reason);
}

// At least once and in no particular order: deduplicate by $event['id'].
if ($event['type'] === 'payment_intent.settled') {
    fulfillOrder($event['data']['metadata']['order_id']);
}

  $dead = $relay->webhooks->listDeadLetters(20);
  $relay->webhooks->replayDeadLetter($dead['data'][0]['id']);  // same event, same id
  

use CoatiPay\Errors\CoatiPaySDKError;
use CoatiPay\Errors\NetworkError;

try {
    $relay->paymentIntents->create($amount, 'usdc', 'base', idempotencyKey: $orderId);
} catch (NetworkError $e) {
    // No CoatiPay answer ($e->status: the HTTP status, or null). Whether it took effect is
    // unknown: retrying with the same idempotencyKey returns the same intent.
} catch (CoatiPaySDKError $e) {
    echo $e->errorCode, ' ', $e->getMessage(), ' ', $e->param, ' ', $e->docUrl;
}

$relay = new CoatiPay(
    apiKey:         'sk_live_...',                  // ay API host
    timeout:        30.0,                           // optional — seconds
    merchantWallet: '0x...',                         // optional — receives x402 payments
);