You are here

public function PayflowLink::capturePayment in Commerce PayPal 8

Captures the given authorized payment.

Only payments in the 'authorization' state can be captured.

Parameters

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

\Drupal\commerce_price\Price $amount: The amount to capture. If NULL, defaults to the entire payment amount.

Throws

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

Overrides SupportsAuthorizationsInterface::capturePayment

File

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

Class

PayflowLink
Provides the PayPal Payflow Link payment gateway.

Namespace

Drupal\commerce_paypal\Plugin\Commerce\PaymentGateway

Code

public function capturePayment(PaymentInterface $payment, Price $amount = NULL) {

  // If not specified, capture the entire amount.
  $amount = $amount ?: $payment
    ->getAmount();
  $order = $payment
    ->getOrder();

  // Prepare a name-value pair array to capture the requested amount.
  $nvp = [
    'TRXTYPE' => 'D',
    'ORIGID' => $payment
      ->getRemoteId(),
    'AMT' => Calculator::trim($amount
      ->getNumber()),
    'CAPTURECOMPLETE' => 'Y',
  ];

  // Submit the capture 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>', []),
    ]);
  }
  switch (intval($response['RESULT'])) {
    case 0:
      $payment->amount = $amount;
      $payment->remote_id = $response['PNREF'];
      $payment->state = 'completed';
      $payment->remote_state = 'D';
      break;
    default:
      throw new PaymentGatewayException($this
        ->t('Capture failed: @reason.', [
        '@reason' => $response['RESPMSG'],
      ]), $response['RESULT']);
  }
  $payment
    ->save();
}