You are here

function uc_paypal_ec_review in Ubercart 7.3

Same name and namespace in other branches
  1. 5 payment/uc_paypal/uc_paypal.module \uc_paypal_ec_review()
  2. 6.2 payment/uc_paypal/uc_paypal.pages.inc \uc_paypal_ec_review()

Handles the review page for Express Checkout Shortcut Flow.

1 string reference to 'uc_paypal_ec_review'
uc_paypal_menu in payment/uc_paypal/uc_paypal.module
Implements hook_menu().

File

payment/uc_paypal/uc_paypal.pages.inc, line 204
Paypal administration menu items.

Code

function uc_paypal_ec_review() {
  if (!isset($_SESSION['TOKEN']) || ($order = uc_order_load($_SESSION['cart_order'])) == FALSE) {
    unset($_SESSION['cart_order']);
    unset($_SESSION['have_details']);
    unset($_SESSION['TOKEN'], $_SESSION['PAYERID']);
    drupal_set_message(t('An error has occurred in your PayPal payment. Please review your cart and try again.'));
    drupal_goto('cart');
  }
  if (!isset($_SESSION['have_details'][$order->order_id])) {
    $nvp_request = array(
      'METHOD' => 'GetExpressCheckoutDetails',
      'TOKEN' => $_SESSION['TOKEN'],
    );
    $nvp_response = uc_paypal_api_request($nvp_request, variable_get('uc_paypal_wpp_server', 'https://api-3t.sandbox.paypal.com/nvp'));
    $_SESSION['PAYERID'] = $nvp_response['PAYERID'];
    $shipname = check_plain($nvp_response['SHIPTONAME']);
    if (strpos($shipname, ' ') > 0) {
      $order->delivery_first_name = substr($shipname, 0, strrpos(trim($shipname), ' '));
      $order->delivery_last_name = substr($shipname, strrpos(trim($shipname), ' ') + 1);
    }
    else {
      $order->delivery_first_name = $shipname;
      $order->delivery_last_name = '';
    }
    $country_id = db_query("SELECT country_id FROM {uc_countries} WHERE country_iso_code_2 = :code", array(
      ':code' => $nvp_response['SHIPTOCOUNTRYCODE'],
    ))
      ->fetchField();
    $zone_id = 0;
    if (!empty($country_id) && isset($nvp_response['SHIPTOSTATE'])) {
      $zone = $nvp_response['SHIPTOSTATE'];
      $zone_id = db_query("SELECT zone_id FROM {uc_zones} WHERE zone_country_id = :id AND (zone_code = :code OR zone_name = :name)", array(
        ':id' => $country_id,
        ':code' => $zone,
        ':name' => $zone,
      ))
        ->fetchField();
    }
    $order->delivery_street1 = check_plain($nvp_response['SHIPTOSTREET']);
    $order->delivery_street2 = isset($nvp_response['SHIPTOSTREET2']) ? check_plain($nvp_response['SHIPTOSTREET2']) : '';
    $order->delivery_city = check_plain($nvp_response['SHIPTOCITY']);
    $order->delivery_zone = !empty($zone_id) ? $zone_id : 0;
    $order->delivery_postal_code = check_plain($nvp_response['SHIPTOZIP']);
    $order->delivery_country = !empty($country_id) ? $country_id : 840;
    $order->billing_first_name = check_plain($nvp_response['FIRSTNAME']);
    $order->billing_last_name = check_plain($nvp_response['LASTNAME']);
    $order->billing_street1 = check_plain($nvp_response['EMAIL']);
    if (empty($order->primary_email)) {
      $order->primary_email = $nvp_response['EMAIL'];
    }
    $order->payment_method = 'paypal_ec';
    uc_order_save($order);
    $_SESSION['have_details'][$order->order_id] = TRUE;
  }
  $build['instructions'] = array(
    '#markup' => t("Your order is almost complete!  Please fill in the following details and click 'Continue checkout' to finalize the purchase."),
  );
  $build['form'] = drupal_get_form('uc_paypal_ec_review_form', $order);
  return $build;
}