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;
 }
Ejemplo n.º 2
0
/**
 * Specifies the PDF template name to be used for a given order.
 *
 * @param object $order
 *   The order the PDF invoice should be generated for.
 *
 * @return string
 *   The PDF template name string defined in
 *   hook_commerce_billy_pdf_templates().
 */
function hook_commerce_billy_pdf_get_order_template($order)
{
    $template_name = 'default';
    $line_item_id_0 = $order->commerce_line_items['und'][0]['line_item_id'];
    $line_item_0 = commerce_line_item_load($line_item_id_0);
    if ($line_item_0->type == 'my_line_item_type') {
        // We checked for the type of the order's first line item.
        $template_name = 'my_template';
    }
    return $template_name;
}
 /**
  * Replace with your own cart here to try out
  * different promotions, tax, shipping, etc. 
  * 
  * @param merchantID
  * @param awsAccessKeyID
  */
 private function getCartXML($merchantID, $awsAccessKeyID)
 {
     $xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" . "<Order xmlns=\"http://payments.amazon.com/checkout/2008-06-15/\">" . "    <ClientRequestId>123457</ClientRequestId>" . "    <Cart>" . "    <Items>";
     global $user;
     $cart = commerce_cart_order_load($user->uid);
     foreach ($cart->commerce_line_items['und'] as $line_item_id) {
         $line_item = commerce_line_item_load($line_item_id);
         list($quantity) = explode('.', $line_item->quantity);
         $xml .= "      <Item>" . "         <SKU>" . $line_item->line_item_label . "</SKU>" . "         <MerchantId>" . $merchantID . "</MerchantId>" . "         <Title>" . $line_item->line_item_label . "</Title>" . "         <Description>" . $line_item->line_item_label . "</Description>" . "         <Price>" . "            <Amount>" . (0 + $line_item->commerce_unit_price['und'][0]['amount'] / 100) . "</Amount>" . "            <CurrencyCode>INR</CurrencyCode>" . "         </Price>" . "         <Quantity> " . $quantity . "</Quantity>" . "         <Weight>" . "            <Amount>.5</Amount>" . "            <Unit>kg</Unit>" . "         </Weight>" . "         <Category>Tshirts</Category>" . "      </Item>";
     }
     $xml .= "    </Items>" . "    </Cart>" . "</Order>";
     return $xml;
 }
/**
 * Returns the initial usage for a license's usage group.
 *
 * Initial usage is registered when the license is first activated.
 * The first hook to provide initial usage wins. If no hooks are found,
 * the 'initial_quantity' key on group info.
 *
 * Right now initial usage is only registered for gauge usage groups.
 *
 * @param $license
 *   The license entity.
 * @param $group_name
 *   The name of the usage group, as defined in $license->usageGroups().
 *
 * @return
 *   The numeric quantity.
 */
function hook_commerce_license_billing_initial_usage($license, $group_name)
{
    if ($group_name == 'environments') {
        // In this example the initial usage is stored on the line item because
        // the customer selected the desired number of environments during checkout.
        $query = new EntityFieldQuery();
        $query->entityCondition('entity_type', 'commerce_line_item')->entityCondition('bundle', 'recurring', '<>')->fieldCondition('commerce_license', 'target_id', $license->license_id);
        $result = $query->execute();
        if ($result) {
            $line_item_id = key($result['commerce_line_item']);
            $line_item = commerce_line_item_load($line_item_id);
            $line_item_wrapper = entity_metadata_wrapper('commerce_line_item', $line_item);
            return $line_item_wrapper->field_user_environments->value();
        }
    }
}
Ejemplo n.º 5
0
 /**
  * Default constructor for the Commerce Line Item object. Do not call this
  * class directly. Create a separate class for each line item type and use
  * its constructor.
  *
  * You can also pass a second argument, and it will be interpreted as
  * order_id. It is used for creating a new line item.
  *
  * @param int $line_item_id
  *   Product id if an existing product is to be loaded.
  */
 public function __construct($line_item_id = NULL)
 {
     $args = func_get_args();
     array_shift($args);
     $order_id = array_shift($args);
     if (is_null($order_id)) {
         $order_id = 0;
     }
     if (!is_null($line_item_id) && is_numeric($line_item_id)) {
         $line_item = commerce_line_item_load($line_item_id);
         if (!$line_item) {
             $this->setErrors("Line item with id {$line_item_id} does not exist.");
             $this->setInitialized(FALSE);
             return;
         }
     } else {
         $class = new \ReflectionClass(get_called_class());
         $type = Utils::makeSnakeCase($class->getShortName());
         $line_item = commerce_line_item_new($type, $order_id);
     }
     parent::__construct($line_item);
 }
Ejemplo n.º 6
0
  <td align="left"  width="{$widths['0']}" >DESC</td>
  <td align="right" width="{$widths['1']}" >Price</td>
  <td align="right" width="{$widths['2']}" > no vat </td>
  <td align="right" width="{$widths['3']}" > Qté</td>
  <td align="right" width="{$widths['4']}" > Total HT</td>
</tr>
HERE;
$line_items = $o->commerce_line_items[LANGUAGE_NONE];
$i = -1;
if (!empty($line_items)) {
    $line_items_notes = $o->field_product_discount_notes[LANGUAGE_NONE];
    //print_r($line_items_notes[1]['value']);
    foreach ($line_items as $line_item) {
        $i++;
        //$l = "<pre>".print_r( commerce_line_item_load( $line_item['line_item_id'] ) , true);
        $l = commerce_line_item_load($line_item['line_item_id']);
        // get line items notes and display them next to each line item
        $single_price = number_format($l->commerce_unit_price['und'][0]['amount'] / 100, 2, '.', '');
        $quantity = number_format($l->quantity, 0);
        $total = $quantity * $single_price;
        $total = number_format($total, 2, '.', '');
        $line_item_note = "";
        if (isset($line_items_notes[$i])) {
            $val = $line_items_notes[$i]['value'];
            $line_item_note = " " . $val;
        }
        // get global invoice line item checkbox value and set it
        if ($o->field_in_bond[LANGUAGE_NONE][0]['value'] == 1 && $l->line_item_label != "DISCOUNT" && $l->line_item_label != "COMMISSION" && $l->line_item_label != "SHIPPING") {
            $line_item_note .= " IN BOND";
        }
        // get product title out of line item label aka SKU