示例#1
0
/**
 * Allows modules to confirm that an order may proceed to checkout.
 *
 * If any implementation of this hook returns TRUE, the given order can proceed
 * to checkout. However, if no implementations of this hook exist and return
 * TRUE, the checkout router will simply redirect away to the front page.
 *
 * @param $order
 *   The order being confirmed for checkout.
 *
 * @return
 *   Boolean value indicating whether or not the order can proceed to checkout.
 */
function hook_commerce_checkout_order_can_checkout($order)
{
    // Allow orders with one or more product line items to proceed to checkout.
    // If there are no line items on the order, redirect away.
    $wrapper = entity_metadata_wrapper('commerce_order', $order);
    if (commerce_line_items_quantity($wrapper->commerce_line_items, commerce_product_line_item_types()) > 0) {
        return TRUE;
    }
}
/**
 * Obtenir tous les produits d'une commande en drupal commerce
 * @param $order
 * @return array
 */
function commerce_get_products_by_order($order)
{
    foreach (entity_metadata_wrapper('commerce_order', $order)->commerce_line_items as $delta => $line_item_wrapper) {
        if (in_array($line_item_wrapper->type->value(), commerce_product_line_item_types())) {
            $product_ids[] = $line_item_wrapper->commerce_product->raw();
        }
    }
    return $product_ids;
}
 public function submitFormCharge($payment_method, $pane_form, $pane_values, $order, $charge)
 {
     $config = array();
     $shipping_array = array();
     $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
     $billing_address = $order_wrapper->commerce_customer_billing->commerce_customer_address->value();
     $order_array = $order_wrapper->commerce_order_total->value();
     $default_currency = commerce_default_currency();
     $amountCents = number_format(commerce_currency_convert($charge['amount'], $order_array['currency_code'], $default_currency), 0, '', '');
     $config['authorization'] = $payment_method['settings']['private_key'];
     $config['mode'] = $payment_method['settings']['mode'];
     $currency_code = $order_array['currency_code'];
     $i = 0;
     $config['postedParam'] = array('email' => $order->mail, 'value' => $amountCents, 'currency' => $default_currency, 'trackId' => $order->order_id, 'card' => array('name' => "{$billing_address['first_name']} {$billing_address['last_name']}", 'billingDetails' => array('addressLine1' => $billing_address['thoroughfare'], 'addressLine2' => $billing_address['premise'], 'postcode' => $billing_address['postal_code'], 'country' => $billing_address['country'], 'city' => $billing_address['locality'])));
     $products = null;
     foreach ($order_wrapper->commerce_line_items as $delta => $line_item_wrapper) {
         $product_id = $line_item_wrapper->commerce_product->raw();
         $product = commerce_product_load($product_id);
         $price = commerce_product_calculate_sell_price($product);
         $sell_price = number_format(commerce_currency_amount_to_decimal($price['amount'], $price['currency_code']), 2, '.', '');
         // Add the line item to the return array.
         $products[$i] = array('productName' => commerce_line_item_title($line_item_wrapper->value()), 'price' => $sell_price, 'quantity' => round($line_item_wrapper->quantity->value()), 'sku' => '');
         // If it was a product line item, add the SKU.
         if (in_array($line_item_wrapper->type->value(), commerce_product_line_item_types())) {
             $products[$i]['sku'] = $line_item_wrapper->line_item_label->value();
         }
         $i++;
     }
     if ($products && !empty($products)) {
         $config['postedParam']['products'] = $products;
     }
     if (module_exists('commerce_shipping') && !empty($order_wrapper->commerce_customer_shipping->commerce_customer_address)) {
         $shipping_address = $order_wrapper->commerce_customer_shipping->commerce_customer_address->value();
         // Add the shipping address parameters to the request.
         $shipping_array = array('addressLine1' => $shipping_address['thoroughfare'], 'addressLine2' => $shipping_address['premise'], 'postcode' => $shipping_address['postal_code'], 'country' => $shipping_address['country'], 'city' => $shipping_address['locality']);
         $config['postedParam']['shippingDetails'] = $shipping_array;
     }
     if ($payment_method['settings']['payment_action'] == COMMERCE_CREDIT_AUTH_CAPTURE) {
         $config = array_merge_recursive($this->_captureConfig($payment_method), $config);
     } else {
         $config = array_merge_recursive($this->_authorizeConfig($payment_method), $config);
     }
     return $config;
 }
<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
global $user;
$quantity = 0;
$total = '$0.00';
$order = commerce_cart_order_load($user->uid);
if ($order) {
    $wrapper = entity_metadata_wrapper('commerce_order', $order);
    $line_items = $wrapper->commerce_line_items;
    $quantity = commerce_line_items_quantity($line_items, commerce_product_line_item_types());
    $order_total = commerce_line_items_total($line_items);
}
?>
  <a href="<?php 
print url('cart');
?>
">
    <span>
      <i class="fa fa-shopping-cart"></i>
    </span> 
    <?php 
print $quantity;
?>
 item(s) - 
    <?php 
if ($order) {
    print commerce_currency_format($order_total['amount'], $order_total['currency_code']);
 /**
  * Returns a payment form to be used during checkout or elsewhere
  */
 public function paymentForm($form, &$form_state, &$request_state = array())
 {
     if (!$this->isValid()) {
         return $form;
     }
     // Resolve state
     $this->controller->resolvePaymentState($request_state);
     // Get plugin settings
     $settings = $this->getSettings();
     // Set transaction type based on settings.
     $txn_type = $this->controller->getSettings('txn_type');
     $x_type = 'AUTH_CAPTURE';
     if ($txn_type == COMMERCE_CREDIT_AUTH_ONLY) {
         $x_type = 'AUTH_ONLY';
     }
     // Initialize variables
     $order = $request_state['order'];
     $description = array();
     $card = $request_state['card'];
     $charge = $request_state['charge'];
     $cancel_path = '';
     // Order data
     if (!empty($order)) {
         $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
         $cancel_path = 'checkout/' . $order->order_id . '/payment/back/' . $order->data['payment_redirect_key'];
         // Build a description for the order.
         /** @todo: create details for x_line_item instead of x_description which is not used ***/
         foreach ($order_wrapper->commerce_line_items as $delta => $line_item_wrapper) {
             if (in_array($line_item_wrapper->type->value(), commerce_product_line_item_types())) {
                 $description[] = round($line_item_wrapper->quantity->value(), 2) . 'x ' . $line_item_wrapper->line_item_label->value();
             }
         }
     }
     // Card data
     if (!empty($card)) {
         if (empty($cancel_path) && !empty($card->uid)) {
             $cancel_path = 'user/' . $card->uid . '/cards';
         }
     }
     // Resolve charge - convert to decimal, fallback to 0
     if (!empty($charge['amount'])) {
         $charge['amount_decimal'] = commerce_currency_amount_to_decimal($charge['amount'], $charge['currency_code']);
     } else {
         // Fallback to Zero dollar authorization
         $x_type = 'AUTH_ONLY';
         $charge = array('amount' => 0, 'amount_decimal' => 0, 'currency_code' => isset($charge['currency_code']) ? $charge['currency_code'] : commerce_default_currency());
     }
     // Build submit data
     $data = array('x_login' => $settings['page_id'], 'x_type' => $x_type, 'x_amount' => !empty($charge['amount_decimal']) ? number_format($charge['amount_decimal'], 2, '.', '') : '0', 'x_currency_code' => $charge['currency_code'], 'x_show_form' => 'PAYMENT_FORM', 'x_customer_ip' => ip_address(), 'x_receipt_link_method' => 'AUTO-POST', 'x_receipt_link_url' => $this->getAutoPostURL(), 'x_relay_response' => 'TRUE', 'x_relay_url' => $this->getRelayURL(), 'commerce_payment_method' => $this->controller->payment_instance['instance_id']);
     // Conditional fields
     // Order info
     if (!empty($order->order_id)) {
         $data += array('commerce_order_id' => $order->order_id, 'x_invoice_num' => $order->order_number, 'x_description' => substr(implode(', ', $description), 0, 255));
     }
     // Customer
     if (!empty($request_state['customer']->uid)) {
         $data['x_cust_id'] = substr($request_state['customer']->uid, 0, 20);
         // Set customer_ref similar to web service
         // - x_po_num is passed to customer_ref in response
         $data['x_po_num'] = $data['x_cust_id'];
     }
     if (!empty($request_state['customer']->mail)) {
         $data['x_email'] = substr($request_state['customer']->mail, 0, 255);
     }
     // Billing address
     if (!empty($request_state['billing_address'])) {
         $billing_address = $request_state['billing_address'];
         $data += array('x_first_name' => substr($billing_address['first_name'], 0, 50), 'x_last_name' => substr($billing_address['last_name'], 0, 50), 'x_company' => substr($billing_address['organisation_name'], 0, 20), 'x_address' => substr($billing_address['street_line'], 0, 28), 'x_city' => substr($billing_address['locality'], 0, 20), 'x_state' => $this->controller->getStateName($billing_address['administrative_area'], $billing_address['country']), 'x_zip' => substr($billing_address['postal_code'], 0, 9), 'x_country' => $this->controller->getCountryName($billing_address['country']));
     }
     // Allow other plugins and modules to alter
     $this->controller->alter('hpp_post_data', $data, $request_state);
     // Create the hash fingerprint
     $hmac_encryption_type = !empty($settings['hmac_encryption_type']) ? $settings['hmac_encryption_type'] : 'md5';
     $data['x_fp_timestamp'] = REQUEST_TIME;
     $data['x_fp_sequence'] = mt_rand(1, 1000);
     $hash_seeds = array($data['x_login'], $data['x_fp_sequence'], $data['x_fp_timestamp'], $data['x_amount'], $data['x_currency_code']);
     $data['x_fp_hash'] = hash_hmac($hmac_encryption_type, implode('^', $hash_seeds), $settings['transaction_key']);
     // Log "request"
     $log_settings = $this->controller->getSettings('log');
     if ($log_settings['request'] == 'request') {
         $this->controller->log('First Data GGe4 HPP submit data', $data);
     }
     // Set post url and transaction mode
     $submit_url = $this->getServerUrl();
     $data['x_test_request'] = $this->isTestMode() ? 'TRUE' : 'FALSE';
     // Build form elements
     $form['#action'] = $submit_url;
     $form['#method'] = "post";
     foreach ($data as $name => $value) {
         $form[$name] = array('#type' => 'hidden', '#value' => $value);
     }
     $form['actions'] = array('#type' => 'actions', '#weight' => 50);
     $form['actions']['submit'] = array('#type' => 'submit', '#value' => t('Continue'));
     $form['actions']['cancel'] = array('#type' => 'link', '#title' => t('Cancel'), '#href' => $cancel_path, '#options' => array('absolute' => TRUE, 'html' => FALSE));
     // Allow other plugins and modules to alter
     $this->controller->alter('hpp_payment_form', $form, $request_state);
     return $form;
 }
/**
 * Overrides theme_menu_link().
 */
function cubert_menu_link(array $variables)
{
    $element = $variables['element'];
    $sub_menu = '';
    $team_menus = array('menu-teampayment-navigation', 'user-menu');
    $is_user_menu = !empty($element['#original_link']['menu_name']) && $element['#original_link']['menu_name'] == 'user-menu';
    // On primary navigation menu, class 'active' is not set on active menu item.
    // @see https://drupal.org/node/1896674
    if (($element['#href'] == $_GET['q'] || $element['#href'] == '<front>' && drupal_is_front_page()) && empty($element['#localized_options']['language'])) {
        $element['#attributes']['class'][] = 'active';
    }
    // Add FontAwesome icons based on link function
    $icon = "";
    if (!empty($element['#original_link']['menu_name']) && $element['#original_link']['menu_name'] == 'user-menu') {
        $title = !empty($element['#title']) ? $element['#title'] : '';
        $element['#attributes']['class'][] = 'user-action';
        if ($element['#href'] == 'user') {
            global $user;
            $element['#title'] = $user->name;
            $icon = "<i class='fa fa-user'></i>&nbsp;&nbsp;";
            $element['#attributes']['class'][] = 'user-action-account';
        }
        if ($element['#href'] == 'user/logout') {
            $icon = "<i class='fa fa-sign-out'></i> ";
            $element['#attributes']['class'][] = 'user-action-logout';
        }
        if ($element['#href'] == 'cart/my') {
            $icon = "<i class='fa fa-shopping-cart'></i> ";
            $element['#attributes']['class'][] = 'user-action-cart';
            global $user;
            $quantity = 0;
            if ($order = commerce_cart_order_load($user->uid)) {
                $wrapper = entity_metadata_wrapper('commerce_order', $order);
                $quantity = commerce_line_items_quantity($wrapper->commerce_line_items, commerce_product_line_item_types());
            }
            $title = $title . " (" . $quantity . ")";
            $element['#title'] = $title;
        }
    }
    $element['#localized_options'] += array('html' => TRUE);
    if (!empty($element['#original_link']['menu_name']) && ($element['#original_link']['menu_name'] == 'menu-teampayment-navigation' || $element['#original_link']['menu_name'] == 'user-menu')) {
        if ($element['#href'] == 'dashboard') {
            $icon = "<i class='fa fa-home'></i> ";
            $element['#title'] = '';
            $element['#attributes']['class'][] = 'main-nav-home';
        }
        $commerce_links = array('products', 'cart', 'cart/my');
        if (in_array($element['#href'], $commerce_links)) {
            if (module_exists('team_payment')) {
                $group = team_payment_get_group_info();
                $products = team_payment_get_all_commerce_products($group);
                if (count($products) == 0) {
                    return;
                }
            }
        }
    }
    if ($element['#below']) {
        // Prevent dropdown functions from being added to management menu so it
        // does not affect the navbar module.
        if ($element['#original_link']['menu_name'] == 'management' && module_exists('navbar')) {
            $sub_menu = drupal_render($element['#below']);
        } elseif (!empty($element['#original_link']['depth']) && in_array($element['#original_link']['menu_name'], $team_menus)) {
            $depth = $element['#original_link']['depth'];
            // Add our own wrapper.
            if ($depth == 2 && !$is_user_menu || $depth == 1 && $is_user_menu) {
                unset($element['#below']['#theme_wrappers']);
                $sub_menu = '<ul class="dropdown-menu">' . drupal_render($element['#below']) . '</ul>';
                // Generate as standard dropdown.
                $element['#title'] .= ' <i class="fa fa-chevron-down"></i>';
                $element['#attributes']['class'][] = 'dropdown';
                $element['#localized_options']['html'] = TRUE;
                // Set dropdown trigger element to # to prevent inadvertant page loading
                // when a submenu link is clicked.
                $element['#localized_options']['attributes']['data-target'] = '#';
                $element['#localized_options']['attributes']['class'][] = 'dropdown-toggle';
                $element['#localized_options']['attributes']['data-toggle'] = 'dropdown';
            } else {
                if ($is_user_menu && ($depth = 2)) {
                    unset($element['#below']['#theme_wrappers']);
                    $sub_menu = '<ul class="menu__nested">' . drupal_render($element['#below']) . '</ul>';
                }
            }
        }
    }
    $prefix = "";
    $suffix = "";
    $output = l($icon . $element['#title'], $element['#href'], $element['#localized_options']);
    if (!empty($element['#original_link']['depth']) && $element['#original_link']['depth'] == 2) {
        //$prefix = '<li role="separator" class="divider"></li>';
        //$suffix = '<li role="separator" class="divider"></li>';
    }
    return $prefix . '<li ' . drupal_attributes($element['#attributes']) . '>' . $output . $sub_menu . "</li>" . $suffix . "\n";
}
示例#7
0
 /**
  * Perform a Contract Charges query call to Amazon API.
  *
  * @param commerce_order $order
  * @internal param $action
  * @return bool|object
  */
 public function setContractCharges($order)
 {
     $params = $this->constructParams('SetContractCharges');
     $discount_amount = $shipping_amount = $tax_amount = 0;
     $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
     $total_price = commerce_price_wrapper_value($order_wrapper, 'commerce_order_total');
     $tax_amount = commerce_tax_total_amount($total_price['data']['components'], FALSE, $total_price['currency_code']);
     foreach ($order_wrapper->commerce_line_items as $line_item_wrapper) {
         if (!in_array($line_item_wrapper->type->value(), commerce_product_line_item_types())) {
             // Shipping line items.
             if ($line_item_wrapper->type->value() == 'shipping') {
                 $shipping_amount += $line_item_wrapper->commerce_total->amount->value();
                 $shipping_currency_code = $line_item_wrapper->commerce_total->currency_code->value();
             }
             // We assume that a negative amount is a discount or similar.
             if ($line_item_wrapper->commerce_total->amount->value() < 0) {
                 $discount_amount += $line_item_wrapper->commerce_total->amount->value();
                 $discount_currency_code = $line_item_wrapper->commerce_total->currency_code->value();
             }
         }
     }
     // Providing taxes and shipping information, even if it's 0.
     $params['Charges.Tax.Amount'] = commerce_currency_amount_to_decimal($tax_amount, $total_price['currency_code']);
     $params['Charges.Tax.CurrencyCode'] = $total_price['currency_code'];
     $shipping_currency_code = !empty($shipping_currency_code) ? $shipping_currency_code : $total_price['currency_code'];
     $params['Charges.Shipping.Amount'] = commerce_currency_amount_to_decimal($shipping_amount, $shipping_currency_code);
     $params['Charges.Shipping.CurrencyCode'] = $shipping_currency_code;
     if ($discount_amount != 0) {
         $params['Charges.Promotions.Promotion.1.PromotionId'] = t('Discount');
         $params['Charges.Promotions.Promotion.1.Description'] = t('Discount');
         $params['Charges.Promotions.Promotion.1.Discount.Amount'] = abs(commerce_currency_amount_to_decimal($discount_amount, $discount_currency_code));
         $params['Charges.Promotions.Promotion.1.Discount.CurrencyCode'] = $discount_currency_code;
     }
     // Allow other modules to alter the params on demand.
     drupal_alter('commerce_cba_contract_charges', $params, $order);
     if (!empty($params)) {
         $query = $this->prepareQuery($params);
         return $this->query($query);
     }
 }