PHP code example of factpulse / sdk

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

    

factpulse / sdk example snippets



FactPulse\SDK\FactPulseClient;

// Create the client
$client = new FactPulseClient(
    "[email protected]",
    "your_password",
    "your-client-uuid"  // From dashboard: Configuration > Clients
);

// Read your source PDF
$pdfB64 = base64_encode(file_get_contents("source_invoice.pdf"));

// Generate Factur-X and submit to PDP in one call
$result = $client->post("processing/invoices/submit-complete-async", [
    "invoiceData" => [
        "number" => "INV-2025-001",
        "supplier" => [
            "name" => "ACME Corporation",
            "siret" => "12345678901234",
            "iban" => "FR7630001007941234567890185",
            "routing_address" => "12345678901234",
        ],
        "recipient" => [
            "name" => "Client Company SA",
            "siret" => "98765432109876",
            "routing_address" => "98765432109876",
        ],
        "lines" => [
            [
                "description" => "Consulting services",
                "quantity" => 10,
                "unitPrice" => 100.0,
                "vatRate" => 20.0,
            ],
        ],
    ],
    "sourcePdf" => $pdfB64,
    "profile" => "EN16931",
    "destination" => ["type" => "afnor"],
]);

// PDF is in $result["content"] (auto-polled, auto-decoded from base64)
file_put_contents("facturx_invoice.pdf", $result["content"]);

echo "Flow ID: " . $result["afnorResult"]["flowId"] . "\n";

// POST /api/v1/{path}
$result = $client->post("path/to/endpoint", ["key1" => $value1, "key2" => $value2]);

// GET /api/v1/{path}
$result = $client->get("path/to/endpoint", ["param1" => $value1]);

// Submit with webhook - returns immediately
$result = $client->post("processing/invoices/submit-complete-async", [
    "invoiceData" => $invoiceData,
    "sourcePdf" => $pdfB64,
    "destination" => ["type" => "afnor"],
    "callbackUrl" => "https://your-server.com/webhook/factpulse",
    "webhookMode" => "INLINE",  // or "DOWNLOAD_URL"
]);

$taskId = $result["taskId"];
// Result will be POSTed to your webhook URL


$webhookSecret = "your-shared-secret";

function verifySignature(string $payload, string $signature): bool {
    if (strpos($signature, "sha256=") !== 0) {
        return false;
    }
    $expected = hash_hmac("sha256", $payload, $GLOBALS["webhookSecret"]);
    return hash_equals($expected, substr($signature, 7));
}

// Get raw POST body
$payload = file_get_contents("php://input");
$signature = $_SERVER["HTTP_X_WEBHOOK_SIGNATURE"] ?? "";

if (!verifySignature($payload, $signature)) {
    http_response_code(401);
    echo json_encode(["error" => "Invalid signature"]);
    exit;
}

$event = json_decode($payload, true);
$eventType = $event["event_type"];
$data = $event["data"];

if ($eventType === "submission.completed") {
    $flowId = $data["afnorResult"]["flowId"] ?? null;
    error_log("Invoice submitted: $flowId");
} elseif ($eventType === "submission.failed") {
    error_log("Submission failed: " . ($data["error"] ?? "Unknown"));
}

header("Content-Type: application/json");
echo json_encode(["status" => "received"]);

$result = $client->post("processing/invoices/submit-complete-async", [
    "invoiceData" => $invoiceData,
    "sourcePdf" => $pdfB64,
    "destination" => [
        "type" => "afnor",
        "flowServiceUrl" => "https://api.pdp.example.com/flow/v1",
        "tokenUrl" => "https://auth.pdp.example.com/oauth/token",
        "clientId" => "your_pdp_client_id",
        "clientSecret" => "your_pdp_client_secret",
    ],
]);

use FactPulse\SDK\FactPulseClient;
use FactPulse\SDK\FactPulseError;

try {
    $result = $client->post("processing/validate-xml", ["xmlContent" => $xml]);
} catch (FactPulseError $e) {
    echo "Error: " . $e->getMessage() . "\n";
    echo "Status code: " . $e->getStatusCode() . "\n";
    echo "Details: " . print_r($e->getDetails(), true) . "\n";
}