diff --git a/modules/wps/commerce_paypal_wps.module b/modules/wps/commerce_paypal_wps.module index 157c1cb..e5be361 100644 --- a/modules/wps/commerce_paypal_wps.module +++ b/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', ); } @@ -63,6 +64,15 @@ 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'), @@ -312,8 +322,42 @@ 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(); + $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; + } + } + 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', @@ -354,11 +398,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.