Advertisement
kotvalera83

Zend 1 CheckoutController.php

Oct 22nd, 2014
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 23.73 KB | None | 0 0
  1. <?php
  2. require_once(APPLICATION_PATH . '/modules/default/models/Core.php');
  3. require_once(APPLICATION_PATH . '/modules/default/models/DbTable/Users.php');
  4. require_once(APPLICATION_PATH . '/modules/default/models/DbTable/Products.php');
  5. require_once(APPLICATION_PATH . '/modules/default/models/DbTable/Lcl.php');
  6. require_once(APPLICATION_PATH . '/modules/default/models/DbTable/Orders.php');
  7. require_once(APPLICATION_PATH . '/modules/default/models/DbTable/OrderItems.php');
  8. require_once(APPLICATION_PATH . '/modules/default/models/DbTable/ContainerSale.php');
  9. require_once(APPLICATION_PATH . '/modules/default/models/DbTable/Container.php');
  10. require_once(APPLICATION_PATH . '/modules/default/models/DbTable/OrderContainer.php');
  11. require_once(APPLICATION_PATH . '/modules/default/models/DbTable/Container.php');
  12. require_once(APPLICATION_PATH . '/modules/default/models/DbTable/Option.php');
  13. require_once(APPLICATION_PATH . '/modules/default/models/DbTable/Harmonized_Code.php');
  14. require_once(APPLICATION_PATH . '/../library/My/simple_html_dom.php');
  15.  
  16.  
  17. class CheckoutController extends Zend_Controller_Action
  18. {
  19.     public $month;
  20.     private $mess;
  21.    
  22.     public function init() {
  23.         if ($this->getRequest()->isXmlHttpRequest()) {
  24.             Zend_Controller_Action_HelperBroker::removeHelper('viewRenderer');
  25.             $this->_helper->layout->disableLayout();
  26.         }
  27.        
  28.         if(!$_SESSION['order'])
  29.             $this->_helper->redirector('index', '');
  30.        
  31.  
  32.         $this->month = array('01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12');
  33.     }
  34.    
  35.     public function indexAction () {
  36.         $this->view->order = $_SESSION['order'];
  37.         $this->view->month = $this->month;
  38.        
  39.         $users = new Model_DbTable_Users();
  40.        
  41.         if(!empty($_SESSION['Logins']['user_id']))
  42.             $this->view->user = $users->fetchRow('id=' . $users->getAdapter()->quote($_SESSION['Logins']['user_id']));
  43.            
  44.         if(!empty($this->mess))
  45.             $this->view->mess = $this->mess;
  46.     }
  47.    
  48.     public function processAction() {
  49.        
  50.         $req = $this->getRequest();
  51.         if ($req->isPost()) {
  52.                     $fee = $this->_getParam('fee', 0);
  53.                     if($fee != 0){
  54.                         $additional_charges =  str_replace('_', ' ', implode(", ", $fee));
  55.                     }else{
  56.                         $additional_charges = NULL;
  57.                     }
  58.             $data = array(
  59.                           'firstname' => $this->_getParam('firstname', 0),
  60.                           'lastname' => $this->_getParam('lastname', 0),
  61.                           'gender' => $this->_getParam('gender', 0),
  62.                           'company' => $this->_getParam('company', 0),
  63.                           'address_1' => $this->_getParam('address_1', 0),
  64.                           'address_2' => $this->_getParam('address_2', 0),
  65.                           'country' => $this->_getParam('country', 0),
  66.                           'city' => $this->_getParam('city', 0),
  67.                           'state' => $this->_getParam('state', 0),
  68.                           'postal_code' => $this->_getParam('postal_code', 0),
  69.                           'email' => $this->_getParam('email', 0),
  70.                           'phone' => $this->_getParam('phone', 0),
  71.                           );
  72.  
  73.  
  74.            
  75.             if(empty($_SESSION['Logins']['user_id'])) {
  76.                 $data_register = $data;
  77.                 $data_register['username'] = $this->_getParam('username', 0);
  78.                 $data_register['password'] = $this->_getParam('password', 0);
  79.                 $data_register['confirm'] = $this->_getParam('confirm', 0);
  80.                 $data_register['role_id'] = $this->_getParam('role_id', 0);
  81.  
  82.                 $users = new Model_DbTable_Users();
  83.                 $user_id = $users->register($data_register);
  84.                 $_SESSION['Logins']['user_id'] = $user_id;
  85.             }
  86.             else
  87.                 $user_id = $_SESSION['Logins']['user_id'];
  88.                
  89.             $billing = $this->_getParam('billing', 0);
  90.             if($billing == 1) {
  91.                 $data = array(
  92.                               'user_id' => $user_id,
  93.                               'firstname' => $this->_getParam('sh_firstname', 0),
  94.                               'lastname' => $this->_getParam('sh_lastname', 0),
  95.                               'gender' => $this->_getParam('sh_gender', 0),
  96.                               'company' => $this->_getParam('sh_company', 0),
  97.                               'address_1' => $this->_getParam('sh_address_1', 0),
  98.                               'address_2' => $this->_getParam('sh_address_2', 0),
  99.                               'country' => $this->_getParam('sh_country', 0),
  100.                               'city' => $this->_getParam('sh_city', 0),
  101.                               'state' => $this->_getParam('sh_state', 0),
  102.                               'postal_code' => $this->_getParam('sh_postal_code', 0),
  103.                               'email' => $this->_getParam('sh_email', 0),
  104.                               'phone' => $this->_getParam('sh_phone', 0),
  105.                               'sum' => $this->_getParam('txt_total', 0),
  106.                               'card' => $this->_getParam('card_number', 0),
  107.                                                           'additional_charges' => $additional_charges,
  108.                               );
  109.             }
  110.             else {
  111.                 $data['user_id'] = $user_id;
  112.                 $data['sum'] = $this->_getParam('txt_total', 0);
  113.                 $data['card'] = $this->_getParam('card_number', 0);
  114.             }
  115.             $ship = $this->getshippingprice($data['postal_code'], $fee);
  116.             $data['ship'] = $ship['shipp'];
  117.  
  118.             $a = new My_Authorizenet;
  119.             $a->add_field('x_login', '73MxA6ue');
  120.             $a->add_field('x_tran_key', '48FuHz62EfKx772w');
  121.             //$a->add_field('x_password', 'CHANGE THIS TO YOUR PASSWORD');
  122.             $a->add_field('x_version', '3.1');
  123.             $a->add_field('x_type', 'AUTH_CAPTURE');
  124.             $a->add_field('x_test_request', 'TRUE');    // Just a test transaction
  125.             $a->add_field('x_relay_response', 'FALSE');
  126.             $a->add_field('x_delim_data', 'TRUE');
  127.             $a->add_field('x_delim_char', '|');    
  128.             $a->add_field('x_encap_char', '');
  129.        
  130.             $a->add_field('x_first_name', $data['firstname']);
  131.             $a->add_field('x_last_name', $data['lastname']);
  132.             $a->add_field('x_address', $data['address_1']);
  133.             $a->add_field('x_city', $data['city']);
  134.             $a->add_field('x_state', $data['state']);
  135.             $a->add_field('x_zip', $data['postal_code']);
  136.             $a->add_field('x_country', $data['country']);
  137.             $a->add_field('x_email', $data['email']);
  138.             $a->add_field('x_phone', $data['phone']);
  139.            
  140.             $a->add_field('x_method', 'CC');
  141.             $a->add_field('x_card_num', $this->_getParam('card_number', 0));   // test successful visa 4007000000027
  142.             $a->add_field('x_amount', $this->_getParam('txt_total', 0));
  143.             $a->add_field('x_exp_date', $this->_getParam('expiration_month', 0).$this->_getParam('expiration_year', 0));    // march of 2008
  144.             $a->add_field('x_card_code', $this->_getParam('code', 0));    // Card CAVV Security code           
  145.            
  146.             $pay = $a->process();
  147.             if($pay == 1) {
  148.                 $orders = new Model_DbTable_Orders();
  149.                 $order_id = $orders->add($data);
  150.        
  151.                 $products = new Model_DbTable_Products();
  152.                 $orderitem = new Model_DbTable_OrderItems();
  153.                 $order_container = new Model_DbTable_OrderContainer();
  154.                                 $container_sale = new Model_DbTable_ContainerSale();
  155.                                 $container = new Model_DbTable_Container();
  156.                                 $active_container = $container->getContainer();
  157.                                 $container_id = $active_container->id;
  158.                                 $lcl = new Model_DbTable_Lcl();
  159.                 foreach($_SESSION['order'] as $item) {
  160.                     $product = $products->getProduct($item);
  161.                                         $product_id = $product->product_id;
  162.                                         $p_price = $product->price;
  163.                                         $lcl_id = $product->lcl_id;
  164.                     $data = array(
  165.                                   'order_id' => $order_id,
  166.                                   'product_id' => $product_id,
  167.                                   'price' => $p_price,
  168.                                   );
  169.                     $orderitem->add($data);
  170.                     $products->sale($product_id);
  171.                                         $square = $product->height_ft *  $product->width_ft * $product->depth_ft;
  172.                                         $order_containers = $order_container->getOrderItems($lcl_id);
  173.                                         if(!empty($order_containers)){
  174.                                             $i = 0;
  175.                                             foreach ($order_containers as $value) {
  176.                                                 $i++;
  177.                                                 $square_all = $square + $value['filled'];
  178.                                                 if($square_all < $active_container->square){
  179.                                                     $full = 2;
  180.                                                     $next = 0;
  181.                                                 }elseif($square_all == $active_container->square){
  182.                                                     $full = 1;
  183.                                                     $next = 0;
  184.                                                 }else{
  185.                                                     $next = 1;
  186.                                                 }
  187.                                                 if($next == 0){
  188.                                                     $order_container->update(array(
  189.                                                         'filled' => $square_all,
  190.                                                         'full' => $full,
  191.                                                     ), $value['id']);
  192.                                                     $order_container_id = $value['id'];
  193.                                                 }  else {
  194.                                                     $container_code = $lcl->getLcl($lcl_id)->port_code . '-' . rand(1, 999999999);
  195.                                                     if($square < $active_container->square){
  196.                                                         $full = 2;
  197.                                                     }else{
  198.                                                         $full = 1;
  199.                                                     }
  200.                                                     $order_container->add(array(
  201.                                                         'lcl_id' => $lcl_id,
  202.                                                         'container_code' => $container_code,
  203.                                                         'container_id' => $active_container->id,
  204.                                                         'filled' => $square,
  205.                                                         'full' => $full,
  206.                                                     ));
  207.                                                     $order_container_id = $order_container->getAdapter()->lastInsertId();
  208.                                                 }
  209.                                             }
  210.                                             if($i == 0){
  211.                                             $container_code = $lcl->getLcl($lcl_id)->port_code . '-' . rand(1, 999999999);
  212.                                             if($square < $active_container->square){
  213.                                                 $full = 2;
  214.                                             }else{
  215.                                                 $full = 1;
  216.                                             }
  217.                                             $order_container->add(array(
  218.                                                 'lcl_id' => $lcl_id,
  219.                                                 'container_code' => $container_code,
  220.                                                 'container_id' => $active_container->id,
  221.                                                 'filled' => $square,
  222.                                                 'full' => $full,
  223.                                             ));
  224.                                             $order_container_id = $order_container->getAdapter()->lastInsertId();
  225.                                         }
  226.                                         if(!empty($order_container_id)){
  227.                                             $container_sale->add(array(
  228.                                                 'order_container_id' => $order_container_id,
  229.                                                 'order_id' => $order_id,
  230.                                                 'products_id' => $product->product_id,
  231.                                                 'user_id' => $_SESSION['Logins']['user_id'],
  232.                                                 'additional_charges' => $additional_charges,
  233.                                             ));
  234.                                         }
  235.                                         }
  236.                                        
  237.                 }
  238.                
  239.                 $_SESSION['order'] = '';
  240.                 $this->_helper->redirector->gotoUrlAndExit('confirmation?id='.$order_id);
  241.             }
  242.            
  243.            
  244.             $this->mess = '<div id="featuredHeader"><h2>Error with Transaction</h2></div>';
  245.             $this->mess .= '<br /><div>'.$a->get_response_reason_text().'<br /><br /><br /></div>';
  246.            
  247.  
  248.             $this->indexAction();
  249.             $this->_helper->viewRenderer->setNoController(true);
  250.             $this->_helper->viewRenderer('checkout/index');
  251.            
  252. /*          switch ($a->process()) {
  253.            
  254.                case 1:  // Successs
  255.                   echo "<b>Success:</b><br>";
  256.                   echo $a->get_response_reason_text();
  257.                   echo "<br><br>Details of the transaction are shown below...<br><br>";
  258.                   break;
  259.                  
  260.                case 2:  // Declined
  261.                   echo "<b>Payment Declined:</b><br>";
  262.                   echo $a->get_response_reason_text();
  263.                   echo "<br><br>Details of the transaction are shown below...<br><br>";
  264.                   break;
  265.                  
  266.                case 3:  // Error
  267.                   echo "<b>Error with Transaction:</b><br>";
  268.                   echo $a->get_response_reason_text();
  269.                   echo "<br><br>Details of the transaction are shown below...<br><br>";
  270.                   break;
  271.             }
  272. */         
  273.             // The following two functions are for debugging and learning the behavior
  274.             // of authorize.net's response codes.  They output nice tables containing
  275.             // the data passed to and recieved from the gateway.
  276.            
  277.         }
  278.     }
  279.    
  280.     public function getshippingprice($zipcode, $fees) {
  281.         $products = new Model_DbTable_Products();
  282.         $users = new Model_DbTable_Users();
  283.                 $lcl = new Model_DbTable_Lcl();
  284.                 $option = new Model_DbTable_Option();
  285.                 $container = new Model_DbTable_Container();
  286.                 $harmonized_code = new Model_DbTable_Harmonized_Code();
  287.                 $active_comtainer = $container->getContainer();
  288.                 if($active_comtainer->length == 20){
  289.                     $a_comtainer = 'rates20';
  290.                 }else{
  291.                     $a_comtainer = 'rates40';
  292.                 }
  293. //      $shippingvals = $this->getOption('shippingvals');
  294.         $price = 0;
  295.         $shipp_price = 0;
  296.                 $lcl_price = 0;
  297.                 $misc_charge = 0;
  298.                 $add_charge = 0;
  299.                 $customs_charge = 0;
  300.                 $lcl_arr = array();
  301.         if(!empty($zipcode)) {
  302.             foreach($_SESSION['order'] as $item) {
  303.                 $product = $products->getProduct($item);
  304.                 $from = $users->fetchRow('id=' . $users->getAdapter()->quote($product->user_id_fk));
  305.                                 if(is_numeric($product->height_metric) && $product->height_metric > 0){
  306.                                     $height = $product->height_metric;
  307.                                 }else{
  308.                                     $height = 0;
  309.                                 }
  310.                                 if(is_numeric($product->width_metric) && $product->width_metric > 0){
  311.                                     $width = $product->width_metric;
  312.                                 }else{
  313.                                     $width = 0;
  314.                                 }
  315.                                 if(is_numeric($product->depth_metric) && $product->depth_metric > 0){
  316.                                     $depth = $product->depth_metric;
  317.                                 }else{
  318.                                     $depth = 0;
  319.                                 }
  320.                                 if(!in_array($product->lcl_id, $lcl_arr)){
  321.                                     array_push($lcl_arr, $product->lcl_id);
  322.                                     $add_charge += $option->fetchRow("name = 'driver waiting fee'")->value;
  323.                                     if($fees != 0){
  324.                                         foreach ($fees as $fee){
  325.                                             switch ($fee) {
  326.                                                 case 'residential_delivery':
  327.                                                     $add_charge += $option->fetchRow("name = 'residential delivery'")->value;
  328.                                                     break;
  329.                                                 case 'inside_delivery':
  330.                                                     $inside_delivery = $option->fetchRow("name = 'inside delivery fee'")->value;
  331.                                                     $inside_delivery_min = $option->fetchRow("name = 'inside delivery fee min'")->value;
  332.                                                     $inside_delivery_max = $option->fetchRow("name = 'inside delivery fee max'")->value;
  333.                                                     $inside = $product['weight'] / 100 * $inside_delivery;
  334.                                                     if($inside < $inside_delivery_min){
  335.                                                         $add_charge += $inside_delivery_min;
  336.                                                     }elseif ($inside > $inside_delivery_max) {
  337.                                                         $add_charge += $inside_delivery_max;
  338.                                                     }else{
  339.                                                         $add_charge += $inside;
  340.                                                     }
  341.                                                     break;
  342.                                                 case 'lift_gate':
  343.                                                     $add_charge += $option->fetchRow("name = 'lift gate surcharge'")->value;
  344.                                                     break;
  345.                                                 case 'delivery_notification':
  346.                                                     $add_charge += $option->fetchRow("name = 'delivery notification pricing'")->value;
  347.                                                     break;
  348.                                             }
  349.                                         }
  350.                                     }
  351.                                    
  352.                                 }
  353.                                 $square = $height * $width * $depth;
  354.                                 $rate = new My_UPS;
  355.                                 if($product['weight'] > 150){
  356.                                     $shipp_price += $this->upsrate($option->fetchRow("name = 'US-ZIP'")->value, $zipcode, $product['weight'], $square);
  357.                                 }else{
  358.                                     $rate->upsProduct("GND");   // See upsProduct() function for codes
  359.                                     $rate->origin($option->fetchRow("name = 'US-ZIP'")->value, "US"); // Use ISO country codes!
  360.                                     $rate->dest($zipcode, "US");   // Use ISO country codes!
  361.                                     $rate->rate("RDP");     // See the rate() function for codes
  362.                                     $rate->container("CP"); // See the container() function for codes
  363.                                     $rate->weight($product['weight']);
  364.                                     $rate->rescom("RES");   // See the rescom() function for codes
  365.                                     $shipp_price += $rate->getQuote();                                    
  366.                                 }
  367.                                 $lcl_cbn = $lcl->getLcl($product->lcl_id);
  368.                                 $cbn =  $lcl_cbn->$a_comtainer / $active_comtainer->square_m;
  369.                 $lcl_price = ($square * $cbn) + $lcl_price;
  370.                                 $harmonized = $harmonized_code->getCode($product);
  371.                                 $customs_charge += round($rate->price($product->price, $harmonized, 1), 2);
  372.                                 $price += $product->price;
  373.                                 $misc_charge += $option->fetchRow("name = 'Picking-fee'")->value;
  374.                                 if($product->weight > 70){
  375.                                     $misc_charge += $option->fetchRow("name = 'Unloading-fee'")->value;
  376.                                 }
  377.             }
  378.             $ofr_charge = round($lcl_price, 2);
  379.             $domestic_charge = $shipp_price;
  380.             $shipp_price_all = $ofr_charge + $add_charge + $misc_charge + $customs_charge + $domestic_charge;
  381.             return array(
  382.                         'ofr_charge' => $ofr_charge,
  383.                         'add_charge' => $add_charge,
  384.                         'misc_charge' => $misc_charge,
  385.                         'customs_charge' => $customs_charge,
  386.                         'domestic_charge' => $domestic_charge,
  387.                         'shipp' => $shipp_price_all,
  388.                         'price' => $price,
  389.                         'total' => ($shipp_price_all + $price),
  390.                         );
  391.         }
  392.         else
  393.             return 0;
  394.     }
  395.    
  396.     public function loadshippingpriceAction() {
  397.         $zipcode = $this->_getParam('zipcode', 0);
  398.                 $fee = $this->_getParam('fee', 0);
  399.                 $fees = explode(',', $fee);
  400.         $result = $this->getshippingprice($zipcode, $fees);
  401.         if(!empty($result))
  402.             echo json_encode($result);
  403.     }
  404.    
  405.     function upsrate($shipFromZip, $shipToZip, $weight, $freight, $shipmentDate = NULL ) {
  406.        
  407.         if(is_null($shipmentDate)){
  408.             $shipmentDate = date("mdY");
  409.         }
  410.         $fr = round(($freight / 160), 2);
  411.         if($fr < 50){
  412.             $frclass = 50;
  413.         }elseif($fr >= 50 && $fr > 55){
  414.         $frclass = 55;
  415.         }elseif($fr >= 55 && $fr > 60){
  416.         $frclass = 60;
  417.         }elseif($fr >= 60 && $fr > 65){
  418.         $frclass = 65;
  419.         }elseif($fr >= 60 && $fr > 70){
  420.         $frclass = 70;
  421.         }elseif($fr >= 70 && $fr > 77.5){
  422.         $frclass = 77.5;
  423.         }elseif($fr >= 77.5 && $fr > 85){
  424.         $frclass = 85;
  425.         }elseif($fr >= 77.5 && $fr > 85){
  426.         $frclass = 85;
  427.         }elseif($fr >= 85 && $fr > 92.5){
  428.         $frclass = 92.5;
  429.         }elseif($fr >= 92.5 && $fr > 100){
  430.         $frclass = 100;
  431.         }elseif($fr >= 100 && $fr > 110){
  432.         $frclass = 110;
  433.         }elseif($fr >= 110 && $fr > 125){
  434.         $frclass = 125;
  435.         }elseif($fr >= 125 && $fr > 150){
  436.         $frclass = 150;
  437.         }elseif($fr >= 150 && $fr > 175){
  438.         $frclass = 175;
  439.         }elseif($fr >= 150 && $fr > 175){
  440.         $frclass = 175;
  441.         }elseif($fr >= 175 && $fr > 200){
  442.         $frclass = 200;
  443.         }elseif($fr >= 200 && $fr > 250){
  444.         $frclass = 250;
  445.         }elseif($fr >= 250 && $fr > 300){
  446.         $frclass = 300;
  447.         }elseif($fr >= 300 && $fr > 400){
  448.         $frclass = 400;
  449.         }elseif($fr >= 400 && $fr > 500){
  450.         $frclass = 500;
  451.         }else{
  452.         $frclass = 50;
  453.         }
  454.         $url = "https://wwwapps.ups.com/fctc/processLtlEntry?contractUser=false&loc=en_US&original_loc=en_US&shipFromCountry=US&shipFromZip=$shipFromZip&shipToCountry=US&shipToZip=$shipToZip&shipmentDate=$shipmentDate&domesticDiscount=&freightClass1=$frclass&weight1=$weight&submit_button_next=Next";
  455.         $data =  file_get_html($url);
  456.         $res = $data->find('table.dataTable tr.odd td', 1)->plaintext;
  457.         $re =  explode('USD', $res);
  458.         $r =  str_replace('                                         ', '', $re[0]);
  459.         $return =  str_replace(' ', '', $r);
  460.         return $return;
  461.  
  462.     }
  463.     /**
  464.    * sends contact email
  465.    *
  466.    * @return
  467.    * @access public
  468.    */
  469.     public function addAction() {
  470.         $id = $this->_getParam('id', 0);
  471.         if(!empty($id)) {
  472.             $data = $_SESSION['order'];
  473.             $data[$id] = $id;
  474.             $_SESSION['order'] = $data;
  475.             $this->_helper->redirector('cart', '');
  476.         }
  477.         else
  478.             $this->_helper->redirector('custom', '');
  479.     }
  480.    
  481.     public function deleteAction() {
  482.         $id = $this->_getParam('id', 0);
  483.         $data = $_SESSION['order'];
  484.         unset($data[$id]);
  485.         $_SESSION['order'] = $data;
  486.     }
  487.    
  488.     public function ajaxValidateFieldUserAction() {
  489.         $users = new Model_DbTable_Users();
  490.         $this->_helper->json(array(
  491.             $this->_getParam('fieldId'),
  492.             !$users->hasColumnValue($this->_getParam('fieldId'), $this->_getParam('fieldValue')),
  493.         ));
  494.     }  
  495. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement