Skip to content

Latest commit

 

History

History
301 lines (236 loc) · 10.5 KB

File metadata and controls

301 lines (236 loc) · 10.5 KB

Notifications {#notification_en}

Notification is a POST-request (callback). The request's body contains all relevant data of the invoice serialized as HTTP-request parameters and encoded by UTF-8 plus parameter command=bill.

Request → POST

Example of notification request

user@server:~$ curl "https://example.com/qiwi-notify.php"
  -v -w "%{http_code}"
  -X POST --header "Accept: text/xml"
  --header "Content-Type: application/x-www-form-urlencoded; charset=utf-8"
  --Authorization: "Basic MjA0Mjp0ZXN0Cg=="
  -d "bill_id=BILL-1%26status=paid%26amount=1.00%26user=tel%3A%2B79031811737%26prv_name=TEST%26ccy=RUB%26comment=test%26command=bill"
  • URL

To receive notifications, specify your notifications server URL in Server notification section of Settings section at QIWI partners web site.
  • Details

    • Click "Create password and save" to obtain password for notification Basic authorization
  • Parameters

    Invoice parameters are in the request's body.
Parameter Description Type Required
bill_id Merchant invoice number String Y
status Current invoice status String Y
amount The invoice amount. The number is rounded down with two decimal places Number(6.2) Y
user The QIWI Wallet user’s ID, to whom the invoice is issued. It is the user’s phone number with tel: prefix String Y
prv_name Merchant’s site name specified on kassa.qiwi.com in "Settings" section String Y
ccy Invoice currency identifier (Alpha-3 ISO 4217 code) String(3) Y
comment Comment to the invoice String(255) Y
command Always bill by default String Y
As new parameters of the invoice might be introduced on QIWI Wallet side, a list of invoice parameters in HTTP-request should not be fixed on the merchant's side and should be taken from the request itself.

Response ←

Example of XML response to notification

HTTP/1.1 200 OK
Content-Type: text/xml

<?xml version="1.0"?>
<result>
<result_code>0</result_code>
</result>

Response must be in XML format.

  • HEADERS

    • Content-type: text/xml
  • Parameters

XML Tag Description
result Grouping tag. Describes notification processing result.
result_code Notification result code (positive integer). We recommend that the result codes returned by the merchant be in accordance with Notification codes table.
Any response with result code other than 0 ("Success") and/or HTTP status code other than 200 (OK) will be treated as a temporary error. Thus the server will continue repeating requests with increased time intervals within the next 24 hours (50 attempts in total) until it gets a response with result code 0 ("Success") and HTTP status code 200 (OK). If the response with result code 0 ("Success") and HTTP status code 200 has not been received within 24 hours since the first request, QIWI Wallet server will stop sending the requests. The merchant would receive an email with new invoice status code and indication on the possible technical issues on the merchant’s server side. To receive notifications merchant must whitelist following IP subnets connected by 80, 443 ports exclusively:
  • 91.232.230.0/23
  • 79.142.16.0/20
  • Authorization on Merchant's Server {#notifications_auth}

    Merchant's server should use basic-authorization or authorization by signature. Merchant may also use client SSL certificate verification (self-signed certificates may be used as well). QIWI Wallet server certificate should be verified in HTTPS requests.

    If the client SSL-certificate is self-generated and is not issued by one of the standard certification centers, this certificate should be uploaded to the QIWI Wallet server via Certificate field in Settings - Protocols details - REST-protocol section of QIWI partners web site).
    • Details

    The merchant's certificate is treated as trusted after the upload. Certificate must be in one of the following formats:

    • PEM (text file with .pem extension) – (Privacy-enhanced Electronic Mail) BASE64 encoded DER certificate placed between -----BEGIN CERTIFICATE----- and -----END CERTIFICATE----- strings.
    • DER (binary file with .cer, .crt, .der extensions) – usually in binary DER format, though PEM certificates are also accepted with this extensions.

    Basic authorization {#basic_notify}

    Example of notification with Basic auth

    POST /qiwi-notify.php HTTP/1.1
    Accept: text/xml
    Content-type: application/x-www-form-urlencoded
    Authorization: Basic ***
    Host: example.com
    
    command=bill&bill_id=BILL-1&status=paid&error=0&amount=1.00&user=tel%3A%2B79031811737&prv_name=Retail_Store&ccy=RUB&comment=test

    The login is taken from Shop ID parameter. To obtain password, click on Change notification password button in Protocols details - REST-protocol section of QIWI partners web site.

    • Details

    Authorization by signature {#sign_notify}

    Example of notification with Signature

    POST /qiwi-notify.php HTTP/1.1
    Accept: text/xml
    Content-type: application/x-www-form-urlencoded
    X-Api-Signature: J4WNfNZd***V5mv2w=
    Host: example.com
    
    command=bill&bill_id=LocalTest17&status=paid&error=0&amount=0.01&user=tel%3A%2B78000005122&prv_name=Test&ccy=RUB&comment=Some+Descriptor

    Example of notification processing with signature check

    <?php
    
    function hexToStr($hex){
        $string='';
        for ($i=0; $i < strlen($hex)-1; $i+=2){
            $string .= chr(hexdec($hex[$i].$hex[$i+1]));
        }
        return $string;
    }
    
    //Signature generation by key and string
    function checkSign($key, $req){
        $sign_hash = hash_hmac("sha1", $req, $key);
        $sign_tr = hexToStr($sign_hash);
        $sign = base64_encode($sign_tr);
        return $sign;
    }
    
    //Sort POST-request parameters and return values
    function getReqParams(){
        $reqparams = "";
        ksort($_POST);
        foreach ($_POST as $param => $valuep) {
            $reqparams = "$reqparams|$valuep";
        }
        return substr($reqparams,1);
    }
    
    //Take signature from the request
    function getSign(){
        $HEADERS = getallheaders();
        foreach ($HEADERS as $header => $value) {
           if ($header == 'X-Api-Signature') {
                $SIGN_REQ = $value;
           }
        }
        return $SIGN_REQ;
    }
    
    // Sort parameters
    $Request = getReqParams();
    // Notification password
    $NOTIFY_PWD = "***";
    // Get sign
    $reqres = checkSign($NOTIFY_PWD, $Request);
    
    // Get sign from the request
    $SIGN_REQ = getSign();
    
    if ($reqres == $SIGN_REQ) {
        $error = 0;
    }
    else $error = 151;
    
    //Response
    header('Content-Type: text/xml');
    $xmlres = <<<XML
    <?xml version="1.0"?>
    <result>
    <result_code>$error</result_code>
    </result>
    XML;
    echo $xmlres;
    ?>
    Merchant should enable "Use HMAC sign instead of basic auth" flag in Protocols details - REST-protocol section of QIWI partners web site.
    • Details

    The HTTP header X-Api-Signature with signature is added to the POST-request. Signature is calculated as HMAC algorithm with SHA1-hash function.

    • Parameters' separator is |.
    • Signed are all the parameters in the original invoice request.
    • Parameters are in alphabetical order and UTF-8 byte-encoded.
    • Secret key for signature is the password for notification basic-authorization.

    Signature verification algorithm is as follows:

    1. Prepare a string of all parameters values from the notification POST-request sorted in alphabetical order and separated by |:

      {parameter1}|{parameter2}|…

      where {parameter1} is the value of the notification parameter. All values should be treated as strings.

    2. Transform obtained string and password for the notification basic-authorization into bytes encoded in UTF-8.

    3. Apply HMAC-SHA1 function:

      hash = HMAС(SHA1, Notification_password_bytes, Invoice_parameters_bytes) Where:

      • Notification_password_bytes – secret key (bytecoded notification password);
      • Invoice_parameters_bytes – bytecoded POST-request body;
      • hash – hash-function result.
    4. Transform HMAC-hash value into bytes with UTF-8 and Base64-encode it.

    5. Compare X-Api-Signature header's value with the result of step 4.

    PHP Implementation Example {#php_apisign}

    The given PHP example implements notification authorization by signature verification. Open the PHP tab on the right.

    Notification Codes {#notify_codes}

    Code Description
    0 Success
    5 The format of the request parameters is incorrect
    13 Database connection error
    150 Incorrect password
    151 Signature authorization failed
    300 Server connection error