You are here

function uc_paypal_wpp_charge in Ubercart 5

Same name and namespace in other branches
  1. 6.2 payment/uc_paypal/uc_paypal.module \uc_paypal_wpp_charge()
  2. 7.3 payment/uc_paypal/uc_paypal.module \uc_paypal_wpp_charge()
1 string reference to 'uc_paypal_wpp_charge'
uc_paypal_payment_gateway in payment/uc_paypal/uc_paypal.module
Implementation of hook_payment_gateway().

File

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

Code

function uc_paypal_wpp_charge($order_id, $amount, $data) {
  global $user, $response;
  if (!function_exists('curl_init')) {
    drupal_set_message(t('PayPal Website Payments Pro requires curl for PHP.  Please talk to your system administrator to get this.'));
    return array(
      'success' => FALSE,
    );
  }
  $order = uc_order_load($order_id);
  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', t('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',
      ),
    );
  }
  if ($data['txn_type'] == UC_CREDIT_PRIOR_AUTH_CAPTURE) {
    $nvp_request = array(
      'METHOD' => 'DoCapture',
      'AUTHORIZATIONID' => $data['auth_id'],
      'AMT' => uc_currency_format($amount, FALSE, FALSE, '.'),
      'CURRENCYCODE' => variable_get('uc_paypal_wpp_currency', 'USD'),
      'COMPLETETYPE' => 'Complete',
    );
  }
  else {
    $nvp_request = array(
      'METHOD' => 'DoDirectPayment',
      'PAYMENTACTION' => $data['txn_type'] == UC_CREDIT_AUTH_ONLY ? 'Authorization' : 'Sale',
      'IPADDRESS' => $_SERVER['REMOTE_ADDR'],
      'AMT' => uc_currency_format($amount, FALSE, FALSE, '.'),
      '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, NULL, NULL, 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();
  if ($nvp_response['ACK'] != 'Success') {
    $message = t('<b>@type failed.</b><br /><b>@severity:</b> @error', array(
      '@type' => $types[$data['txn_type']],
      '@severity' => $nvp_response['L_SEVERITYCODE0'],
      '@error' => $nvp_response['L_ERRORCODE0'] . '. ' . $nvp_response['L_LONGMESSAGE0'],
    ));
    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' => FALSE,
      'message' => $message,
      'uid' => $user->uid,
    );
  }
  else {
    $message = t('<b>@type</b><br /><b>Success: </b>@amount @currency', array(
      '@type' => $types[$data['txn_type']],
      '@amount' => uc_currency_format($nvp_response['AMT'], 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} VALUES (%d, '%s', '%s', '%s', '%s', '%s', '%s', %d)", $order->order_id, check_plain($nvp_response['TRANSACTIONID']), 'web_accept', $amount, 'Completed', '', $order->primary_email, time());
  }
  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;
}