PHP code example of scanandpay / php

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

    

scanandpay / php example snippets


use ScanAndPay\ScanAndPay;
use ScanAndPay\Exceptions\WebhookSignatureException;

$client = new ScanAndPay(
    merchantId: getenv('SCANANDPAY_MERCHANT_ID'),
    apiSecret: getenv('SCANANDPAY_API_SECRET'),
    webhookSecret: getenv('SCANANDPAY_WEBHOOK_SECRET'), // optional
    baseUrl: getenv('SCANANDPAY_API_BASE_URL') ?: 'https://api.scanandpay.com.au',
);

// 1. Create a session at checkout. amountCents is an integer number of
//    cents. payId and merchantName are no longer needed — payments go
//    to the PayTo PayID and business name on your Business Portal
//    profile.
$session = $client->createSession(
    amountCents: 1990,  // $19.90
    platformOrderId: 'order_456',
);

// 2. Render the QR widget on the page.
echo scanandpay_checkout($session, pollUrl: '/scanandpay/status');

// 3. In your webhook handler, verify and consume the event.
try {
    $event = $client->webhooks()->verify(
        signature: $_SERVER['HTTP_X_SCANPAY_SIGNATURE'] ?? '',
        body: file_get_contents('php://input'),
    );

    if ($event->isPaid()) {
        // Mark order paid using $event->orderId, $event->txId, ...
    }
} catch (WebhookSignatureException $e) {
    http_response_code(401);
    exit('Invalid webhook');
}

$client->createSession(amountCents: 1990, ...);   // ✓ $19.90
$client->createSession(amountCents: 50, ...);     // ✓ $0.50
$client->createSession(amountCents: 100000, ...); // ✓ $1,000.00
$client->createSession(amountCents: -1, ...);     // ✗ ValidationException
$client->createSession(amountCents: 0, ...);      // ✗ ValidationException

$display = number_format($session->amountCents / 100, 2);  // "19.90"

$session = $client->createSession(
    amountCents: 1990,
    platformOrderId: 'order_456',
    merchantName: 'Acme Coffee',
    metadata: [
        'customer_id' => 'cus_42',
        'cart_id'     => 'cart_99',
    ],
);

// Later in your webhook handler:
$event->metadata['customer_id']; // 'cus_42'

$refund = $client->createRefund(
    paymentSessionId: $session->sessionId,
    amountCents: 1990,
    reason: 'customer_request',
);

HttpClient::API_VERSION;  // '2026-05-11'
HttpClient::VERSION;      // '0.4.2'

$client->createSession(
    amountCents: 1990,
    platformOrderId: 'order_456',
    merchantName: 'Acme Coffee',
    idempotencyKey: 'order_456:attempt_1',
);

use ScanAndPay\HttpClient;

$http = new HttpClient(
    apiSecret: getenv('SCANANDPAY_API_SECRET'),
    baseUrl: 'https://api.scanandpay.com.au',
    timeoutSeconds: 30,
    retries: 5,
    baseMs: 100,
);
$client = new ScanAndPay(
    merchantId: 'm', apiSecret: 's', http: $http,
);

$client = new ScanAndPay(
    merchantId: 'm',
    apiSecret: 's',
    webhookSecret: $current,           // signs new outbound deliveries
    previousWebhookSecret: $previous,  // still accepted on inbound
);
bash
composer