예제 #1
0
 public function actionIndex()
 {
     $message['content'] = '';
     $save = false;
     if (isset($_POST['submit'])) {
         try {
             $inputs = array();
             foreach (new Model($this->db->query(QueryBuilder::getInstance()->select()->from('input'))) as $key => $value) {
                 $value['data'] = unserialize($value['data']);
                 $messages = Input::validate($value, $_POST['input'][$key]);
                 if ($messages !== true) {
                     throw new Exception(implode('<br>', $messages));
                 }
                 $inputs[$key] = $value;
                 $inputs[$key]['value'] = $_POST['input'][$key];
             }
             if (!is_array($_POST['product'])) {
                 throw new Exception("لطفا یک محصول انتخاب کنید");
             }
             if (empty($_POST['product'])) {
                 throw new Exception("لطفا یک محصول انتخاب کنید");
             }
             $product = array_keys($_POST['product']);
             $products = array();
             $sql = $this->db->prepare(QueryBuilder::getInstance()->select()->from('product')->where('product.id IN (' . implode(',', array_fill(0, count($product), '?')) . ')'));
             $sql->execute($product);
             while ($row = $sql->fetch()) {
                 $products[$row['id']] = $row;
             }
             $items = $this->db->prepare(QueryBuilder::getInstance()->select('item.*,price')->from('item')->leftJoin('product')->on('product.id = productid')->where('productid IN (' . implode(',', array_fill(0, count($product), '?')) . ')')->andWith('reservetime < ' . time())->andWith('status =' . Application::STATUS_PENDING));
             $items->execute($product);
             $itemids = array();
             while ($item = $items->fetch()) {
                 if ($item['id']) {
                     $itemids[$item['productid']][] = $item;
                 }
             }
             $buyids = array();
             $additems = array();
             $price = 0;
             foreach ($products as $key => $p) {
                 $value = $_POST['product'][$key];
                 if (!$value) {
                     $value = 1;
                 }
                 if ($p['skipitem'] == 1) {
                     $price = $p['price'] * $value + $price;
                     $additems[$key] = $value;
                     continue;
                 }
                 if ($value > count($itemids[$key])) {
                     throw new Exception("محصول انتخاب شده موجود نیست");
                 } else {
                     for ($i = 0; $i < $value; $i++) {
                         $item = array_shift($itemids[$key]);
                         $price = $item['price'] + $price;
                         $buyids[] = $item['id'];
                     }
                 }
             }
             $gateway = $this->db->prepare(QueryBuilder::getInstance()->select()->from('gateway')->leftJoin('option')->on('`class` = category')->where('gateway.id = ?'));
             $gateway->execute(array($_POST['gatewayid']));
             $gateway = $gateway->fetchAll();
             if (!$gateway) {
                 throw new Exception("درگاه وارد شده معتبر نیست");
             }
             $sql = $this->db->prepare(QueryBuilder::getInstance()->insert('payment')->into('`requesttime`, `status`, `clientip`, `gatewayid`, `amount`', true));
             $param = array('requesttime' => time(), 'status' => Application::STATUS_PENDING, 'clientip' => $_SERVER['REMOTE_ADDR'], 'gatewayid' => $_POST['gatewayid'], 'amount' => $price);
             $sql->execute($param);
             $param['id'] = $this->db->lastInsertId();
             foreach ($inputs as $key => $value) {
                 $sql = $this->db->prepare(QueryBuilder::getInstance()->insert('payment_meta')->into(array('paymentid', 'inputid', 'value'), true, false));
                 $sql->execute(array($param['id'], $key, $value['value']));
             }
             if (!empty($buyids)) {
                 $this->db->exec(QueryBuilder::getInstance()->update('item')->set('paymentid=' . $param['id'] . ',reservetime=' . (time() + CShop::app()->systemConfig()->reservetime * 60 * 60))->where('id IN (' . implode(',', $buyids) . ')'));
             }
             $additemids = array();
             if (!empty($additems)) {
                 foreach ($additems as $key => $value) {
                     for ($i = 0; $i < $value; $i++) {
                         $sql = $this->db->prepare(QueryBuilder::getInstance()->insert('item')->into(array('productid', 'status', 'createtime', 'paymentid', 'reservetime'), true, false));
                         $sql->execute(array($key, Application::STATUS_SYSTEM_ADDED, time(), $param['id'], time() + CShop::app()->systemConfig()->reservetime * 60 * 60));
                         $additemids[] = $this->db->lastInsertId();
                     }
                 }
             }
             $save = true;
             $param['input'] = $inputs;
             CShop::app()->raise(Application::EVENT_BEFORE_PAYMENT, array(&$param, &$products));
             CShop::import(Cshop::$gatewaypath . DIRECTORY_SEPARATOR . $gateway[0]['class'] . '.php');
             /* @var $plugin GatewayBase */
             $plugin = new $gateway[0]['class']($gateway[0]['id'], $gateway);
             $message = $plugin->sendToGateway($param, Cshop::siteURL() . Cshop::$baseurl . '/payment.php?gateway=' . $_POST['gatewayid']);
         } catch (Exception $e) {
             $message['content'] = $e->getMessage();
         }
         if (isset($message) && $save) {
             if (!empty($buyids)) {
                 $this->db->exec(QueryBuilder::getInstance()->update('item')->set('reservetime=0')->where('id IN (' . implode(',', $buyids) . ')'));
             }
             if (!empty($additemids)) {
                 $this->db->exec(QueryBuilder::getInstance()->delete('item')->where('id IN (' . implode(',', $additemids) . ')'));
             }
         }
     }
     $product = new Model($this->db->query(QueryBuilder::getInstance()->select()->from('product')->order('`order`')));
     $category = new Model($this->db->query(QueryBuilder::getInstance()->select()->from('category')->order('`order`')));
     $gateway = new Model($this->db->query(QueryBuilder::getInstance()->select()->from('plugin')->where('type=' . Application::PLUGIN_TYPE_GATEWAY)->order('`order`')));
     $input = new Model($this->db->query(QueryBuilder::getInstance()->select()->from('input')->order('`order`')));
     $this->render('site/index', array('input' => $input, 'product' => $product, 'category' => $category, 'gateway' => $gateway, 'message' => $message));
 }