Exemplo n.º 1
0
 public function testCreate()
 {
     $quote = new Quote();
     $quote->setCode('-code-');
     $quote->setName('Test Name');
     $quote->setCost(500);
     $quote->setTransitTime('-transit-time-');
     $quote->setDeliveryEstimate(new DateTime());
     $quote->setCarrier('-carrier-');
     $this->assertEquals('-code-', $quote->getCode());
     $this->assertEquals('Test Name', $quote->getName());
     $this->assertEquals(500, $quote->getCost());
     $this->assertEquals('-transit-time-', $quote->getTransitTime());
     $this->assertTrue($quote->getDeliveryEstimate() instanceof DateTime);
     $this->assertEquals('-carrier-', $quote->getCarrier());
 }
Exemplo n.º 2
0
 protected function process()
 {
     try {
         $dom = new DOMDocument('1.0', 'UTF-8');
         $dom->loadXml($this->response);
         $postage_list = @$dom->getElementsByTagName('Postage');
         if (empty($postage_list)) {
             throw new Exception('Unable to get USPS Rates.');
         }
     } catch (Exception $e) {
         throw $e;
     }
     /** @var Quote[] $rates */
     $rates = [];
     foreach ($postage_list as $postage) {
         $code = @$postage->getAttribute('CLASSID');
         $cost = @$postage->getElementsByTagName('Rate')->item(0)->nodeValue;
         $deliveryEstimate = @$postage->getElementsByTagName('CommitmentDate')->item(0)->nodeValue;
         $transitTime = @$postage->getElementsByTagName('CommitmentName')->item(0)->nodeValue;
         //$name = @$postage->getElementsByTagName('MailService')->item(0)->nodeValue;
         $name = Arr::get($this->shipping_codes['domestic'], $code);
         if (!empty($this->approvedCodes) && !in_array($code, $this->approvedCodes)) {
             continue;
         }
         if (array_key_exists($code, $rates)) {
             $cost = $rates[$code]->getCost() + $cost * 100;
         } else {
             $cost = $cost * 100;
         }
         $quote = new Quote();
         $quote->setCarrier('usps')->setCode($code)->setName(html_entity_decode($name))->setCost((int) $cost);
         if (!empty($deliveryEstimate)) {
             $deliveryEstimateDT = new \DateTime($deliveryEstimate);
             $transitTime = $deliveryEstimateDT->diff(new \DateTime(date('Y-m-d')));
             $quote->setTransitTime($transitTime->d);
             $quote->setDeliveryEstimate($deliveryEstimateDT);
         }
         $rates[$quote->getCode()] = $quote;
     }
     $this->rates = array_values($rates);
     return $this;
 }