/** * method to clean account cache * * @param int $aid * * @return true on success, else false */ protected function cleanAccountCache($aid) { $cache = Billrun_Factory::cache(); if (empty($cache)) { return false; } $aids = array_unique(array_diff(Billrun_Util::verify_array(explode(',', $aid), 'int'), array(0))); $billrunKey = Billrun_Util::getBillrunKey(time()); $cachePrefix = 'balance_'; // this is not the action name because it's clear the balance cache foreach ($aids as $aid) { $cleanCacheKeys = array(Billrun_Util::generateArrayStamp(array_values(array('aid' => $aid, 'subscribers' => array(), 'stamp' => $billrunKey))), Billrun_Util::generateArrayStamp(array_values(array('aid' => $aid, 'subscribers' => null, 'stamp' => (int) $billrunKey))), Billrun_Util::generateArrayStamp(array_values(array('aid' => $aid, 'subscribers' => "", 'stamp' => (int) $billrunKey))), Billrun_Util::generateArrayStamp(array_values(array('aid' => $aid, 'subscribers' => 0, 'stamp' => (int) $billrunKey)))); foreach ($cleanCacheKeys as $cacheKey) { $cache->remove($cacheKey, $cachePrefix); } } return true; }
/** * method to store and fetch by global cache layer * * @param type $params params to be used by cache to populate and store * * @return mixed the cached results */ protected function cache($params) { if (!isset($params['stampParams'])) { $params['stampParams'] = $params['fetchParams']; } $cache = Billrun_Factory::cache(); if (empty($cache)) { return $this->fetchData($params['fetchParams']); } $actionName = $this->getAction(); $cachePrefix = $this->getCachePrefix(); $cacheKey = Billrun_Util::generateArrayStamp(array_values($params['stampParams'])); $cachedData = $cache->get($cacheKey, $cachePrefix); if (empty($cachedData)) { $cachedData = $this->fetchData($params['fetchParams']); $lifetime = Billrun_Factory::config()->getConfigValue('api.cacheLifetime.' . $actionName, $this->getCacheLifeTime()); $cache->set($cacheKey, $cachedData, $cachePrefix, $lifetime); } else { Billrun_Factory::log()->log("Fetch data from cache for " . $actionName . " api call", Zend_Log::INFO); } return $cachedData; }
/** * method to retrieve the plan instance * * @return Billrun_Plan */ public static function plan($params) { if (!isset($params['disableCache']) || !$params['disableCache']) { // unique stamp per plan $stamp = Billrun_Util::generateArrayStamp($params); if (!isset(self::$plan[$stamp])) { self::$plan[$stamp] = new Billrun_Plan($params); } return self::$plan[$stamp]; } return new Billrun_Plan($params); }
protected function parseRow($credit_row) { // @TODO: take to config $required_fields = array(array('credit_type', 'charge_type'), 'amount_without_vat', 'reason', 'account_id', 'subscriber_id', 'credit_time', 'service_name'); // @TODO: take to config $optional_fields = array('vatable' => '1'); $filtered_request = array(); foreach ($required_fields as $field) { $found_field = false; if (is_array($field)) { foreach ($field as $req) { if (isset($credit_row[$req])) { if ($found_field) { unset($credit_row[$req]); // so the stamp won't be calculated on it. } else { $filtered_request[$req] = $credit_row[$req]; $found_field = true; } } } } else { if (isset($credit_row[$field])) { $filtered_request[$field] = $credit_row[$field]; $found_field = true; } } if (!$found_field) { return $this->setError('required field(s) missing: ' . print_r($field, true), $credit_row); } } foreach ($optional_fields as $field => $default_value) { if (!isset($credit_row[$field])) { $filtered_request[$field] = $default_value; } else { $filtered_request[$field] = $credit_row[$field]; } } if (isset($filtered_request['charge_type'])) { $filtered_request['credit_type'] = $filtered_request['charge_type']; unset($filtered_request['charge_type']); } if ($filtered_request['credit_type'] != 'charge' && $filtered_request['credit_type'] != 'refund') { return $this->setError('credit_type could be either "charge" or "refund"', $credit_row); } $amount_without_vat = Billrun_Util::filter_var($filtered_request['amount_without_vat'], FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION); if (!is_numeric($filtered_request['amount_without_vat']) || $amount_without_vat === false) { return $this->setError('amount_without_vat is not a number', $credit_row); } else { // TODO: Temporary conversion. Remove it once they send negative values! if ($filtered_request['credit_type'] == 'refund' && floatval($amount_without_vat) > 0) { $filtered_request['amount_without_vat'] = -floatval($amount_without_vat); } else { $filtered_request['amount_without_vat'] = floatval($amount_without_vat); } } if (is_string($filtered_request['reason'])) { $filtered_request['reason'] = preg_replace('/[^a-zA-Z0-9-_]+/', '_', $filtered_request['reason']); // removes unwanted characters from the string (especially dollar sign and dots) } else { return $this->setError('reason error', $credit_row); } if (!empty($filtered_request['service_name']) && is_string($filtered_request['service_name'])) { $filtered_request['service_name'] = preg_replace('/[^a-zA-Z0-9-_]+/', '_', $filtered_request['service_name']); // removes unwanted characters from the string (especially dollar sign and dots) as they are not allowed as mongo keys } else { return $this->setError('service_name error', $credit_row); } if (isset($filtered_request['account_id'])) { $filtered_request['aid'] = (int) $filtered_request['account_id']; unset($filtered_request['account_id']); } if (isset($filtered_request['subscriber_id'])) { $filtered_request['sid'] = (int) $filtered_request['subscriber_id']; unset($filtered_request['subscriber_id']); } if ($filtered_request['aid'] == 0 || $filtered_request['sid'] == 0) { return $this->setError('account, subscriber ids must be positive integers', $credit_row); } $credit_time = new Zend_Date($filtered_request['credit_time']); $filtered_request['urt'] = new MongoDate($credit_time->getTimestamp()); unset($filtered_request['credit_time']); $filtered_request['vatable'] = filter_var($filtered_request['vatable'], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE); if (!is_null($filtered_request['vatable'])) { $filtered_request['vatable'] = (int) $filtered_request['vatable']; } else { return $this->setError('vatable could be either "0" or "1"', $credit_row); } $filtered_request['source'] = 'api'; $filtered_request['usaget'] = $filtered_request['type'] = 'credit'; ksort($filtered_request); $filtered_request['stamp'] = Billrun_Util::generateArrayStamp($filtered_request); return $filtered_request; }
/** * Gets a variable from the request / session and sets it to the session if found * @param Object $session the session object * @param string $source_name the variable name in the request * @param type $target_name the variable name in the session * @param type $default the default value for the variable * @return type */ protected function getSetVar($session, $source_name, $target_name = null, $default = null) { if (is_null($target_name)) { $target_name = $source_name; } $request = $this->getRequest(); $new_search = $request->get("new_search") == "1"; if (is_array($source_name)) { $key = Billrun_Util::generateArrayStamp($source_name); } else { $key = $source_name; } $var = $request->get($key); if ($new_search) { if (is_string($var) || is_array($var)) { $session->{$target_name} = $var; } else { $session->{$target_name} = $default; } } else { if (is_string($var) || is_array($var)) { $session->{$target_name} = $var; } else { if (!isset($session->{$target_name})) { $session->{$target_name} = $default; } } } return $session->{$target_name}; }
/** * Get the unified row stamp for a given single line. * @param type $newRow the single line to extract the unified row stamp from. * @return a string with the unified row stamp. */ protected function getLineUnifiedLineStamp($newRow) { $typeData = $this->unificationFields[$newRow['type']]; $serialize_array = array(); foreach ($typeData['stamp']['value'] as $field) { if (isset($newRow[$field])) { $serialize_array[$field] = $newRow[$field]; } } foreach ($typeData['stamp']['field'] as $field) { $serialize_array['exists'][$field] = isset($newRow[$field]) ? '1' : '0'; } $dateSeperation = isset($typeData['date_seperation']) ? $typeData['date_seperation'] : $this->dateSeperation; $serialize_array['dateSeperation'] = date($dateSeperation, $newRow['urt']->sec); return Billrun_Util::generateArrayStamp($serialize_array); }
/** * method to update service row from API * @param array $service_row * @return $service_row after addition of fields */ public static function parseServiceRow($service_row, $billrun_key) { $service_row['source'] = 'api'; $service_row['usaget'] = $service_row['type'] = 'service'; $service_row['urt'] = new MongoDate(Billrun_Util::getEndTime($billrun_key)); ksort($service_row); $service_row['stamp'] = Billrun_Util::generateArrayStamp($service_row); return $service_row; }