USPSRateRequest.php in Commerce USPS 8
File
src/USPSRateRequest.php
View source
<?php
namespace Drupal\commerce_usps;
use Drupal\commerce_price\Price;
use Drupal\commerce_shipping\ShippingRate;
use Drupal\commerce_shipping\ShippingService;
use USPS\ServiceDeliveryCalculator;
class USPSRateRequest extends USPSRateRequestBase implements USPSRateRequestInterface {
const FIRST_CLASS_MAIL_ENVELOPE = 9999;
const FIRST_CLASS_MAIL_LETTER = 9998;
const FIRST_CLASS_MAIL_POSTCARDS = 9997;
const FIRST_CLASS_MAIL_PACKAGE = 9996;
public function resolveRates(array $response) {
$rates = [];
if (!empty($response['RateV4Response']['Package']['Postage'])) {
if (!empty($response['RateV4Response']['Package']['Postage']['Rate'])) {
$response['RateV4Response']['Package']['Postage'] = [
$response['RateV4Response']['Package']['Postage'],
];
}
foreach ($response['RateV4Response']['Package']['Postage'] as $rate) {
$price = $rate['Rate'];
if (!empty($this->configuration['rate_options']['rate_class'])) {
switch ($this->configuration['rate_options']['rate_class']) {
case 'commercial_plus':
$price = !empty($rate['CommercialPlusRate']) ? $rate['CommercialPlusRate'] : $price;
break;
case 'commercial':
$price = !empty($rate['CommercialRate']) ? $rate['CommercialRate'] : $price;
break;
}
}
$service_code = $rate['@attributes']['CLASSID'];
$service_name = $this
->cleanServiceName($rate['MailService']);
if ($service_code == 0) {
if (stripos($service_name, 'Envelope') !== FALSE) {
$service_code = self::FIRST_CLASS_MAIL_ENVELOPE;
}
elseif (stripos($service_name, 'Letter') !== FALSE) {
$service_code = self::FIRST_CLASS_MAIL_LETTER;
}
elseif (stripos($service_name, 'Postcards') !== FALSE) {
$service_code = self::FIRST_CLASS_MAIL_POSTCARDS;
}
elseif (stripos($service_name, 'Package') !== FALSE) {
$service_code = self::FIRST_CLASS_MAIL_PACKAGE;
}
else {
continue;
}
}
if (!in_array($service_code, $this->configuration['services'])) {
continue;
}
$rates[] = new ShippingRate([
'shipping_method_id' => $this->shippingMethod
->id(),
'service' => new ShippingService($service_code, $service_name),
'amount' => new Price($price, 'USD'),
]);
}
}
return $rates;
}
public function checkDeliveryDate() {
$to_address = $this->commerceShipment
->getShippingProfile()
->get('address');
$from_address = $this->commerceShipment
->getOrder()
->getStore()
->getAddress();
$delivery = new ServiceDeliveryCalculator($this->configuration['api_information']['user_id']);
$delivery
->addRoute(3, $from_address
->getPostalCode(), $to_address->postal_code);
$delivery
->getServiceDeliveryCalculation();
return $delivery
->getArrayResponse();
}
public function buildRate() {
parent::buildRate();
foreach ($this
->getPackages() as $package) {
$this->uspsRequest
->addPackage($package);
}
}
protected function translateServiceLables($service_code) {
$label = '';
if (strtolower($service_code) == 'parcel') {
$label = 'ground';
}
return $label;
}
protected function validateUsaZip($zip_code) {
return preg_match("/^([0-9]{5})(-[0-9]{4})?\$/i", $zip_code);
}
}