public function generatePaymentToken($order, $payment_method) { global $user; $config = array(); $shippingAddressConfig = null; $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(); $product_line_items = $order->commerce_line_items[LANGUAGE_NONE]; if (isset($order)) { $orderId = $order->order_id; $default_currency = commerce_default_currency(); $amountCents = number_format(commerce_currency_convert($order->commerce_order_total['und'][0]['amount'], $order_array['currency_code'], $default_currency), 0, '', ''); $scretKey = $payment_method['settings']['private_key']; $mode = $payment_method['settings']['mode']; $timeout = $payment_method['settings']['timeout']; $config['authorization'] = $scretKey; $config['mode'] = $mode; $config['timeout'] = $timeout; if ($payment_method['settings']['payment_action'] == 'authorize') { $config = array_merge($config, $this->_authorizeConfig()); } else { $config = array_merge($config, $this->_captureConfig($payment_method)); } $products = array(); if (!empty($product_line_items)) { foreach ($product_line_items as $key => $item) { $line_item[$key] = commerce_line_item_load($item['line_item_id']); $product_id = $line_item[$key]->commerce_product['und'][0]['product_id']; $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, '.', ''); $products[$key] = array('name' => commerce_line_item_title($line_item[$key]), 'sku' => $line_item[$key]->line_item_label, 'price' => $sell_price, 'quantity' => (int) $line_item[$key]->quantity); } } $billingAddressConfig = array('addressLine1' => $billing_address['thoroughfare'], 'addressLine2' => $billing_address['premise'], 'postcode' => $billing_address['postal_code'], 'country' => $billing_address['country'], 'city' => $billing_address['locality']); 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. $shippingAddressConfig = array('addressLine1' => $shipping_address['thoroughfare'], 'addressLine2' => $shipping_address['premise'], 'postcode' => $shipping_address['postal_code'], 'country' => $shipping_address['country'], 'city' => $shipping_address['locality']); } $config['postedParam'] = array_merge($config['postedParam'], array('email' => $order->mail, 'value' => $amountCents, 'trackId' => $orderId, 'currency' => $default_currency, 'description' => 'Order number::' . $orderId, 'shippingDetails' => $shippingAddressConfig, 'products' => $products, 'card' => array('billingDetails' => $billingAddressConfig))); $Api = CheckoutApi_Api::getApi(array('mode' => $mode)); $paymentTokenCharge = $Api->getPaymentToken($config); $paymentTokenArray = array('message' => '', 'success' => '', 'eventId' => '', 'token' => ''); if ($paymentTokenCharge->isValid()) { $paymentTokenArray['token'] = $paymentTokenCharge->getId(); $paymentTokenArray['success'] = true; } else { $paymentTokenArray['message'] = $paymentTokenCharge->getExceptionState()->getErrorMessage(); $paymentTokenArray['success'] = false; $paymentTokenArray['eventId'] = $paymentTokenCharge->getEventId(); } } return $paymentTokenArray; }
/** * Functions as a secondary hook_field_formatter_prepare_view() for price fields, * allowing modules to alter prices prior to display. * * This hook is used by modules like the Product Pricing module that implement * ways to alter prices prior to display. Modules implementing this hook are * currently responsible to make sure they do not alter price data twice on the * same pageload. */ function hook_commerce_price_field_formatter_prepare_view($entity_type, $entities, $field, $instances, $langcode, $items, $displays) { static $calculated_prices = array(); // If this is a single value purchase price field attached to a product... if ($entity_type == 'commerce_product' && $field['field_name'] == 'commerce_price' && $field['cardinality'] == 1) { // Prepare the items for each entity passed in. foreach ($entities as $product_id => $product) { // If this price should be calculated and hasn't been already... if (!empty($displays[$product_id]['settings']['calculation']) && $displays[$product_id]['settings']['calculation'] == 'calculated_sell_price' && empty($calculated_prices[$product_id][$field['field_name']])) { // Replace the data being displayed with data from a calculated price. $items[$product_id] = array(commerce_product_calculate_sell_price($product)); // Keep track of which prices have already been calculated. $calculated_prices[$product_id][$field['field_name']] = TRUE; } } } }
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; }
/** * Find the prices for a given legal status based on the products. */ function devis_get_prices_from_legal_status($legal_status) { $product_key = trois_devis_get_product_from_status($legal_status); $products = commerce_product_load_multiple(array(), array('type' => 'devis')); $keep = $temp = $order = array(); foreach ($products as $key => $product) { if (substr($product->sku, 0, -5) == $product_key) { $price = commerce_product_calculate_sell_price($product); $price_display = commerce_currency_format($price['amount'], $price['currency_code'], $product); $temp[$key] = $price_display; $order[$key] = $price['amount']; } } asort($order, SORT_NUMERIC); foreach ($order as $key => $price) { $keep[$key] = $temp[$key]; } return $keep; }