class USPSRateRequestInternational 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\USPSRateRequestInternational implements USPSRateRequestInterface
- class \Drupal\commerce_usps\USPSRateRequestBase implements USPSRateRequestInterface
Expanded class hierarchy of USPSRateRequestInternational
1 file declares its use of USPSRateRequestInternational
- USPSInternationalRateRequestTest.php in tests/
src/ Unit/ USPSInternationalRateRequestTest.php
1 string reference to 'USPSRateRequestInternational'
1 service uses USPSRateRequestInternational
File
- src/
USPSRateRequestInternational.php, line 14
Namespace
Drupal\commerce_uspsView source
class USPSRateRequestInternational extends USPSRateRequestBase implements USPSRateRequestInterface {
/**
* 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['IntlRateV2Response']['Package']['Service'])) {
// Convert the service response to an array of rates when
// only 1 rate is returned.
if (!empty($response['IntlRateV2Response']['Package']['Service']['Postage'])) {
$response['IntlRateV2Response']['Package']['Service'] = [
$response['IntlRateV2Response']['Package']['Service'],
];
}
foreach ($response['IntlRateV2Response']['Package']['Service'] as $service) {
$price = $service['Postage'];
// 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($service['CommercialPlusPostage']) ? $service['CommercialPlusPostage'] : $price;
break;
case 'commercial':
$price = !empty($service['CommercialPostage']) ? $service['CommercialPostage'] : $price;
break;
}
}
$service_code = $service['@attributes']['ID'];
$service_name = $this
->cleanServiceName($service['SvcDescription']);
// 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;
}
/**
* Initialize the rate request object needed for the USPS API.
*/
public function buildRate() {
// Invoke the parent to initialize the uspsRequest.
parent::buildRate();
$this->uspsRequest
->setInternationalCall(TRUE);
$this->uspsRequest
->addExtraOption('Revision', 2);
// Add each package to the request.
// Todo: IntlRateV2 is limited to 25 packages per txn.
foreach ($this
->getPackages() as $package) {
$this->uspsRequest
->addPackage($package);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
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. | |
USPSRateRequestInternational:: |
public | function |
Initialize the rate request object needed for the USPS API. Overrides USPSRateRequestBase:: |
|
USPSRateRequestInternational:: |
public | function |
Resolve the rates from the RateRequest response. Overrides USPSRateRequestInterface:: |
|
USPSRequest:: |
protected | function | Returns authentication array for a request. | |
USPSRequest:: |
protected | function | Determines if the shipping method is in test method.. |