/** * Build the validate address request * @internal * @return string * @throws \SimpleUPS\Api\MissingParameterException */ public function buildXml() { if ($this->getShipment()->getDestination() == null) { throw new MissingParameterException('Shipment destination is missing'); } $dom = new \DomDocument('1.0'); $dom->formatOutput = $this->getDebug(); $dom->appendChild($ratingRequest = $dom->createElement('RatingServiceSelectionRequest')); $addressRequestLang = $dom->createAttribute('xml:lang'); $addressRequestLang->value = parent::getXmlLang(); $ratingRequest->appendChild($request = $dom->createElement('Request')); $request->appendChild($transactionReference = $dom->createElement('TransactionReference')); $transactionReference->appendChild($dom->createElement('CustomerContext', $this->getCustomerContext())); $request->appendChild($dom->createElement('RequestAction', 'Rate')); $request->appendChild($dom->createElement('RequestOption', 'Shop')); //@todo test with "Rate" as setting to determine difference $ratingRequest->appendChild($pickupType = $dom->createElement('PickupType')); $pickupType->appendChild($shipmentType = $dom->createElement('Code', $this->getPickupType())); if ($this->getRateType() != null) { $ratingRequest->appendChild($customerClassification = $dom->createElement('CustomerClassification')); $customerClassification->appendChild($dom->createElement('Code', $this->getRateType())); } // Shipment $shipment = $this->getShipment(); $shipment->setShipper(UPS::getShipper()); $ratingRequest->appendChild($shipment->toXml($dom)); $xml = parent::buildAuthenticationXml() . $dom->saveXML(); return $xml; }
public function testship() { print_r(get_declared_classes()); try { //set shipper $fromAddress = new \SimpleUPS\InstructionalAddress(); $fromAddress->setAddressee('Mark Stevens'); $fromAddress->setStreet('10571 Pico Blvd'); $fromAddress->setStateProvinceCode('CA'); $fromAddress->setCity('Los Angeles'); $fromAddress->setPostalCode(90064); $fromAddress->setCountryCode('US'); $shipper = new \SimpleUPS\Shipper(); $shipper->setNumber('CCE85AD5154DDC46'); $shipper->setAddress($fromAddress); \SimpleUPS\UPS::setShipper($shipper); //define a shipping destination $shippingDestination = new \SimpleUPS\InstructionalAddress(); $shippingDestination->setStreet('220 Bowery'); $shippingDestination->setStateProvinceCode('NY'); $shippingDestination->setCity('New York'); $shippingDestination->setPostalCode(10453); $shippingDestination->setCountryCode('US'); //define a package, we could specify the dimensions of the box if we wanted a more accurate estimate $package = new \SimpleUPS\Rates\Package(); $package->setWeight('7'); $shipment = new \SimpleUPS\Rates\Shipment(); $shipment->setDestination($shippingDestination); $shipment->addPackage($package); echo 'Rates: '; echo '<ul>'; foreach (UPS::getRates($shipment) as $shippingMethod) { echo '<li>' . $shippingMethod->getService()->getDescription() . ' ($' . $shippingMethod->getTotalCharges() . ')</li>'; } echo '</ul>'; } catch (Exception $e) { //doh, something went wrong echo 'Failed: (' . get_class($e) . ') ' . $e->getMessage() . '<br/>'; echo 'Stack trace:<br/><pre>' . $e->getTraceAsString() . '</pre>'; } }
/** * Perform the request and get the response XML for a given request * @return Response|\SimpleXMLElement Returns SimpleXMLElement when response class is omitted * @throws \SimpleUPS\Api\InvalidResponseException * @throws \SimpleUPS\Api\RequestFailedException * @throws \SimpleUPS\Api\ResponseErrorException * @throws \SimpleUPS\Api\MissingParameterException */ public function sendRequest() { try { $xml = $this->buildXml(); } catch (\SimpleUPS\Api\MissingParameterException $e) { throw new \SimpleUPS\Api\MissingParameterException($e->getMessage()); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->getUrl()); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml); $result = curl_exec($ch); if (curl_errno($ch)) { throw new \SimpleUPS\Api\RequestFailedException('#' . curl_errno($ch) . ': ' . curl_error($ch)); } else { $returnCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($returnCode != 200) { throw new \SimpleUPS\Api\RequestFailedException('Request returned header: ' . $returnCode); } } curl_close($ch); if ($this->getDebug()) { UPS::$request = $this; $dom = new \DOMDocument(); $dom->preserveWhiteSpace = false; $dom->formatOutput = true; //if xml fails to load, still record the response even if it's not formatted if ($dom->loadXML($result)) { UPS::$response = $dom->saveXML(); } else { UPS::$response = $result; } } try { $xml = new \SimpleXMLElement($result); if ($xml === false) { throw new \Exception(); } } catch (\Exception $e) { throw new \SimpleUPS\Api\InvalidResponseException('Unable to parse XML response'); } $this->handleResponseErrors($xml); if ($this->responseClass === false) { return $xml; } $response = new $this->responseClass(); $response->fromXml($xml); return $response; }
public function validateAddress($address) { try { if (!UPS::isValidAddress($address)) { return UPS::getCorrectedAddress($address); } return true; } catch (Exception $e) { echo 'Failed: (' . get_class($e) . ') ' . $e->getMessage() . '<br/>'; echo 'Stack trace:<br/><pre>' . $e->getTraceAsString() . '</pre>'; } //UPS::getDebugOutput(); }