class USPSDomesticRate in Ubercart 8.4
Provides a percentage rate shipping quote plugin.
Plugin annotation
@UbercartShippingQuote(
id = "usps",
admin_label = @Translation("USPS Domestic")
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\uc_quote\ShippingQuotePluginBase implements ShippingQuotePluginInterface
- class \Drupal\uc_usps\Plugin\Ubercart\ShippingQuote\USPSRateBase
- class \Drupal\uc_usps\Plugin\Ubercart\ShippingQuote\USPSDomesticRate
- class \Drupal\uc_usps\Plugin\Ubercart\ShippingQuote\USPSRateBase
- class \Drupal\uc_quote\ShippingQuotePluginBase implements ShippingQuotePluginInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of USPSDomesticRate
File
- shipping/
uc_usps/ src/ Plugin/ Ubercart/ ShippingQuote/ USPSDomesticRate.php, line 17
Namespace
Drupal\uc_usps\Plugin\Ubercart\ShippingQuoteView source
class USPSDomesticRate extends USPSRateBase {
// id = "usps_env",
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return [
'base_rate' => 0,
'product_rate' => 0,
'field' => '',
];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$fields = [
'' => $this
->t('- None -'),
];
$result = \Drupal::entityQuery('field_config')
->condition('field_type', 'number')
->execute();
foreach (FieldConfig::loadMultiple($result) as $field) {
$fields[$field
->getName()] = $field
->label();
}
$form['base_rate'] = [
'#type' => 'uc_price',
'#title' => $this
->t('Base price'),
'#description' => $this
->t('The starting price for shipping costs.'),
'#default_value' => $this->configuration['base_rate'],
'#required' => TRUE,
];
$form['product_rate'] = [
'#type' => 'number',
'#title' => $this
->t('Default product shipping rate'),
'#min' => 0,
'#step' => 'any',
'#description' => $this
->t('The percentage of the item price to add to the shipping cost for an item.'),
'#default_value' => $this->configuration['product_rate'],
'#field_suffix' => $this
->t('% (percent)'),
'#required' => TRUE,
];
$form['field'] = [
'#type' => 'select',
'#title' => $this
->t('Product shipping rate override field'),
'#description' => $this
->t('Overrides the default shipping rate per product for this percentage rate shipping method, when the field is attached to a product content type and has a value.'),
'#options' => $fields,
'#default_value' => $this->configuration['field'],
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
$this->configuration['base_rate'] = $form_state
->getValue('base_rate');
$this->configuration['product_rate'] = $form_state
->getValue('product_rate');
$this->configuration['field'] = $form_state
->getValue('field');
}
/**
* {@inheritdoc}
*/
public function getDescription() {
return $this
->t('USPS Web Tools® rate');
}
/**
* {@inheritdoc}
*/
public function getQuotes(OrderInterface $order) {
$rate = $this->configuration['base_rate'];
$field = $this->configuration['field'];
foreach ($order->products as $product) {
if (isset($product->nid->entity->{$field}->value)) {
$product_rate = $product->nid->entity->{$field}->value * $product->qty->value;
}
else {
$product_rate = $this->configuration['product_rate'] * $product->qty->value;
}
$rate += $product->price->value * floatval($product_rate) / 100;
}
return [
$rate,
];
}
/**
* Callback for retrieving USPS shipping quote.
*
* @param $products
* Array of cart contents.
* @param $details
* Order details other than product information.
* @param $method
* The shipping method to create the quote.
*
* @return
* JSON object containing rate, error, and debugging information.
*/
//public function getQuotes(OrderInterface $order) {
public function quote($products, $details, $method) {
$usps_config = \Drupal::config('uc_usps.settings');
$quote_config = \Drupal::config('uc_quote.settings');
// The uc_quote AJAX query can fire before the customer has completely
// filled out the destination address, so check to see whether the address
// has all needed fields. If not, abort.
$destination = (object) $details;
// Country code is always needed.
if (empty($destination->country)) {
// Skip this shipping method.
return [];
}
// Shipments to the US also need zone and postal_code.
if ($destination->country == 'US' && (empty($destination->zone) || empty($destination->postal_code))) {
// Skip this shipping method.
return [];
}
// USPS Production server.
$connection_url = 'http://production.shippingapis.com/ShippingAPI.dll';
// Initialize $debug_data to prevent PHP notices here and in uc_quote.
$debug_data = [
'debug' => NULL,
'error' => [],
];
$services = [];
$addresses = [
$quote_config
->get('store_default_address'),
];
$packages = $this
->packageProducts($products, $addresses);
if (!count($packages)) {
return [];
}
foreach ($packages as $key => $ship_packages) {
$orig = $addresses[$key];
$orig->email = uc_store_email();
if (strpos($method['id'], 'intl') && $destination->country != 'US') {
// Build XML for international rate request.
$request = $this
->intlRateRequest($ship_packages, $orig, $destination);
}
elseif ($destination->country == 'US') {
// Build XML for domestic rate request.
$request = $this
->rateRequest($ship_packages, $orig, $destination);
}
$account = \Drupal::currentUser();
if ($account
->hasPermission('configure quotes') && $quote_config
->get('display_debug')) {
$debug_data['debug'] .= htmlentities(urldecode($request)) . "<br />\n";
}
// Send request
$result = \Drupal::httpClient()
->post($connection_url, NULL, $request)
->send();
if ($account
->hasPermission('configure quotes') && $quote_config
->get('display_debug')) {
$debug_data['debug'] .= htmlentities($result
->getBody(TRUE)) . "<br />\n";
}
$rate_type = $usps_config
->get('online_rates');
$response = new \SimpleXMLElement($result
->getBody(TRUE));
// Map double-encoded HTML markup in service names to Unicode characters.
$service_markup = [
'<sup>&reg;</sup>' => '®',
'<sup>&trade;</sup>' => '™',
'<sup>®</sup>' => '®',
'<sup>™</sup>' => '™',
'**' => '',
];
// Use this map to fix USPS service names.
if (strpos($method['id'], 'intl')) {
// Find and replace markup in International service names.
foreach ($response
->xpath('//Service') as $service) {
$service->SvcDescription = str_replace(array_keys($service_markup), $service_markup, $service->SvcDescription);
}
}
else {
// Find and replace markup in Domestic service names.
foreach ($response
->xpath('//Postage') as $postage) {
$postage->MailService = str_replace(array_keys($service_markup), $service_markup, $postage->MailService);
}
}
if (isset($response->Package)) {
foreach ($response->Package as $package) {
if (isset($package->Error)) {
$debug_data['error'][] = (string) $package->Error[0]->Description . '<br />';
}
else {
if (strpos($method['id'], 'intl')) {
foreach ($package->Service as $service) {
$id = (string) $service['ID'];
$services[$id]['label'] = t('U.S.P.S. @service', [
'@service' => (string) $service->SvcDescription,
]);
// Markup rate before customer sees it.
if (!isset($services[$id]['rate'])) {
$services[$id]['rate'] = 0;
}
$services[$id]['rate'] += $this
->rateMarkup((string) $service->Postage);
}
}
else {
foreach ($package->Postage as $postage) {
$classid = (string) $postage['CLASSID'];
if ($classid === '0') {
if ((string) $postage->MailService == "First-Class Mail® Parcel") {
$classid = 'zeroParcel';
}
elseif ((string) $postage->MailService == "First-Class Mail® Letter") {
$classid = 'zeroFlat';
}
else {
$classid = 'zero';
}
}
if (!isset($services[$classid]['rate'])) {
$services[$classid]['rate'] = 0;
}
$services[$classid]['label'] = t('U.S.P.S. @service', [
'@service' => (string) $postage->MailService,
]);
// Markup rate before customer sees it.
// Rates are stored differently if the ONLINE $rate_type is
// requested. First Class doesn't have online rates, so if
// CommercialRate is missing use Rate instead.
if ($rate_type && !empty($postage->CommercialRate)) {
$services[$classid]['rate'] += $this
->rateMarkup((string) $postage->CommercialRate);
}
else {
$services[$classid]['rate'] += $this
->rateMarkup((string) $postage->Rate);
}
}
}
}
}
}
}
// Strip leading 'usps_'.
$method_services = substr($method['id'] . '_services', 5);
//$method_services is the name of the callback function
// array_keys($method['quote']['accessorials'])
$usps_services = array_filter($usps_config
->get($method_services));
foreach ($services as $service => $quote) {
if (!in_array($service, $usps_services)) {
unset($services[$service]);
}
}
foreach ($services as $key => $quote) {
if (isset($quote['rate'])) {
$services[$key]['rate'] = $quote['rate'];
$services[$key]['option_label'] = $this
->getDisplayLabel($quote['label']);
}
}
uasort($services, 'uc_quote_price_sort');
// Merge debug data into $services. This is necessary because
// $debug_data is not sortable by a 'rate' key, so it has to be
// kept separate from the $services data until this point.
if (isset($debug_data['debug']) || isset($debug_data['error']) && count($debug_data['error'])) {
$services['data'] = $debug_data;
}
return $services;
}
/**
* Constructs a quote request for domestic shipments.
*
* @param array $packages
* Array of packages received from the cart.
* @param $origin
* Delivery origin address information.
* @param $destination
* Delivery destination address information.
*
* @return string
* RateV4Request XML document to send to USPS.
*/
public function rateRequest(array $packages, $origin, $destination) {
$usps_config = \Drupal::config('uc_usps.settings');
$request = '<RateV4Request USERID="' . $usps_config
->get('user_id') . '">';
$request .= '<Revision>2</Revision>';
$rate_type = $usps_config
->get('online_rates');
$package_id = 0;
foreach ($packages as $package) {
$qty = $package->qty;
for ($i = 0; $i < $qty; $i++) {
$request .= '<Package ID="' . $package_id . '">' . '<Service>' . ($rate_type ? 'ONLINE' : 'ALL') . '</Service>' . '<ZipOrigination>' . substr(trim($origin->postal_code), 0, 5) . '</ZipOrigination>' . '<ZipDestination>' . substr(trim($destination->postal_code), 0, 5) . '</ZipDestination>' . '<Pounds>' . intval($package->pounds) . '</Pounds>' . '<Ounces>' . number_format($package->ounces, 1, '.', '') . '</Ounces>' . '<Container>' . $package->container . '</Container>' . '<Size>' . $package->size . '</Size>' . '<Width>' . $package->width . '</Width>' . '<Length>' . $package->length . '</Length>' . '<Height>' . $package->height . '</Height>' . '<Girth>' . $package->girth . '</Girth>' . '<Value>' . $package->price . '</Value>' . '<Machinable>' . ($package->machinable ? 'TRUE' : 'FALSE') . '</Machinable>' . '<ReturnLocations>TRUE</ReturnLocations>' . '<ShipDate Option="EMSH">' . \Drupal::service('date.formatter')
->format(\Drupal::time()
->getCurrentTime(), 'custom', 'd-M-Y', 'America/New_York', 'en') . '</ShipDate>';
// Check if we need to add any special services to this package.
if ($usps_config
->get('insurance') || $usps_config
->get('delivery_confirmation') || $usps_config
->get('signature_confirmation')) {
$request .= '<SpecialServices>';
if ($usps_config
->get('insurance')) {
$request .= '<SpecialService>1</SpecialService>';
}
if ($usps_config
->get('delivery_confirmation')) {
$request .= '<SpecialService>13</SpecialService>';
}
if ($usps_config
->get('signature_confirmation')) {
$request .= '<SpecialService>15</SpecialService>';
}
$request .= '</SpecialServices>';
}
// Close off Package tag.
$request .= '</Package>';
$package_id++;
}
}
$request .= '</RateV4Request>';
return 'API=RateV4&XML=' . UrlHelper::encodePath($request);
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
ShippingQuotePluginBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
ShippingQuotePluginBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
ShippingQuotePluginBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
|
ShippingQuotePluginBase:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
USPSDomesticRate:: |
public | function |
Form constructor. Overrides USPSRateBase:: |
|
USPSDomesticRate:: |
public | function |
Gets default configuration for this plugin. Overrides USPSRateBase:: |
|
USPSDomesticRate:: |
public | function |
Returns a description of this shipping quote. Overrides USPSRateBase:: |
|
USPSDomesticRate:: |
public | function |
Retrieves shipping quotes for this method. Overrides USPSRateBase:: |
|
USPSDomesticRate:: |
public | function |
Callback for retrieving USPS shipping quote. Overrides USPSRateBase:: |
|
USPSDomesticRate:: |
public | function | Constructs a quote request for domestic shipments. | |
USPSDomesticRate:: |
public | function |
Form submission handler. Overrides USPSRateBase:: |
|
USPSRateBase:: |
public | function |
Returns the shipping quote method label with logo. Overrides ShippingQuotePluginBase:: |
|
USPSRateBase:: |
protected | function | Pseudo-constructor to set default values of a package. | |
USPSRateBase:: |
protected | function | Organizes products into packages for shipment. | |
USPSRateBase:: |
protected | function | Modifies the rate received from USPS before displaying to the customer. | |
USPSRateBase:: |
protected | function | Modifies the weight of shipment before sending to USPS for a quote. |