/** * method to execute the refund * it's called automatically by the api main controller */ public function execute() { Billrun_Factory::log()->log("Execute credit", Zend_Log::INFO); $request = $this->getRequest()->getRequest(); // supports GET / POST requests $parsed_row = $this->parseRow($request); if (is_null($parsed_row)) { return; } try { $linesCollection = Billrun_Factory::db()->linesCollection(); if ($linesCollection->query('stamp', $parsed_row['stamp'])->count() > 0) { return $this->setError('Transaction already exists in the DB', $request); } $parsed_row['process_time'] = date(Billrun_Base::base_dateformat); $entity = new Mongodloid_Entity($parsed_row); if ($entity->save($linesCollection, 1) === false) { return $this->setError('failed to store into DB lines', $request); } if ($this->insertToQueue($entity) === false) { return $this->setError('failed to store into DB queue', $request); } else { $this->getController()->setOutput(array(array('status' => 1, 'desc' => 'success', 'stamp' => $entity['stamp'], 'input' => $request))); Billrun_Factory::log()->log("Added credit line " . $entity['stamp'], Zend_Log::INFO); return true; } } catch (\Exception $e) { Billrun_Factory::log()->log('failed to store into DB got error : ' . $e->getCode() . ' : ' . $e->getMessage(), Zend_Log::ALERT); Billrun_Factory::log()->log('failed saving request :' . print_r($request, 1), Zend_Log::ALERT); Billrun_Factory::log()->log('failed saving :' . json_encode($parsed_row), Zend_Log::ALERT); Billrun_Util::logFailedCreditRow($parsed_row); return $this->setError('failed to store into DB queue', $request); } }
/** * create and save credit lines * @param type $subscriber * @param type $billrun_key * @return array of inserted lines */ protected function saveCreditLines($subscriber, $billrun_key) { $credits = $subscriber->getCredits($billrun_key, true); $ret = array(); foreach ($credits as $credit) { $rawData = $credit->getRawData(); try { $this->lines->insert($rawData, array("w" => 1)); } catch (Exception $e) { if ($e->getCode() == 11000) { Billrun_Factory::log("Credit already exists for subscriber " . $subscriber->sid . " for billrun " . $billrun_key . " credit details: " . print_R($rawData, 1), Zend_log::ALERT); } else { Billrun_Factory::log("Problem inserting credit for subscriber " . $subscriber->sid . " for billrun " . $billrun_key . ". error message: " . $e->getMessage() . ". error code: " . $e->getCode() . ". credit details:" . print_R($rawData, 1), Zend_log::ALERT); Billrun_Util::logFailedCreditRow($rawData); } } $ret[$credit['stamp']] = $credit; } return $ret; }