📜  PHP Create Swiss QR-Bill API - PHP 代码示例

📅  最后修改于: 2022-03-11 14:53:42.490000             🧑  作者: Mango

代码示例1
//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;
    }
}