1<?php 2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project 3// 4// All Rights Reserved. See copyright.txt for details and a complete list of authors. 5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details. 6// $Id$ 7 8class ShippingProvider_Fedex implements ShippingProvider 9{ 10 private $key; 11 private $password; 12 private $meter; 13 14 function __construct(array $config) 15 { 16 $this->key = $config['key']; 17 $this->password = $config['password']; 18 $this->meter = $config['meter']; 19 } 20 21 function getRates(array $from, array $to, array $packages) 22 { 23 if (! class_exists('SoapClient')) { 24 return []; 25 } 26 27 $wsdl = __DIR__ . '/FedEx_v8.wsdl'; 28 $args = []; 29 30 $request = $this->getRequest($from, $to, $packages); 31 32 try { 33 $client = new SoapClient($wsdl, $args); 34 $response = $client->getRates($request); 35 36 $options = $response->RateReplyDetails; 37 $out = $this->extractRates($options); 38 39 return $out; 40 } catch (SoapFault $e) { 41 return []; 42 } 43 } 44 45 private function extractRates($options) 46 { 47 $out = []; 48 49 foreach ($options as $option) { 50 if ($detail = reset($option->RatedShipmentDetails)) { 51 $charge = $detail->ShipmentRateDetail->TotalNetCharge; 52 $out[] = [ 53 'provider' => 'FedEx', 54 'service' => $option->ServiceType, 55 'readable' => tra($option->ServiceType), 56 'cost' => number_format($charge->Amount, 2, '.', ''), 57 'currency' => $charge->Currency, 58 ]; 59 } 60 } 61 62 return $out; 63 } 64 65 private function getRequest($from, $to, $packages) 66 { 67 $request = [ 68 'WebAuthenticationDetail' => [ 69 'UserCredential' => [ 70 'Key' => $this->key, 71 'Password' => $this->password, 72 ], 73 ], 74 'ClientDetail' => [ 75 'AccountNumber' => $this->account, 76 'MeterNumber' => $this->meter, 77 ], 78 'Version' => [ 79 'ServiceId' => 'crs', 80 'Major' => '8', 81 'Intermediate' => '0', 82 'Minor' => '0', 83 ], 84 'RequestedShipment' => [ 85 'PackagingType' => 'YOUR_PACKAGING', 86 'Shipper' => $this->buildAddress($from), 87 'Recipient' => $this->buildAddress($to), 88 'RateRequestTypes' => 'LIST', 89 'PackageDetail' => 'INDIVIDUAL_PACKAGES', 90 'RequestedPackageLineItems' => array_map([ $this, 'buildPackage' ], $packages), 91 ], 92 ]; 93 94 return $request; 95 } 96 97 private function buildAddress($address) 98 { 99 return [ 100 'Address' => [ 101 'PostalCode' => $address['zip'], 102 'CountryCode' => $address['country'], 103 ], 104 ]; 105 } 106 107 private function buildPackage($package) 108 { 109 return [ 110 'Weight' => [ 111 'Value' => $package['weight'], 112 'Units' => 'KG', 113 ], 114 ]; 115 } 116} 117