public function login($email = false, $passwd = false) { $email || ($email = $this->getPost('email')); $passwd || ($passwd = $this->getPost('passwd')); $orbit = new Orbit(); $client = $orbit->get('client/login', 1, 1, array('email' => $email, 'passwd' => $passwd)); if ($client['status'] == 200) { $phones = $orbit->get('client/phones/' . $client['uid']['id']); UID::set($client['uid']); UID::set('phones', $phones['phones']); $orbit = new Orbit(); $favRequest = $orbit->get('client/countfav', 1, 1, array('id' => UID::get('id'))); $favs = $favRequest['fav']; $cartRequest = $orbit->get('client/countcart', 1, 1, array('id' => UID::get('id'))); $carts = $cartRequest['cart']; $this->commitReplace($carts, '#cartitems'); $this->commitReplace($favs, '#favitems'); $product_id = $this->getQueryString('product_id'); if ($product_id) { $productPage = Services::get('products'); $productPage->setId($product_id); $productPage->viewProduct(); return; } $home = Services::get('home'); $home->homePage(); return; } $this->commitReplace('O e-mail e senha não foram encontrados', '#loginmsg'); $this->commitShow('#loginmsg'); }
public function purchase() { $shipping = UID::get('shipping_options', $this->getPost('shipping_option')); $address_id = $this->getPost('address_id'); if (!$address_id) { $this->commitReplace('Informe o endereço de entrega', '#submitmsg'); $this->commitShow('#submitmsg'); return; } $order_id = $this->getQueryString('order_id'); $cartItems = $this->getCartItems($order_id); if (!$order_id) { $cart = $this->createCart(); foreach ($cartItems as $item) { $this->addCartItem($cart['id'], $item['id']); } } Session::del('cart'); $hash = String::generateHash(); if (!$shipping) { $this->commitReplace('Informe o tipo de envio', '#submitmsg'); $this->commitShow('#submitmsg'); return; } $shipping_fields = array('Codigo' => 'shipping_code', 'Valor' => 'shipping_value', 'PrazoEntrega' => 'delivery_time', 'ValorMaoPropria' => 'hand_value', 'ValorAvisoRecebimento' => 'notify_value', 'ValorValorDeclarado' => 'recover_value', 'EntregaDomiciliar' => 'home_delivery', 'EntregaSabado' => 'weekend_delivery'); $orbit = new Orbit(); $clientAddr = $orbit->get('client/address/' . $address_id); $address = $clientAddr['address']; $getCart = array('client_id' => UID::get('id')); if (isset($cart['id'])) { $getCart['id'] = $cart['id']; } $request = $orbit->get('request/cart', 1, 1, $getCart); if (!isset($request['cart']) || $request['cart'] === 0) { $this->commitReplace('Ocorreu um problema na sua sessão. Faça o login novamente.', '#submitmsg'); $this->commitShow('#submitmsg'); return; } $cart = $request['cart']; $purchaseData = array(); foreach ($shipping_fields as $key => $field) { $purchaseData[$field] = $shipping[$key]; } $requestItems = $orbit->get('client/cartitems', 1, 100, array('id' => UID::get('id'), 'request_id' => $cart['id'])); if (!isset($requestItems['cart']) || count($requestItems['cart']) == 0) { $this->commitReplace('Não foi possível iniciar a transação. Por favor, entre em contato conosco e nos informe: ' . $requestItems['message'], '#submitmsg'); $this->commitShow('#submitmsg'); } $cartItems = $requestItems['cart']; $pagSeguro = new PagSeguro(); $totalPrice = 0; foreach ($cartItems as $item) { //TODO: Quantidade variável $pagSeguro->addItem($item['id'], $item['product_name'], $item['price'], '1', $item['weight']); $totalPrice += $item['price']; } $orbit->put('request/' . $cart['id'], array('final_price' => $totalPrice)); $totalPrice += floatval(str_replace(',', '.', $shipping['Valor'])); $purchaseData['client_id'] = UID::get('id'); $purchaseData['request_id'] = $cart['id']; $purchaseData['address_id'] = $address_id; $purchaseData['pay_hash'] = $hash; $purchaseData['price'] = $totalPrice; $config = $orbit->get('config'); $config = current($config['config']); $phones = UID::get('phones'); $area = ''; $phone = ''; if (count($phones) > 0) { $firstPhone = current($phones); $phoneInfo = explode(' ', $firstPhone['phone_number'], 2); if (count($phoneInfo) > 1) { $area = $phoneInfo[0]; $phone = str_replace('-', '', $phoneInfo[1]); } } $pagSeguro->addSender(UID::get('client_name'), UID::get('email'), $area, $phone); $pagSeguro->addShipping(PagSeguro::getShippingType($purchaseData['shipping_code']), $purchaseData['shipping_value'], $address['street_addr'], $address['street_number'], $address['street_additional'], $address['hood'], $address['city'], $address['zip_code'], $address['state']); $pagSeguro->setReference($hash); $pagSeguro->setRedirectURL(MAINURL . '/cart/confirmed?order=' . $hash); $pagSeguro->setAccountEmail($config['payment_account']); $pagSeguro->setToken($config['token']); UID::set('purchase_data', $hash, $purchaseData); $code = $pagSeguro->submit(); if ($code) { $response = $orbit->put('request/' . $cart['id'], array('pay_hash' => $hash, 'pay_token' => $code)); $pagSeguro->redirect(); } $submit = $pagSeguro->getResponse(); if (isset($submit['error'])) { $this->commitReplace('Não foi possível iniciar a transação. Por favor, entre em contato conosco e nos informe: ' . $submit['error']['message'], '#submitmsg'); $this->commitShow('#submitmsg'); } }