Beispiel #1
0
/**
 * Get current currency
 */
function ichado_currency($order = NULL)
{
    if ($order) {
        return $order->commerce_order_total['und'][0]['currency_code'];
    } else {
        return commerce_default_currency();
    }
}
 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;
 }
 protected function _placeorder($config, $charge, $order, $payment_method)
 {
     //building charge
     $respondCharge = $this->_createCharge($config);
     $transaction = commerce_payment_transaction_new('commerce_checkoutpayment', $order->order_id);
     $transaction->instance_id = $payment_method['instance_id'];
     $transaction->amount = $charge['amount'];
     $transaction->currency_code = $charge['currency_code'];
     $transaction->payload[REQUEST_TIME] = $respondCharge->getCreated();
     $default_currency = commerce_default_currency();
     $amountCents = number_format(commerce_currency_convert($charge['amount'], $charge['currency_code'], $default_currency), 0, '', '');
     $toValidate = array('currency' => $default_currency, 'value' => $amountCents, 'trackId' => $order->order_id);
     $Api = CheckoutApi_Api::getApi(array('mode' => $config['mode']));
     $validateRequest = $Api::validateRequest($toValidate, $respondCharge);
     if ($respondCharge->isValid()) {
         if (preg_match('/^1[0-9]+$/', $respondCharge->getResponseCode())) {
             if ($validateRequest['status']) {
                 $transaction->message = 'Your transaction has been successfully authorized with transaction id : ' . $respondCharge->getId();
             } else {
                 $transaction->message = '';
                 foreach ($validateRequest['message'] as $errormessage) {
                     $transaction->message .= $errormessage . '. ';
                 }
                 $transaction->message .= 'Please contact your merchant.';
             }
             $transaction->status = COMMERCE_PAYMENT_STATUS_PENDING;
             commerce_payment_transaction_save($transaction);
             return true;
         }
         $transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
         drupal_set_message(t('We could not process your card. Please verify your information again or try a different card.'), 'error');
         drupal_set_message(check_plain($respondCharge->getMessage()), 'error');
         $transaction->message = $respondCharge->getRawRespond();
         commerce_payment_transaction_save($transaction);
         return false;
     } else {
         $transaction->status = COMMERCE_PAYMENT_STATUS_FAILURE;
         $transaction->message = $respondCharge->getRawRespond();
         drupal_set_message(t('We received the following error processing your card. Please verify your information again or try a different card.'), 'error');
         drupal_set_message(check_plain($respondCharge->getExceptionState()->getErrorMessage()), 'error');
         commerce_payment_transaction_save($transaction);
         return false;
     }
 }
 function testPaymentMethodViewsDataExport()
 {
     $this->createOrders(10, TRUE);
     $transactions = 0;
     $revenue = 0;
     foreach ($this->orders as $order) {
         $transactions++;
         $revenue += $order['commerce_transaction']->amount;
     }
     $rendered = $this->getRenderedExport('commerce_reports_payment_methods');
     $this->assertCsvHeaders('"Payment method","Transactions","Revenue"', $rendered[0]);
     array_shift($rendered);
     $this->assertEqual(count($rendered), 1, t('The amount of payment methods (%reported) that is reported (%generated) upon is correct.', array('%reported' => count($rendered), '%generated' => 1)));
     // Verify data
     $data = reset($rendered);
     $data = str_getcsv($data);
     $this->assertEqual("Example payment", $data[0]);
     $this->assertEqual($transactions, $data[1]);
     $this->assertEqual(commerce_currency_format($revenue, commerce_default_currency()), $data[2]);
 }
 /**
  * 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;
 }
    ?>
  <div class="multivendor-shipping-vendor-base">
    <?php 
    echo t('Base shipping rate is @amount', array('@amount' => commerce_currency_format($vendor->base_amount, commerce_default_currency())), array('context' => 'Multivendor shipping'));
    ?>
  </div>
  <?php 
}
?>

  <?php 
if ($vendor->package_amount > 0) {
    ?>
  <div class="multivendor-shipping-vendor-package">
    <?php 
    echo t('Each package with @size items costs @amount', array('@size' => $vendor->package_size, '@amount' => commerce_currency_format($vendor->package_amount, commerce_default_currency())), array('context' => 'Multivendor shipping'));
    ?>
  </div>
  <?php 
}
?>

  <?php 
if (!empty($vendor->description)) {
    ?>
  <div class="multivendor-shipping-vendor-description">
    <?php 
    echo t($vendor->description, array(), array('context' => 'Multivendor shipping'));
    ?>
  </div>
  <?php