public static function EGOP_transaction_o_complete()
 {
     $id = Core::validate($_GET['id']);
     $hash = Core::validate($_GET['hash']);
     $at = new AtEgop();
     if (!$at->findById($id)) {
         header('Location: ' . URL_WRONG_DATA_INPUT);
         exit;
     }
     if ($hash != self::hash_for_money_output_link($at->getId(), $at->getUID())) {
         header('Location: ' . URL_WRONG_DATA_INPUT);
         exit;
     }
     $currency = new Currency();
     $currency->findById($at->getCurrencyId());
     $wallets = self::EGOP_MinAndMaxWallets($currency);
     $amount = $at->getAmount();
     if ($wallets['max'] == null || $wallets['max']['current_balance'] < $amount) {
         Core::write_log(EGOPAY_LOG_PATH, __FUNCTION__ . ' : Not enough money on this wallet (id) : ' . $wallets['max']['id'] . '(at_egop id=' . $at->getId() . ')');
         header('Location: ' . URL_SERVER_ERROR);
         exit;
     }
     $wallet = new WalletsEgop();
     $wallet->findById($wallets['max']['id']);
     $oAuth = new EgoPayAuth($wallet->getEmail(), $wallet->getApiId(), $wallet->getApiPassword());
     $oEgoPayJsonAgent = new EgoPayJsonApiAgent($oAuth);
     $oEgoPayJsonAgent->setVerifyPeer(false);
     try {
         $oResponse = $oEgoPayJsonAgent->getTransfer($at->getClientAccount(), $at->getAmount(), $currency->getName(), 'Transfer from Bitmonex');
         // for debug
         Core::write_log(EGOPAY_LOG_PATH, __FUNCTION__ . ' : getTransfer response: ' . print_r($oResponse, true));
         reset($oResponse);
         //
     } catch (EgoPayApiException $e) {
         // error codes from here: https://www.egopay.com/developers/api/payments/send-payment
         if ($e->getCode() == 11 || $e->getCode() == 12) {
             header('Location: ' . URL_WRONG_DATA_INPUT);
             exit;
         }
         Core::write_log(EGOPAY_LOG_PATH, __FUNCTION__ . ' : getTransfer Error: ' . $e->getCode() . " : " . $e->getMessage());
         header('Location: ' . URL_SERVER_ERROR);
         exit;
     }
     $limits = self::transactionLimits($currency->getId(), 'EGOP', 1);
     $feeVolume = $amount * $limits['fee'];
     $feeVolume = Core::round_up($feeVolume, 2);
     $purses = Purse::findBy(array('UID' => $at->getUID(), 'CurId' => $currency->getId()));
     $purse = new Purse();
     $purse->findById($purses[0]['id']);
     $purse->addValue(-($amount + $feeVolume));
     $purse->save();
     $systemFeeVolume = $amount * $limits['system_fee'];
     $systemFeeVolume = Core::round_up($systemFeeVolume, 2);
     $profit = ($feeVolume - $systemFeeVolume) * $wallet->getShare();
     $wallet->setProfit($wallet->getProfit() + $profit);
     $wallet->save();
     $at->setStatus(1);
     $at->setOurWalletId($wallet->getId());
     $at->setTransactionId($oResponse->transaction->sId);
     $at->setTimestamp(Core::timestamp_gmp());
     $at->save();
     header('Location: ' . URL_SUCCESS_PAYMENT);
 }
 private static function getPurse($userId, $curId)
 {
     $result = Purse::findBy(array('UID' => $userId, 'CurId' => $curId));
     if (empty($result)) {
         return null;
     }
     $purse = new Purse();
     $purse->findById($result[0]['id']);
     return $purse;
 }
 private static function getFunds($userId)
 {
     $funds = array();
     $currency = new Currency();
     $purseStorage = Purse::findBy(array('UID' => $userId));
     foreach ($purseStorage as $purse) {
         $currency->findById($purse['CurId']);
         $funds[$currency->getName()] = $purse['Value'];
     }
     return $funds;
 }
 private static function getUserPurses($userId)
 {
     $session_id = Core::validate($_COOKIE['PHPSESSID']);
     if ($session_id == null) {
         return null;
     }
     $purse = new Purse();
     $purseStorage = $purse->findBy(array('UID' => $userId));
     $curr = new Currency();
     foreach ($purseStorage as $key => $value) {
         $curr->findById($value['CurId']);
         $purseStorage[$key]['CurName'] = $curr->getName();
         $purseStorage[$key]['tradeName'] = $curr->getTradeName();
         $purseStorage[$key]['minOrderAmount'] = $curr->getMinOrderAmount();
     }
     return $purseStorage;
 }