예제 #1
0
use Distantia\CanadaPostWs\Type\Shipment\ParcelCharacteristicsType;
use Distantia\CanadaPostWs\Type\Shipment\PreferencesType;
use Distantia\CanadaPostWs\Type\Shipment\PrintPreferencesType;
use Distantia\CanadaPostWs\Type\Shipment\ReferencesType;
use Distantia\CanadaPostWs\Type\Shipment\SenderType;
use Distantia\CanadaPostWs\Type\Shipment\SettlementInfoType;
use Distantia\CanadaPostWs\Type\Shipment\ShipmentInfoType;
use Distantia\CanadaPostWs\Type\Shipment\ShipmentType;
use Distantia\CanadaPostWs\WebService;
require_once __DIR__ . '/../../vendor/autoload.php';
// Config
define('CANADA_POST_API_CUSTOMER_NUMBER', '2004381');
define('CANADA_POST_API_CONTRACT_ID', '42708517');
define('CANADA_POST_API_KEY', '6e93d53968881714:0bfa9fcb9853d1f51ee57a');
// Shipment object to create
$Shipment = new ShipmentType();
$Shipment->setGroupId('4326432');
$Shipment->setRequestedShippingPoint('H2B1A0');
$Shipment->setCpcPickupIndicator(true);
$Shipment->setExpectedMailingDate(new \DateTime('2016-10-24'));
$DeliverySpec = new DeliverySpecType();
$DeliverySpec->setServiceCode(WebService::$serviceCodes[WebService::SHIPPING_CODE_DOMESTIC_EXPEDITED]);
$Sender = new SenderType();
$Sender->setName('Bulma');
$Sender->setCompany('Capsule Corp.');
$Sender->setContactPhone('1 (514) 820 5879');
$AddressDetails = new AddressDetailsType();
$AddressDetails->setAddressLine1('502 MAIN ST N');
$AddressDetails->setCity('Montréal');
$AddressDetails->setProvState('QC');
$AddressDetails->setCountryCode('CA');
예제 #2
0
 /**
  * @param ShipmentType $Shipment
  * @return bool|MessagesType|ShipmentInfoType
  */
 public function createShipment(ShipmentType $Shipment)
 {
     $XmlShipment = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><shipment xmlns="http://www.canadapost.ca/ws/shipment-v' . self::API_VERSION . '"/>');
     if (null !== $Shipment->getGroupId()) {
         $XmlShipment->addChild('group-id', $Shipment->getGroupId());
     } else {
         $XmlShipment->addChild('transmit-shipment', ['false', 'true'][(int) $Shipment->isTransmitShipment()]);
     }
     if (null !== $Shipment->isQuickshipLabelRequested()) {
         $XmlShipment->addChild('quickship-label-requested', ['false', 'true'][(int) $Shipment->isQuickshipLabelRequested()]);
     }
     if (null !== $Shipment->getRequestedShippingPoint()) {
         $XmlShipment->addChild('requested-shipping-point', $Shipment->getRequestedShippingPoint());
     }
     if (null !== $Shipment->isCpcPickupIndicator()) {
         $XmlShipment->addChild('cpc-pickup-indicator', ['false', 'true'][(int) $Shipment->isCpcPickupIndicator()]);
     }
     if (null !== $Shipment->getExpectedMailingDate()) {
         $XmlShipment->addChild('expected-mailing-date', $Shipment->getExpectedMailingDate()->format('Y-m-d'));
     }
     $XmlShipmentDeliverySpec = $XmlShipment->addChild('delivery-spec');
     $XmlShipmentDeliverySpec->addChild('service-code', $Shipment->getDeliverySpec()->getServiceCode());
     $XmlShipmentDeliverySpecSender = $XmlShipmentDeliverySpec->addChild('sender');
     if (null !== $Shipment->getDeliverySpec()->getSender()->getName()) {
         $XmlShipmentDeliverySpecSender->addChild('name', $Shipment->getDeliverySpec()->getSender()->getName());
     }
     $XmlShipmentDeliverySpecSender->addChild('company', $Shipment->getDeliverySpec()->getSender()->getCompany());
     $XmlShipmentDeliverySpecSender->addChild('contact-phone', $Shipment->getDeliverySpec()->getSender()->getContactPhone());
     $XmlShipmentDeliverySpecSenderAddressDetails = $XmlShipmentDeliverySpecSender->addChild('address-details');
     $XmlShipmentDeliverySpecSenderAddressDetails->addChild('address-line-1', $Shipment->getDeliverySpec()->getSender()->getAddressDetails()->getAddressLine1());
     if (null !== $Shipment->getDeliverySpec()->getSender()->getAddressDetails()->getAddressLine2()) {
         $XmlShipmentDeliverySpecSenderAddressDetails->addChild('address-line-2', $Shipment->getDeliverySpec()->getSender()->getAddressDetails()->getAddressLine2());
     }
     $XmlShipmentDeliverySpecSenderAddressDetails->addChild('city', $Shipment->getDeliverySpec()->getSender()->getAddressDetails()->getCity());
     $XmlShipmentDeliverySpecSenderAddressDetails->addChild('prov-state', $Shipment->getDeliverySpec()->getSender()->getAddressDetails()->getProvState());
     $XmlShipmentDeliverySpecSenderAddressDetails->addChild('country-code', $Shipment->getDeliverySpec()->getSender()->getAddressDetails()->getCountryCode());
     if (null !== $Shipment->getDeliverySpec()->getSender()->getAddressDetails()->getPostalZipCode()) {
         $XmlShipmentDeliverySpecSenderAddressDetails->addChild('postal-zip-code', $Shipment->getDeliverySpec()->getSender()->getAddressDetails()->getPostalZipCode());
     }
     $XmlShipmentDeliverySpecDestination = $XmlShipmentDeliverySpec->addChild('destination');
     if (null !== $Shipment->getDeliverySpec()->getDestination()->getName()) {
         $XmlShipmentDeliverySpecDestination->addChild('name', $Shipment->getDeliverySpec()->getDestination()->getName());
     }
     if (null !== $Shipment->getDeliverySpec()->getDestination()->getCompany()) {
         $XmlShipmentDeliverySpecDestination->addChild('company', $Shipment->getDeliverySpec()->getDestination()->getCompany());
     }
     if (null !== $Shipment->getDeliverySpec()->getDestination()->getAdditionalAddressInfo()) {
         $XmlShipmentDeliverySpecDestination->addChild('additional-address-info', $Shipment->getDeliverySpec()->getDestination()->getAdditionalAddressInfo());
     }
     if (null !== $Shipment->getDeliverySpec()->getDestination()->getClientVoiceNumber()) {
         $XmlShipmentDeliverySpecDestination->addChild('client-voice-number', $Shipment->getDeliverySpec()->getDestination()->getClientVoiceNumber());
     }
     $XmlShipmentDeliverySpecDestinationAddressDetails = $XmlShipmentDeliverySpecDestination->addChild('address-details');
     if (null !== $Shipment->getDeliverySpec()->getDestination()->getAddressDetails()->getAddressLine1()) {
         $XmlShipmentDeliverySpecDestinationAddressDetails->addChild('address-line-1', $Shipment->getDeliverySpec()->getDestination()->getAddressDetails()->getAddressLine1());
     }
     if (null !== $Shipment->getDeliverySpec()->getDestination()->getAddressDetails()->getAddressLine2()) {
         $XmlShipmentDeliverySpecDestinationAddressDetails->addChild('address-line-2', $Shipment->getDeliverySpec()->getDestination()->getAddressDetails()->getAddressLine2());
     }
     if (null !== $Shipment->getDeliverySpec()->getDestination()->getAddressDetails()->getCity()) {
         $XmlShipmentDeliverySpecDestinationAddressDetails->addChild('city', $Shipment->getDeliverySpec()->getDestination()->getAddressDetails()->getCity());
     }
     if (null !== $Shipment->getDeliverySpec()->getDestination()->getAddressDetails()->getProvState()) {
         $XmlShipmentDeliverySpecDestinationAddressDetails->addChild('prov-state', $Shipment->getDeliverySpec()->getDestination()->getAddressDetails()->getProvState());
     }
     $XmlShipmentDeliverySpecDestinationAddressDetails->addChild('country-code', $Shipment->getDeliverySpec()->getDestination()->getAddressDetails()->getCountryCode());
     if (null !== $Shipment->getDeliverySpec()->getDestination()->getAddressDetails()->getPostalZipCode()) {
         $XmlShipmentDeliverySpecDestinationAddressDetails->addChild('postal-zip-code', $Shipment->getDeliverySpec()->getDestination()->getAddressDetails()->getPostalZipCode());
     }
     $OptionsList = $Shipment->getDeliverySpec()->getOptions();
     if ($OptionsList) {
         $Options = $OptionsList->getOptions();
         if ($Options) {
             $XmlShipmentDeliverySpecOptions = $XmlShipmentDeliverySpec->addChild('options');
             foreach ($Options as $Option) {
                 $XmlShipmentDeliverySpecOptionsOption = $XmlShipmentDeliverySpecOptions->addChild('option');
                 $XmlShipmentDeliverySpecOptionsOption->addChild('option-code', $Option->getOptionCode());
                 if (null !== $Option->getOptionAmount()) {
                     $XmlShipmentDeliverySpecOptionsOption->addChild('option-amount', number_format($Option->getOptionAmount(), 2, '.', ''));
                 }
                 if (null !== $Option->isOptionQualifier1()) {
                     $XmlShipmentDeliverySpecOptionsOption->addChild('option-qualifier-1', ['false', 'true'][(int) $Option->isOptionQualifier1()]);
                 }
                 if (null !== $Option->getOptionQualifier2()) {
                     $XmlShipmentDeliverySpecOptionsOption->addChild('option-amount', $Option->getOptionQualifier2());
                 }
             }
         }
     }
     $XmlShipmentDeliverySpecParcelCharacteristics = $XmlShipmentDeliverySpec->addChild('parcel-characteristics');
     $XmlShipmentDeliverySpecParcelCharacteristics->addChild('weight', $Shipment->getDeliverySpec()->getParcelCharacteristics()->getWeight());
     if (null !== $Shipment->getDeliverySpec()->getParcelCharacteristics()->getDimensions()) {
         $XmlShipmentDeliverySpecParcelCharacteristicsDimensions = $XmlShipmentDeliverySpecParcelCharacteristics->addChild('dimensions');
         $XmlShipmentDeliverySpecParcelCharacteristicsDimensions->addChild('length', $Shipment->getDeliverySpec()->getParcelCharacteristics()->getDimensions()->getLength());
         $XmlShipmentDeliverySpecParcelCharacteristicsDimensions->addChild('width', $Shipment->getDeliverySpec()->getParcelCharacteristics()->getDimensions()->getWidth());
         $XmlShipmentDeliverySpecParcelCharacteristicsDimensions->addChild('height', $Shipment->getDeliverySpec()->getParcelCharacteristics()->getDimensions()->getHeight());
     }
     if (null !== $Shipment->getDeliverySpec()->getParcelCharacteristics()->isUnpackaged()) {
         $XmlShipmentDeliverySpecParcelCharacteristics->addChild('unpackaged', ['false', 'true'][(int) $Shipment->getDeliverySpec()->getParcelCharacteristics()->isUnpackaged()]);
     }
     if (null !== $Shipment->getDeliverySpec()->getParcelCharacteristics()->isMailingTube()) {
         $XmlShipmentDeliverySpecParcelCharacteristics->addChild('mailing-tube', ['false', 'true'][(int) $Shipment->getDeliverySpec()->getParcelCharacteristics()->isMailingTube()]);
     }
     if (null !== $Shipment->getDeliverySpec()->getParcelCharacteristics()->isOversized()) {
         $XmlShipmentDeliverySpecParcelCharacteristics->addChild('oversized', ['false', 'true'][(int) $Shipment->getDeliverySpec()->getParcelCharacteristics()->isOversized()]);
     }
     if (null !== $Shipment->getDeliverySpec()->getNotification()) {
         $XmlShipmentDeliverySpecNotification = $XmlShipmentDeliverySpec->addChild('notification');
         $XmlShipmentDeliverySpecNotification->addChild('email', $Shipment->getDeliverySpec()->getNotification()->getEmail());
         $XmlShipmentDeliverySpecNotification->addChild('on-shipment', ['false', 'true'][(int) $Shipment->getDeliverySpec()->getNotification()->isOnShipment()]);
         $XmlShipmentDeliverySpecNotification->addChild('on-exception', ['false', 'true'][(int) $Shipment->getDeliverySpec()->getNotification()->isOnException()]);
         $XmlShipmentDeliverySpecNotification->addChild('on-delivery', ['false', 'true'][(int) $Shipment->getDeliverySpec()->getNotification()->isOnDelivery()]);
     }
     if (null !== $Shipment->getDeliverySpec()->getPrintPreferences()) {
         $XmlShipmentDeliverySpecPrintPreferences = $XmlShipmentDeliverySpec->addChild('print-preferences');
         if (null !== $Shipment->getDeliverySpec()->getPrintPreferences()->getOutputFormat()) {
             $XmlShipmentDeliverySpecPrintPreferences->addChild('output-format', $Shipment->getDeliverySpec()->getPrintPreferences()->getOutputFormat());
         }
         if (null !== $Shipment->getDeliverySpec()->getPrintPreferences()->getEncoding()) {
             $XmlShipmentDeliverySpecPrintPreferences->addChild('encoding', $Shipment->getDeliverySpec()->getPrintPreferences()->getEncoding());
         }
     }
     $XmlShipmentDeliverySpecPreferences = $XmlShipmentDeliverySpec->addChild('preferences');
     $XmlShipmentDeliverySpecPreferences->addChild('show-packing-instructions', ['false', 'true'][(int) $Shipment->getDeliverySpec()->getPreferences()->isShowPackingInstructions()]);
     if (null !== $Shipment->getDeliverySpec()->getPreferences()->isShowPostageRate()) {
         $XmlShipmentDeliverySpecPreferences->addChild('show-postage-rate', ['false', 'true'][(int) $Shipment->getDeliverySpec()->getPreferences()->isShowPostageRate()]);
     }
     if (null !== $Shipment->getDeliverySpec()->getPreferences()->isShowInsuredValue()) {
         $XmlShipmentDeliverySpecPreferences->addChild('show-insured-value', ['false', 'true'][(int) $Shipment->getDeliverySpec()->getPreferences()->isShowInsuredValue()]);
     }
     if (null !== $Shipment->getDeliverySpec()->getReferences()) {
         $XmlShipmentDeliverySpecReferences = $XmlShipmentDeliverySpec->addChild('references');
         if (null !== $Shipment->getDeliverySpec()->getReferences()->getCostCentre()) {
             $XmlShipmentDeliverySpecReferences->addChild('cost-centre', $Shipment->getDeliverySpec()->getReferences()->getCostCentre());
         }
         if (null !== $Shipment->getDeliverySpec()->getReferences()->getCustomerRef1()) {
             $XmlShipmentDeliverySpecReferences->addChild('customer-ref-1', $Shipment->getDeliverySpec()->getReferences()->getCustomerRef1());
         }
         if (null !== $Shipment->getDeliverySpec()->getReferences()->getCustomerRef2()) {
             $XmlShipmentDeliverySpecReferences->addChild('customer-ref-2', $Shipment->getDeliverySpec()->getReferences()->getCustomerRef2());
         }
     }
     if (null !== $Shipment->getDeliverySpec()->getCustoms()) {
         $XmlShipmentDeliverySpecCustoms = $XmlShipmentDeliverySpec->addChild('customs');
         $XmlShipmentDeliverySpecCustoms->addChild('currency', $Shipment->getDeliverySpec()->getCustoms()->getCurrency());
         if (null !== $Shipment->getDeliverySpec()->getCustoms()->getConversionFromCad()) {
             $XmlShipmentDeliverySpecCustoms->addChild('conversion-from-cad', $Shipment->getDeliverySpec()->getCustoms()->getConversionFromCad());
         }
         $XmlShipmentDeliverySpecCustoms->addChild('reason-for-export', $Shipment->getDeliverySpec()->getCustoms()->getReasonForExport());
         if (null !== $Shipment->getDeliverySpec()->getCustoms()->getOtherReason()) {
             $XmlShipmentDeliverySpecCustoms->addChild('other-reason', $Shipment->getDeliverySpec()->getCustoms()->getOtherReason());
         }
         $SkuList = $Shipment->getDeliverySpec()->getCustoms()->getSkuList();
         if (null !== $SkuList) {
             $XmlShipmentDeliverySpecCustomsSkuList = $XmlShipmentDeliverySpecCustoms->addChild('sku-list');
             if ($SkuList->getItems()) {
                 foreach ($SkuList->getItems() as $Sku) {
                     $XmlShipmentDeliverySpecCustomsSkuListItem = $XmlShipmentDeliverySpecCustomsSkuList->addChild('item');
                     $XmlShipmentDeliverySpecCustomsSkuListItem->addChild('customs-number-of-units', $Sku->getCustomsNumberOfUnits());
                     $XmlShipmentDeliverySpecCustomsSkuListItem->addChild('customs-description', $Sku->getCustomsDescription());
                     if (null != $Sku->getSku()) {
                         $XmlShipmentDeliverySpecCustomsSkuListItem->addChild('sku', $Sku->getSku());
                     }
                     if (null != $Sku->getHsTariffCode()) {
                         $XmlShipmentDeliverySpecCustomsSkuListItem->addChild('hs-tariff-code', $Sku->getHsTariffCode());
                     }
                     $XmlShipmentDeliverySpecCustomsSkuListItem->addChild('unit-weight', $Sku->getUnitWeight());
                     $XmlShipmentDeliverySpecCustomsSkuListItem->addChild('customs-value-per-unit', $Sku->getCustomsValuePerUnit());
                     if (null != $Sku->getCustomsUnitOfMeasure()) {
                         $XmlShipmentDeliverySpecCustomsSkuListItem->addChild('customs-unit-of-measure', $Sku->getCustomsUnitOfMeasure());
                     }
                     if (null != $Sku->getCountryOfOrigin()) {
                         $XmlShipmentDeliverySpecCustomsSkuListItem->addChild('country-of-origin', $Sku->getCountryOfOrigin());
                     }
                     if (null != $Sku->getProvinceOfOrigin()) {
                         $XmlShipmentDeliverySpecCustomsSkuListItem->addChild('province-of-origin', $Sku->getProvinceOfOrigin());
                     }
                 }
             }
         }
         if (null !== $Shipment->getDeliverySpec()->getCustoms()->getDutiesAndTaxesPrepaid()) {
             $XmlShipmentDeliverySpecCustoms->addChild('duties-and-taxes-prepaid', $Shipment->getDeliverySpec()->getCustoms()->getDutiesAndTaxesPrepaid());
         }
         if (null !== $Shipment->getDeliverySpec()->getCustoms()->getCertificateNumber()) {
             $XmlShipmentDeliverySpecCustoms->addChild('certificate-number', $Shipment->getDeliverySpec()->getCustoms()->getCertificateNumber());
         }
         if (null !== $Shipment->getDeliverySpec()->getCustoms()->getLicenceNumber()) {
             $XmlShipmentDeliverySpecCustoms->addChild('licence-number', $Shipment->getDeliverySpec()->getCustoms()->getLicenceNumber());
         }
         if (null !== $Shipment->getDeliverySpec()->getCustoms()->getInvoiceNumber()) {
             $XmlShipmentDeliverySpecCustoms->addChild('invoice-number', $Shipment->getDeliverySpec()->getCustoms()->getInvoiceNumber());
         }
     }
     $XmlShipmentDeliverySpecSettlementInfo = $XmlShipmentDeliverySpec->addChild('settlement-info');
     if (null !== $Shipment->getDeliverySpec()->getSettlementInfo()->getPaidByCustomer()) {
         $XmlShipmentDeliverySpecSettlementInfo->addChild('paid-by-customer', $Shipment->getDeliverySpec()->getSettlementInfo()->getPaidByCustomer());
     }
     if (null !== $Shipment->getDeliverySpec()->getSettlementInfo()->getContractId()) {
         $XmlShipmentDeliverySpecSettlementInfo->addChild('contract-id', $Shipment->getDeliverySpec()->getSettlementInfo()->getContractId());
     }
     if (true === $Shipment->getDeliverySpec()->getSettlementInfo()->isCifShipment()) {
         $XmlShipmentDeliverySpecSettlementInfo->addChild('cif-shipment', ['false', 'true'][(int) $Shipment->getDeliverySpec()->getSettlementInfo()->isCifShipment()]);
     }
     $XmlShipmentDeliverySpecSettlementInfo->addChild('intended-method-of-payment', $Shipment->getDeliverySpec()->getSettlementInfo()->getIntendedMethodOfPayment());
     if (null !== $Shipment->getReturnSpec()) {
         $XmlShipmentReturnSpec = $XmlShipment->addChild('return-spec');
         $XmlShipmentReturnSpec->addChild('service-code', $Shipment->getReturnSpec()->getServiceCode());
         $XmlShipmentReturnSpecReturnRecipient = $XmlShipmentReturnSpec->addChild('return-recipient');
         if (null !== $Shipment->getReturnSpec()->getReturnRecipient()->getName()) {
             $XmlShipmentReturnSpecReturnRecipient->addChild('name', $Shipment->getReturnSpec()->getReturnRecipient()->getName());
         }
         if (null !== $Shipment->getReturnSpec()->getReturnRecipient()->getCompany()) {
             $XmlShipmentReturnSpecReturnRecipient->addChild('company', $Shipment->getReturnSpec()->getReturnRecipient()->getCompany());
         }
         $XmlShipmentReturnSpecReturnRecipientAddressDetails = $XmlShipmentReturnSpecReturnRecipient->addChild('address-details');
         $XmlShipmentReturnSpecReturnRecipientAddressDetails->addChild('address-line-1', $Shipment->getReturnSpec()->getReturnRecipient()->getAddressDetails()->getAddressLine1());
         if (null !== $Shipment->getReturnSpec()->getReturnRecipient()->getAddressDetails()->getAddressLine2()) {
             $XmlShipmentReturnSpecReturnRecipientAddressDetails->addChild('address-line-2', $Shipment->getReturnSpec()->getReturnRecipient()->getAddressDetails()->getAddressLine2());
         }
         $XmlShipmentReturnSpecReturnRecipientAddressDetails->addChild('city', $Shipment->getReturnSpec()->getReturnRecipient()->getAddressDetails()->getCity());
         $XmlShipmentReturnSpecReturnRecipientAddressDetails->addChild('prov-state', $Shipment->getReturnSpec()->getReturnRecipient()->getAddressDetails()->getProvState());
         $XmlShipmentReturnSpecReturnRecipientAddressDetails->addChild('postal-zip-code', $Shipment->getReturnSpec()->getReturnRecipient()->getAddressDetails()->getPostalZipCode());
         if (null !== $Shipment->getReturnSpec()->getReturnNotification()) {
             $XmlShipmentReturnSpec->addChild('return-notification', $Shipment->getReturnSpec()->getReturnNotification());
         }
     }
     $request = $XmlShipment->asXML();
     $response = $this->processRequest(['request_url' => '/shipment', 'headers' => ['Content-Type: application/vnd.cpc.shipment-v' . self::API_VERSION . '+xml', 'Accept: application/vnd.cpc.shipment-v' . self::API_VERSION . '+xml'], 'request' => $request]);
     $responseXML = new SimpleXMLElement($response);
     switch ($responseXML->getName()) {
         case 'shipment-info':
             $ShipmentInfoType = new ShipmentInfoType();
             $ShipmentInfoType->setShipmentId((string) $responseXML->{'shipment-id'});
             $ShipmentInfoType->setShipmentStatus((string) $responseXML->{'shipment-status'});
             $ShipmentInfoType->setTrackingPin((string) $responseXML->{'tracking-pin'});
             if ($responseXML->{'links'}->link) {
                 foreach ($responseXML->{'links'}->link as $link) {
                     $LinkType = new LinkType();
                     $LinkType->setHref((string) $link['href']);
                     $LinkType->setRel((string) $link['rel']);
                     $LinkType->setMediaType((string) $link['media-type']);
                     if (isset($link['index'])) {
                         $LinkType->setIndex((string) $link['index']);
                     }
                     $ShipmentInfoType->addLink($LinkType);
                 }
             }
             return $ShipmentInfoType;
             break;
         case 'messages':
             return WebService::getMessagesType($responseXML);
             break;
         default:
             return false;
             break;
     }
 }