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; }
/** * Generate payment token. * * @param $order * The order transaction * @param $payment_method * The payment method used * * @return array * Payment token form array */ public function generatePaymentToken($order, $payment_method) { $config = array(); $shipping_address_config = 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)) { $order_id = $order->order_id; $default_currency = commerce_default_currency(); $amount_cents = number_format(commerce_currency_convert($order->commerce_order_total[LANGUAGE_NONE][0]['amount'], $order_array['currency_code'], $default_currency), 0, '', ''); $secret_key = $payment_method['settings']['private_key']; $mode = $payment_method['settings']['mode']; $timeout = $payment_method['settings']['timeout']; $config['authorization'] = $secret_key; $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[LANGUAGE_NONE][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); } } $billing_address_config = 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. $shipping_address_config = 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' => $amount_cents, 'trackId' => $order_id, 'currency' => $default_currency, 'description' => 'Order number::' . $order_id, 'shippingDetails' => $shipping_address_config, 'products' => $products, 'card' => array('billingDetails' => $billing_address_config))); $api = CheckoutApi_Api::getApi(array('mode' => $mode)); $payment_token_charge = $api->getPaymentToken($config); $payment_token_array = array('message' => '', 'success' => '', 'eventId' => '', 'token' => ''); if ($payment_token_charge->isValid()) { $payment_token_array['token'] = $payment_token_charge->getId(); $payment_token_array['success'] = TRUE; } else { $payment_token_array['message'] = $payment_token_charge->getExceptionState()->getErrorMessage(); $payment_token_array['success'] = FALSE; $payment_token_array['eventId'] = $payment_token_charge->getEventId(); } } return $payment_token_array; }