private static function EGOP_MinAndMaxWallets(Currency $currency)
 {
     $wallets = WalletsEgop::findBy(array('currency_id' => $currency->getId()));
     if (empty($wallets)) {
         return null;
     }
     $maxBalance = null;
     $keyMax = null;
     $minBalance = null;
     $keyMin = null;
     foreach ($wallets as $key => $value) {
         $oAuth = new EgoPayAuth($value['email'], $value['api_id'], $value['api_password']);
         $oEgoPayJsonAgent = new EgoPayJsonApiAgent($oAuth);
         $oEgoPayJsonAgent->setVerifyPeer(false);
         try {
             $balance = $oEgoPayJsonAgent->getBalance($currency->getName());
             if ($minBalance === null || $minBalance > $balance) {
                 $minBalance = $balance;
                 $keyMin = $key;
             }
             if ($maxBalance === null || $maxBalance < $balance) {
                 $maxBalance = $balance;
                 $keyMax = $key;
             }
         } catch (EgoPayApiException $e) {
             Core::write_log(EGOPAY_LOG_PATH, __FUNCTION__ . ' : getBalance Error: ' . $e->getCode() . " : " . $e->getMessage());
         }
     }
     $result['min'] = $wallets[$keyMin];
     $result['max'] = $wallets[$keyMax];
     return $result;
 }
 public static function addNewEGOPWallet()
 {
     $email = Core::validate($_POST['EMAIL']);
     $api_id = Core::validate($_POST['API_ID']);
     $api_password = Core::validate($_POST['API_PASSWORD']);
     $store_id = Core::validate($_POST['STORE_ID']);
     $store_password = Core::validate($_POST['STORE_PASSWORD']);
     $checksum_key = Core::validate($_POST['CHECKSUM_KEY']);
     $currency_name = Core::validate($_POST['CURRENCY']);
     $share = Core::validate($_POST['SHARE']);
     // percent
     if ($email == null || $api_id == null || $api_password == null || $store_id == null || $store_password == null || $checksum_key == null || $currency_name == null || $share == null || !Core::isDouble($share)) {
         print 'Incorrect data';
         exit;
     }
     $currency = new Currency();
     if (!$currency->findBy(array('Name' => $currency_name))) {
         exit;
     }
     $result = WalletsEgop::findBy(array('currency_id' => $currency->getId(), 'email' => $email));
     if (!empty($result)) {
         print 'This wallet already exists';
         exit;
     }
     $oAuth = new EgoPayAuth($email, $api_id, $api_password);
     $oEgoPayJsonAgent = new EgoPayJsonApiAgent($oAuth);
     $oEgoPayJsonAgent->setVerifyPeer(false);
     try {
         $balance = $oEgoPayJsonAgent->getBalance($currency->getName());
     } catch (EgoPayApiException $e) {
         Core::write_log(EGOPAY_LOG_PATH, __FUNCTION__ . ' : getBalance Error: ' . $e->getCode() . " : " . $e->getMessage());
         print $e->getMessage();
         exit;
     }
     $wallet = new WalletsEgop();
     $wallet->setCurrencyId($currency->getId());
     $wallet->setEmail($email);
     $wallet->setApiId($api_id);
     $wallet->setApiPassword($api_password);
     $wallet->setStoreId($store_id);
     $wallet->setStorePassword($store_password);
     $wallet->setChecksumKey($checksum_key);
     $wallet->setValue($balance);
     $wallet->setShare($share / 100.0);
     $wallet->insert();
     header('Location: /admin/egop');
 }