You are here

abstract class PayPalPaymentNVPAPIPaymentMethodControllerBase in PayPal for Payment 7

A base class for payment method controllers that talk to PayPal's NVP API.

Hierarchy

Expanded class hierarchy of PayPalPaymentNVPAPIPaymentMethodControllerBase

File

paypal_payment/includes/PayPalPaymentNVPAPIPaymentMethodControllerBase.inc, line 10

View source
abstract class PayPalPaymentNVPAPIPaymentMethodControllerBase extends PaymentMethodController {

  /**
   * The production server.
   */
  const NVP_API_SERVER_PRODUCTION = 0;

  /**
   * The sandbox server.
   */
  const NVP_API_SERVER_SANDBOX = 1;

  /**
   * The production API server URL.
   */
  const NVP_API_URL_SERVER_PRODUCTION = NULL;

  /**
   * The sandbox API server URL.
   */
  const NVP_API_URL_SERVER_SANDBOX = NULL;

  /**
   * The PayPAL API version that is used.
   */
  const NVP_API_PAYPAL_VERSION = '86';

  /**
   * {@inheritdoc}
   */
  public $controller_data_defaults = array(
    'password' => '',
    'server' => self::NVP_API_SERVER_PRODUCTION,
    'signature' => '',
    'username' => '',
  );

  /**
   * Returns the API server URL.
   *
   * @throws InvalidArgumentException
   *
   * @param int $server
   *   One of the self::NVP_API_SERVER_* constants.
   *
   * @return string
   */
  public function NVPAPIServerURL($server) {
    $urls = array(
      $this::NVP_API_SERVER_PRODUCTION => $this::NVP_API_URL_SERVER_PRODUCTION,
      $this::NVP_API_SERVER_SANDBOX => $this::NVP_API_URL_SERVER_SANDBOX,
    );
    if (array_key_exists($server, $urls)) {
      return url($urls[$server], array(
        'external' => TRUE,
      ));
    }
    else {
      throw new InvalidArgumentException(t('Server type does not exist.'));
    }
  }

  /**
   * Parses an API response.
   *
   * @param string $response
   *
   * @return array
   */
  public function NVPAPIParseResponse($response) {
    $nvp = array();
    foreach (explode('&', $response) as $variable) {
      $fragments = explode('=', $variable);
      if (count($fragments) == 2) {
        $nvp[$fragments[0]] = urldecode($fragments[1]);
      }
    }
    return $nvp;
  }

  /**
   * Executes an API request.
   *
   * @param array $nvp_request
   *   NVP variables to POST.
   * @param Payment $payment
   *
   * @return array|false
   *   The NVP response data or FALSE in case of failure.
   */
  public function NVPAPIRequest(array $nvp_request, Payment $payment) {
    $nvp_request = array(
      'USER' => $payment->method->controller_data['username'],
      'PWD' => $payment->method->controller_data['password'],
      'SIGNATURE' => $payment->method->controller_data['signature'],
      'VERSION' => self::NVP_API_PAYPAL_VERSION,
    ) + $nvp_request;
    $post_data = array();
    foreach ($nvp_request as $name => $value) {
      $post_data[] = $name . '=' . urlencode($value);
    }
    $post_data = implode('&', $post_data);
    $response = chr_curl_http_request($this
      ->NVPAPIServerURL($payment->method->controller_data['server']), array(
      'method' => 'POST',
      'data' => $post_data,
      'curl_opts' => [
        CURLOPT_SSL_VERIFYPEER => TRUE,
        CURLOPT_SSL_VERIFYHOST => 2,
      ],
    ));
    if (isset($response->error)) {
      watchdog('paypal_payment_ec', 'An API request failed with error @code: %error.', array(
        '@code' => $response->code,
        '%error' => $response->error,
      ), WATCHDOG_ERROR);
      return FALSE;
    }
    else {
      $nvp_response = $this
        ->NVPAPIParseResponse($response->data);
      if ($nvp_response && isset($nvp_response['ACK']) && in_array($nvp_response['ACK'], array(
        'Success',
        'SuccessWithWarning',
      ))) {
        return $nvp_response;
      }
    }
    return FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PaymentMethodController::$currencies public property An array with ISO 4217 currency codes that this controller supports.
PaymentMethodController::$description public property A human-readable plain text description of this payment method controller.
PaymentMethodController::$name public property The machine name.
PaymentMethodController::$payment_configuration_form_elements_callback public property The function name of the payment configuration form elements. 1
PaymentMethodController::$payment_method_configuration_form_elements_callback public property The function name of the payment method configuration form elements. 1
PaymentMethodController::$title public property The human-readable plain text title.
PaymentMethodController::descendants static function Returns an array with the names of all available payment method controllers that inherit of this one.
PaymentMethodController::execute function Execute a payment. 2
PaymentMethodController::validate function Validate a payment against a payment method and this controller. Don't call directly. Use PaymentMethod::validate() instead. 2
PayPalPaymentNVPAPIPaymentMethodControllerBase::$controller_data_defaults public property Default values for the controller_data property of a PaymentMethod that uses this controller. Overrides PaymentMethodController::$controller_data_defaults 1
PayPalPaymentNVPAPIPaymentMethodControllerBase::NVPAPIParseResponse public function Parses an API response.
PayPalPaymentNVPAPIPaymentMethodControllerBase::NVPAPIRequest public function Executes an API request.
PayPalPaymentNVPAPIPaymentMethodControllerBase::NVPAPIServerURL public function Returns the API server URL.
PayPalPaymentNVPAPIPaymentMethodControllerBase::NVP_API_PAYPAL_VERSION constant The PayPAL API version that is used.
PayPalPaymentNVPAPIPaymentMethodControllerBase::NVP_API_SERVER_PRODUCTION constant The production server.
PayPalPaymentNVPAPIPaymentMethodControllerBase::NVP_API_SERVER_SANDBOX constant The sandbox server.
PayPalPaymentNVPAPIPaymentMethodControllerBase::NVP_API_URL_SERVER_PRODUCTION constant The production API server URL. 1
PayPalPaymentNVPAPIPaymentMethodControllerBase::NVP_API_URL_SERVER_SANDBOX constant The sandbox API server URL. 1