You are here

function uc_paypal_ec_checkout in Ubercart 5

Same name and namespace in other branches
  1. 6.2 payment/uc_paypal/uc_paypal.module \uc_paypal_ec_checkout()
  2. 7.3 payment/uc_paypal/uc_paypal.module \uc_paypal_ec_checkout()

File

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

Code

function uc_paypal_ec_checkout($form_id, $form_values) {
  if ($form_values['panes']['payment']['payment_method'] != 'paypal_ec') {
    return;
  }
  $order_id = intval($_SESSION['cart_order']);
  $order = uc_order_load($order_id);
  if ($order === FALSE || uc_order_status_data($order->order_status, 'state') != 'in_checkout') {
    $_SESSION['cart_order'] = NULL;
    unset($_SESSION['cart_order']);
    drupal_goto('cart');
  }
  list($desc, $subtotal) = _uc_paypal_product_details($order->products);
  $country = uc_get_country_data(array(
    'country_id' => $order->billing_country,
  ));
  if ($country === FALSE) {
    $country = array(
      0 => array(
        'country_iso_code_2' => 'US',
      ),
    );
  }
  $nvp_request = array(
    'METHOD' => 'SetExpressCheckout',
    'RETURNURL' => url('cart/echeckout/selected', NULL, NULL, TRUE),
    'CANCELURL' => url('uc_paypal/wps/cancel', NULL, NULL, TRUE),
    'AMT' => uc_currency_format($order->order_total, FALSE, FALSE, '.'),
    'CURRENCYCODE' => variable_get('uc_paypal_wpp_currency', 'USD'),
    'PAYMENTACTION' => variable_get('uc_paypal_wpp_payment_action', 'Sale'),
    'DESC' => substr($desc, 0, 127),
    'INVNUM' => $order->order_id . '-' . time(),
    'REQCONFIRMSHIPPING' => variable_get('uc_paypal_ec_rqconfirmed_addr', 0),
    'ADDROVERRIDE' => 1,
    'BUTTONSOURCE' => 'Ubercart_ShoppingCart_EC_US',
    'NOTIFYURL' => url('uc_paypal/ipn/' . $order->order_id, NULL, NULL, TRUE),
    'SHIPTONAME' => substr($order->delivery_first_name . ' ' . $order->delivery_last_name, 0, 32),
    'SHIPTOSTREET' => substr($order->delivery_street1, 0, 100),
    'SHIPTOSTREET2' => substr($order->delivery_street2, 0, 100),
    'SHIPTOCITY' => substr($order->delivery_city, 0, 40),
    'SHIPTOSTATE' => uc_get_zone_code($order->delivery_zone),
    'SHIPTOCOUNTRYCODE' => $country[0]['country_iso_code_2'],
    'SHIPTOZIP' => substr($order->delivery_postal_code, 0, 20),
    'PHONENUM' => substr($order->delivery_phone, 0, 20),
  );
  if (!uc_cart_is_shippable()) {
    $nvp_request['NOSHIPPING'] = 1;
    unset($nvp_request['ADDROVERRIDE']);
  }
  $nvp_response = uc_paypal_api_request($nvp_request, variable_get('uc_paypal_wpp_server', 'https://api-3t.sandbox.paypal.com/nvp'));
  if ($nvp_response['ACK'] != 'Success') {
    drupal_set_message(t('Error message from PayPal:<br />@message', array(
      '@message' => $nvp_response['L_LONGMESSAGE0'],
    )), 'error');
    drupal_goto('cart/checkout');
  }
  $_SESSION['TOKEN'] = $nvp_response['TOKEN'];
  if (strpos(variable_get('uc_paypal_wpp_server', 'https://api-3t.sandbox.paypal.com/nvp'), 'sandbox') > 0) {
    $sandbox = 'sandbox.';
  }
  header('Location: https://www.' . $sandbox . 'paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=' . $_SESSION['TOKEN']);
  exit;
}