'LicenseeContact', 'delivery' => 'DeliveryContact', 'billing' => 'BillingContact'); private $errorStack = array();# -- -- -- -- -- -- --private functions-- -- -- -- -- -- -- -- -- --#private function XPathQuery($queryStr) { if (!is_object($this - > dom) || !is_object($this - > xpQuery)) return false; if ($queryStr == '') { $this - > AddError(__METHOD__. "($queryStr)", 'Empty XPath Expression'); return false; } $result = @$this - > xpQuery - > query($queryStr); if (!$result) { $this - > AddError(__METHOD__. "($queryStr)", 'Invalid XPath Expression'); return false; } if ($result - > length < 1) return false; return $result; } private function addError($title, $message) { return array_push($this - > errorStack, array('title' => $title, 'message' => $message)); }#-- -- -- -- -- -- --public functions-- -- -- -- -- -- -- -- -- --#public function loadXML($XMLStr) { $this - > dom = new DomDocument('1.0', 'UTF-8'); $this - > dom - > loadXML($XMLStr); $this - > xpQuery = new DomXPath($this - > dom); preg_match('/XML\.cleverbridge\.com\/([^\/]+)\//i', $XMLStr, $found); $XMLVersion = $found[1]; // registration of namespace cbn is in root element - not necessary // $this->xpQuery->registerNamespace('cbn','http://XML.cleverbridge.com/'.$XMLVersion.'/cleverbridgeNotification.xsd'); // registration of namespace cbt is not in root element $this->xpQuery->registerNamespace('cbt','http://XML.cleverbridge.com/'.$XMLVersion.'/cleverbridgeTypes.xsd'); return true; } public function getCustomerData($fieldName, $contactTypeId = 'licensee') { if (!isset($this->contactTypeIds[trim(strtolower($contactTypeId))])) { $this->AddError(__METHOD__."($fieldName, $contactTypeId)", 'Invalid ContactType - Type not definded'); return false; } $queryStr = '//cbt:'.$this->contactTypeIds[trim(strtolower($contactTypeId))].'/cbt:'.trim($fieldName); if (($result = $this->xPathQuery($queryStr)) === false) { $this->AddError(__METHOD__."($fieldName, $contactTypeId)", 'Customer data not found at '.$queryStr); return false; } return $result->item(0)->nodeValue; } public function getPaymentData($fieldName) { if (($result = $this->xPathQuery('//cbn:Purchase/cbt:PaymentInfo/cbt:'.trim($fieldName))) === false) { $this->AddError(__METHOD__."($fieldName)", 'Payment data not found'); return false; } return $result->item(0)->nodeValue; } public function getPurchaseItemData($runningNo, $nodeName) { if (($result = $this->xPathQuery("//cbn:Purchase/cbt:Items/cbt:Item[@cbt:RunningNo='$runningNo']/cbt:".trim($nodeName))) === false) { $this->AddError(__METHOD__."($runningNo, $nodeName)", 'Item data not found'); return false; } return $result->item(0)->nodeValue; } public function getPurchaseId() { $purchaseIdXPath = '//cbn:Purchase/@cbt:Id'; if (($result = $this->xPathQuery($purchaseIdXPath)) === false) { $this->AddError(__METHOD__.'()', 'PurchaseId is missing in XML Doc at: '. $purchaseIdXPath); return false; } return $result->item(0)->nodeValue; } public function getErrors() { return $this->errorStack; } public function hasErrors() { return (sizeof($this->errorStack) > 0); } } # ----------------------------------------------------- # # Usage Sample # # ----------------------------------------------------- # // see if XML data is included in request if (!isset($_REQUEST['XML'])) { Header('HTTP/1.1 400 Bad Request'); Header('Content-Type: text/plain'); print ('XML not found!'); exit; } $cbn = new cbNotification(); $cbn->loadXML($_REQUEST['XML']); // Get customer and purchase data from the XML // Let us use the customer lastname and firstname, the product ID and the purchase ID. // lastname in native (also for Chinese, Japanse, Russian, ...) characters: $customerLastname = $cbn->getCustomerData('Lastname'); $customerFirstname = $cbn->getCustomerData('Firstname'); // Alternatively you can use romanized customer data // $customerLastname = $cbn->getCustomerData('LastnameRomanized'); // $customerFirstname = $cbn->getCustomerData('FirstnameRomanized'); $productId = $cbn->getPurchaseItemData($_REQUEST['RUNNING_NO'], 'ProductId'); // Aternatively use your internal product ID. Please note that it then always has to be set. // $productId = $cbn->getPurchaseItemData($_REQUEST['RUNNING_NO'], 'YourProductId'); // If that is an upgrade purchase you can read the previous key that has been validated during the checkout // $previousKey = $cbn->getPurchaseItemData($_REQUEST['RUNNING_NO'], 'PreviousLicense'); // // you might want to update that key in your database $purchaseId = $cbn->getPurchaseID(); if ($cbn->hasErrors()) { // set your Error Header Header('HTTP/1.1 400 Bad Request'); Header('Content-Type: text/plain'); print_r ($cbn->getErrors()); } else { // Generate the license key based on the XML input // We use the md5 as an example function only. Your code goes here. $key = md5($purchaseId.$customerLastname.$customerFirstname.$productId); // example how to return a text key Header('Content-Type: text/plain'); print $key; // example how to return a file key // Header('Content-Type: application/octet-stream'); // Header('Content-Disposition: attachment; filename="yourFilename.dat"'); // print $key; } ?>