You are here

public function Payflow::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/Payflow.php, line 389

Class

Payflow
Provides the PayPal Payflow payment gateway.

Namespace

Drupal\commerce_paypal\Plugin\Commerce\PaymentGateway

Code

public function capturePayment(PaymentInterface $payment, Price $amount = NULL) {
  $this
    ->validatePayment($payment, 'authorization');

  // If not specified, capture the entire amount.
  $amount = $amount ?: $payment
    ->getAmount();
  $amount = $this->rounder
    ->round($amount);
  try {
    $data = $this
      ->executeTransaction([
      'trxtype' => 'D',
      'amt' => $this->rounder
        ->round($amount)
        ->getNumber(),
      'currency' => $amount
        ->getCurrencyCode(),
      'origid' => $this
        ->getTransactionNumber($payment),
    ]);
    if ($data['result'] !== '0') {
      throw new PaymentGatewayException('Count not capture payment. Message: ' . $data['respmsg'], $data['result']);
    }
    $payment
      ->setState('completed');
    $payment
      ->setAmount($amount);
    $payment
      ->save();
  } catch (RequestException $e) {
    throw new PaymentGatewayException('Count not capture payment. Message: ' . $e
      ->getMessage(), $e
      ->getCode(), $e);
  }
}