You are here

public function AcceptJs::approvePayment in Commerce Authorize.Net 8

Approves the given payment.

Only payments in the 'unauthorized_review' and 'authorization_review' states can be approved.

Parameters

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

Throws

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

Overrides AcceptJsInterface::approvePayment

File

src/Plugin/Commerce/PaymentGateway/AcceptJs.php, line 365

Class

AcceptJs
Provides the Accept.js payment gateway.

Namespace

Drupal\commerce_authnet\Plugin\Commerce\PaymentGateway

Code

public function approvePayment(PaymentInterface $payment) {

  /** @var \Drupal\commerce_payment\Entity\PaymentMethod $payment_method */
  $this
    ->assertPaymentState($payment, [
    'unauthorized_review',
    'authorization_review',
  ]);
  if ($payment
    ->isExpired()) {
    throw new HardDeclineException('This payment has expired.');
  }
  $request = new UpdateHeldTransactionRequest($this->authnetConfiguration, $this->httpClient);
  $request
    ->setAction(UpdateHeldTransactionRequest::APPROVE);
  $request
    ->setRefTransId($payment
    ->getRemoteId());
  $response = $request
    ->execute();
  if ($response
    ->getResultCode() != 'Ok') {
    $this
      ->logResponse($response);
    $message = $response
      ->getMessages()[0];
    throw new PaymentGatewayException($message
      ->getText());
  }
  $new_state = $payment
    ->getState()
    ->getId() == 'unauthorized_review' ? 'authorization' : 'completed';
  $payment
    ->setState($new_state);
  $payment
    ->save();
}