Пример #1
0
 /**
  * Order info: total price, quantity, num of items, currency,
  * shipping price, grand total price
  * 
  * @param type $id
  * @return type
  */
 public function order_info($id = false)
 {
     $out = array();
     if ($item = \Order\Model_Order::find_one_by_id($id)) {
         if (!empty($item->products)) {
             $currency = $item->currency;
             $sum_products = count($item->products);
             $sum_products_quantity = 0;
             $total_price = 0;
             foreach ($item->products as $key => $product) {
                 $total_price += $product->sub_total_with_tax;
             }
             $out = array('total_price' => $total_price, 'quantity' => $sum_products_quantity, 'num_items' => $sum_products, 'currency' => $currency, 'shipping_price' => $item->shipping_price, 'grand_total_price' => $total_price + $item->shipping_price);
             if ($item->is_sample_order == 1) {
                 $out = array('total_price' => $item->total_price, 'quantity' => $sum_products_quantity, 'num_items' => $sum_products, 'currency' => $currency, 'shipping_price' => $item->shipping_price, 'grand_total_price' => $item->total_price + $item->shipping_price);
             }
         }
     }
     return $out;
 }
Пример #2
0
 public static function credit_account($user_id = null, $new_amount = 0)
 {
     if (is_numeric($user_id) && \Sentry::user_exists((int) $user_id)) {
         $user = \Sentry::user((int) $user_id);
     } else {
         $user = \Sentry::user();
     }
     $out['credit'] = false;
     if ($user->get('metadata.credit_account') != 1) {
         return $out;
     }
     $out['credit'] = true;
     if ($month = $user->get('metadata.purchase_limit_period')) {
         $start = strtotime(date('m-01-Y', strtotime("-{$month} month")));
         $orders_total = 0;
         $orders = \Order\Model_Order::find(array('where' => array(array('created_at', '>', $start), 'user_id' => $user->get('id'), 'finished' => 1)));
         if ($orders) {
             foreach ($orders as $order) {
                 $orders_total += $order->total_price + $order->shipping_price;
             }
         }
         if ($orders_total + $new_amount > $user->get('metadata.purchase_limit_value')) {
             $out['over_limit'] = true;
             $out['over_limit_amount'] = $orders_total + $new_amount - $user->get('metadata.purchase_limit_value');
         } else {
             $out['over_limit'] = false;
             $out['over_limit_amount'] = 0;
         }
         $out['orders_total'] = $orders_total;
         $out['limit'] = $user->get('metadata.purchase_limit_value');
         $out['period'] = $user->get('metadata.purchase_limit_period');
     }
     if (is_numeric(\Session::get('order.id'))) {
         $order = \Order\Model_Order::find_one_by_id(\Session::get('order.id'));
         if (isset($order)) {
             $new_amount = $order->total_price + $order->shipping_price - $order->discount_amount;
         }
     }
     $out['current_total'] = $new_amount;
     return $out;
 }
Пример #3
0
 /**
  * Catch IPN response from PayPal
  *
  * @access  public
  * @return  void
  */
 public function action_ipn()
 {
     \Package::load('paypalipn');
     $post = \Input::post();
     $admin_email = '*****@*****.**';
     if (isset($post['invoice']) || is_numeric($post['invoice'])) {
         if ($order = \Order\Model_Order::find_one_by_id($post['invoice'])) {
             $order_info = $this->order_info($post['invoice']);
             //$validate_order['mc_currency'] =  strtoupper($order['currency']);
             $validate_order['mc_currency'] = "AUD";
             $validate_order['mc_gross'] = $order_info['grand_total_price'];
             $response = \PaypalIpn::forge()->validate($validate_order);
             $response_check = $response !== false;
             //                @mail($admin_email, \Config::get('site_title', 'Giant Invitation Australia') . ' Pre response ' . $response['invoice'],
             //                    'Order id : ' . $response['invoice'] . "\n" .
             //                    'Response:' . print_r($response, true) . "\n" .
             //                    'Check:' . $response_check);
             $payment = \Payment\Model_Payment::forge();
             /*
             [mc_gross] => 527.00
             [invoice] => 9
             [protection_eligibility] => Eligible
             [address_status] => confirmed
             [item_number1] =>
             [tax] => 0.00
             [item_number2] =>
             [payer_id] => 2AY578XVRY42G
             [address_street] => 1 Main St
             [payment_date] => 05:19:51 Dec 20, 2012 PST
             [payment_status] => Completed
             [charset] => windows-1252
             [address_zip] => 95131
             [mc_shipping] => 0.00
             [mc_handling] => 0.00
             [first_name] => Djordje
             [mc_fee] => 20.90
             [address_country_code] => US
             [address_name] => Djordje Dimitrijev
             [notify_version] => 3.7
             [custom] =>
             [payer_status] => verified
             [business] => s01_1354717870_biz@eximius-solutions.com
             [address_country] => United States
             [num_cart_items] => 2
             [mc_handling1] => 0.00
             [mc_handling2] => 0.00
             [address_city] => San Jose
             [verify_sign] => Amjz0My6wXvXmjP5pfTStQO3QZ2QA4.Ti6ln42PKcmRKuS-ZegoVx6nF
             [payer_email] => b01_1354717574_per@eximius-solutions.com
             [mc_shipping1] => 0.00
             [mc_shipping2] => 0.00
             [tax1] => 0.00
             [tax2] => 0.00
             [txn_id] => 3AR12649JA9996934
             [payment_type] => instant
             [last_name] => Dimitrijev
             [address_state] => CA
             [item_name1] => Fujiyama
             [receiver_email] => s01_1354717870_biz@eximius-solutions.com
             [item_name2] => Musala
             [payment_fee] =>
             [quantity1] => 2
             [quantity2] => 1
             [receiver_id] => W4MYKU8N4SVHS
             [txn_type] => cart
             [mc_gross_1] => 398.00
             [mc_currency] => EUR
             [mc_gross_2] => 129.00
             [residence_country] => US
             [test_ipn] => 1
             [transaction_subject] => Shopping CartFujiyamaMusala
             [payment_gross] =>
             [ipn_track_id] => 72c4d71c8638a
             */
             if ($response !== false) {
                 $json_response = json_encode($response);
                 $payment->order_id = $response['invoice'];
                 $payment->total_price = $response['mc_gross'];
                 $payment->method = 'paypal';
                 $payment->status = $response['payment_status'];
                 $payment->response = $json_response;
                 if (isset($response['pending_reason'])) {
                     $payment->status_detail = $response['pending_reason'];
                 }
                 if (strtolower($response['payment_status']) == 'completed') {
                     // Send email
                     /*$emailer = new \PaymentProccess\PaymentEmailer($order);
                       $emailer->send();*/
                     $emailer = new \Autorespondertrigger\Trigger();
                     $emailer->sendOrderPaymentReceivedPaypalCredit($order);
                     $order->total_price = $order_info['total_price'];
                     $order->paymentmethod = 'N/A';
                     if (isset($order->payments[0]->method)) {
                         $order->paymentmethod = $order->payments[0]->method;
                     }
                     // Create redemption code ( if order is sample order - this will be checked in RedemptionCodeSaverClass)
                     $redemptionCodeSaver = new \DiscountCodeApplier\RedemptionCodeSaver($order);
                     $redemptionCodeSaver->save();
                 } else {
                     logger(\Fuel::L_INFO, 'PayPal IPN - Pending order. Order ID: ' . $response['invoice'] . '. - ' . $json_response);
                     @mail($admin_email, \Config::get('site_title', 'Giant Invitation Australia') . ' | PayPal IPN - Pending order. Order ID: ' . $response['invoice'], 'PayPal IPN - Pending order. Order ID: ' . $response['invoice'] . "\n" . print_r($response, true));
                 }
             } else {
                 $payment->order_id = $post['invoice'];
                 $payment->total_price = $post['mc_gross'];
                 $payment->method = 'paypal';
                 $payment->status = 'Pending';
                 $payment->response = json_encode($post);
                 // Log order as failed
                 logger(\Fuel::L_INFO, 'PayPal IPN - Failed order. Order ID: ' . $response['invoice'] . '. - ' . $json_response);
                 @mail($admin_email, \Config::get('site_title', 'Giant Invitation Australia') . ' | PayPal IPN - Failed order. Order ID: ' . $response['invoice'], 'PayPal IPN - Failed order. Order ID: ' . $response['invoice'] . "\n" . print_r($response, true));
             }
             // Save payment to database
             try {
                 $payment->save();
             } catch (\Database_Exception $e) {
                 logger(\Fuel::L_INFO, 'PayPal IPN - error during inserting data into the database. Order ID: ' . $response['invoice'] . '. - ' . $json_response);
                 @mail($admin_email, \Config::get('site_title', 'My Shortlist') . ' | PayPal IPN - DB error. Order ID: ' . $response['invoice'], 'PayPal IPN - error during inserting data into the database. Order ID: ' . $response['invoice'] . "\n" . print_r($response, true));
             }
         } else {
             logger(\Fuel::L_INFO, 'PayPal IPN - error. There is no order with ID: ' . $post['invoice'] . '. - ' . json_encode($post));
             @mail($admin_email, \Config::get('site_title', 'My Shortlist') . ' | PayPal IPN - error. There is no order with ID: ' . $post['invoice'], 'PayPal IPN - there is no order with ID: ' . $post['invoice'] . "\n" . print_r($post, true));
         }
     } else {
         logger(\Fuel::L_INFO, 'PayPal IPN - error. Missing Order ID. - ' . json_encode($post));
         @mail($admin_email, \Config::get('site_title', 'My Shortlist') . ' | PayPal IPN - error. Missing Order ID.', 'PayPal IPN - Missing Order ID.' . "\n" . print_r($post, true));
     }
 }
Пример #4
0
 protected function save_order()
 {
     if (!$this->check_logged()) {
         \Messages::error('You must be logged in if you want to continue with your order.');
         \Response::redirect(\Uri::create('order/checkout/address'));
     }
     // Save order
     $user = false;
     $order = false;
     $items = \Cart::items();
     if (\Sentry::check()) {
         $user = \Sentry::user();
     }
     if (\Input::post() && $items && $user) {
         $group_id = $user['groups'][0]['id'];
         $item_with_discount = array();
         foreach ($items as $item) {
             $id = $item->get('id');
             $product_groups = \Product\Model_Product_To_Groups::find_by_product_id($item->get('id'));
             foreach ($product_groups as $group) {
                 $all_discounts = \Product\Model_Group_Discounts::find_by(array('user_group_id' => $group_id, 'product_group_id' => $group->group_id), null, null, null);
                 foreach ($all_discounts as $discount) {
                     $discount = (int) $item_with_discount[$id]['discount'] + $discount->discount;
                     $sub_total = $item->totalPrice(true) - (int) $discount / $item->totalPrice(true) * 100;
                     $item_with_discount[$id] = array('product_group_id' => $group->product_id, 'user_group_id' => $group->group_id, 'discount' => $discount, 'sub_total' => $sub_total);
                 }
             }
             $item_with_discount['total_discount'] = (int) $item_with_discount['total_discount'] + (int) $item_with_discount[$id]['total_discount'];
             $item_with_discount['total_price'] = (double) $item_with_discount['total_price'] + (double) $item_with_discount[$id]['sub_total'];
         }
         // check for a valid CSRF token
         if (!\Security::check_token()) {
             \Messages::error('CSRF attack or expired CSRF token.');
             \Response::redirect(\Input::referrer(\Uri::create('order/checkout/cost')));
         }
         try {
             // Update or create order
             if (is_numeric(\Session::get('order.id'))) {
                 $order = \Order\Model_Order::find_one_by_id(\Session::get('order.id'));
             }
             if (!$order) {
                 $order = \Order\Model_Order::forge();
             }
             $shipping_price = $order->shipping_price(null, null, true);
             $metadata = $user['metadata'];
             if ($billing = \Arr::filter_prefixed($metadata, 'shipping_')) {
                 foreach ($billing as $key => $value) {
                     $order->{$key} = $metadata[$key];
                     unset($metadata[$key]);
                 }
             }
             foreach ($metadata as $key => $value) {
                 $order->{$key} = $value;
             }
             $order->email = $user->get('email');
             $order->user_id = $user->get('id');
             $order->status = 'Pending';
             $order->discount_amount = $item_with_discount['total_discount'];
             //\Cart::getTotal('price');
             $order->total_price = $item_with_discount['total_price'];
             //\Cart::getTotal('price');
             $order->finished = 1;
             $order->guest = $metadata['guest'] ? 1 : 0;
             $order->accepted = $metadata['master'] == 1 ? 1 : 0;
             $order->credit_account = $metadata['credit_account'] == 1 ? 1 : 0;
             $order->shipping_price = $shipping_price;
             // Save order, add products to order products
             if ($order->save()) {
                 foreach ($items as $item) {
                     $product_data = null;
                     if ($product = \Product\Model_Product::find_one_by_id($item->get('id'))) {
                         $product_data = \Product\Model_Product::product_data($product, $item->get('attributes'));
                     }
                     if ($product_data) {
                         $order_products = \Order\Model_Products::forge();
                         $order_products->order_id = $order->id;
                         $order_products->title = $product->title;
                         $order_products->code = $product_data['code'];
                         $order_products->price = $item->singlePrice(true);
                         $order_products->price_type = $product_data['price_type'];
                         $order_products->quantity = $item->get('quantity');
                         $order_products->product_id = $product->id;
                         $order_products->artwork_required = $product->artwork_required;
                         $order_products->artwork_free_over = $product->artwork_free_over;
                         $order_products->subtotal = $item_with_discount[$item->get('id')]['sub_total'];
                         //$item->totalPrice(true);
                         $order_products->attributes = json_encode(\Product\Model_Attribute::get_combination($item->get('attributes')));
                         if (!empty($product->categories)) {
                             $categories = array();
                             foreach ($product->categories as $category) {
                                 $categories[] = $category->title;
                             }
                             if ($categories) {
                                 $order_products->product_category = implode(',', $categories);
                             }
                         }
                         $order_products->save();
                         // Find artworks
                         if ($unique_id = $item->get('unique_id')) {
                             if ($artworks = \Order\Model_Artwork::find(array('where' => array('unique_id' => $unique_id, 'order_id' => $order->id)))) {
                                 $ysi = \Yousendit\Base::forge();
                                 // Artworks (update, delete)
                                 foreach ($artworks as $artwork) {
                                     // Remove deleted artwork
                                     if ($artwork->deleted_at > 0) {
                                         $ysi->delete_artwork($artwork->file_id);
                                         $artwork->delete();
                                     } else {
                                         $artwork->order_product_id = $order_products->id;
                                         $artwork->save();
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             if ($order) {
                 return $order;
             } else {
                 return false;
             }
         } catch (\Database_Exception $e) {
             // show validation errors
             \Messages::error('There was an error while trying to save your order.');
             // Uncomment lines below to show database errors
             $errors = $e->getMessage();
             \Messages::error($errors);
             \Response::redirect(\Uri::create('order/checkout/cost'));
         }
         return false;
     }
 }
Пример #5
0
 public function action_clear_discount()
 {
     $order = \Order\Model_Order::forge();
     if (is_numeric(\Session::get('order.id'))) {
         $order = \Order\Model_Order::find_one_by_id(\Session::get('order.id'));
         $order->set(array('id_discount' => NULL, 'discount_amount' => 0));
         $order->save();
     }
 }
Пример #6
0
 public function action_accept_order($id = null)
 {
     // Get order
     $order = \Order\Model_Order::find_one_by_id($id);
     if (!$order) {
         \Messages::error('Order with that ID does not exist or has been deleted.');
         \Response::redirect(\Uri::front_create('user/account/orders'));
     }
     // Get current user
     $user = \Sentry::user();
     $master_user = false;
     if (isset($user['metadata']['master']) && $user['metadata']['master']) {
         $master_user = true;
     }
     if (!$master_user) {
         \Messages::error("You don't have permssion for this action.");
         \Response::redirect(\Uri::front_create('user/account'));
     }
     try {
         $order->accepted = 1;
         $order->save();
         \Messages::success('Order successfully updated.');
         \Response::redirect(\Input::referrer(\Uri::front_create('user/account')));
     } catch (\Database_Exception $e) {
         \Messages::error('There was an error while trying to update order.');
     }
 }
Пример #7
0
 public function action_pdf($order_id = false)
 {
     if (!($order = \Order\Model_Order::find_one_by_id($order_id))) {
         \Messages::error('There was an error while trying to create PDF for order ID: ' . $order_id);
         \Response::redirect(\Input::referrer(\Uri::create('admin/order/list')));
     }
     $pdf = \Pdf\Pdf::forge('tcpdf')->init('P', 'mm', 'A4', true, 'UTF-8', false);
     // set document information
     $pdf->SetCreator(PDF_CREATOR);
     $pdf->SetAuthor('Cool Kids Rooms');
     $pdf->SetTitle('Invoice');
     $pdf->SetSubject('Invoice');
     //$pdf->SetKeywords('TCPDF, PDF, example, test, guide');
     // remove default header/footer
     $pdf->setPrintHeader(false);
     $pdf->setPrintFooter(false);
     // set default monospaced font
     $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
     // set margins
     $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
     // set auto page breaks
     $pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
     // set image scale factor
     $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
     // set font
     //$pdf->SetFont('times', 'BI', 20);
     // add a page
     $pdf->AddPage();
     // ---------------------------------------------------------
     $content['content']['data'] = $order;
     $html = \Theme::instance()->view($this->view_dir . 'invoice/order', $content, false);
     // output the HTML content
     $pdf->writeHTML($html, true, false, true, false, '');
     // ---------------------------------------------------------
     //Close and output PDF document
     $pdf->Output('Invoice Order ' . $order_id . '.pdf', 'I');
 }
Пример #8
0
 public function action_clear_cart()
 {
     $items = \Cart::items();
     $order = null;
     if (is_numeric(\Session::get('order.id'))) {
         $order = \Order\Model_Order::find_one_by_id(\Session::get('order.id'));
     }
     if ($order) {
         $order->delete();
     }
     if ($items) {
         foreach ($items as $item) {
             // Find artworks
             if ($unique_id = $item->get('unique_id')) {
                 if ($artworks = \Order\Model_Artwork::find(array('where' => array('unique_id' => $unique_id, 'order_id' => \Session::get('order.id'))))) {
                     $ysi = \Yousendit\Base::forge();
                     // Artworks (update, delete)
                     foreach ($artworks as $artwork) {
                         // Remove deleted artwork
                         if ($artwork->file_id) {
                             $ysi->delete_artwork($artwork->file_id);
                         }
                     }
                 }
             }
         }
     }
     // Delete order & cart session
     \Session::delete('order.id');
     \Cart::clear();
     \Messages::info('Your cart is empty.');
     if (\Input::is_ajax()) {
         echo \Messages::display();
         exit;
     } else {
         \Response::redirect(\Input::referrer(\Uri::create('order/checkout/address')));
     }
 }