uc_google_checkout_merchant_calculation

  1. drupal
    1. 6 uc_google_checkout.pages.inc function
    2. 7 uc_google_checkout.pages.inc function
Drupal 7 uc_google_checkout_merchant_calculation()

Verifies Google Checkout shipping calculation.

1 string reference to 'uc_google_checkout_merchant_calculation'

File

sites/all/modules/ubercart/payment/uc_google_checkout/uc_google_checkout.pages.inc, line 59
Callback functions to handle responses from Google Checkout.

Code

function uc_google_checkout_merchant_calculation() {
  // Error checking.
  if (variable_get('uc_google_checkout_mode', 'checkout') == 'checkout') {
    $merchant_id = variable_get('uc_google_checkout_merchant_id', '');
    $merchant_key = variable_get('uc_google_checkout_merchant_key', '');
  }
  else {
    $merchant_id = variable_get('uc_google_checkout_test_merchant_id', '');
    $merchant_key = variable_get('uc_google_checkout_test_merchant_key', '');
  }

  if ($_SERVER['PHP_AUTH_USER'] != $merchant_id || $_SERVER['PHP_AUTH_PW'] != $merchant_key) {
    watchdog('google', 'HTTP Authorization header does not match settings.', array(), WATCHDOG_ERROR);
    return MENU_ACCESS_DENIED;
  }

  $input = file_get_contents('php://input');
  $calc = new SimpleXMLElement($input);

  if ($calc->getName() != 'merchant-calculation-callback') {
    uc_google_checkout_notification_error();
  }

  $order = new stdClass();
  $order->uid = 0;

  $products = array();
  foreach ($calc->{'shopping-cart'}->items->item as $item) {
    list($nid, $model) = explode('|', $item->{'merchant-item-id'});
    $node = node_load($nid);
    $node->model = (string) $item->{'item-name'};
    $node->title = (string) $item->{'item-description'};
    $node->price = (string) $item->{'unit-price'};
    $node->qty = (string) $item->quantity;
    $node->weight = (string) $item->{'item-weight'};

    $products[] = $node;
  }

  $order->products = $products;

  $address = $calc->calculate->addresses->{'anonymous-address'};

  $country = uc_get_country_data(array('country_iso_code_2' => $address->{'country-code'}));
  $order->delivery_country = $country[0]['country_id'];
  $order->delivery_city = $address->city;
  $zone_id = db_query("SELECT zone_id FROM {uc_zones} WHERE zone_code = :code AND zone_country_id = :cid", array(':code' => $address->region, ':cid' => $order->delivery_country))->fetchField();
  $order->delivery_zone = $zone_id;
  $order->delivery_postal_code = $address->{'postal-code'};

  $order->order_total = uc_order_get_total($order, TRUE);

  $methods = module_invoke_all('uc_shipping_method');
  module_load_include('inc', 'uc_quote', 'uc_quote.pages');
  $quote_data = uc_quote_assemble_quotes($order);
  $results = '';
  foreach ($quote_data as $method_id => $options) {
    foreach ($options as $accsrl => $data) {
      $results .= '<result shipping-name="' . check_plain($methods[$method_id]['quote']['accessorials'][$accsrl]) . '" address-id="' . $address['id'] . '">';

      $results .= '<shipping-rate currency="' . variable_get('uc_currency_code', 'USD') . '">' . $data['rate'] . '</shipping-rate>';
      $results .= '<shippable>true</shippable>';

      $results .= '</result>';
    }
  }

  if ($results) {
    $response = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
    $response .= '<merchant-calculation-results xmlns="http://checkout.google.com/schema/2">';
    $response .= '<results>';
    $response .= $results;
    $response .= '</results>';
    $response .= '</merchant-calculation-results>';

    drupal_add_http_header('Status', '200 OK');
    print $response;
    exit();
  }

  drupal_add_http_header('Status', '204 No Content');
  exit();
  // So there!
}