_message = json_decode($json, true); $json_error = json_last_error(); if ($json_error != 0) { $errorMsg = "Error with message - content is not in json format" . self::_getErrorMessageForJsonError($json_error) . " " . $json; throw new OffAmazonPaymentsNotifications_InvalidMessageException( $errorMsg ); } } /** * Set the notification metadata * * @param NotificationMetadata $notificationMetadata value to set * * @return void */ public function setNotificationMetadata($notificationMetadata) { $this->_notificationMetadata = $notificationMetadata; } /** * Get the notification metadata * * @return NotificationMetadata */ public function getNotificationMetadata() { return $this->_notificationMetadata; } /** * Extract the mandatory field from the message and return the contents * * @param string $fieldName name of the field to extract * * @throws OffAmazonPaymentsNotifications_InvalidMessageException if not found * * @return string field contents if found */ public function getMandatoryField($fieldName) { $value = $this->getField($fieldName); if (is_null($value)) { throw new OffAmazonPaymentsNotifications_InvalidMessageException( "Error with json message - mandatory field " . $fieldName . " cannot be found" ); } return $value; } /** * Extract the field if present, return null if not defined * * @param string $fieldName name of the field to extract * * @return string field contents if found, null otherwise */ public function getField($fieldName) { if (array_key_exists($fieldName, $this->_message)) { return $this->_message[$fieldName]; } else { return null; } } /** * Convert a json error code to a descriptive error * message * * @param int $json_error message code * * @return string error message */ private static function _getErrorMessageForJsonError($json_error) { switch ($json_error) { case JSON_ERROR_DEPTH: return " - maximum stack depth exceeded."; break; case JSON_ERROR_STATE_MISMATCH: return " - invalid or malformed JSON."; break; case JSON_ERROR_CTRL_CHAR: return " - control character error."; break; case JSON_ERROR_SYNTAX: return " - syntax error."; break; default: return "."; break; } } } ?>