/**
  * Calculate quote callback: the bulk of the shipping method is usually found
  * here. This is where we do the actual calculations to figure out what the
  * shipping costs should be. We can return a single price or for more control
  * an array of arrays containing:
  *    - label
  *    - quantity
  *    - amount
  *    - currency code
  *
  * Only the amount is needed as the rest have default values.
  */
 public function calculate_quote($currency_code, $form_values = array(), $order = NULL, $pane_form = NULL, $pane_values = NULL)
 {
     if (empty($order)) {
         $order = $this->order;
     }
     $settings = $this->settings;
     $shipping_line_items = array();
     $shipping_line_items[] = array('amount' => commerce_currency_decimal_to_amount($settings['shipping_price'], $currency_code), 'currency_code' => $currency_code, 'label' => t('Normal shipping'));
     if (!empty($form_values['express'])) {
         $shipping_line_items[] = array('amount' => commerce_currency_decimal_to_amount($settings['shipping_price'], $currency_code), 'currency_code' => $currency_code, 'label' => t('Express fee'));
     }
     return $shipping_line_items;
 }
 public function calculate_quote($currency_code, $form_values = array(), $order = NULL, $pane_form = NULL, $pane_values = NULL)
 {
     if (empty($order)) {
         $order = $this->order;
     }
     $settings = $this->settings;
     $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
     if (isset($settings['shipping_rates'][$currency_code]) && $settings['shipping_rates'][$currency_code]) {
         $amount = $settings['shipping_rates'][$currency_code][$currency_code];
         $tax_type = !empty($settings['shipping_rates'][$currency_code]['include_tax']) ? $settings['shipping_rates'][$currency_code]['include_tax'] : '_none';
     } else {
         $amount = $settings['shipping_price'];
         $tax_type = !empty($settings['include_tax']) ? $settings['include_tax'] : '_none';
     }
     $amount = commerce_currency_decimal_to_amount($amount, $currency_code);
     if ($tax_type != '_none' && module_exists('commerce_tax') && ($tax_rate = commerce_tax_rate_load($tax_type))) {
         // Create base price component
         $price = array('amount' => $amount, 'currency_code' => $currency_code, 'data' => array());
         // Add quote component part.
         $quote = $price;
         $quote['amount'] = $price['amount'] / (1 + $tax_rate['rate']);
         $price['data'] = commerce_price_component_add($price, 'quote', $quote, TRUE, FALSE);
         // Add tax component part.
         $tax = $quote;
         $tax['amount'] = $price['amount'] - $quote['amount'];
         $price['data'] = commerce_price_component_add($price, $tax_rate['price_component'], $tax, TRUE, FALSE);
     }
     $quantity = 0;
     foreach ($order_wrapper->commerce_line_items as $line_item_wrapper) {
         if ($line_item_wrapper->type->value() == 'product') {
             if ($settings['rate_type'] == 'product') {
                 $quantity += $line_item_wrapper->quantity->value();
             } elseif ($settings['rate_type'] == 'line_item') {
                 $quantity += 1;
             } elseif ($settings['rate_type'] == 'order') {
                 $quantity = 1;
             }
         }
     }
     $line_item_data = array('label' => $settings['label'], 'quantity' => $quantity);
     if (!empty($price)) {
         $line_item_data['price'] = $price;
     } else {
         $line_item_data['amount'] = $amount;
     }
     $shipping_line_items = array();
     $shipping_line_items[] = $line_item_data;
     return $shipping_line_items;
 }
 /**
  * Build a state array for relay response processing
  *
  * @param $response
  *  A web service converted response
  */
 protected function buildRelayResponseState($response)
 {
     $state = array();
     if (isset($response['amount']) && isset($response['currency_code'])) {
         $state['charge'] = array('amount' => commerce_currency_decimal_to_amount($response['amount'], $response['currency_code']), 'currency_code' => $response['currency_code']);
     }
     if (!empty($response['commerce_order_id'])) {
         $state['order'] = commerce_order_load($response['commerce_order_id']);
     }
     $customer = array();
     if (!empty($response['customer_ref'])) {
         $customer['uid'] = $response['customer_ref'];
     } elseif (!empty($response['x_cust_id'])) {
         $customer['uid'] = $response['x_cust_id'];
     }
     if (!empty($response['client_email'])) {
         $customer['mail'] = $response['client_email'];
     }
     if (!empty($customer)) {
         $state['customer'] = (object) $customer;
     }
     // Add billing address per response parameters
     $state['billing_address'] = $this->createResponseBillingAddress($response);
     // Allow others to alter
     $alter_context = array('response' => $response);
     $this->controller->alter('hpp_relay_response_state', $state, $alter_context);
     // Resolve state
     $this->controller->resolvePaymentState($state);
     return $state;
 }