From 4393a39934db1656423bacef462bc5ec13523124 Mon Sep 17 00:00:00 2001 From: Vadim Eremeev Date: Tue, 23 Oct 2012 00:20:56 +0800 Subject: [PATCH] removed debug line --- .../modules/wps/commerce_paypal_wps.module | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sites/all/modules/commerce_paypal/modules/wps/commerce_paypal_wps.module b/sites/all/modules/commerce_paypal/modules/wps/commerce_paypal_wps.module index 4df9c8f..7470cbb 100644 --- a/sites/all/modules/commerce_paypal/modules/wps/commerce_paypal_wps.module +++ b/sites/all/modules/commerce_paypal/modules/wps/commerce_paypal_wps.module sites/all/modules/commerce_paypal/modules/wps/commerce_paypal_wps.module @@ -44,6 +44,7 @@ function commerce_paypal_wps_default_settings() { 'server' => 'sandbox', 'payment_action' => 'sale', 'ipn_logging' => 'notification', + 'cart_type' => 'summary', 'show_payment_instructions' => FALSE, 'ipn_create_billing_profile' => FALSE, ); @@ -65,6 +66,16 @@ function commerce_paypal_wps_settings_form($settings = array()) { '#default_value' => $settings['business'], '#required' => TRUE, ); + $form['cart_type'] = array( + '#type' => 'radios', + '#title' => t('Select the cart options'), + '#options' => array( + 'summary' => t('Send a summary of cart contents as one line item to PayPal.'), + 'itemized' => t('Send itemized list of cart items to PayPal.'), + ), + '#default_value' => $settings['cart_type'], + ); + $form['currency_code'] = array( '#type' => 'select', '#title' => t('Default currency'), @@ -381,8 +392,89 @@ function commerce_paypal_wps_order_form($form, &$form_state, $order, $settings) // Ensure a default value for the payment_method setting. $settings += array('payment_method' => ''); +// If an itemized shopping cart should be sent to PayPal. + if ($settings['cart_type'] == 'itemized') { + $wrapper = entity_metadata_wrapper('commerce_order', $order); + $shipping_cost = 0; + $count = 1; + foreach ($wrapper->commerce_line_items as $delta => $line_item_wrapper) { + if (in_array($line_item_wrapper->type->value(), commerce_product_line_item_types())) { + if ($line_item_wrapper->commerce_unit_price->amount->value() > 0) { + $line_item_price = commerce_currency_amount_to_decimal($line_item_wrapper->commerce_unit_price->amount->value(), $line_item_wrapper->commerce_unit_price->currency_code->value()); + $data['item_name_' . $count] = $line_item_wrapper->commerce_product->title->value(); + $data['amount_' . $count] = $line_item_price; + $data['quantity_' . $count] = round($line_item_wrapper->quantity->value()); + $data['currency_code_' . $count] = $line_item_wrapper->commerce_unit_price->currency_code->value(); + + // add product attributes + if (module_exists('commerce_option')) { + $product_options = ''; + $options_count = 0; + $iterated_line_item = $line_item_wrapper->value(); + $options = commerce_option_load_by_line_item($iterated_line_item->line_item_id); + + foreach ($options as $option) { + field_attach_prepare_view('commerce_option', array($option->option_id => $option), 'attribute_view'); + $option_view = field_attach_view('commerce_option', $option, 'attribute_view'); + $product_options .= check_markup(drupal_render($option_view), 'crop_html'); + $product_options = explode(':', $product_options); + $data['on'.$options_count.'_' . $count] = $product_options[0]; + $data['os'.$options_count.'_' . $count] = $product_options[1]; + $options_count++; + } + } + $count++; + } + } + if ($line_item_wrapper->type->value() == 'shipping') { + $shipping_cost += commerce_currency_amount_to_decimal($line_item_wrapper->commerce_unit_price->amount->value(), $line_item_wrapper->commerce_unit_price->currency_code->value()); + } + } + if ($shipping_cost != 0) { + $data['shipping_1'] = $shipping_cost; + } + + + // Pass billing information to Paypal. + $paypal_address = ''; + if ($wrapper->commerce_customer_billing->commerce_customer_address->value()) { + $paypal_address = $wrapper->commerce_customer_billing->commerce_customer_address->value(); + } + elseif ($wrapper->commerce_customer_shipping->commerce_customer_address->value()) { + $paypal_address = $wrapper->commerce_customer_shipping->commerce_customer_address->value(); + } + + // Set address in Paypal data array. + if (is_array($paypal_address)) { + if (!$paypal_address['first_name'] && !$paypal_address['last_name']) { + $names = explode(' ', $paypal_address['name_line']); + $paypal_address['first_name'] = $names[0]; + $paypal_address['last_name'] = $names[1]; + } + + $data['first_name'] = $paypal_address['first_name']; + $data['last_name'] = $paypal_address['last_name']; + $data['address1'] = $paypal_address['thoroughfare']; + $data['address2'] = $paypal_address['premise']; + $data['city'] = $paypal_address['locality']; + $data['state'] = $paypal_address['administrative_area']; + $data['zip'] = $paypal_address['postal_code']; + $data['country'] = $paypal_address['country']; + $data['email'] = $wrapper->mail->value(); + } + } + else { + // Define a single item in the cart representing the whole order + $data = array( + 'amount_1' => commerce_currency_amount_to_decimal(commerce_currency_convert($amount, $order_currency_code, $currency_code), $currency_code), + 'item_name_1' => t('Order @order_number at @store', array('@order_number' => $order->order_number, '@store' => variable_get('site_name', url('', array('absolute' => TRUE))))), + 'on0_1' => t('Product count'), + 'os0_1' => commerce_line_items_quantity($wrapper->commerce_line_items, commerce_product_line_item_types()), + ); + } + // Build the data array that will be translated into hidden form values. - $data = array( + $data = (array) $data + array( // Specify the checkout experience to present to the user. 'cmd' => '_cart', @@ -423,11 +515,6 @@ function commerce_paypal_wps_order_form($form, &$form_state, $order, $settings) // Use the timestamp to generate a unique invoice number 'invoice' => commerce_paypal_ipn_invoice($order), - // Define a single item in the cart representing the whole order - 'amount_1' => commerce_currency_amount_to_decimal(commerce_currency_convert($amount, $order_currency_code, $currency_code), $currency_code), - 'item_name_1' => t('Order @order_number at @store', array('@order_number' => $order->order_number, '@store' => variable_get('site_name', url('', array('absolute' => TRUE))))), - 'on0_1' => t('Product count'), - 'os0_1' => commerce_line_items_quantity($wrapper->commerce_line_items, commerce_product_line_item_types()), ); // Allow modules to alter parameters of the API request.