You are here

function commerce_paypal_api_request in Commerce PayPal 7.2

Submits an API request to PayPal.

This function is currently used by PayPal Payments Pro and Express Checkout.

This function may be used for any PayPal payment method that uses the same settings array structure as these other payment methods and whose API requests should be submitted to the same URLs as determined by the function commerce_paypal_api_server_url().

Parameters

$payment_method: The payment method instance array associated with this API request.

$nvp: The set of name-value pairs describing the transaction to submit.

$order: The order the payment request is being made for.

Return value

The response array from PayPal if successful or FALSE on error.

8 calls to commerce_paypal_api_request()
commerce_paypal_ec_capture_form_submit in modules/ec/includes/commerce_paypal_ec.admin.inc
Submit handler: process a prior authorization capture via PayPal EC.
commerce_paypal_ec_do_payment in modules/ec/commerce_paypal_ec.module
Confirm an Express Checkout payment for an order for the specified charge amount with a DoExpressCheckoutPayment API request.
commerce_paypal_ec_redirect_form_validate in modules/ec/commerce_paypal_ec.module
Payment method callback: redirect form return validation.
commerce_paypal_ec_refund_form_submit in modules/ec/includes/commerce_paypal_ec.admin.inc
Submit handler: process a refund request.
commerce_paypal_ec_set_express_checkout in modules/ec/commerce_paypal_ec.module
Submits a SetExpressCheckout request to PayPal for the given order.

... See full list

File

./commerce_paypal.module, line 329
Implements PayPal payment services for use with Drupal Commerce.

Code

function commerce_paypal_api_request($payment_method, $nvp = array(), $order = NULL) {

  // Get the API endpoint URL for the payment method's transaction mode.
  $url = commerce_paypal_api_server_url($payment_method['settings']['server']);

  // Add the default name-value pairs to the array.
  $nvp += array(
    // API credentials
    'USER' => $payment_method['settings']['api_username'],
    'PWD' => $payment_method['settings']['api_password'],
    'SIGNATURE' => $payment_method['settings']['api_signature'],
    'VERSION' => '76.0',
  );

  // Allow modules to alter parameters of the API request.
  drupal_alter('commerce_paypal_api_request', $nvp, $order, $payment_method);

  // Log the request if specified.
  if ($payment_method['settings']['log']['request'] == 'request') {

    // Mask the credit card number and CVV.
    $log_nvp = $nvp;
    $log_nvp['PWD'] = str_repeat('X', strlen($log_nvp['PWD']));
    $log_nvp['SIGNATURE'] = str_repeat('X', strlen($log_nvp['SIGNATURE']));
    if (!empty($log_nvp['ACCT'])) {
      $log_nvp['ACCT'] = str_repeat('X', strlen($log_nvp['ACCT']) - 4) . substr($log_nvp['ACCT'], -4);
    }
    if (!empty($log_nvp['CVV2'])) {
      $log_nvp['CVV2'] = str_repeat('X', strlen($log_nvp['CVV2']));
    }
    watchdog('commerce_paypal', 'PayPal API request to @url: !param', array(
      '@url' => $url,
      '!param' => '<pre>' . check_plain(print_r($log_nvp, TRUE)) . '</pre>',
    ), WATCHDOG_DEBUG);
  }

  // Prepare the name-value pair array to be sent as a string.
  $pairs = array();
  foreach ($nvp as $key => $value) {
    $pairs[] = $key . '=' . urlencode($value);
  }

  // Setup the cURL request.
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_VERBOSE, 0);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $pairs));
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_NOPROGRESS, 1);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);

  // Commerce PayPal requires SSL peer verification, which may prevent out of
  // date servers from successfully processing API requests. If you get an error
  // related to peer verification, you may need to download the CA certificate
  // bundle file from http://curl.haxx.se/docs/caextract.html, place it in a
  // safe location on your web server, and update your settings.php to set the
  // commerce_paypal_cacert variable to contain the absolute path of the file.
  // Alternately, you may be able to update your php.ini to point to the file
  // with the curl.cainfo setting.
  if (variable_get('commerce_paypal_cacert', FALSE)) {
    curl_setopt($ch, CURLOPT_CAINFO, variable_get('commerce_paypal_cacert', ''));
  }
  $result = curl_exec($ch);

  // Log any errors to the watchdog.
  if ($error = curl_error($ch)) {
    watchdog('commerce_paypal', 'cURL error: @error', array(
      '@error' => $error,
    ), WATCHDOG_ERROR);
    return FALSE;
  }
  curl_close($ch);

  // Make the response an array.
  $response = array();
  foreach (explode('&', $result) as $nvp) {
    list($key, $value) = explode('=', $nvp);
    $response[urldecode($key)] = urldecode($value);
  }

  // Log the response if specified.
  if ($payment_method['settings']['log']['response'] == 'response') {
    watchdog('commerce_paypal', 'PayPal server response: !param', array(
      '!param' => '<pre>' . check_plain(print_r($response, TRUE)) . '</pre>',
    ), WATCHDOG_DEBUG);
  }
  return $response;
}