You are here

function uc_paypal_wps_form in Ubercart 5

Same name and namespace in other branches
  1. 6.2 payment/uc_paypal/uc_paypal.module \uc_paypal_wps_form()
  2. 7.3 payment/uc_paypal/uc_paypal.module \uc_paypal_wps_form()
1 string reference to 'uc_paypal_wps_form'
uc_paypal_form_alter in payment/uc_paypal/uc_paypal.module
Implementation of hook_form_alter(). Notice how we alter the checkout review form to post the order to PayPal.

File

payment/uc_paypal/uc_paypal.module, line 1202
Integrates various PayPal payment services and Instant Payment Notifications (IPN) with Ubercart!

Code

function uc_paypal_wps_form($order) {
  $shipping = 0;
  foreach ($order->line_items as $item) {
    if ($item['type'] == 'shipping') {
      $shipping += $item['amount'];
    }
  }
  $tax = 0;
  if (module_exists('uc_taxes')) {
    foreach (uc_taxes_calculate($order) as $tax_item) {
      $tax += $tax_item['amount'];
    }
  }
  $address = variable_get('uc_paypal_wps_address_selection', 'billing');
  $country = uc_get_country_data(array(
    'country_id' => $order->{$address . '_country'},
  ));
  if ($country === FALSE) {
    $country = array(
      0 => array(
        'country_iso_code_2' => 'US',
      ),
    );
  }
  for ($i = 0; $i < strlen($order->{$address . '_phone'}); $i++) {
    if (is_numeric($order->{$address . '_phone'}[$i])) {
      $phone .= $order->{$address . '_phone'}[$i];
    }
  }

  /**
   * night_phone_a: The area code for U.S. phone numbers, or the country code
   *                for phone numbers outside the U.S.
   * night_phone_b: The three-digit prefix for U.S. phone numbers, or the
   *                entire phone number for phone numbers outside the U.S.,
   *                excluding country code.
   * night_phone_c: The four-digit phone number for U.S. phone numbers.
   *                (Not Used for UK numbers)
   **/
  if ($country[0]['country_iso_code_2'] == 'US' || $country[0]['country_iso_code_2'] == 'CA') {
    $phone = substr($phone, -10);
    $phone_a = substr($phone, 0, 3);
    $phone_b = substr($phone, 3, 3);
    $phone_c = substr($phone, 6, 4);
  }
  $data = array(
    // PayPal command variable
    'cmd' => '_cart',
    // IPN control notify URL
    'notify_url' => url('uc_paypal/ipn/' . $order->order_id, NULL, NULL, TRUE),
    // Display information
    'cancel_return' => url('uc_paypal/wps/cancel', NULL, NULL, TRUE),
    'no_note' => 1,
    'no_shipping' => variable_get('uc_paypal_wps_no_shipping', 1),
    'return' => url('uc_paypal/wps/complete/' . $order->order_id, NULL, NULL, TRUE),
    'rm' => 2,
    // Transaction information
    'currency_code' => variable_get('uc_paypal_wps_currency', 'USD'),
    'handling_cart' => uc_currency_format($shipping, FALSE, FALSE, '.'),
    'invoice' => $order->order_id . '-' . time(),
    'tax_cart' => uc_currency_format($tax, FALSE, FALSE, '.'),
    // Shopping cart specific variables
    'business' => variable_get('uc_paypal_wps_email', ''),
    'upload' => 1,
    'lc' => variable_get('uc_paypal_wps_language', 'US'),
    // Prepopulating forms/address overriding
    'address1' => substr($order->{$address . '_street1'}, 0, 100),
    'address2' => substr($order->{$address . '_street2'}, 0, 100),
    'city' => substr($order->{$address . '_city'}, 0, 40),
    'country' => $country[0]['country_iso_code_2'],
    'email' => $order->primary_email,
    'first_name' => substr($order->{$address . '_first_name'}, 0, 32),
    'last_name' => substr($order->{$address . '_last_name'}, 0, 64),
    'state' => uc_get_zone_code($order->{$address . '_zone'}),
    'zip' => $order->{$address . '_postal_code'},
    'night_phone_a' => $phone_a,
    'night_phone_b' => $phone_b,
    'night_phone_c' => $phone_c,
  );
  if (variable_get('uc_paypal_wps_address_override', TRUE)) {
    $data['address_override'] = 1;
  }

  // Account for stores that just want to authorize funds instead of capture.
  if (variable_get('uc_paypal_wps_payment_action', 'Sale') == 'Authorization') {
    $data['paymentaction'] = 'authorization';
  }
  if (variable_get('uc_paypal_wps_submit_method', 'single') == 'itemized') {

    // List individual items
    $i = 0;
    foreach ($order->products as $item) {
      $i++;
      $data['amount_' . $i] = uc_currency_format($item->price, FALSE, FALSE, '.');
      $data['item_name_' . $i] = $item->title;
      $data['item_number_' . $i] = $item->model;
      $data['quantity_' . $i] = $item->qty;

      // PayPal will only display the first two...
      if (is_array($item->data['attributes']) && count($item->data['attributes']) > 0) {
        $o = 0;
        foreach ($item->data['attributes'] as $name => $setting) {
          $data['on' . $o . '_' . $i] = $name;
          $data['os' . $o . '_' . $i] = $setting;
          $o++;
        }
      }
    }
  }
  else {

    // List the whole cart as a single item to account for fees/discounts
    $data['amount_1'] = uc_currency_format($order->order_total - $shipping - $tax, FALSE, FALSE, '.');
    $data['item_name_1'] = t('Order @order_id at @store', array(
      '@order_id' => $order->order_id,
      '@store' => variable_get('uc_store_name', url('<front>', NULL, NULL, TRUE)),
    ));
    $data['on0_1'] = t('Product count');
    $data['os0_1'] = count($order->products);
  }
  $form['#action'] = variable_get('uc_paypal_wps_server', 'https://www.sandbox.paypal.com/cgi-bin/webscr');
  foreach ($data as $name => $value) {
    if (!empty($value)) {
      $form[$name] = array(
        '#type' => 'hidden',
        '#value' => $value,
      );
    }
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => variable_get('uc_paypal_wps_checkout_button', t('Submit Order')),
  );
  return $form;
}