tribulant

FedEx Class

Mar 20th, 2013
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.80 KB | None | 0 0
  1. <?php
  2.  
  3. class wpcofedex extends wpCheckoutPlugin {
  4.    
  5.     function wpcofedex() {
  6.         return;
  7.     }
  8.    
  9.     function savemethod($cu_prices = null, $cu_shipmethod = null) {
  10.         global $Order, $wpdb, $wpcoDb, $wpcoShipmethod, $user_ID, $wpcoHtml;
  11.    
  12.         if (!empty($cu_shipmethod)) {                          
  13.             if ($order_id = $Order -> current_order_id()) {
  14.                 $wpcoDb -> model = $Order -> model;
  15.                 if ($order = $wpcoDb -> find(array('id' => $order_id))) {
  16.                     $wpcoDb -> model = $wpcoShipmethod -> model;
  17.                     if ($shipmethod = $wpcoDb -> find(array('id' => $order -> shipmethod_id))) {   
  18.                         $wpcoDb -> model = $Order -> model;
  19.                         $wpcoDb -> save_field('cu_shipmethod', $cu_shipmethod, array('id' => $order_id));
  20.                         if ($user_ID) { update_user_meta($user_ID, 'cu_shipmethod', $cu_shipmethod); }                     
  21.                         $shipping = $cu_prices[$cu_shipmethod];
  22.                         $shipmethod_name = $wpcoHtml -> shipmethod_name($shipmethod -> id, $order_id);                     
  23.                         $wpcoDb -> model = $Order -> model;  $wpcoDb -> save_field('api_shipping', $shipping, array('id' => $order_id));   
  24.                         $wpcoDb -> model = $Order -> model;  $wpcoDb -> save_field('shipmethod_name', $shipmethod_name, array('id' => $order_id));                         
  25.                         $cp_saved = true;                          
  26.                         return $shipping;
  27.                     }
  28.                 }
  29.             }
  30.         }
  31.        
  32.         return false;
  33.     }
  34.    
  35.     function shipmethod($shipmethod = null) {
  36.         global $wpdb, $wpcoDb, $wpcoShipmethod, $Order, $Item;
  37.         $order_id = $Order -> current_order_id();
  38.        
  39.         if (!empty($shipmethod)) {
  40.             $wpcoDb -> model = $Order -> model;    
  41.             if ($order = $wpcoDb -> find(array('id' => $order_id))) {
  42.                 $this -> order = $order;
  43.                 $api_options = maybe_unserialize($shipmethod -> api_options);
  44.                 $this -> api_options = $api_options;
  45.                
  46.                 if (empty($api_options['FedExServer']) || (!empty($api_options['FedExServer']) && $api_options['FedExServer'] == "beta")) {
  47.                     $path_to_wsdl = $this -> plugin_base() . DS . 'vendors' . DS . 'shipping' . DS . 'fedex' . DS . 'RateService_v10.wsdl';
  48.                 } else {
  49.                     $path_to_wsdl = $this -> plugin_base() . DS . 'vendors' . DS . 'shipping' . DS . 'fedex' . DS . 'RateService_v10_live.wsdl';
  50.                 }
  51.                    
  52.                 ini_set("soap.wsdl_cache_enabled", "0");
  53.                 $client = new SoapClient($path_to_wsdl, array('trace' => 1));              
  54.                 $request = array();
  55.                 $request['WebAuthenticationDetail'] = array('UserCredential' => array('Key' => $api_options['fedex_key'], 'Password' => $api_options['fedex_password']));
  56.                 $request['ClientDetail'] = array('AccountNumber' => $api_options['fedex_account'], 'MeterNumber' => $api_options['fedex_meter']);
  57.                 $request['TransactionDetail'] = array('CustomerTransactionId' => ' *** Rate Request v10 using PHP ***');
  58.                 $request['Version'] = array('ServiceId' => 'crs', 'Major' => '10', 'Intermediate' => '0', 'Minor' => '0');
  59.                 $request['ReturnTransitAndCommit'] = true;
  60.                 $request['RequestedShipment']['DropoffType'] = $api_options['fedex_dropofftype'];
  61.                 $request['RequestedShipment']['ShipTimestamp'] = date('c');
  62.                 //$request['RequestedShipment']['ServiceType'] = 'INTERNATIONAL_PRIORITY';
  63.                 //$request['RequestedShipment']['PackagingType'] = $api_options['fedex_packagingtype'];
  64.                 $request['RequestedShipment']['TotalInsuredValue'] = array('Ammount' => $Order -> total($order_id), 'Currency' => $this -> get_option('currency'));
  65.                 $request['RequestedShipment']['Shipper'] = $this -> addShipper();
  66.                 $request['RequestedShipment']['Recipient'] = $this -> addRecipient();
  67.                 $request['RequestedShipment']['ShippingChargesPayment'] = $this -> addShippingChargesPayment();
  68.                 $request['RequestedShipment']['RateRequestTypes'] = 'ACCOUNT';
  69.                 $request['RequestedShipment']['RateRequestTypes'] = 'LIST';
  70.                 $request['RequestedShipment']['PackageCount'] = '1';
  71.                 //$request['RequestedShipment']['RequestedPackageLineItems'] = $this -> addPackageLineItem();
  72.                
  73.                 $requestitems = array();
  74.                 $wpcoDb -> model = $Item -> model;
  75.                 $items = $wpcoDb -> find_all(array('order_id' => $order_id));
  76.                
  77.                 if (!empty($items)) {
  78.                     foreach ($items as $item) {
  79.                         $requestitems[] = $this -> addPackageLineItem($item);
  80.                     }
  81.                 }
  82.                
  83.                 $request['RequestedShipment']['RequestedPackageLineItems'] = $requestitems;
  84.                
  85.                 try {
  86.                     if ($this -> setEndpoint('changeEndpoint')) {
  87.                         $newLocation = $client -> __setLocation($this -> setEndpoint('endpoint'));
  88.                     }
  89.                
  90.                     $response = $client -> getRates($request);
  91.                
  92.                     if ($response -> HighestSeverity != 'FAILURE' && $response -> HighestSeverity != 'ERROR') {    
  93.                         $rateReply = $response -> RateReplyDetails;
  94.  
  95.                         if (!empty($api_options['Service'])) {                     
  96.                             foreach ($rateReply as $rate_key => $rate) {
  97.                                 if (!in_array($rate -> ServiceType, $api_options['Service'])) {
  98.                                     unset($rateReply[$rate_key]);
  99.                                 }
  100.                             }
  101.                         }
  102.                        
  103.                         $prices = $rateReply;
  104.                         $this -> render('fedex', array('order' => $order, 'prices' => $prices, 'ajaxquote' => true), true, 'default');
  105.                     } else {
  106.                        $this -> printError($client, $response);
  107.                     }
  108.                 } catch (SoapFault $exception) {
  109.                     $this -> printFault($exception, $client);        
  110.                 }
  111.             }
  112.         }
  113.     }
  114.    
  115.     function printSuccess($client, $response) {
  116.         echo '<h2>Transaction Successful</h2>';  
  117.         echo "\n";
  118.         $this -> printRequestResponse($client);
  119.     }
  120.    
  121.     function printRequestResponse($client){
  122.         return;
  123.    
  124.         echo '<h2>Request</h2>' . "\n";
  125.         echo '<pre>' . htmlspecialchars($client -> __getLastRequest()). '</pre>';  
  126.         echo "\n";
  127.        
  128.         echo '<h2>Response</h2>'. "\n";
  129.         echo '<pre>' . htmlspecialchars($client -> __getLastResponse()). '</pre>';
  130.         echo "\n";
  131.     }
  132.    
  133.     function printNotifications($notes){
  134.         foreach($notes as $noteKey => $note){
  135.             if(is_string($note)){    
  136.                 echo $noteKey . ': ' . $note . '<br/>';;
  137.             }
  138.             else{
  139.                 $this -> printNotifications($note);
  140.             }
  141.         }
  142.         echo '<br/>';
  143.     }
  144.    
  145.     function printError($client, $response){
  146.         echo '<h2>Error returned in processing transaction</h2>';
  147.         echo "\n";
  148.         $this -> printNotifications($response -> Notifications);
  149.         $this -> printRequestResponse($client, $response);
  150.     }
  151.    
  152.     function printFault($exception, $client) {     
  153.         echo '<h3>' . __('An error occurred', $this -> plugin_name) . '</h3>';
  154.         echo '<p>' . $exception -> faultstring . ' (' . $exception -> faultcode . ')</p>';
  155.     }
  156.    
  157.     function setEndpoint($var){
  158.         if($var == 'changeEndpoint') Return false;
  159.         if($var == 'endpoint') Return '';
  160.     }
  161.    
  162.     function addShipper() {
  163.         $shipper = array(
  164.             'Contact'       =>  array(
  165.                 'PersonName'            =>  $this -> order -> ship_fname . ' ' . $this -> order -> bill_lname,
  166.                 'CompanyName'           =>  get_bloginfo('name'),
  167.             ),
  168.             'Address'       =>  array(
  169.                 'StateOrProvinceCode'   => $this -> api_options['orig_StateOrProvinceCode'],
  170.                 'PostalCode'            => $this -> api_options['orig_PostalCode'],
  171.                 'CountryCode'           => $this -> api_options['orig_CountryCode'],
  172.             )
  173.         );
  174.        
  175.         return $shipper;
  176.     }
  177.    
  178.     function addRecipient(){
  179.         $recipient = array(
  180.             'Contact'       => array(
  181.                 'PersonName'            => $this -> order -> ship_fname . ' ' . $this -> order -> ship_lname,
  182.                 'CompanyName'           => $this -> order -> ship_company,
  183.                 'PhoneNumber'           => $this -> order -> ship_phone
  184.             ),
  185.             'Address' => array(
  186.                 'StreetLines' => array($this -> order -> ship_address, $this -> order -> ship_address2),
  187.                 'City' => $this -> order -> ship_city,
  188.                 //'StateOrProvinceCode' => 'BC',
  189.                 'PostalCode' => $this -> order -> ship_zipcode,
  190.                 'CountryCode' => $this -> order -> ship_countrycode,
  191.                 'Residential' => true)
  192.         );
  193.         return $recipient;                                     
  194.     }
  195.    
  196.     function addShippingChargesPayment(){
  197.         $shippingChargesPayment = array(
  198.             'PaymentType' => 'SENDER',
  199.             'Payor' => array(
  200.                 'AccountNumber' => $this -> api_options['fedex_account'],
  201.                 'CountryCode' => $this -> api_options['orig_CountryCode'])
  202.         );
  203.         return $shippingChargesPayment;
  204.     }
  205.    
  206.     function addPackageLineItem($item = null){
  207.         if (!empty($item)) {
  208.             $packageLineItem = array(
  209.                 'SequenceNumber'                =>  1,
  210.                 'GroupPackageCount'             =>  1,
  211.                 'Weight'            =>  array(
  212.                     'Value'             =>  ($item -> product -> weight * $item -> count),
  213.                     'Units'             =>  strtoupper($this -> get_option('weightm')),
  214.                 )
  215.             );
  216.            
  217.             if (!empty($item -> product -> length)) { $packageLineItem['Dimensions']['Length'] = $item -> product -> length; }
  218.             if (!empty($item -> product -> width)) { $packageLineItem['Dimensions']['Width'] = $item -> product -> width; }
  219.             if (!empty($item -> product -> height)) { $packageLineItem['Dimensions']['Height'] = $item -> product -> height; }
  220.             if (!empty($item -> product -> lengthmeasurement)) { $packageLineItem['Dimensions']['Units'] = strtoupper($item -> product -> lengthmeasurement); }
  221.            
  222.             return $packageLineItem;
  223.         }
  224.     }
  225. }
  226.  
  227. ?>
Advertisement
Add Comment
Please, Sign In to add comment