You are here

public function Square::capturePayment in Commerce Square Connect 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/Square.php, line 357

Class

Square
Provides the Square payment gateway.

Namespace

Drupal\commerce_square\Plugin\Commerce\PaymentGateway

Code

public function capturePayment(PaymentInterface $payment, Price $amount = NULL) {
  $this
    ->assertPaymentState($payment, [
    'authorization',
  ]);
  $amount = $amount ?: $payment
    ->getAmount();

  // Square only accepts integers and not floats.
  // @see https://docs.connect.squareup.com/api/connect/v2/#workingwithmonetaryamounts
  list($transaction_id, $tender_id) = explode('|', $payment
    ->getRemoteId());
  $mode = $this
    ->getMode();
  try {
    $transaction_api = new TransactionsApi($this
      ->getApiClient());
    $result = $transaction_api
      ->captureTransaction($this->configuration[$mode . '_location_id'], $transaction_id);
  } catch (ApiException $e) {
    throw ErrorHelper::convertException($e);
  }
  $payment
    ->setState('completed');
  $payment
    ->setAmount($amount);
  $payment
    ->setCompletedTime($this->time
    ->getRequestTime());
  $payment
    ->save();
}