<?php

// Seller /admin
//$senderName = $_POST["first"]." ".$_POST["last"];
//$senderPhone = $_POST["phone"];
//$senderStreet = $_POST["street"];
//$senderLine2 = $_POST["line2"];
//$senderCity = $_POST["city"];
//$senderState = $_POST["state"];
//$senderZip = $_POST["zip"];
//$senderCountry = "US";
//
//// buyer
//$recipientName = $_POST["first"]." ".$_POST["last"];
//$recipientPhone = $_POST["phone"];
//$recipientStreet = $_POST["street"];
//$recipientLine2 = $_POST["line2"];
//$recipientCity = $_POST["city"];
//$recipientState = $_POST["state"];
//$recipientZip = $_POST["zip"];
//$recipientCountry = "US";



//print_r($_POST);



//echo $senderName; //test to make sure the POST values are coming through.

// Copyright 2009, FedEx Corporation. All rights reserved.
// Version 9.0.0

require_once('library/fedex-common.php');



//The WSDL is not included with the sample code.
//Please include and reference in $path_to_wsdl variable.

function getfedexShippingLabel($sender, $recipient ,$fedexSettings, $wsdlpath = ""){

if($wsdlpath != "")
    $path_to_wsdl = $wsdlpath;
else
    $path_to_wsdl = "../shippingclient/fedex_label/wsdl/ShipService_v12.wsdl";


// Seller /admin
if(count($sender) > 0){
    $senderName = $sender["first"]." ".$sender["last"];
    $senderPhone = $sender["phone"];
    $senderStreet = $sender["street"];
    $senderLine2 = $sender["line2"];
    $senderCity = $sender["city"];
    $senderState = $sender["state"];
    $senderZip = $sender["zip"];
    $senderCountry = $sender['country'];

}
//// buyer
//
if(count($recipient) > 0){
    $recipientName = $recipient["first"]." ".$recipient["last"];
    $recipientPhone = $recipient["phone"];
    $recipientStreet = $recipient["street"];
    $recipientLine2 = $recipient["line2"];
    $recipientCity = $recipient["city"];
    $recipientState = $recipient["state"];
    $recipientZip = $recipient["zip"];
    $recipientCountry = $recipient['country'];
}
$number=time();
define('SHIP_LABEL', 'shipexpresslabel'.$number.'.png');  // PNG label file. Change to file-extension .pdf for creating a PDF label (e.g. shiplabel.pdf)

ini_set("soap.wsdl_cache_enabled", "0");

$client = new SoapClient($path_to_wsdl, array('trace' => 1)); // Refer to http://us3.php.net/manual/en/ref.soap.php for more information


$request['WebAuthenticationDetail'] = array(
        'UserCredential' => array(
                'Key' => $fedexSettings['key'],
                'Password' => $fedexSettings['password']
        )
);
$request['ClientDetail'] = array(
        'AccountNumber' => $fedexSettings['shipaccount'],
        'MeterNumber' => $fedexSettings['meter']
);
$request['TransactionDetail'] = array('CustomerTransactionId' => '*** Express Domestic Shipping Request v10 using PHP ***');
$request['Version'] = array(
        'ServiceId' => 'ship',
        'Major' => '12',
        'Intermediate' => '1',
        'Minor' => '0'
);

$billaccount = $fedexSettings['billaccount'];
$request['RequestedShipment'] = array(
        'ShipTimestamp' => date('c'),
        'DropoffType' => 'REGULAR_PICKUP', // valid values REGULAR_PICKUP, REQUEST_COURIER, DROP_BOX, BUSINESS_SERVICE_CENTER and STATION
        'ServiceType' => 'FEDEX_EXPRESS_SAVER', // valid values STANDARD_OVERNIGHT, PRIORITY_OVERNIGHT, FEDEX_GROUND, ...
        'PackagingType' => 'YOUR_PACKAGING', // valid values FEDEX_BOX, FEDEX_PAK, FEDEX_TUBE, YOUR_PACKAGING, ...
        'TotalWeight' => array('Value' => 1.0, 'Units' => 'LB'), // valid values LB and KG
        'Shipper' => addShipper($senderName,$senderPhone,$senderStreet,$senderLine2,$senderCity,$senderState,$senderZip,$senderCountry),
        'Recipient' => addRecipient($recipientName,$recipientPhone,$recipientStreet,$recipientLine2,$recipientCity,$recipientState,$recipientZip,$recipientCountry),
        'ShippingChargesPayment' => addShippingChargesPayment($billaccount),
        'SpecialServicesRequested' => addSpecialServices(),
        'LabelSpecification' => addLabelSpecification(),
        'RateRequestTypes' => array('ACCOUNT'), // valid values ACCOUNT and LIST
        'PackageCount' => 1,
        'RequestedPackageLineItems' => array(
                '0' => addPackageLineItem1()
        )
);


try
{
        if(setEndpoint('changeEndpoint'))
        {
                $newLocation = $client->__setLocation(setEndpoint('endpoint'));
        }

        $response = $client->processShipment($request);  // FedEx web service invocation

    if ($response->HighestSeverity != 'FAILURE' && $response->HighestSeverity != 'ERROR')
    {
        printSuccess($client, $response);

        // Create PNG or PDF label
        // Set LabelSpecification.ImageType to 'PDF' or 'PNG for generating a PDF or a PNG label
        $fp = fopen(SHIP_LABEL, 'wb');
        fwrite($fp, $response->CompletedShipmentDetail->CompletedPackageDetails->Label->Parts->Image); //Create PNG or PDF file
        fclose($fp);
        return SHIP_LABEL;
        exit;
       // getProperty('billaccount');
       // echo '<h3>This is your printable shipping label.</h3> <strong>For your convenience a copy has been sent to the email address provided.</strong><br /><img src="./'.SHIP_LABEL.'" />';
    }
    else
    {
        return 'error';
        //return false;
       // printError($client, $response);
    }

    writeToLog($client);    // Write to log file

} catch (SoapFault $exception) {
    return 'error';
    //return false;
   // printFault($exception, $client);
}

}



function addShipper($senderName,$senderPhone,$senderStreet,$senderLine2,$senderCity,$senderState,$senderZip,$senderCountry){
        $shipper = array(
                'Contact' => array(
                        'PersonName' => $senderName, //doesn't process at all if at least this one isn't a text value
                        'PhoneNumber' => $senderPhone),
                'Address' => array(
                        'StreetLines' => array($senderStreet, $senderLine2 ),
                        'City' => $senderCity ,
                        'StateOrProvinceCode' => $senderState,
                        'PostalCode' => $senderZip,
                        'CountryCode' => 'US')
        );

       // echo "<pre>";
      //  print_r($shipper);
        return $shipper;
}
function addRecipient($recipientName,$recipientPhone,$recipientStreet,$recipientLine2,$recipientCity,$recipientState,$recipientZip,$recipientCountry){
        $recipient = array(
                'Contact' => array(
                        'PersonName' => $recipientName,
                        'CompanyName' => 'Money for Old Jewelry',
                        'PhoneNumber' => $recipientPhone),
                'Address' => array(
                        'StreetLines' => array($recipientStreet),
                        'City' => $recipientCity,
                        'StateOrProvinceCode' => $recipientState,
                        'PostalCode' => $recipientZip,
                        'CountryCode' => 'US',
                        'Residential' => false)
        );

       //  echo "<pre>";
       // print_r($recipient);
  
        return $recipient;
}
function addShippingChargesPayment($billaccount = "510087461"){
        $shippingChargesPayment = array(
                'PaymentType' => 'SENDER', // valid values RECIPIENT, SENDER and THIRD_PARTY
                'Payor' => array(
                        'AccountNumber' => $billaccount,
                        'CountryCode' => 'US')
        );
        return $shippingChargesPayment;
}
function addLabelSpecification(){
        $labelSpecification = array(
                'LabelFormatType' => 'COMMON2D', // valid values COMMON2D, LABEL_DATA_ONLY
                'ImageType' => 'PNG',  // valid values DPL, EPL2, PDF, ZPLII and PNG
                'LabelStockType' => 'PAPER_7X4.75');
        return $labelSpecification;
}
function addSpecialServices(){
        $specialServices = array(
                'SpecialServiceTypes' => array('COD'),
                'CodDetail' => array(
                        'CodCollectionAmount' => array('Currency' => 'USD', 'Amount' => 150),
                        'CollectionType' => 'ANY')// ANY, GUARANTEED_FUNDS
        );
        return $specialServices;
}
function addPackageLineItem1(){
        $packageLineItem = array(
                'SequenceNumber'=>1,
                'GroupPackageCount'=>1,
                'Weight' => array(
                        'Value' => 1.0,
                        'Units' => 'LB')
        );
        return $packageLineItem;
}

?>