class USPSRateRequest in Commerce USPS 8
Class USPSRateRequest.
@package Drupal\commerce_usps
Hierarchy
- class \Drupal\commerce_usps\USPSRequest implements USPSRequestInterface
- class \Drupal\commerce_usps\USPSRateRequestBase implements USPSRateRequestInterface
- class \Drupal\commerce_usps\USPSRateRequest implements USPSRateRequestInterface
- class \Drupal\commerce_usps\USPSRateRequestBase implements USPSRateRequestInterface
Expanded class hierarchy of USPSRateRequest
1 file declares its use of USPSRateRequest
- USPSRateRequestTest.php in tests/
src/ Unit/ USPSRateRequestTest.php
1 string reference to 'USPSRateRequest'
1 service uses USPSRateRequest
File
- src/
USPSRateRequest.php, line 15
Namespace
Drupal\commerce_uspsView source
class USPSRateRequest extends USPSRateRequestBase implements USPSRateRequestInterface {
/**
* Temporary IDs used for the shared service ids by USPS API.
*/
const FIRST_CLASS_MAIL_ENVELOPE = 9999;
const FIRST_CLASS_MAIL_LETTER = 9998;
const FIRST_CLASS_MAIL_POSTCARDS = 9997;
const FIRST_CLASS_MAIL_PACKAGE = 9996;
/**
* Resolve the rates from the RateRequest response.
*
* @param array $response
* The rate request array.
*
* @return array
* An array of ShippingRates or an empty array.
*/
public function resolveRates(array $response) {
$rates = [];
// Parse the rate response and create shipping rates array.
if (!empty($response['RateV4Response']['Package']['Postage'])) {
// Convert the postage response to an array of rates when
// only 1 rate is returned.
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'];
// Attempt to use an alternate rate class if selected.
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']);
// Class code 0 is used for multiple services in the
// response. The only way to determine which service
// is returned is to parse the service name for matching
// strings based on the service type. All other service
// codes are unique and do not require this extra step.
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;
}
}
// Only add the rate if this service is enabled.
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;
}
/**
* Checks the delivery date of a USPS shipment.
*
* @return array
* The delivery rate response.
*/
public function checkDeliveryDate() {
$to_address = $this->commerceShipment
->getShippingProfile()
->get('address');
$from_address = $this->commerceShipment
->getOrder()
->getStore()
->getAddress();
// Initiate and set the username provided from usps.
$delivery = new ServiceDeliveryCalculator($this->configuration['api_information']['user_id']);
// Add the zip code we want to lookup the city and state.
$delivery
->addRoute(3, $from_address
->getPostalCode(), $to_address->postal_code);
// Perform the call and print out the results.
$delivery
->getServiceDeliveryCalculation();
return $delivery
->getArrayResponse();
}
/**
* Initialize the rate request object needed for the USPS API.
*/
public function buildRate() {
// Invoke the parent to initialize the uspsRequest.
parent::buildRate();
// Add each package to the request.
foreach ($this
->getPackages() as $package) {
$this->uspsRequest
->addPackage($package);
}
}
/**
* Utility function to translate service labels.
*
* @param string $service_code
* The service code.
*
* @return string
* The translated service code.
*/
protected function translateServiceLables($service_code) {
$label = '';
if (strtolower($service_code) == 'parcel') {
$label = 'ground';
}
return $label;
}
/**
* Utility function to validate a USA zip code.
*
* @param string $zip_code
* The zip code.
*
* @return bool
* Returns TRUE if the zip code was validated.
*/
protected function validateUsaZip($zip_code) {
return preg_match("/^([0-9]{5})(-[0-9]{4})?\$/i", $zip_code);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
USPSRateRequest:: |
public | function |
Initialize the rate request object needed for the USPS API. Overrides USPSRateRequestBase:: |
|
USPSRateRequest:: |
public | function | Checks the delivery date of a USPS shipment. | |
USPSRateRequest:: |
constant | Temporary IDs used for the shared service ids by USPS API. | ||
USPSRateRequest:: |
constant | |||
USPSRateRequest:: |
constant | |||
USPSRateRequest:: |
constant | |||
USPSRateRequest:: |
public | function |
Resolve the rates from the RateRequest response. Overrides USPSRateRequestInterface:: |
|
USPSRateRequest:: |
protected | function | Utility function to translate service labels. | |
USPSRateRequest:: |
protected | function | Utility function to validate a USA zip code. | |
USPSRateRequestBase:: |
protected | property | The commerce shipment entity. | |
USPSRateRequestBase:: |
protected | property |
The configuration array from a CommerceShippingMethod. Overrides USPSRequest:: |
|
USPSRateRequestBase:: |
protected | property | The event dispatcher. | |
USPSRateRequestBase:: |
protected | property | The shipping method being rated. | |
USPSRateRequestBase:: |
protected | property | The USPS rate request API. | |
USPSRateRequestBase:: |
protected | property | The USPS Shipment object. | |
USPSRateRequestBase:: |
public | function |
Allow rate to be altered. Overrides USPSRateRequestInterface:: |
|
USPSRateRequestBase:: |
public | function | Utility function to clean the USPS service name. | |
USPSRateRequestBase:: |
public | function | Get an array of USPS packages. | |
USPSRateRequestBase:: |
public | function |
Fetch rates from the USPS API. Overrides USPSRateRequestInterface:: |
|
USPSRateRequestBase:: |
public | function | Logs the request data. | |
USPSRateRequestBase:: |
public | function | Logs the response data. | |
USPSRateRequestBase:: |
public | function |
Set the request configuration. Overrides USPSRequest:: |
|
USPSRateRequestBase:: |
protected | function | Set the mode to either test/live. | |
USPSRateRequestBase:: |
public | function | Set the commerce shipment. | |
USPSRateRequestBase:: |
public | function | Set the shipping method being rated. | |
USPSRateRequestBase:: |
public | function | USPSRateRequest constructor. | |
USPSRequest:: |
protected | function | Returns authentication array for a request. | |
USPSRequest:: |
protected | function | Determines if the shipping method is in test method.. |