/**
  * sample_AddItem::dispatchCall()
  * 
  * Dispatch the call
  *
  * @param array $params array of parameters for the eBay API call
  * 
  * @return boolean success
  */
 public function dispatchCall($params)
 {
     $a = new ShippingServiceOptionsType();
     $a->setShippingService('UPS2ndDay');
     $a->setShippingServicePriority('1');
     $a->setShippingServiceCost('5');
     $b = new ShippingDetailsType();
     //print '<pre>'.print_r(get_class_methods($b),true).'</pre><br>';
     $b->setShippingServiceOptions($a);
     //$b->setDefaultShippingCost(5);
     $b->setShippingType('Flat');
     $req = new AddItemRequestType();
     $item = new ItemType();
     $item->setTitle($params['Title']);
     $item->setQuantity($params['Quantity']);
     $item->setCurrency($params['Currency']);
     $item->setCountry($params['Country']);
     $item->setStartPrice($params['StartPrice']);
     $item->setListingDuration($params['ListingDuration']);
     $item->setLocation($params['Location']);
     $item->setPaymentMethods($params['PaymentMethods']);
     $item->setListingType($params['ListingType']);
     $item->setDescription($params['Description']);
     $item->ShippingService = 'UPS Ground';
     $item->setShippingDetails($b);
     //print '<pre>'.print_r(get_class_methods($a),true).'</pre><br>';
     //$this->dumpObject($a);
     //$this->dumpObject($item);
     $primaryCategory = new CategoryType();
     $primaryCategory->setCategoryID($params['CategoryID']);
     $item->setPrimaryCategory($primaryCategory);
     $req->setItem($item);
     $res = $this->proxy->AddItem($req);
     $this->dumpObject($res);
     if ($this->testValid($res)) {
         $this->dumpObject($res);
         return true;
     } else {
         return false;
     }
 }
 public function buildShipping($id, $item, $post_id, $profile_details, $listing, $isVariation)
 {
     // no shipping options for classified ads
     if ($item->ListingType == 'LeadGeneration') {
         return $item;
     }
     // handle flat and calc shipping
     WPLE()->logger->info('shipping_service_type: ' . $profile_details['shipping_service_type']);
     // $isFlat = $profile_details['shipping_service_type'] != 'calc' ? true : false;
     // $isCalc = $profile_details['shipping_service_type'] == 'calc' ? true : false;
     // if this is a single split variation, use variation post_id instead of parent post_id for weight and dimensions
     $actual_post_id = $isVariation ? $listing['post_id'] : $post_id;
     // handle flat and calc shipping (new version)
     $service_type = $profile_details['shipping_service_type'];
     if ($service_type == '') {
         $service_type = 'Flat';
     }
     if ($service_type == 'flat') {
         $service_type = 'Flat';
     }
     if ($service_type == 'calc') {
         $service_type = 'Calculated';
     }
     $isFlatLoc = in_array($service_type, array('Flat', 'FreightFlat', 'FlatDomesticCalculatedInternational')) ? true : false;
     $isFlatInt = in_array($service_type, array('Flat', 'FreightFlat', 'CalculatedDomesticFlatInternational')) ? true : false;
     $hasWeight = in_array($service_type, array('Calculated', 'FreightFlat', 'FlatDomesticCalculatedInternational', 'CalculatedDomesticFlatInternational')) ? true : false;
     $isCalcLoc = !$isFlatLoc;
     $isCalcInt = !$isFlatInt;
     $shippingDetails = new ShippingDetailsType();
     $shippingDetails->ShippingType = $service_type;
     WPLE()->logger->info('shippingDetails->ShippingType: ' . $shippingDetails->ShippingType);
     // local shipping options
     $localShippingOptions = $profile_details['loc_shipping_options'];
     WPLE()->logger->debug('localShippingOptions: ' . print_r($localShippingOptions, 1));
     $pr = 1;
     $localShippingServices = array();
     foreach ($localShippingOptions as $opt) {
         $price = $this->getDynamicShipping($opt['price'], $post_id);
         $add_price = $this->getDynamicShipping($opt['add_price'], $post_id);
         if ($price == '') {
             $price = 0;
         }
         if ($opt['service_name'] == '') {
             continue;
         }
         $ShippingServiceOptions = new ShippingServiceOptionsType();
         $ShippingServiceOptions->setShippingService($opt['service_name']);
         $ShippingServiceOptions->setShippingServicePriority($pr);
         // set shipping costs for flat services
         if ($isFlatLoc) {
             $ShippingServiceOptions->setShippingServiceCost($price);
             // FreeShipping is only allowed for the first shipping service
             if ($price == 0 && $pr == 1) {
                 $ShippingServiceOptions->setFreeShipping(true);
             }
             // price for additonal items
             if (trim($add_price) == '') {
                 $ShippingServiceOptions->setShippingServiceAdditionalCost($price);
             } else {
                 $ShippingServiceOptions->setShippingServiceAdditionalCost($add_price);
             }
         } else {
             // enable FreeShipping option for calculated shipping services if specified in profile (or product meta)
             $free_shipping_enabled = isset($profile_details['shipping_loc_enable_free_shipping']) ? $profile_details['shipping_loc_enable_free_shipping'] : false;
             // $free_shipping_enabled = $free_shipping_enabled || get_post_meta( $post_id, '_ebay_shipping_loc_enable_free_shipping', true );
             if ($free_shipping_enabled && $pr == 1) {
                 $ShippingServiceOptions->setFreeShipping(true);
             }
         }
         $localShippingServices[] = $ShippingServiceOptions;
         $pr++;
         $EbayShippingModel = new EbayShippingModel();
         $lastShippingCategory = $EbayShippingModel->getShippingCategoryByServiceName($opt['service_name']);
         WPLE()->logger->debug('ShippingCategory: ' . print_r($lastShippingCategory, 1));
     }
     // apply filter and set shipping services
     $localShippingServices = apply_filters('wple_local_shipping_services', $localShippingServices, $post_id, $actual_post_id, $listing);
     $shippingDetails->setShippingServiceOptions($localShippingServices, null);
     // $intlShipping = array(
     // 	'UK_RoyalMailAirmailInternational' => array (
     // 		'Europe' => 1,
     // 		'Worldwide' => 1.50
     // 	),
     // 	'UK_RoyalMailInternationalSignedFor' => array (
     // 		'Europe' => 5,
     // 	)
     // );
     $intlShipping = $profile_details['int_shipping_options'];
     WPLE()->logger->debug('intlShipping: ' . print_r($intlShipping, 1));
     $pr = 1;
     $shippingInternational = array();
     foreach ($intlShipping as $opt) {
         // foreach ($opt as $loc=>$price) {
         $price = $this->getDynamicShipping($opt['price'], $post_id);
         $add_price = $this->getDynamicShipping($opt['add_price'], $post_id);
         // if ( ( $price == '' ) || ( $opt['service_name'] == '' ) ) continue;
         if ($price == '') {
             $price = 0;
         }
         if ($opt['location'] == '') {
             continue;
         }
         if ($opt['service_name'] == '') {
             continue;
         }
         $InternationalShippingServiceOptions = new InternationalShippingServiceOptionsType();
         $InternationalShippingServiceOptions->setShippingService($opt['service_name']);
         $InternationalShippingServiceOptions->setShippingServicePriority($pr);
         // $InternationalShippingServiceOptions->setShipToLocation( $opt['location'] );
         if (is_array($opt['location'])) {
             foreach ($opt['location'] as $location) {
                 $InternationalShippingServiceOptions->addShipToLocation($location);
             }
         } else {
             $InternationalShippingServiceOptions->setShipToLocation($opt['location']);
         }
         $InternationalShippingServiceOptions->setShipToLocation($opt['location']);
         // set shipping costs for flat services
         if ($isFlatInt) {
             $InternationalShippingServiceOptions->setShippingServiceCost($price);
             if (trim($add_price) == '') {
                 $InternationalShippingServiceOptions->setShippingServiceAdditionalCost($price);
             } else {
                 $InternationalShippingServiceOptions->setShippingServiceAdditionalCost($add_price);
             }
         }
         $shippingInternational[] = $InternationalShippingServiceOptions;
         $pr++;
         // }
     }
     // filter international shipping services
     $shippingInternational = apply_filters('wple_international_shipping_services', $shippingInternational, $post_id, $actual_post_id, $listing);
     // only set international shipping if $intlShipping array contains one or more valid items
     if (isset($intlShipping[0]['service_name']) && $intlShipping[0]['service_name'] != '') {
         $shippingDetails->setInternationalShippingServiceOption($shippingInternational, null);
     }
     // set CalculatedShippingRate
     if ($isCalcLoc || $isCalcInt) {
         $calculatedShippingRate = new CalculatedShippingRateType();
         $calculatedShippingRate->setOriginatingPostalCode($profile_details['postcode']);
         // set ShippingPackage if calculated shipping is used
         if ($isCalcInt) {
             $calculatedShippingRate->setShippingPackage($profile_details['shipping_package']);
         }
         if ($isCalcLoc) {
             $calculatedShippingRate->setShippingPackage($profile_details['shipping_package']);
         }
         if ($isCalcLoc) {
             $calculatedShippingRate->setPackagingHandlingCosts(self::dbSafeFloatval(@$profile_details['PackagingHandlingCosts']));
         }
         if ($isCalcInt) {
             // $calculatedShippingRate->setPackagingHandlingCosts( self::dbSafeFloatval( @$profile_details['PackagingHandlingCosts'] ) );
             $calculatedShippingRate->setInternationalPackagingHandlingCosts(self::dbSafeFloatval(@$profile_details['InternationalPackagingHandlingCosts']));
         }
         list($weight_major, $weight_minor) = ProductWrapper::getEbayWeight($actual_post_id);
         $calculatedShippingRate->setWeightMajor(self::dbSafeFloatval($weight_major));
         $calculatedShippingRate->setWeightMinor(self::dbSafeFloatval($weight_minor));
         $dimensions = ProductWrapper::getDimensions($actual_post_id);
         if (trim(@$dimensions['width']) != '') {
             $calculatedShippingRate->setPackageWidth($dimensions['width']);
         }
         if (trim(@$dimensions['length']) != '') {
             $calculatedShippingRate->setPackageLength($dimensions['length']);
         }
         if (trim(@$dimensions['height']) != '') {
             $calculatedShippingRate->setPackageDepth($dimensions['height']);
         }
         // debug
         // $weight = ProductWrapper::getWeight( $actual_post_id ) ;
         // WPLE()->logger->info('weight: '.print_r($weight,1));
         // WPLE()->logger->info('dimensions: '.print_r($dimensions,1));
         $shippingDetails->setCalculatedShippingRate($calculatedShippingRate);
     }
     // handle option to always send weight and dimensions
     if (get_option('wplister_send_weight_and_size', 'default') == 'always') {
         $hasWeight = ProductWrapper::getWeight($actual_post_id);
     }
     // set ShippingPackageDetails
     if ($hasWeight) {
         $shippingPackageDetails = new ShipPackageDetailsType();
         // set ShippingPackage if calculated shipping is used
         if ($isCalcInt) {
             $shippingPackageDetails->setShippingPackage($profile_details['shipping_package']);
         }
         if ($isCalcLoc) {
             $shippingPackageDetails->setShippingPackage($profile_details['shipping_package']);
         }
         list($weight_major, $weight_minor) = ProductWrapper::getEbayWeight($actual_post_id);
         $shippingPackageDetails->setWeightMajor(self::dbSafeFloatval($weight_major));
         $shippingPackageDetails->setWeightMinor(self::dbSafeFloatval($weight_minor));
         $dimensions = ProductWrapper::getDimensions($actual_post_id);
         if (trim(@$dimensions['width']) != '') {
             $shippingPackageDetails->setPackageWidth($dimensions['width']);
         }
         if (trim(@$dimensions['length']) != '') {
             $shippingPackageDetails->setPackageLength($dimensions['length']);
         }
         if (trim(@$dimensions['height']) != '') {
             $shippingPackageDetails->setPackageDepth($dimensions['height']);
         }
         // debug
         // $weight = ProductWrapper::getWeight( $actual_post_id ) ;
         // WPLE()->logger->info('weight: '.print_r($weight,1));
         // WPLE()->logger->info('dimensions: '.print_r($dimensions,1));
         $item->setShippingPackageDetails($shippingPackageDetails);
     }
     // set local shipping discount profile
     if ($isFlatLoc) {
         $local_profile_id = isset($profile_details['shipping_loc_flat_profile']) ? $profile_details['shipping_loc_flat_profile'] : false;
         if ($custom_profile_id = get_post_meta($post_id, '_ebay_shipping_loc_flat_profile', true)) {
             $local_profile_id = $custom_profile_id;
         }
     } else {
         $local_profile_id = isset($profile_details['shipping_loc_calc_profile']) ? $profile_details['shipping_loc_calc_profile'] : false;
         if ($custom_profile_id = get_post_meta($post_id, '_ebay_shipping_loc_calc_profile', true)) {
             $local_profile_id = $custom_profile_id;
         }
     }
     if ($local_profile_id) {
         $shippingDetails->setShippingDiscountProfileID($local_profile_id);
     }
     // set international shipping discount profile
     if ($isFlatLoc) {
         $int_profile_id = isset($profile_details['shipping_int_flat_profile']) ? $profile_details['shipping_int_flat_profile'] : false;
         if ($custom_profile_id = get_post_meta($post_id, '_ebay_shipping_int_flat_profile', true)) {
             $int_profile_id = $custom_profile_id;
         }
     } else {
         $int_profile_id = isset($profile_details['shipping_int_calc_profile']) ? $profile_details['shipping_int_calc_profile'] : false;
         if ($custom_profile_id = get_post_meta($post_id, '_ebay_shipping_int_calc_profile', true)) {
             $int_profile_id = $custom_profile_id;
         }
     }
     if ($int_profile_id) {
         $shippingDetails->setInternationalShippingDiscountProfileID($int_profile_id);
     }
     // PromotionalShippingDiscount
     $PromotionalShippingDiscount = isset($profile_details['PromotionalShippingDiscount']) ? $profile_details['PromotionalShippingDiscount'] : false;
     if ($PromotionalShippingDiscount == '1') {
         $shippingDetails->setPromotionalShippingDiscount(true);
     }
     // InternationalPromotionalShippingDiscount
     $InternationalPromotionalShippingDiscount = isset($profile_details['InternationalPromotionalShippingDiscount']) ? $profile_details['InternationalPromotionalShippingDiscount'] : false;
     if ($InternationalPromotionalShippingDiscount == '1') {
         $shippingDetails->setInternationalPromotionalShippingDiscount(true);
     }
     // ShipToLocations
     if (is_array($ShipToLocations = maybe_unserialize($profile_details['ShipToLocations']))) {
         foreach ($ShipToLocations as $location) {
             $item->addShipToLocations($location);
         }
     }
     // ExcludeShipToLocations
     if (is_array($ExcludeShipToLocations = maybe_unserialize($profile_details['ExcludeShipToLocations']))) {
         foreach ($ExcludeShipToLocations as $location) {
             $shippingDetails->addExcludeShipToLocation($location);
         }
     }
     // global shipping
     if (@$profile_details['global_shipping'] == 1) {
         $shippingDetails->setGlobalShipping(true);
         // available since api version 781
     }
     if (get_post_meta($post_id, '_ebay_global_shipping', true) == 'yes') {
         $shippingDetails->setGlobalShipping(true);
     }
     // store pickup
     if (@$profile_details['store_pickup'] == 1) {
         $item->PickupInStoreDetails = new PickupInStoreDetailsType();
         $item->PickupInStoreDetails->setEligibleForPickupInStore(true);
     }
     // Payment Instructions
     if (trim(@$profile_details['payment_instructions']) != '') {
         $shippingDetails->setPaymentInstructions(nl2br($profile_details['payment_instructions']));
     }
     if (trim(get_post_meta($post_id, '_ebay_payment_instructions', true)) != '') {
         $shippingDetails->setPaymentInstructions(nl2br(get_post_meta($post_id, '_ebay_payment_instructions', true)));
     }
     // COD cost
     if (isset($profile_details['cod_cost']) && trim($profile_details['cod_cost'])) {
         $shippingDetails->setCODCost(str_replace(',', '.', $profile_details['cod_cost']));
     }
     // check if we have local pickup only
     if (count($localShippingOptions) == 1 && $lastShippingCategory == 'PICKUP') {
         $item->setShipToLocations('None');
         $item->setDispatchTimeMax(null);
         WPLE()->logger->info('PICKUP ONLY mode');
         // don't set ShippingDetails for pickup-only in UK!
         if ($item->Site != 'UK') {
             $item->setShippingDetails($shippingDetails);
         }
     } else {
         $item->setShippingDetails($shippingDetails);
     }
     // force AutoPay off for Freight shipping
     if ($service_type == 'FreightFlat') {
         $item->setAutoPay(0);
     }
     return $item;
 }