You are here

protected function PayflowLink::apiRequest in Commerce PayPal 8

Submits an API request to Payflow.

Parameters

string $api: Either 'pro' or 'link' indicating which API server the request should be sent to.

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

null|\Drupal\commerce_order\Entity\OrderInterface $order: (optional) The order the payment request is being made for.

Return value

array|\Psr\Http\Message\ResponseInterface The response array from PayPal if successful or FALSE on error.

5 calls to PayflowLink::apiRequest()
PayflowLink::capturePayment in src/Plugin/Commerce/PaymentGateway/PayflowLink.php
Captures the given authorized payment.
PayflowLink::createSecureToken in src/Plugin/Commerce/PaymentGateway/PayflowLink.php
Requests a secure token from Payflow for use in follow-up API requests.
PayflowLink::referencePayment in src/Plugin/Commerce/PaymentGateway/PayflowLink.php
Creates a reference payment.
PayflowLink::refundPayment in src/Plugin/Commerce/PaymentGateway/PayflowLink.php
Refunds the given payment.
PayflowLink::voidPayment in src/Plugin/Commerce/PaymentGateway/PayflowLink.php
Voids the given payment.

File

src/Plugin/Commerce/PaymentGateway/PayflowLink.php, line 815

Class

PayflowLink
Provides the PayPal Payflow Link payment gateway.

Namespace

Drupal\commerce_paypal\Plugin\Commerce\PaymentGateway

Code

protected function apiRequest($api, array $nvp = [], $order = NULL) {
  $configuration = $this
    ->getConfiguration();
  $mode = $configuration['mode'];

  // Get the API endpoint URL for the payment method's transaction mode.
  if ($api === 'pro') {
    $url = $this
      ->getProServerUrl();
  }
  else {
    $url = $this
      ->getRedirectUrl();
  }

  // Add the default name-value pairs to the array.
  $nvp += [
    // API credentials.
    'PARTNER' => $configuration['partner'],
    'VENDOR' => $configuration['vendor'],
    'USER' => $configuration['user'],
    'PWD' => $configuration['password'],
    // Set the mode based on which server we're submitting to.
    'MODE' => $mode === 'test' ? 'TEST' : 'LIVE',
  ];

  // Allow modules to alter the NVP request.
  $event = new PayflowLinkRequestEvent($nvp, $order);
  $this->eventDispatcher
    ->dispatch(PayPalEvents::PAYFLOW_LINK_REQUEST, $event);
  $nvp = $event
    ->getNvpData();

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

    // Mask sensitive request data.
    $log_nvp = $nvp;
    $log_nvp['PWD'] = str_repeat('X', strlen($log_nvp['PWD']));
    if (!empty($log_nvp['ACCT'])) {
      $log_nvp['ACCT'] = str_repeat('X', strlen($log_nvp['ACCT']) - 4) . mb_substr($log_nvp['ACCT'], -4);
    }
    if (!empty($log_nvp['CVV2'])) {
      $log_nvp['CVV2'] = str_repeat('X', strlen($log_nvp['CVV2']));
    }
    $this->logger
      ->debug('Payflow API request to @url: @param', [
      '@url' => $url,
      '@param' => new FormattableMarkup('<pre>' . print_r($log_nvp, 1) . '</pre>', []),
    ]);
  }

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

    /* Since we aren't supposed to urlencode parameter values for PFL/PPA API
       requests, we strip out ampersands and equals signs to. */
    $pairs[] = $key . '=' . str_replace([
      '&',
      '=',
      '#',
    ], [
      '',
    ], $value);
  }
  $body = implode('&', $pairs);
  try {
    $response = $this->httpClient
      ->post($url, [
      'headers' => [
        'Content-Type' => 'text/namevalue',
        'Content-Length' => strlen($body),
      ],
      'body' => $body,
      'timeout' => 45,
    ]);
  } catch (BadResponseException $exception) {
    $this->logger
      ->error($exception
      ->getResponse()
      ->getBody()
      ->getContents());
    throw new PaymentGatewayException('Redirect to PayPal failed. Please try again or contact an administrator to resolve the issue.');
  }
  $result = $response
    ->getBody()
    ->getContents();

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

  // Log the response if specified.
  if (!empty($configuration['log']['response'])) {
    $this->logger
      ->debug('Payflow server response: @param', [
      '@param' => new FormattableMarkup('<pre>' . print_r($response, 1) . '</pre>', []),
    ]);
  }
  return $response;
}