Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

PHP Create Swiss QR-Bill API

//service and docs
https://qr.livingtech.ch

// Configuration
$myConfiguration = [
    "Account" => "CH4431999123000889012",
    "CreditorName" => "Muster AG",
    "CreditorAddress1" => "Hauptstrasse 1",
    "CreditorAddress2" => "8000 Zürich",
    "CreditorCountryCode" => "CH",
    "DebtorName" => "LivingTech GmbH",
    "DebtorAddress1" => "Dörflistrasse 10",
    "DebtorAddress2" => "8057 Zürich",
    "DebtorCountryCode" => "CH",
    "Amount" => "1.50",
    "ReferenceNr" => "21000000000313947143000901",
    "UnstructuredMessage" => "Mitteilung zur Rechnung",
    "Currency" => "CHF",
    "IsQrOnly" => "false",
    "Format" => "PDF",
    "Language" => "DE",
];

// Call function to create invoice
$myFile = generateQrInvoice($myConfiguration);

// Work with binary data
if($myFile != null) {
    // ...
}

function generateQrInvoice($myRequestConfiguration) {
    // Main configuration
    $myEndpointUrl = "http://qrbillservice.livingtech.ch";
    $myEndpointPath = "/api/qrinvoice/create/";
    $myApiKey = "mySecretApiKey";

    // GET parameters
    $myGetParams = http_build_query($myRequestConfiguration);

    // CURL
    $myCurl = curl_init();
    curl_setopt($myCurl, CURLOPT_URL, $myEndpointUrl . $myEndpointPath . "?" . $myGetParams);
    curl_setopt($myCurl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($myCurl, CURLOPT_HTTPHEADER, array(
        "APIKEY: " . $myApiKey, 
        "Accept: application/json"
    ));

    // Debug only
    curl_setopt($myCurl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($myCurl, CURLOPT_SSL_VERIFYPEER, false);

    try {
        // Perform request
        $myResponse = curl_exec($myCurl);

        // Check status
        if (!curl_errno($myCurl)) {
            if(curl_getinfo($myCurl, CURLINFO_HTTP_CODE) == 200) {
                // Read and parse JSON
                $myJsonObject  = json_decode($myResponse, true);

                // Check if error
                if($myJsonObject['isSuccessed'] == "true") {
                    if(isset($myJsonObject['base64Image']) && !empty($myJsonObject['base64Image'])) {
                        // E.g. save file
                        file_put_contents($_SERVER['DOCUMENT_ROOT'] . "/output/" . uniqid("", true) . ".pdf", base64_decode($myJsonObject['base64Image']));

                        // Return data
                        return base64_decode($myJsonObject['base64Image']);
                    } else {
                        throw new Exception("no data provided");
                    }
                } else {
                    throw new Exception($myJsonObject["Message"]);
                }
            } else {
                throw new Exception("status code " . curl_getinfo($myCurl, CURLINFO_HTTP_CODE));
            }
        } else {
            throw new Exception(curl_error($myCurl));
        }

        // Close curl
        curl_close($myCurl);
    } catch(Exception $e) {
        // Handle exception
        echo "Error: " . $e->getMessage();
        return null;
    }
}
Source by qr.livingtech.ch #
 
PREVIOUS NEXT
Tagged: #PHP #Create #Swiss #API
ADD COMMENT
Topic
Name
8+2 =