Creating Licences


/*  This is a sample PHP script to be used as a guide for integrating
 *  the OffByZero Cobalt with your website and create licences
 *  automatically.
 *  You will need to ensure the cURL extension is installed and configured
 *  to work with PHP. On Ubuntu, this is as simple as:
 *    sudo apt-get install php5-curl
 *  To test out the code below simply add the following
 *  PHP code to your website, adjust the "strLicence", "strAuth" and
 *  "strCobaltServerBaseURI" variables, adjust the licence XML string
 *  to include the users submitted values (for name, address, etc.),
 *  and you are done. If you configure appropriate defaults for
 *  feature and product parameters, then you can skip the respective
 *  XML fields in "strLicence".
 *  The product_id and feature_id values can be found by examining the
 *  appropriate tables in the Cobalt UI. The licence-type value
 *  should be one of the following:
 *  PERIODIC = 0
 *  PURCHASE = 1
 *  DEMO = 2
 *  NONE = 3

$strAuth = "username:password";
$strCobaltServerBaseURI = "";

$strLicence = <<< XML
    <customer-address><![CDATA[123 New Street, Melbourne, VIC 3000, Australia]]></customer-address>
    <customer-company><![CDATA[BigCorp Pty. Ltd.]]></customer-company>
    <customer-company-division><![CDATA[Finance Division]]></customer-company-division>
    <customer-name><![CDATA[J. Bloggs]]></customer-name>
    <customer-notes><![CDATA[This is a repeat customer.]]></customer-notes>
    <customer-phone><![CDATA[+61 1 2345 6789]]></customer-phone>
        <value><![CDATA[Gold membership]]></value>
        <value><![CDATA[Gold membership]]></value>

// Licences can be emailed to the user by the server either based on
// the default template or custom fields.
//$strEmail = '<use-template>true</use-template>';
$strEmail = <<< XML
    <subject>Your new licence</subject>
    <body>Dear Joe, please find attached your new licence</body>

// Create a new licence.
$xmlRes = SendPost($strCobaltServerBaseURI.'licences.xml', $strLicence, $strAuth);
if ($xmlRes != NULL)
  // Determine the new licence_id from the HttpMessage returned.
  $xmlLicence = new SimpleXMLElement($xmlRes);
  $nLicenceId = $xmlLicence->id;
  echo "New licence created (licence_id: ".$nLicenceId.") ";

  // Email the new licence to the user.
  $xmlRes = SendPost($strCobaltServerBaseURI.'licences/'.$nLicenceId.'/send_email.xml', $strEmail, $strAuth);
  if ($xmlRes != NULL) echo "and emailed to user.\nDone.\n";

// SendPost() - helper function to send an HTTP POST and check for errors.
function SendPost($strUrl, $strRawPostData, $strAuth)
  $xmlRes = NULL;
  $urlNew = curl_init();

  curl_setopt($urlNew, CURLOPT_URL, $strUrl);
  curl_setopt($urlNew, CURLOPT_HTTPHEADER, array('Content-Type: application/xml', 'Expect:'));
  curl_setopt($urlNew, CURLOPT_HEADER, 0);
  curl_setopt($urlNew, CURLOPT_POST, 1);
  curl_setopt($urlNew, CURLOPT_POSTFIELDS, $strRawPostData);
  curl_setopt($urlNew, CURLOPT_USERPWD, $strAuth);
  curl_setopt($urlNew, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($urlNew, CURLOPT_RETURNTRANSFER,1);
  $xmlRes = curl_exec($urlNew);

  return $xmlRes;