You are here

public function PayflowLink::voidPayment in Commerce PayPal 8

Voids the given payment.

Parameters

\Drupal\commerce_payment\Entity\PaymentInterface $payment: The payment to void.

Throws

\Drupal\commerce_payment\Exception\PaymentGatewayException Thrown when the transaction fails for any reason.

Overrides SupportsVoidsInterface::voidPayment

File

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

Class

PayflowLink
Provides the PayPal Payflow Link payment gateway.

Namespace

Drupal\commerce_paypal\Plugin\Commerce\PaymentGateway

Code

public function voidPayment(PaymentInterface $payment) {

  // Build a name-value pair array for this transaction.
  $nvp = [
    'TRXTYPE' => 'V',
    'ORIGID' => $payment
      ->getRemoteId(),
  ];
  $order = $payment
    ->getOrder();

  // Submit the request to Payflow Pro.
  $response = $this
    ->apiRequest('pro', $nvp, $order);

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

  // If we got an approval response code...
  if (intval($response['RESULT']) === 0) {

    // Set the remote and local status accordingly.
    $payment->remote_id = $response['PNREF'];
    $payment->state = 'voided';
    $payment->remote_state = 'V';
  }
  else {
    throw new PaymentGatewayException('Prior authorization capture failed, so the payment will remain in a pending status.');
  }
  $payment
    ->save();
}