<?php

/**
 * This is the same example as presented in example1.php, but shortened by passing all data through the constructor as an array.
 *
 */
//remember to update credentials.php or replace 'FEDEX_KEY', 'FEDEX_PASSWORD', 'FEDEX_ACCOUNT_NUMBER', and 'FEDEX_METER_NUMBER'
require_once 'credentials.php';
require_once 'bootstrap.php';
use FedEx\RateService, FedEx\RateService\ComplexType, FedEx\RateService\SimpleType;
$rateRequest = new ComplexType\RateRequest();
//WebAuthenticationDetail
$rateRequest->setWebAuthenticationDetail(new ComplexType\WebAuthenticationDetail(array('UserCredential' => new ComplexType\WebAuthenticationCredential(array('Key' => FEDEX_KEY, 'Password' => FEDEX_PASSWORD)))));
//ClientDetail
$rateRequest->setClientDetail(new ComplexType\ClientDetail(array('AccountNumber' => FEDEX_ACCOUNT_NUMBER, 'MeterNumber' => FEDEX_METER_NUMBER)));
//TransactionDetail
$rateRequest->setTransactionDetail(new ComplexType\TransactionDetail(array('CustomerTransactionId' => ' *** Rate Available Services Request v8 using PHP ***')));
//Version
$rateRequest->setVersion(new ComplexType\VersionId(array('ServiceId' => 'crs', 'Major' => 10, 'Intermediate' => 0, 'Minor' => 0)));
//ReturnTransitAndCommit
$rateRequest->setReturnTransitAndCommit(true);
//RequestedShipment
$rateRequest->setRequestedShipment(new ComplexType\RequestedShipment(array('DropoffType' => new SimpleType\DropoffType(SimpleType\DropoffType::_REGULAR_PICKUP), 'ShipTimestamp' => date('c'), 'Shipper' => new ComplexType\Party(array('Address' => new ComplexType\Address(array('StreetLines' => array('10 Fed Ex Pkwy'), 'City' => 'Memphis', 'StateOrProvinceCode' => 'TN', 'PostalCode' => 38115, 'CountryCode' => 'US')))), 'Recipient' => new ComplexType\Party(array('Address' => new ComplexType\Address(array('StreetLines' => array('13450 Farmcrest Ct'), 'City' => 'Herndon', 'StateOrProvinceCode' => 'VA', 'PostalCode' => 20171, 'CountryCode' => 'US')))), 'ShippingChargesPayment' => new ComplexType\Payment(array('PaymentType' => new SimpleType\PaymentType(SimpleType\PaymentType::_SENDER), 'Payor' => new ComplexType\Payor(array('AccountNumber' => FEDEX_ACCOUNT_NUMBER, 'CountryCode' => 'US')))), 'RateRequestTypes' => array(new SimpleType\RateRequestType(SimpleType\RateRequestType::_ACCOUNT), new SimpleType\RateRequestType(SimpleType\RateRequestType::_LIST)), 'PackageCount' => 2, 'PackageDetail' => new SimpleType\RequestedPackageDetailType(SimpleType\RequestedPackageDetailType::_INDIVIDUAL_PACKAGES), 'RequestedPackageLineItems' => array(new ComplexType\RequestedPackageLineItem(array('Weight' => new ComplexType\Weight(array('Units' => new SimpleType\WeightUnits(SimpleType\WeightUnits::_LB), 'Value' => 2.0)), 'Dimensions' => new ComplexType\Dimensions(array('Length' => 10, 'Width' => 10, 'Height' => 3, 'Units' => new SimpleType\LinearUnits(SimpleType\LinearUnits::_IN))), 'GroupPackageCount' => 1)), new ComplexType\RequestedPackageLineItem(array('Weight' => new ComplexType\Weight(array('Units' => new SimpleType\WeightUnits(SimpleType\WeightUnits::_LB), 'Value' => 2)), 'Dimensions' => new ComplexType\Dimensions(array('Length' => 20, 'Width' => 20, 'Height' => 10, 'Units' => new SimpleType\LinearUnits(SimpleType\LinearUnits::_IN))), 'GroupPackageCount' => 1))))));
var_dump($rateRequest->toArray());
echo "<hr />";
$validateShipmentRequest = new RateService\Request();
var_dump($validateShipmentRequest->getGetRatesReply($rateRequest));
 /**
  * Calculates the value of the fedex shipping modifier. If the order information has changed or is not present in the session a call is made to the api to request the rates.
  * @return {float} Cost of the shipping
  */
 public function value($subtotal = 0)
 {
     if ($this->Order()->Items()->count() == 0) {
         return 0;
     }
     $orderItems = implode(',', $this->Order()->Items()->column('ID'));
     $orderItemsCount = implode(',', $this->Order()->Items()->column('Quantity'));
     $shippingAddress = array();
     $shippingAddressStr = '';
     if ($this->Order()) {
         if ($this->Order()->ShippingAddress && $this->Order()->ShippingAddress->exists()) {
             $destination = $this->Order()->ShippingAddress;
             $shippingAddress = array('StreetLines' => array($destination->Address), 'City' => $destination->City, 'StateOrProvinceCode' => $destination->State, 'PostalCode' => $destination->PostalCode, 'CountryCode' => $destination->Country);
             $addrLine2 = $destination->AddressLine2;
             $shippingAddressStr = $shippingAddress;
             $shippingAddressStr['StreetLines'] = implode(' ', $shippingAddressStr['StreetLines']);
             $shippingAddressStr = implode(',', $shippingAddressStr);
         } else {
             if ($this->Order()->Member()->DefaultShippingAddress() && $this->Order()->Member()->DefaultShippingAddress()->exists()) {
                 $destination = $this->Order()->Member()->DefaultShippingAddress();
                 $shippingAddress = array('StreetLines' => array($destination->Address), 'City' => $destination->City, 'StateOrProvinceCode' => $destination->State, 'PostalCode' => $destination->PostalCode, 'CountryCode' => $destination->Country);
                 $addrLine2 = $destination->AddressLine2;
                 $shippingAddressStr = $shippingAddress;
                 $shippingAddressStr['StreetLines'] = implode(' ', $shippingAddressStr['StreetLines']);
                 $shippingAddressStr = implode(',', $shippingAddressStr);
             }
         }
     }
     if ($this->Amount > 0 && $orderItems . '|' . $orderItemsCount . '|' . $shippingAddressStr == Session::get('FedExShipping_' . $this->Order()->ID . '.orderhash')) {
         return $this->Amount;
     }
     //Store the default charge in case something goes wrong
     $this->Amount = self::config()->default_charge;
     if (!isset($destination)) {
         return $this->Amount;
     }
     $packageItems = array();
     foreach ($this->Order()->Items() as $item) {
         if ($item instanceof Product_OrderItem) {
             $packageItems[] = new ComplexType\RequestedPackageLineItem(array('Weight' => new ComplexType\Weight(array('Units' => new SimpleType\WeightUnits(SimpleType\WeightUnits::_KG), 'Value' => $item->Product()->Weight)), 'Dimensions' => new ComplexType\Dimensions(array('Length' => $item->Product()->Depth, 'Width' => $item->Product()->Width, 'Height' => $item->Product()->Height, 'Units' => new SimpleType\LinearUnits(SimpleType\LinearUnits::_CM))), 'GroupPackageCount' => $item->Quantity));
         }
     }
     $rateRequest = $this->getRateRequestAPI();
     //RequestedShipment
     $rateRequest->setRequestedShipment(new ComplexType\RequestedShipment(array('DropoffType' => new SimpleType\DropoffType(SimpleType\DropoffType::_REGULAR_PICKUP), 'ShipTimestamp' => date('c'), 'Shipper' => new ComplexType\Party(array('Address' => new ComplexType\Address($this->getOriginAddress()))), 'Recipient' => new ComplexType\Party(array('Address' => new ComplexType\Address($shippingAddress))), 'PreferredCurrency' => ShopConfig::config()->base_currency, 'RateRequestType' => new SimpleType\RateRequestType(SimpleType\RateRequestType::_LIST), 'PackageCount' => $this->Order()->Items()->count(), 'RequestedPackageLineItems' => $packageItems)));
     //Allow extensions to modify the
     $this->extend('updateRateRequest', $rateRequest);
     //Initialize the request
     $validateShipmentRequest = new RateService\Request();
     if (!$this->config()->test_mode) {
         $validateShipmentRequest->getSoapClient()->__setLocation('https://ws.fedex.com:443/web-services/rate');
     }
     //Call the api and look through the response
     $response = $validateShipmentRequest->getGetRatesReply($rateRequest);
     if (property_exists($response, 'RateReplyDetails') && count($response->RateReplyDetails) > 0) {
         foreach ($response->RateReplyDetails as $rates) {
             if ($rates->ServiceType == self::config()->service_type) {
                 foreach ($rates->RatedShipmentDetails as $rate) {
                     if (property_exists($rate, 'TotalNetCharge')) {
                         $charge = $rate->TotalNetCharge;
                         if ($charge->Currency != ShopConfig::get_base_currency() && property_exists($rate, 'CurrencyExchangeRate')) {
                             $charge->Amount = (1 - $rate->CurrencyExchangeRate->Rate + 1) * $charge->Amount;
                         }
                         $this->Amount = $charge->Amount;
                     }
                 }
             }
         }
     }
     Session::set('FedExShipping_' . $this->Order()->ID . '.orderhash', $orderItems . '|' . $orderItemsCount . '|' . $shippingAddressStr);
     return $this->Amount;
 }
$requestedShipment->setPackageCount(2);
//RequestedShipment\RequestedPackageLineItems
$lineItems = array();
$item1Weight = new ComplexType\Weight();
$item1Weight->setUnits(new SimpleType\WeightUnits(SimpleType\WeightUnits::_LB))->setValue(2.0);
$item1Dimensions = new ComplexType\Dimensions();
$item1Dimensions->setLength(10)->setWidth(10)->setHeight(3)->setUnits(new SimpleType\LinearUnits(SimpleType\LinearUnits::_IN));
$item1 = new ComplexType\RequestedPackageLineItem();
$item1->setWeight($item1Weight);
$item1->setDimensions($item1Dimensions);
$item1->setGroupPackageCount(1);
$item2Weight = new ComplexType\Weight();
$item2Weight->setUnits(new SimpleType\WeightUnits(SimpleType\WeightUnits::_LB))->setValue(5.0);
$item2Dimensions = new ComplexType\Dimensions();
$item2Dimensions->setLength(20)->setWidth(20)->setHeight(10)->setUnits(new SimpleType\LinearUnits(SimpleType\LinearUnits::_IN));
$item2 = new ComplexType\RequestedPackageLineItem();
$item2->setWeight($item2Weight);
$item2->setDimensions($item2Dimensions);
$item2->setGroupPackageCount(1);
$lineItems[] = $item1;
$lineItems[] = $item2;
$requestedShipment->setRequestedPackageLineItems($lineItems);
$rateRequest->setRequestedShipment($requestedShipment);
var_dump($rateRequest->toArray());
echo "<hr />";
$validateShipmentRequest = new RateService\Request();
//$request->getSoapClient()->__setLocation('https://ws.fedex.com:443/web-services/rate');
var_dump($validateShipmentRequest->getGetRatesReply($rateRequest));
var_dump($validateShipmentRequest->getSoapClient()->__getLastRequest());
var_dump($validateShipmentRequest->getSoapClient()->__getLastRequestHeaders());
var_dump($validateShipmentRequest->getSoapClient()->__getFunctions());
//RequestedShipment\RateRequestTypes
$requestedShipment->setRateRequestTypes(array(new SimpleType\RateRequestType(SimpleType\RateRequestType::_ACCOUNT), new SimpleType\RateRequestType(SimpleType\RateRequestType::_LIST)));
//RequestedShipment\PackageCount
$requestedShipment->setPackageCount(2);
//RequestedShipment\RequestedPackageLineItems
$lineItems = array();
$item1Weight = new ComplexType\Weight();
$item1Weight->setUnits(new SimpleType\WeightUnits(SimpleType\WeightUnits::_LB))->setValue(2.0);
$item1Dimensions = new ComplexType\Dimensions();
$item1Dimensions->setLength(10)->setWidth(10)->setHeight(3)->setUnits(new SimpleType\LinearUnits(SimpleType\LinearUnits::_IN));
$item1 = new ComplexType\RequestedPackageLineItem();
$item1->setWeight($item1Weight);
$item1->setDimensions($item1Dimensions);
$item1->setGroupPackageCount(1);
$item2Weight = new ComplexType\Weight();
$item2Weight->setUnits(new SimpleType\WeightUnits(SimpleType\WeightUnits::_LB))->setValue(5.0);
$item2Dimensions = new ComplexType\Dimensions();
$item2Dimensions->setLength(20)->setWidth(20)->setHeight(10)->setUnits(new SimpleType\LinearUnits(SimpleType\LinearUnits::_IN));
$item2 = new ComplexType\RequestedPackageLineItem();
$item2->setWeight($item2Weight);
$item2->setDimensions($item2Dimensions);
$item2->setGroupPackageCount(1);
$lineItems[] = $item1;
$lineItems[] = $item2;
$requestedShipment->setRequestedPackageLineItems($lineItems);
$rateRequest->setRequestedShipment($requestedShipment);
var_dump($rateRequest->toArray());
echo "<hr />";
$request = new RateService\Request();
var_dump($request->getRateReply($rateRequest));
var_dump($request->getSoapClient()->__getFunctions());
<?php

/**
 * This is the same example as presented in example1.php, but shortened by passing all data through the constructor as an array.
 *
 * This test will send the same test data as in FedEx's documentation:
 * /php/RateAvailableServices/RateAvailableServices.php5
 */
//remember to update /tests/credentials.php
require_once '../tests/bootstrap.php';
use FedEx\RateService, FedEx\RateService\ComplexType, FedEx\RateService\SimpleType;
$rateRequest = new ComplexType\RateRequest();
//WebAuthenticationDetail
$rateRequest->setWebAuthenticationDetail(new ComplexType\WebAuthenticationDetail(array('UserCredential' => new ComplexType\WebAuthenticationCredential(array('Key' => FEDEX_KEY, 'Password' => FEDEX_PASSWORD)))));
//ClientDetail
$rateRequest->setClientDetail(new ComplexType\ClientDetail(array('AccountNumber' => FEDEX_ACCOUNT_NUMBER, 'MeterNumber' => FEDEX_METER_NUMBER)));
//TransactionDetail
$rateRequest->setTransactionDetail(new ComplexType\TransactionDetail(array('CustomerTransactionId' => ' *** Rate Available Services Request v8 using PHP ***')));
//Version
$rateRequest->setVersion(new ComplexType\VersionId(array('ServiceId' => 'crs', 'Major' => 10, 'Intermediate' => 0, 'Minor' => 0)));
//ReturnTransitAndCommit
$rateRequest->setReturnTransitAndCommit(true);
//RequestedShipment
$rateRequest->setRequestedShipment(new ComplexType\RequestedShipment(array('DropoffType' => new SimpleType\DropoffType(SimpleType\DropoffType::_REGULAR_PICKUP), 'ShipTimestamp' => date('c'), 'Shipper' => new ComplexType\Party(array('Address' => new ComplexType\Address(array('StreetLines' => array('10 Fed Ex Pkwy'), 'City' => 'Memphis', 'StateOrProvinceCode' => 'TN', 'PostalCode' => 38115, 'CountryCode' => 'US')))), 'Recipient' => new ComplexType\Party(array('Address' => new ComplexType\Address(array('StreetLines' => array('13450 Farmcrest Ct'), 'City' => 'Herndon', 'StateOrProvinceCode' => 'VA', 'PostalCode' => 20171, 'CountryCode' => 'US')))), 'ShippingChargesPayment' => new ComplexType\Payment(array('PaymentType' => new SimpleType\PaymentType(SimpleType\PaymentType::_SENDER), 'Payor' => new ComplexType\Payor(array('AccountNumber' => FEDEX_ACCOUNT_NUMBER, 'CountryCode' => 'US')))), 'RateRequestTypes' => array(new SimpleType\RateRequestType(SimpleType\RateRequestType::_ACCOUNT), new SimpleType\RateRequestType(SimpleType\RateRequestType::_LIST)), 'PackageCount' => 2, 'PackageDetail' => new SimpleType\RequestedPackageDetailType(SimpleType\RequestedPackageDetailType::_INDIVIDUAL_PACKAGES), 'RequestedPackageLineItems' => array(new ComplexType\RequestedPackageLineItem(array('Weight' => new ComplexType\Weight(array('Units' => new SimpleType\WeightUnits(SimpleType\WeightUnits::_LB), 'Value' => 2.0)), 'Dimensions' => new ComplexType\Dimensions(array('Length' => 10, 'Width' => 10, 'Height' => 3, 'Units' => new SimpleType\LinearUnits(SimpleType\LinearUnits::_IN))), 'GroupPackageCount' => 1)), new ComplexType\RequestedPackageLineItem(array('Weight' => new ComplexType\Weight(array('Units' => new SimpleType\WeightUnits(SimpleType\WeightUnits::_LB), 'Value' => 2)), 'Dimensions' => new ComplexType\Dimensions(array('Length' => 20, 'Width' => 20, 'Height' => 10, 'Units' => new SimpleType\LinearUnits(SimpleType\LinearUnits::_IN))), 'GroupPackageCount' => 1))))));
var_dump($rateRequest->toArray());
echo "<hr />";
$request = new RateService\Request();
var_dump($request->getRateReply($rateRequest));