You are here

function uc_paypal_wpp_charge in Ubercart 6.2

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

Processes a credit card payment through Website Payments Pro.

1 string reference to 'uc_paypal_wpp_charge'
uc_paypal_payment_gateway in payment/uc_paypal/uc_paypal.module
Implements hook_payment_gateway().

File

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

Code

function uc_paypal_wpp_charge($order_id, $amount, $data) {
  global $user;
  $order = uc_order_load($order_id);
  $context = array(
    'revision' => 'formatted-original',
    'type' => 'amount',
  );
  $options = array(
    'sign' => FALSE,
    'thou' => FALSE,
    'dec' => '.',
  );
  if ($data['txn_type'] == UC_CREDIT_PRIOR_AUTH_CAPTURE) {
    $nvp_request = array(
      'METHOD' => 'DoCapture',
      'AUTHORIZATIONID' => $data['auth_id'],
      'AMT' => uc_price($amount, $context, $options),
      'CURRENCYCODE' => variable_get('uc_paypal_wpp_currency', 'USD'),
      'COMPLETETYPE' => 'Complete',
    );
  }
  else {
    list($desc, $subtotal) = _uc_paypal_product_details($order->products);
    if (intval($order->payment_details['cc_exp_month']) < 10) {
      $expdate = '0' . $order->payment_details['cc_exp_month'] . $order->payment_details['cc_exp_year'];
    }
    else {
      $expdate = $order->payment_details['cc_exp_month'] . $order->payment_details['cc_exp_year'];
    }
    $cc_type = NULL;
    if (isset($order->payment_details['cc_type'])) {
      switch (strtolower($order->payment_details['cc_type'])) {
        case 'amex':
        case 'american express':
          $cc_type = 'Amex';
          break;
        case 'visa':
          $cc_type = 'Visa';
          break;
        case 'mastercard':
        case 'master card':
          $cc_type = 'MasterCard';
          break;
        case 'discover':
          $cc_type = 'Discover';
          break;
      }
    }
    if (is_null($cc_type)) {
      $cc_type = _uc_paypal_card_type($order->payment_details['cc_number']);
      if ($cc_type === FALSE) {
        drupal_set_message(t('The credit card type did not pass validation.'), 'error');
        watchdog('uc_paypal', 'Could not figure out cc type: @number / @type', array(
          '@number' => $order->payment_details['cc_number'],
          '@type' => $order->payment_details['cc_type'],
        ), WATCHDOG_ERROR);
        return array(
          'success' => FALSE,
        );
      }
    }
    $billing_country = uc_get_country_data(array(
      'country_id' => $order->billing_country,
    ));
    if ($billing_country === FALSE) {
      $billing_country = array(
        0 => array(
          'country_iso_code_2' => 'US',
        ),
      );
    }
    $delivery_country = uc_get_country_data(array(
      'country_id' => $order->delivery_country,
    ));
    if ($delivery_country === FALSE) {
      $delivery_country = array(
        0 => array(
          'country_iso_code_2' => 'US',
        ),
      );
    }

    // Paypal doesn't accept IPv6 addresses.
    $ip_address = ltrim(ip_address(), '::ffff:');
    $nvp_request = array(
      'METHOD' => 'DoDirectPayment',
      'PAYMENTACTION' => $data['txn_type'] == UC_CREDIT_AUTH_ONLY ? 'Authorization' : 'Sale',
      'IPADDRESS' => $ip_address,
      'AMT' => uc_price($amount, $context, $options),
      'CREDITCARDTYPE' => $cc_type,
      'ACCT' => $order->payment_details['cc_number'],
      'EXPDATE' => $expdate,
      'CVV2' => $order->payment_details['cc_cvv'],
      'FIRSTNAME' => substr($order->billing_first_name, 0, 25),
      'LASTNAME' => substr($order->billing_last_name, 0, 25),
      'STREET' => substr($order->billing_street1, 0, 100),
      'STREET2' => substr($order->billing_street2, 0, 100),
      'CITY' => substr($order->billing_city, 0, 40),
      'STATE' => uc_get_zone_code($order->billing_zone),
      'ZIP' => $order->billing_postal_code,
      'COUNTRYCODE' => $billing_country[0]['country_iso_code_2'],
      'CURRENCYCODE' => variable_get('uc_paypal_wpp_currency', 'USD'),
      'DESC' => substr($desc, 0, 127),
      'INVNUM' => $order_id . '-' . time(),
      'BUTTONSOURCE' => 'Ubercart_ShoppingCart_DP_US',
      'NOTIFYURL' => url('uc_paypal/ipn/' . $order->order_id, array(
        'absolute' => TRUE,
      )),
      'EMAIL' => substr($order->primary_email, 0, 127),
      'PHONENUM' => substr($order->billing_phone, 0, 20),
    );
    if (uc_order_is_shippable($order) && !empty($order->delivery_first_name)) {
      $shipdata = array(
        'SHIPTONAME' => substr($order->delivery_first_name . ' ' . $order->delivery_last_name, 0, 25),
        '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),
        'SHIPTOZIP' => $order->delivery_postal_code,
        'SHIPTOCOUNTRYCODE' => $delivery_country[0]['country_iso_code_2'],
      );
      $nvp_request += $shipdata;
    }
    if (variable_get('uc_credit_cvv_enabled', TRUE)) {
      $nvp_request['CVV2'] = $order->payment_details['cc_cvv'];
    }
  }
  $nvp_response = uc_paypal_api_request($nvp_request, variable_get('uc_paypal_wpp_server', 'https://api-3t.sandbox.paypal.com/nvp'));
  $types = uc_credit_transaction_types();
  switch ($nvp_response['ACK']) {
    case 'SuccessWithWarning':
      watchdog('uc_payment', '<b>@type succeeded with a warning.</b>!paypal_message', array(
        '!paypal_message' => _uc_paypal_build_error_messages($nvp_response),
        '@type' => $types[$data['txn_type']],
      ), WATCHDOG_WARNING, l(t('view order'), 'admin/store/orders/' . $order_id));

    // fall through
    case 'Success':
      $message = t('<b>@type</b><br /><b>Success: </b>@amount @currency', array(
        '@type' => $types[$data['txn_type']],
        '@amount' => uc_price($nvp_response['AMT'], $context, array(
          'sign' => FALSE,
        )),
        '@currency' => $nvp_response['CURRENCYCODE'],
      ));
      if ($data['txn_type'] != UC_CREDIT_PRIOR_AUTH_CAPTURE) {
        $message .= '<br />' . t('<b>Address:</b> @avscode', array(
          '@avscode' => _uc_paypal_avscode_message($nvp_response['AVSCODE']),
        ));
        if (variable_get('uc_credit_cvv_enabled', TRUE)) {
          $message .= '<br />' . t('<b>CVV2:</b> @cvvmatch', array(
            '@cvvmatch' => _uc_paypal_cvvmatch_message($nvp_response['CVV2MATCH']),
          ));
        }
      }
      $result = array(
        'success' => TRUE,
        'comment' => t('PayPal transaction ID: @transactionid', array(
          '@transactionid' => $nvp_response['TRANSACTIONID'],
        )),
        'message' => $message,
        'data' => check_plain($nvp_response['TRANSACTIONID']),
        'uid' => $user->uid,
      );

      // If this was an authorization only transaction...
      if ($data['txn_type'] == UC_CREDIT_AUTH_ONLY) {

        // Log the authorization to the order.
        uc_credit_log_authorization($order_id, $nvp_response['TRANSACTIONID'], $nvp_response['AMT']);
      }
      elseif ($data['txn_type'] == UC_CREDIT_PRIOR_AUTH_CAPTURE) {
        uc_credit_log_prior_auth_capture($order_id, $data['auth_id']);
      }

      // Log the IPN to the database.
      db_query("INSERT INTO {uc_payment_paypal_ipn} (order_id, txn_id, txn_type, mc_gross, status, receiver_email, payer_email, received) VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', %d)", $order->order_id, $nvp_response['TRANSACTIONID'], 'web_accept', $amount, 'Completed', '', $order->primary_email, time());
      break;
    case 'FailureWithWarning':

    // fall through
    case 'Failure':
      $message = t('<b>@type failed.</b>', array(
        '@type' => $types[$data['txn_type']],
      )) . _uc_paypal_build_error_messages($nvp_response);
      $result = array(
        'success' => FALSE,
        'message' => $message,
        'uid' => $user->uid,
      );
      break;
    default:
      $message = t('Unexpected acknowledgement status: @status', array(
        '@status' => $nvp_response['ACK'],
      ));
      $result = array(
        'success' => NULL,
        'message' => $message,
        'uid' => $user->uid,
      );
      break;
  }
  uc_order_comment_save($order_id, $user->uid, $message, 'admin');

  // Don't log this as a payment money wasn't actually captured.
  if (in_array($data['txn_type'], array(
    UC_CREDIT_AUTH_ONLY,
  ))) {
    $result['log_payment'] = FALSE;
  }
  return $result;
}