Skip to main content
knowledgecenter.2checkout.com

LCN code sample

Overview

Use the following example of PHP code for creating a script that reads and validates incoming License Change Notifications.

Example

<?php
/* License Change Notification */
$pass = "AAABBBCCC"; /* your secret key to compute HASH */

/* function used to hash */
function hmac ($key, $data)
{
    $b = 64; // byte length for md5
    if (strlen($key) > $b) {
        $key = pack("H*",md5($key));
    }
    $key  = str_pad($key, $b, chr(0x00));
    $ipad = str_pad('', $b, chr(0x36));
    $opad = str_pad('', $b, chr(0x5c));
    $k_ipad = $key ^ $ipad ;
    $k_opad = $key ^ $opad;
    return md5($k_opad  . pack("H*",md5($k_ipad . $data)));
}

function SerializeArray ($myarray)
{
    $retvalue = "";

    if (isset($myarray) && is_array($myarray) && count($myarray) > 0) {
        foreach ($myarray as $key => $val) {
            if ($key == 'HASH') {
                continue;
            }

            if (is_array($val)) {
                $retvalue .= SerializeArray($val);
            } else {
                $retvalue .= strlen($val) . $val;
            }
        }
    }

    return $retvalue;
}

/* do something only if it's POSTed with the right fields */
if(
    isset($_POST)
    && is_array($_POST)
    && isset($_POST["HASH"]) && is_string($_POST["HASH"]) && strlen($_POST["HASH"]) > 0
    && isset($_POST["LICENSE_CODE"]) && is_string($_POST["LICENSE_CODE"]) && strlen($_POST["LICENSE_CODE"]) > 0
    && isset($_POST["EXPIRATION_DATE"]) && is_string($_POST["EXPIRATION_DATE"]) && strlen($_POST["EXPIRATION_DATE"]) > 0
    && $_POST["HASH"] == hmac($pass, SerializeArray($_POST))
) {
    $returnedDate = date("YmdGis");
    $returnedHash = hmac($pass, SerializeArray(array(
        $pass,
        $_POST["LICENSE_CODE"],
        $_POST["EXPIRATION_DATE"],
        $returnedDate
    )));

    /* must echo this to give feedback to us */
    echo "<EPAYMENT>".$returnedDate."|".$returnedHash."</EPAYMENT>";

    /* put your custom "SUCCESS" code below */
} else {
    /* put your custom "ERROR" code below, for example: */
    mail("your_address@example.com", "BAD LCN", print_r($_POST, TRUE),"");
}