Ejemplo n.º 1
0
 public function walletPayAction($payId, $walletId)
 {
     $pay = Money\Pay::get((int) $payId);
     if (!$pay || $pay->user_id != \Users\User::$cur->id) {
         Tools::redirect('/money/merchants/pay/', 'Такой счет не найден');
     }
     $wallet = Money\Wallet::get((int) $walletId);
     if (!$wallet || $wallet->user_id != \Users\User::$cur->id) {
         Tools::redirect('/money/merchants/pay/' . $pay->id, 'Такой кошелек не найден');
     }
     if ($pay->currency_id != $wallet->currency_id) {
         $rate = \Money\Currency\ExchangeRate::get([['currency_id', $wallet->currency_id], ['target_currency_id', $pay->currency_id]]);
         if (!$rate) {
             Tools::redirect('/money/merchants/pay/' . $pay->id, 'Нет возможности оплатить счет в валюте ' . $pay->currency->name() . ' валютой ' . $wallet->currency->name());
         }
         $sum = $pay->sum / $rate->rate;
     } else {
         $sum = $pay->sum;
     }
     if ($sum > $wallet->amount) {
         Tools::redirect('/money/merchants/pay/' . $pay->id, 'На вашем счете недостаточно средств', 'danger');
     }
     $wallet->diff(-$sum, 'Оплата счета №' . $payId);
     $statuses = \Money\Pay\Status::getList(['key' => 'code']);
     if (!empty($statuses['success'])) {
         $pay->pay_status_id = $statuses['success']->id;
     }
     $pay->date_recive = date('Y-m-d H:i:s');
     $pay->save();
     if ($pay->callback_module && $pay->callback_method) {
         App::$cur->{$pay->callback_module}->{$pay->callback_method}(['status' => 'success', 'payId' => $pay->id, 'pay' => $pay]);
     }
     Tools::redirect('/users/cabinet', 'Вы успешно оплатили счет', 'success');
 }
Ejemplo n.º 2
0
 public function getUserWallets($userId = null, $walletIdasKey = false, $forSelect = false, $transferOnly = false)
 {
     $userId = $userId ? $userId : \Users\User::$cur->id;
     if (!$userId) {
         return [];
     }
     $this->getUserBlocks($userId);
     $where = [['wallet', 1]];
     if ($transferOnly) {
         $where[] = ['transfer', 1];
     }
     $currencies = Money\Currency::getList(['where' => $where]);
     $wallets = Money\Wallet::getList(['where' => ['user_id', $userId], 'key' => 'currency_id']);
     $result = [];
     foreach ($currencies as $currency) {
         if (empty($wallets[$currency->id])) {
             $wallet = new Money\Wallet();
             $wallet->user_id = $userId;
             $wallet->currency_id = $currency->id;
             $wallet->save();
             $result[$walletIdasKey ? $wallet->id : $currency->id] = $forSelect ? $wallet->name() : $wallet;
         } else {
             $result[$walletIdasKey ? $wallets[$currency->id]->id : $currency->id] = $forSelect ? $wallets[$currency->id]->name() : $wallets[$currency->id];
         }
     }
     return $result;
 }